Seqlish.h
#include <stdio.h> #include <string.h> #define &NBSP;MAXSIZE&NBSP;100&NBSP;//defines the maximum length of a linear table typedef struct //defines an array of sequential table structures {data listdata [maxsize+1];//Save Order table int listlen;// Number of stored nodes in sequential table } SeqListType; void SeqListInit (SEQLISTTYPE&NBSP;*SL); // Initialization Order Table int seqlistlength (SEQLISTTYPE&NBSP;*SL); // returns the element quantity of the order table int seqlistadd (seqlisttype *sl, data data); // adding elements to the order table int SeqListInsert (seqlisttype *sl , int n , data data);// Inserting elements int seqlistdelete (seqlisttype *sl, int n) into the sequential table; // Delete elements from the order table data *seqlistfindbynum (seqlisttype *sl,int n);//return elements by ordinal Int seqlistfindbycont (Seqlisttype *sl, char *key);//Find int seqlistall by keyword ( SEQLISTTYPE&NBSP;*SL);//Traversal Order tableof the content
Seqlish.c
Void seqlistinit (SEQLISTTYPE&NBSP;*SL)//initialization sequence table {sl->listlen=0; //initialization, set order table length is zero 0}int Seqlistlength (SEQLISTTYPE&NBSP;*SL)//Returns the number of sequential table elements {return (Sl->listlen);} Int seqlistadd (Seqlisttype *sl,data data)//Add element to order table tail {if (sl->listlen>=maxsize) { printf ("The order is full, no more nodes are added!\n");// order table is full return 0;} sl->listdata[++sl->listlen]=data;return 1;} Int seqlistinsret (seqlisttype *sl,int n,data data) {int i;if (SL->ListLen>= MAXSIZE)//The number of sequential table nodes has exceeded the maximum number {printf ("Order table is full, cannot insert node!\n");return 0; //returns 0 to insert unsuccessful }if (n<1 | | &NBSP;N>SL->LISTLEN-1) //The insertion node ordinal is incorrect {printf ("Insert element ordinal error, cannot insert element!\n"); return 0;// return 0 means insert unsuccessful }for (i=sl->listlen;i>=n;i--) // tell the data in the sequential table to move backwards SL->ListData[i+1] = SL->ListData[i]; Sl->listdata[n] = data; // Insert node SL->ListLen++; // Order table node number increased return 1; // return successful insert }int seqlistdelete (seqlisttype *sl, int n) // Delete data element {int i;if in order table (n<1 | | n>sl->listlen+1) {printf ("Delete node ordinal error, cannot delete node!\n");return 0; // Returning 0 , indicates that the delete is unsuccessful }for (i=n;i<sl->listlen;i++) // moves the elements in the order table forward sl-> listdata[i]=sl->listdata[i+1]; sl->listlen--; // number of sequential table elements minus return 1; // return successfully delete}data *seqlistfindbynum (seqlisttype *sl , int n) // returns the data element {if (n<1 | |) by ordinal n>sl->listlen+1) // element ordinal incorrect {printf ("node ordinal error, cannot return node!\n");return null; // return 0 , indicates unsuccessful }return & (sl->listdata[n]);} Int seqlistfindbycont (seqlisttype *sl, char *key) // query nodes by keyword? {int i;for (i=1;i<=sl->listlen;i++) if (strcmp (sl->listdata[i].key,key) ==0) // If the desired node is found return i; // returns the node ordinal return 0; Traverse successor is not found, then return 0 }
Seqlishtest.c
#include <stdio.h>typedef struct {char key[15]; // node keyword char name[20];int age;} data; // define node type , can be defined as integral type, can also be defined as structure #include "SEQLISH.H" #include "SEQLISH.C" Int seqlistall (SEQLISTTYPE&NBSP;*SL) // Traverse the node in the Order table {int i;for (i=1;i<=sl->listlen;i++) printf ("(%s,%s,%d) \ n", Sl->listdata[i].key , sl->listdata[i].name,sl->listdata[i].age);} Int main () {int i; seqlisttype sl; // defines the order table variable data data, *data1; //defining nodes saving data type variables and pointer variables char key[15]; // Save Keyword seqlistinit (&SL); // Initialization Order table do { // loop Add number of nodes printf ("Enter the added node (School Number name age): Fflush (stdin); // empty input buffer scanf ("%s%s%d",& Data.key,&data.name,&data.age); if (data.age) {if (! Seqlistadd (&sl,data)) break; Else break;} while (1);p rintf ("The Order of nodes in the \ n sequential table is: \ n"); Seqlistall (&SL); Fflush (stdin);p rintf ("\ n to take out the sequence number of the node:"); scanf ("%d,&i"); // input node ordinal data1 = seqlistfindbynum (&sl,i); if (data1) printf ("% The D nodes are: (%s,%s,%d) \ n ", I,data1->key,data1->name,data1->age), Fflush (stdin);p rintf (" \n To find the keyword for the node: ") ;scanf ("%s ", key); // input keyword i=seqlistbycont (&sl,key);d ata1 = seqlistfindbynum (&sl,i); if (data1) //If the node pointer is not NULL printf ("%d node is: (%s,%s,%d) \ n", i,data1->key,data1->name,data1->age); Getch (); return 0;}
Data Structure List Learning Note 2