Single-linked list traversal, reverse order, insert, delete
#include<stdio.h>#include<stdlib.h>#include<string.h>#defineBzero (A, B) memset (A, 0, b)//There is no bzero function under the Windows platform. Increase the expansion of the macro portability structNode {intData//Valid data structNode *pnext;//pointer to a node down }; structNode * Make_node (intdata) { structNode *p= (structnode*)malloc(sizeof(structnode)); if(null==p) {printf ("malloc error\n"); returnNULL; } //to clean up the requested memoryBzero (P,sizeof(structnode)); //Populating NodesP->data=data; P->pnext=null;//the first address to point to the next node in the future//The actual operation is given to the next node by the malloc returned by the pointer to him. returnp; } voidIn_tail (structNode *ph,structNode *new_node)//node Trailing insert { //(1) Find the last node first//(2) Insert intCnt=0; structNode *p=PH; while(null!=p->Pnext) {P=p->Pnext; CNT++; } P->pnext=New_node; PH->data=cnt+1;//Head node data represents the number of linked lists } voidIn_head (structNode *ph,structNode *New_node) { //head node next points to the new node address//The new node next points to the address of the first//head node data++New_node->pnext=ph->Pnext; PH->pnext=New_node; PH->data++; } voidErgodic (structNode *ph)//Traverse { intCnt=0; structNode *p=PH; /*printf ("------begins to traverse------\ n"),//This includes the head node while (Null!=p->pnext) {printf ("%d node data is%d\n", CNT, P->data); p=p->pnext; cnt++; } printf ("%d node data is%d\n", cnt,p->data); printf ("------End traversal------\ n"); } */printf ("------start traversing------\ n"); while(null!=p->Pnext) {CNT++; P=p->Pnext; printf ("The node%d data is%d\n",cnt,p->data); } printf ("------END traversal------\ n"); }voidDel_1 (structNode *ph,intNum//Delete cannot delete 0 (head node) based on number of nodes { //1 found//2 Delete//Delete (frees memory.) Point to Next) intCnt=0; structNode *p=PH; structnode *P_SB;;//temporary variable free memory while(null!=p->Pnext) {CNT++; if(num==CNT) {P_SB=p->pnext;//p is the previous node of the pre-delete pointp->pnext=p->pnext->pnext;//Skip to delete the node to point to the next node. Free(P_SB);//Freeing Memory Break; } P=p->pnext;//find the next node when the above conditions are not met } } voidDel_2 (structNode *ph,intData//Delete specified data { //1 found//2 Delete//Delete (frees memory.) Point to Next) structNode *p=PH; structnode *P_SB;;//temporary variable free memory while(null!=p->Pnext) { if(Data==p->pnext->data)//p is the previous node of the pre-delete point{p_sb=p->Pnext; P->pnext=p->pnext->Pnext; Free(P_SB); Continue; } P=p->Pnext; } } voidNixu (structNode *ph)//Reverse { structNode *p=ph->Pnext; structNode *P_SB;//temporary variable free memory if((Null==ph->pnext) | | (null==p))return;//no node direct exit while(null!=p->Pnext) {P_SB=p->Pnext; if(P==ph->pnext)//first node, put last face{p->pnext=NULL; } Else{p->pnext=ph->pnext;//The current node is inserted in front of the first node. //or, the current node as the first node. } PH->pnext=p;//Update the first node address. P=P_SB;//refers to a node down//p=p->pnext; //refers to a node down} in_head (ph,p); } intMain () {intN; //defining the head pointer//struct node *phead= NULL; structNode *phead=make_node (0);//Initialize the head node; //In_tail (Phead,make_node (2223));In_head (Phead,make_node (1)); In_head (Phead,make_node (2)); In_head (Phead,make_node (3)); In_head (Phead,make_node (4)); In_head (Phead,make_node (5)); In_head (Phead,make_node (6)); Ergodic (Phead); Nixu (Phead); /*printf ("Enter the number of nodes to delete:"); scanf ("%d", &n); Del_1 (Phead,n); */ //del_2 (phead,3);ergodic (Phead); return 0;}
C language implementation of single-linked list traversal, reverse, insert, delete