For linear table of linear storage structure, the main attention to the use of malloc this function, it is used to open up space. Declaration Header file # include <stdlib.h> can call it.
#include <iostream>#include<stdio.h>#include<stdlib.h>#defineTRUE 1#defineFALSE 0#defineOK 1#defineERROR 0#defineOVERFLOW-2typedefintStatus;typedefintElemtype;using namespacestd;#defineList_int_size 100#defineListincrement 10typedefstruct{elemtype*elem;//Storage space Base intLength//Current Length intListsize;//the current allocated storage capacity//(in sizeof (Elemtype) )} sqlist;//Initialize linear table LvoidInitlist (SqList *L) {L->elem= (elemtype*) malloc (list_int_size *sizeof(Elemtype));//Allocate Space if(L->elem==null) exit (ERROR);//If the allocated space is unsuccessfulL->length=0;//Position the current linear table length by 0L->listsize=list_int_size;}//Destroy linear table LvoidDestroylist (SqList *L) {if(l->elem) { if(L->elem) free (L->elem);//FREE all storage space occupied by the linear tableL->length=0; L->listsize=0; L->elem=NULL; } Else{cout<<"sqlist is not exsit!\n"; exit (ERROR); }}//Empty linear table LvoidClearlist (SqList *L) { if(l->elem) L->length=0;//set the length of the linear table to 0 Else{cout<<"sqlist is not exsit!\n"; exit (ERROR); }}//determine if the linear table L is emptyStatus IsEmpty (SqList *L) { if(l->elem)if(l->length==0)returnTRUE; Else returnFALSE; Else{cout<<"sqlist is not exsit!\n"; exit (ERROR); }}//to find the length of the linear table LintListlength (SqList *L) { if(! L->elem) {cout<<"sqlist is not exsit!\n"; exit (ERROR); } Else return(l->length);}//get the contents of a data element in a linear table LStatus Getelem (SqList *l,intI,elemtype *e) { if(l->elem) { if(i<1|| I>l->length)returnERROR;//Determine if I value is reasonable, if not reasonable, return error*e=l->elem[i-1];//The element i-1 in the array stores the contents of the I data elements in a linear table returnOK; } Else{cout<<"sqlist is not exsit!\n"; exit (ERROR); }}//data element decision function for retrievalStatus equal (Elemtype E, Elemtype q) {if(E==Q)returnTRUE; Else returnFALSE;}//retrieving a data element with a value of e in linear table LintLocateelem (sqlist *l,elemtype e,status (*Compare) (Elemtype e,elemtype Q)) { inti; if(l->elem) { for(i=0; i<l->length;i++) if(Compare (E,l->elem[i]))returni+1; return 0; } Else{cout<<"sqlist is not exsit!\n"; exit (ERROR); }}//inserting a data element before the first data element in the linear table LStatus Listinsert (SqList *l,intI,elemtype e) { if(l->elem) { if(i<1|| i>l->length+1)returnERROR;//Check if I value is reasonable if(l->length==l->listsize) {Elemtype*newp= (Elemtype *) realloc (L->elem, (l->listsize+listincrement) *sizeof(Elemtype)); if(!newp) exit (OVERFLOW); L->elem=NEWP; L->listsize+=listincrement; } for(intj=l->length-1; j>=i-1; i++)//move all elements behind a linear table I element backwardl->elem[j+1]=l->Elem[j]; L->elem[i-1]=e;//Place the contents of the new element in the first position of the linear tablel->length++; returnOK; } Else{cout<<"sqlist is not exsit!\n"; exit (ERROR); }}//Delete the first data element of the linear table LStatus Listdelete (SqList *l,intI,elemtype *e) { if(l->elem) { if(IsEmpty (L))returnERROR;//detect if a linear table is empty if(i<1|| I>l->length)returnERROR;//Check if I value is reasonable*e=l->elem[i-1];//keep the content of the data element you want to delete in the storage unit indicated by E for(intj=i;j<=l->length-1; j + +)//moves all elements after the i+1 element of a linear table forwardl->elem[j-1]=l->Elem[j]; L->length--; returnOK; } Else{cout<<"sqlist is not exsit!\n"; exit (ERROR); }}//Traversal Order table mode visit () functionvoidprint (Elemtype e) {cout<<e<<" ";}//traversing the Order table in visit () modevoidListtraverse (SqList *l,void(*visit) (Elemtype e)) { if(l->elem) { for(intI=0; i<l->length;i++) Visit (L-Elem[i]); cout<<Endl; } Else{cout<<"sqlist is not exsit!\n"; exit (ERROR); }}voidListcreate (SqList *L) { intN; cout<<"Number of elements:"; while(1) {cin>>N; if(n<=l->listsize) Break; } for(intI=0; i<n;i++) cin>>l->Elem[i]; L->length=N;}voidListunion (sqlist *la,sqlist *LB) {Elemtype E; intlalen=listlength (LA); intlblen=listlength (LB); for(intI=1; i<=lblen;i++) {Getelem (lb,i,&e); if(! Locateelem (la,e,equal)) Listinsert (la,++lalen,e); }}intMain () {sqlist*l= (SqList *) malloc (sizeof(SqList));//Create a linear table SqList*lb= (SqList *) malloc (sizeof(SqList));//Create another linear table elemtype e; //int a=5;initlist (L); Initlist (LB); //Listtraverse (L,print); //Listtraverse (lb,print);listcreate (L); Listcreate (LB); Listtraverse (L,print); Listtraverse (Lb,print); Listunion (L,LB); Listtraverse (L,print); //cout<<listlength (L); //for (int i=1;i<=10;i++)//Listinsert (l,i,i); //destroylist (L); //Listtraverse (L,print); //Locateelem (l,9,equal); //printf ("%d\n", (Locateelem (l,7,equal))); //Listdelete (L,locateelem (l,7,equal), &e); //Listtraverse (L,print); //if (IsEmpty (L)) cout<< "empty\n"; //else cout<< "not empty\n"; //clearlist (L); //listcreate (L); //Listtraverse (L,print); //Listtraverse (L,print); //destroylist (L); //if (IsEmpty (L)) cout<< "empty\n"; //else cout<< "not empty\n"; //cout<<listlength (L); //Listtraverse (l,print); return 0;}
Linear table of data structure