Project structure:
main.cpp:
#include <iostream> #include<stdio.h> #include<stdlib.h> #include "function_for_SqList.h" using namespace std; int main() { SqList L; Book book1 = { //创建两本书 "001", "book_one", 23.6 }; Book book2 = { "002", "book_two", 34.7 }; int initResult = 0 ; int InsertResult = 0 ; // Books inserted result value is returned initResult = InitList (L); // Create a linear table the printf ( " number of books before the addition: D% \ n- " , a GetLength (L)) ; // get the current number Books // add the first book InsertResult ListInsert_Sq = (L, . 1 , Book1); IF (InsertResult == . 1 ) { the printf ( " inserted in the first book is completed, when the linear table Books The number was:% D \ n- " , a GetLength (L)); } the else { the printf ( "Books insert failed \ n- " ); } InsertResult = ListInsert_Sq (L, 2 , Book2); IF (InsertResult == . 1 ) { the printf ( " inserted into the second book is completed, then the number of books is a linear table:% d \ n " , a GetLength (L)); } the else { the printf ( " Books failed \ n insertion " ); } for ( int J = 0 ; J <L.length; J ++ ) { the printf ( " % S \ n " , L.elem [J] .no); } return 0 ; }
function_for_SqList.h:
// function result status code #define TRUE. 1 #define FALSE 0 #define the OK. 1 #define ERROR 0 #define INFEASIBLE -1 // "impossible" #define the OVERFLOW -2 // "overflow, extended out of bounds" // definition of constant #define the MAXSIZE 100 // linear maximum possible length table typedef struct { char NO [ 20 is ]; // element ISBN number char name [ 50 ]; // element names a float . price; //Books (element) Price } Book; typedef struct { Book * elem; // memory base address int length; // linear current number of elements in the table } SqList; // sequential storage structure type linear table is SqList typedef int the Status ; // the status is a function of the type, which is a function of the result status code typedef Book elemType; // methods // linear initialization list L (reference parameters) the status InitList (SqList & L); // Constructs an empty sequence table / / destruction linear form L void DestroyList (SqList & L); // Clear linear list L void ClearList (SqList & L); // find the length L of the linear table int a GetLength (SqList L); // determines whether the list is empty the linear int the IsEmpty (SqList L); // Get the linear table of contents: the i-th element taken int getElem (SqList L, int i , elemType & E); // Find :( sequential search) to find by value (given by ISBN to find, it is determined whether the presence of the book) / * if present, several elements are output if not present, the output 0 * / int LocateElem (SqList L, elemType e); // insert: the element e inserted in the i-th position on the Status ListInsert_Sq (SqList L &, int i, elemType e); // delete: to delete the i-th element Status ListDelete_Sq (SqList L &, int I);
function_for_SqList.cpp:
#include <stdio.h> #include <stdlib.h> #include " function_for_SqList.h " // must be added to the header files include, or can not identify the keywords runtime // method // linear form L is initialized (reference parameters ) the Status InitList (SqList & L) { // Constructs an empty sequence table L.elem = new new elemType [the MAXSIZE]; // a sequential space allocation table IF (! L.elem) { Exit (the OVERFLOW); // storage allocation failed } L.length = 0 ; // empty list of length 0 return the OK; } // destruction linear form L voidDestroyList (SqList & L) { IF (L.elem) { Delete L.elem; // free storage space (Remove array) } } // Clear linear form L void ClearList (SqList & L) { L.length = 0 ; } // L is the length of linear table int a GetLength (SqList L) { return (L.length); } // Analyzing linear table is empty int the IsEmpty (SqList L) { iF (L.length == 0 ) { return . 1 ; } the else { return 0 ; } } // Get the linear table of contents: the i-th element taken int getElem (SqList L, int i, elemType & E) { IF (i < . 1 || i> L.length) { // determines whether the value of i reasonable and unreasonable if the return ERROR return ERROR; } E = L.elem [I- . 1 ]; // of the (i-1) units of i-th data stored return the OK; } // Find sequential search :( ) to find the value by (ISBN was carried out to find a given, it is determined whether the presence of the book) / * if present, several elements are output if not present, the output 0 * / int LocateElem (SqList L, elemType E) { for ( int i =0 ; I <L.length; I ++ ) { IF (L.elem [I] == .no e.no) { return (I + . 1 ); // search is successful, return number } } return 0 ; // lookup failed return 0 } // insert: the element e inserted in the i-th position on the Status ListInsert_Sq (SqList L &, int i, elemType e) { IF (i < . 1 || i> (+ L.length . 1 )) return ERROR ; // I invalid value IF (== L.length the MAXSIZE) return ERROR; // this storage is full for ( int J = L.length- . 1 ; J> = (I- . 1 ); J, ) { L.elem [J + . 1 ;] = L.elem [J] // the insertion position of the element and thereafter shift } L.elem [i - . 1 ] = e; // new element into the i-th position e L.length ++; // long table plus. 1 return the OK; } // delete: to delete the i-th element Status ListDelete_Sq (SqList L &, int I) { IF (I < . 1 || I> L.length) return ERROR; // I value is not valid for ( intI = J; J <= (L.length- . 1 ); J ++ ) { L.elem [J - . 1 ] = L.elem [J]; // element forward after the deleted element } L.length - ; // long table Save. 1 return the OK; }
Operating results :( attempt to insert two elements, and then sequentially outputs the ISBN number for each element)