#include <stdio.h>#include<stdlib.h>//Defining Nodestypedefstructnode{intdata; structNode *Pnext;}*Pnode,node;//initialization functionPnode create_list ();//Create a node//print out a linked listvoidtraverse_list (pnode);//Delete Node pos starting from 1voidDelete_node (Pnode,int,int*);//Insert Node pos starting from 1voidInsert_data (Pnode,int,int );voidMain () {//int val; //defining the head pointerPnode phead=NULL; Phead=create_list (); //Delete_node (phead,-1,&val);Insert_data (Phead,-2,2); Traverse_list (Phead);} Pnode create_list () {intLen; inti; intVal; //defining the head nodePnode phead= (Pnode)malloc(sizeof(Node)); //An error occurs when allocating memory fails, such as in case of memory failure, which returns null if(Phead = =NULL) {printf ("Memory allocation Failure"); } pnode Ptail=Phead; //point the Pointer field of the head node to null if no first node pointer field is NULLptail->pnext=NULL; /*idea: Define the node Ptail, always point to the tail node, initially only the head knot nod node is also the tail nodes, so point to the head junction will be attached to the new pnew node behind the Ptail node, after mounting, PTA Il points to the new pnew node so that Ptail always points to the tail node*/printf ("Please enter the number of initialization nodes \ n"); scanf ("%d",&Len); for(i=0; i<len;i++) {printf ("Please enter a value for the node"); scanf ("%d",&val); Pnode pnew= (Pnode)malloc(sizeof(Node)); Pnew->data=Val; Ptail->pnext=pnew; Pnew->pnext=NULL; Ptail=pnew; } //return to first node returnPhead;}voidtraverse_list (Pnode phead) {Pnode P= phead->Pnext; //printf ("%d\n", phead->pnext); if(p = =NULL) {printf ("The linked list is empty \ n"); Exit (0); } while(P! =NULL) {printf ("%d\n",p->data); P=p->Pnext; }}voidDelete_node (Pnode Phead,intPosint*val) { intI=0; if(phead->pnext==NULL) {printf ("node is empty"); Exit (0); } if(Pos <0) {printf ("No Data"); Exit (0); } Pnode P=Phead; Pnode T; Pnode R; while(I! =POS) { //printf ("%d\n", I=t->data); exit (0);t=p; if(i = = pos-1){ //printf ("%d\n", I=p->pnext->pnext->data); exit (0); //exit (0);R=p->Pnext; T->pnext=p->pnext->Pnext; Free(R); R=NULL; //exit (0); return; } I++; P=p->Pnext; }}voidInsert_data (Pnode Phead,intPosintval) { intI=0; Pnode P=Phead; while(P! = NULL && i<pos-1) {p=p->Pnext; I++; } if(p = = NULL | | i>pos-1) {printf ("Insert Error"); return ; } pnode pnew= (Pnode)malloc(sizeof(Node)); if(Pnew = =NULL) {printf ("Memory allocation Failure"); Exit (-1); } Pnode T=p->Pnext; Pnew->data=Val; Pnew->pnext=T; P->pnext=pnew;}
C-language implementation of linked list and its operation