Linked list
The first is the structural body
typedef int ElementType; To make it easier to modify the type of elements in the list typedef struct NODE{ELEMENTTYPE date;struct node* Next;} List; List L,*ptrl;
A function to find the length of a chain table
int Length (List *ptrl) {list* P =ptrl;int j=0;while (p) {p=p->next;j++;} return J;}
Find by Location
list* findkth (int k,list *ptrl) {List *p=ptrl;int i=0;while (p!=null && i<k) {p=p->next;i++;} if (i = = k) return p;else return NULL;}
Find by value
list* Find (ElementType X, list* Ptrl) {list* P =ptrl;while (p!=null && p->date!=x) {p=p->next;} return p;}
Insert
/* Construct node, find I-1 nodes, insert node */list* Insert (ElementType x,int i, list* Ptrl) {list* s, * p; if (i==1) {s= (list*) malloc (sizeof (List)); S->date = X; S->next = Ptrl; return s; } p = findkth (I-1,ptrl); if (p== NULL) {cout<< "parameter wrong" <<endl; return NULL; } else{s= (list*) malloc (sizeof (List)); S->date = X; S->next = p->next;//position cannot be reversed p->next = s; return Ptrl; }}
Delete
list* Delete (int i,list* ptrl) {List *p, *s;if (i==1) {//Delete header node S=ptrl;if (ptrl!=null) ptrl=ptrl->next;else return NULL; Free (s); Don't forget to release the space return Ptrl;} p = findkth (I-1,ptrl), if (p== NULL) {printf ("%d node does not exist", i-1); return NULL;} else if (P->next = = NULL) {printf ("%d node does not exist", I); return NULL;} else{s=p->next;//with the newly created pointer to P->next = s->next; free (s), return Ptrl;}
"Main function of the test"
int main () {list* p= (list*) malloc (sizeof (List));p->next = Null;p->date=2;p=insert (5,1,p);p =insert (3,2,p);p = Insert (5,2,p);cout<< "Length: " <<length (p) <<endl; list* po = findkth (2,p); if (po==null) cout<< "does not exist" <<ENDL;ELSE{COUT<<PO->DATE<<ENDL;} P=delete (2,p);p o = findkth (2,p), if (po==null) cout<< "not Present" <<endl;else{cout<<po->Date<< Endl;} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Realization of---Linear table of data structure learning