#include "stdio.h"#include "string.h"#include "ctype.h"#include "stdlib.h"#include "io.h"#include "math.h"#include "time.h"#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0#define MAXSIZE 20/* Storage space Initial allocation * /typedef intStatus;/ * Status is the type of function, whose value is the function result status code, such as OK, etc. * /typedef intElemtype;/ * The Elemtype type is based on the actual situation and is assumed to be int * /Status visit (elemtype c) {printf("%d", c);returnOK;}typedef structnode{elemtype data;structNode *next;} Node;typedef structNode *linklist;/ * Define linklist * // * Initialize sequential linear table * /Status initlist (linklist *l) {*l= (linklist)malloc(sizeof(Node));/ * Generate head node and make L point to this head node * / if(! (*l))/ * Storage Allocation failed * / returnERROR; (*l)->next=null;/ * Pointer field is empty * / returnOK;}/* Initial conditions: Sequential linear table L already exists. Operation Result: Returns True if L is an empty table, otherwise false */Status listempty (linklist L) {if(L->next)returnFALSE;Else returnTRUE;}/* Initial conditions: Sequential linear table L already exists. Operation Result: Reset L to Empty table */Status clearlist (linklist *l) {linklist p,q; p= (*l)->next;/ * p points to the first node * / while(p)/ * Not at the end of the table * *{q=p->next; Free(p); p=q; } (*l)->next=null;/ * head node pointer field is empty * / returnOK;}/* Initial conditions: Sequential linear table L already exists. Operation Result: Returns the number of data elements in L */intListlength (linklist L) {intI=0; Linklist p=l->next;/ * p points to the first node * / while(p) {i++; p=p->next; }returni;}/* Initial conditions: Sequential linear table L already exists, 1≤i≤listlength (l) *//* Operation result: Use E to return the value of the I data element in L */Status Getelem (linklist L,intI,elemtype *e) {intJ Linklist p;/ * Declare a node p * /p = l->next;/ * Let P point to the first node of the list L * /j =1;/ * J for Counter * / while(P && j<i)/* p is not empty or the counter J is not equal to I, the loop continues */{p = p->next;/ * Let P point to the next node. * /++j; }if(!p | | j>i)returnERROR;/ * element I does not exist * /*e = p->data;/ * Take the first element of the data * / returnOK;}/* Initial conditions: Sequential linear table L already exists *// * Operation Result: Returns the bit order of the 1th data element in L that satisfies the relationship with E. *// * If such a data element does not exist, the return value is 0 * /intLocateelem (linklist l,elemtype e) {intI=0; Linklist p=l->next; while(p) {i++;if(p->data==e)/ * Find such data element * / returnI p=p->next; }return 0;}/* Initial conditions: Sequential linear table L already exists, 1≤i≤listlength (l), *//* Operation result: Insert a new data element before the I position in L e,l length plus 1 */Status Listinsert (linklist *l,intI,elemtype e) {intJ Linklist p,s; p = *l; j =1; while(P && J < i)/ * Find the first node * /{p = p->next; ++j; }if(!p | | j > i)returnERROR;/ * element I does not exist * /s = (linklist)malloc(sizeof(Node));/* Generate new node (c standard function) */S->data = e; S->next = p->next;/* Assign the subsequent node of P to the successor of S * /P->next = s;/ * Assign S to the successor of P * / returnOK;}/* Initial conditions: Sequential linear table L already exists, 1≤i≤listlength (l) *//* Operation Result: Delete the I data element of L and return its value with E, the length of L minus 1 */Status Listdelete (linklist *l,intI,elemtype *e) {intJ Linklist p,q; p = *l; j =1; while(P->next && J < i)/ * Traverse search for element i elements * /{p = p->next; ++j; }if(! (P->next) | | J > I)returnERROR;/ * element I does not exist * /Q = p->next; P->next = q->next;/* Assign the successor of Q to the successor of P * /*e = q->data;/ * Send the data in the Q node to E * / Free(q);/ * Let the system reclaim this node and free up memory * / returnOK;}/* Initial conditions: Sequential linear table L already exists *//* Operation result: output per data element of L, in turn */Status Listtraverse (linklist L) {linklist p=l->next; while(p) {visit (p->data); p=p->next; }printf("\ n");returnOK;}/* Generate n-element values randomly, create single-chain linear table with header nodes L (head interpolation) */voidCreatelisthead (linklist *l,intN) {linklist p;intI Srand (Time (0));/ * Initialize random number seed * /*l = (linklist)malloc(sizeof(Node)); (*l)->next = NULL;/ * First set up a single-link list of lead nodes * / for(i=0; i<n; i++) {p = (linklist)malloc(sizeof(Node));/ * Create a new node * /P->data = rand ()% -+1;/ * Randomly generate a number within 100 * /P->next = (*l)->next; (*l)->next = p;/ * Insert to table header * /}}/* Generate n-element values randomly, create single-chain linear table with header nodes L (tail interpolation) */voidCreatelisttail (linklist *l,intN) {linklist p,r;intI Srand (Time (0));/ * Initialize random number seed * /*l = (linklist)malloc(sizeof(Node));/ * L for the entire linear table * /R=*l;/ * R is the node that points to the tail * / for(i=0; i<n; i++) {p = (Node *)malloc(sizeof(Node));/ * Create a new node * /P->data = rand ()% -+1;/ * Randomly generate a number within 100 * /r->next=p;/ * Point the pointer at the end of the footer to the new node * /.r = P;/ * Define the current new node as the footer terminal node * /} r->next = NULL;/ * Indicates end of current list * /}intMain () {linklist L; Elemtype e; Status i;intJ,k; I=initlist (&L);printf("after initializing L: Listlength (L) =%d\n", Listlength (L)); for(j=1; j<=5; j + +) I=listinsert (&l,1, j);printf("The table head in L is inserted after the following: L.data="); Listtraverse (L);printf("Listlength (L) =%d \ n", Listlength (L)); I=listempty (L);printf("L is empty: i=%d (1: Yes 0: NO) \ n", i); I=clearlist (&L);printf("Empty L After: Listlength (l) =%d\n", Listlength (L)); I=listempty (L);printf("L is empty: i=%d (1: Yes 0: NO) \ n", i); for(j=1; j<=Ten; j + +) Listinsert (&L,J,J);printf("After the footer of L is inserted in 1~10: L.data="); Listtraverse (L);printf("Listlength (L) =%d \ n", Listlength (L)); Listinsert (&l,1,0);printf("After the head of L is inserted in 0: l.data="); Listtraverse (L);printf("Listlength (L) =%d \ n", Listlength (L)); Getelem (L,5, &e);printf("The value of the 5th element is:%d\n", e); for(j=3; j<=4; j + +) {K=locateelem (l,j);if(k)printf("%d element has a value of%d\n", k,j);Else printf("no element with value%d \ n", j); } k=listlength (L);/ * k for table length * / for(j=k+1; j>=k;j--) {i=listdelete (&l,j,&e);/ * Delete the first J data * / if(I==error)printf("Deletion of%d data failed \ n", j);Else printf("Delete element%d value is:%d\n", j,e); }printf("Output The elements of L in turn:"); Listtraverse (L); j=5; Listdelete (&l,j,&e);/ * Delete 5th data * / printf("Delete element%d value is:%d\n", j,e);printf("Output The elements of L in turn:"); Listtraverse (L); I=clearlist (&L);printf("\ n empty L after: Listlength (l) =%d\n", Listlength (L)); Createlisthead (&l, -);printf("Integral creation of elements of L (head interpolation method):"); Listtraverse (L); I=clearlist (&L);printf("\ n Delete L after: Listlength (l) =%d\n", Listlength (L)); Createlisttail (&l, -);printf("Integral creation of elements of L (tail interpolation method):"); Listtraverse (L);return 0;}
Review (data structure): Linked list: C language