This implementation is the lead node of the single-linked list initialization, traversal, create, INSERT, delete, determine whether the linked list is empty, the list length function, the compilation environment is vs2013.
The conditions in which loops are inserted and removed in a function are not yet understood.
#include <iostream>using namespaceStd;typedefintStatus;typedefCharElemtype;//define the storage structure of the list, note that there is a different definition of the algorithm, is the definition of the structure of the name Lnode, that *next type as long as the Lnode type can betypedefstructlnode{elemtype data; Lnode*Next;} Lnode,*linklist;//each function declaration partStatus initlinklist (Linklist &L);//Initialize Status CreateList (linklist&l,intLength );//Create List status Traverselist (linklist l);//Traverse status GetLength (linklist l);//For length status IsEmpty (linklist L); /Determine if the empty status Insertlist (linklist&l,intTarget, elemtype e);//The first element of the target is inserted before the element status DeleteList (linklist&l,intTarget, Elemtype &e);//delete the first target elementvoidMain () {linklist link=NULL; intn=5; Initlinklist (link); CreateList (link, n); //printf ("%d", GetLength (link)); //char *s = IsEmpty (link)? " The linked list is empty! ":" The linked list is not empty! ";//printf ("Judging result:%s\n", s); //insertlist (link, 2, ' 3 ');Elemtype E; DeleteList (Link,5, E); printf ("the deleted element is:%c\n", E); Traverselist (link); GetChar ();}//List initialization function: Create a head pointer, assign a null value to the pointer fieldStatus initlinklist (Linklist &L) {L= (linklist)malloc(sizeof(Lnode)); if(!L) { return false; } L->next =NULL; return true;}//To create a linked list function:Status createlist (linklist &l,intlength) {linklist P=null, q=NULL; Q=L; Q-Next; for(inti =0; I <length; i++) {p= (linklist)malloc(sizeof(Lnode)); if(!p) { return false; } printf ("Enter the characters in the linked list: \ n"); scanf_s ("%c", &p->data); //2016.4.11 Error Reason: Microsoft rewrote the C function, added parameter detection//method: Change the scanf () function to the scanf_s () functionGetChar (); //The 2016.4.12 loop is read only for the first time, because the VS does not read into the carriage return, the input enter will be the input of the next loop, and it looks like it is not read into//Workaround: Write GetChar function after scanf, receive carriage returnQ->next =p; P->next =NULL; Q=p; } printf ("linked list created successfully! \ n"); return true;}//Linked list traversal functionStatus traverselist (linklist L) {linklist P=NULL; P= l->Next; //2016.4.12 times the first element is empty, and the last element is discarded because the head node does not contain a valid value.//so the start of the traversal is set at the first element node: L->next if(!p) { return false; } while(p)//Note that the conditions here are p, not p->next.{printf ("elements in a linked list:"); printf ("%c", p->data); printf ("\ n"); P= p->Next; } return true;}intgetlength (linklist L) {linklist P=l->next;//Note that the starting point here is still the first dollar node, so the returned list length is removed from the head node. intCount=0; while(p) {Count++; P= p->Next; } returncount;}//determine if the linked list is emptyStatus IsEmpty (linklist L) {if(L->next = =NULL) { return true; } return false;}//inserts an element in front of the first target elementStatus insertlist (linklist &l,intTarget, Elemtype e) {linklist P=l;//The starting point here is the head node, because it may be inserted before the first elementlinklist s =NULL; inti =0; while(P&&i < target-1){//I don't understand the conditions here.p = p->Next; ++i; } if(!p| | target>i-1){//Judging Illegal locations return false; } s= (linklist)malloc(sizeof(Lnode)); S->data =e; S->next = p->Next; P->next =s; return true;}//Delete the first target elementStatus deletelist (linklist &l,intTarget, Elemtype &e) { intI=0; Linklist P=L; linklist Q=NULL; while(P->next&&i < target-1) {p= p->Next; I++; } if(! (P->next) | | I>target-1){ return false; } q= p->Next; P->next = q->Next; E= q->data; Free(q); }
Data structure (C language Edition) The code implementation of related operation algorithm of linked list