(i) Array implementation linear table
1. Defining a collection (struct implementation)
1 #define MAX2 struct {3 int Data[max]; 4 int Last ; 5 }list;
The data array is used to hold the integer type, and last indicates the subscript of the final element
2. Define various operations
A. Creating an empty table
LIST *makeempty () { *Ptrl; = (list *) malloc (sizeof(list)); Ptrl->last =-1; return Ptrl;}
b. Inserting data
voidInsert (intX, LIST *ptrl,inti) {if(Ptrl->last = = MAX-1) {printf ("Table Full"); return; } if(i<1|| I>ptrl->last +2) {printf ("location is illegal ."); return; } intJ; for(j = ptrl->last; J >= I-1; j--) {Ptrl->data[j +1] = Ptrl->data[j +1]; } Ptrl->data[i-1] =x; Ptrl->last++; return;}
C. Find
int int data) { int0; while (i <= ptrl->last&&ptrl->data[i]! = DATA) i+ + ; if (i > Ptrl-> last) return -1; Else return i;}
D. Delete
void Delete (LIST *l, int i) { int J; if (I<1 | | i>l->last + " Span style= "COLOR: #800080" >1 ) {printf ( " /span> There is no such location! " return ; for (j = i; J <= l->last; J++->data[j-1 ] = L->data[j]; } l ->last--;}
E. Printing (traversal)
void Print (LIST *l) { int i; for 0; I <= l->last; i++) printf ("", l->data[i]);}
Complete implementation
1#include <stdio.h>2#include <stdlib.h>3 #defineMAX 104typedefstruct {5 intData[max];6 intLast ;7 }list;8 9 LIST *Makeempty () {TenLIST *Ptrl; OnePtrl = (LIST *)malloc(sizeof(LIST)); APtrl->last =-1; - returnPtrl; - the } - - intFind (LIST *ptrl,intdata) { - inti =0; + while(i <= ptrl->last&&ptrl->data[i]! =data) -i++; + if(I > ptrl->Last ) A return-1; at Else - returni; - } - - voidInsert (intX, LIST *ptrl,inti) { - if(Ptrl->last = = MAX-1) { inprintf"Table Full"); - return; to } + if(i<1|| I>ptrl->last +2) { -printf"location is illegal ."); the return; * } $ intJ;Panax Notoginseng for(j = ptrl->last; J >= I-1; j--) { -Ptrl->data[j +1] = Ptrl->data[j +1]; the } +Ptrl->data[i-1] =x; Aptrl->last++; the return; + } - $ voidDelete (LIST *l,inti) { $ intJ; - if(i<1|| I>l->last +1) { -printf"There is no such location! "); the return; - }Wuyi for(j = i; J <= l->last; J + +) { theL->data[j-1] = l->Data[j]; - } Wul->last--; - } About $ voidPrint (LIST *l) { - inti; - for(i =0; I <= l->last; i++) -printf"%d", l->data[i]); A}
(i) Linear table structure 1 array implementation