for a single linked list, it is not as complex as a doubly linked list, it has only head nodes, tail nodes, node data, and subsequent pointers. In the following I realized the single-linked list Add delete interpolation check change. #include <stdio.h> #include <assert.h> #include <malloc.h> #include < stdlib.h>typedef int datatype;typedef struct slistnode{datatype data;struct Slistnode*next;} Slistnode;void pushback (slistnode*&phead, datatype x); Void popback (SListNode * &phead); void printslist (Slistnode *&phead); Void pushfrot (Slistnode*&phead,datatype &NBSP;X); Void popfront (Slistnode*&phead); Slistnode *find (slistnode*phead, datatype x);//void insert (SListNode*pHead, Datatype &NBSP;POS,&NBSP;DATATYPE&NBSP;X); Void insert (slistnode*phead, datatype x); void Erase (slistnode*&phead,slistnode *pos ); Void insertnonode (Slistnode *pos, datatype &NBSP;X); Slistnode*&nbsP;_buynode (datatype x) {slistnode *temp = (slistnode*) malloc (sizeof (Slistnode));temp-> Data = x;temp->next = null;return temp;} Void pushback (slistnode*&phead, datatype x) {//1 null 2 not empty if (phead == null) {phead = _buynode (x);} else{slistnode *tail = phead;while (tail->next != null) {tail = Tail->next;} Tail->next = _buynode (x);}} Void popback (slistnode *&phead) {//1 null 2 one node 3 multiple nodes if (Phead == null) {return;} else if (phead->next == null) {free (phead);p head = null;} else{slistnode *tail = phead; slistnode *tem = null;while (tail->next != null) {Tem = tail;tail = tail->next;} Free (tail); Tem->next = null; }}void printslist (Slistnode *&phead) //Print List {slistnode*cur = phead;while (Cur!=NULL) {printf ("%d->", cur->data); cur = cur->next;} printf ("null\n");} Void pushfrot (slistnode*&phead, datatype x) //Head plug {if (pHead == null) {phead = _buynode (x);} Else{slistnode *tmp = _buynode (x); tmp->next = phead;phead = tmp;}} Void popfront (Slistnode*&phead) //single-linked header delete {//1 null//2 a node//3 More than one node if (phead == null) {return;} Else if (phead->next == null) {free (phead);p head = null;} Else{slistnode *tmp = phead;phead = phead->next;free (TMP);}} Slistnode *find (slistnode*phead, datatype x) //find node {//assert (PHead); slistnode *tail = null;//current pointer datatype tmp ; //save node data if (Phead->data == x) {return phead;} else{tail=phead->next;while (tail!= null) {tmp = tail->data;if (tmp == &NBSP;X) //compares the node data to the number to find {return tail; //returns the address}else{tail =tail->next to find the node;}} printf ("No data Found!") \ n ");}} Void insert (slistnode*pos, datatype x) ////at the specified node insert data {assert (POS); Slistnode *tmp = _buynode (x); tmp->next = pos->next;pos->next = tmp ;} Void erase (slistnode *&phead,slistnode *pos) //Delete the node at the specified position {assert (POS); assert (phead); if (Phead == pos) {phead = phead->next;free (POS); return;} slistnode *prv = phead;while (PRV) {if (prv->next == pos) {Prv->next = pos->next;free (POS); break;} Prv = prv->next;}} Removes a headless single-linked list Void delnonode (Slistnode *pos) {assert (POS); assert (Pos->next); slistnode *del = pos->next; Slistnode&nbSp;*next = del->next;pos->data = del->data;pos->next = next;free (Del );} Inserts a node Void insertnonode (slistnode *pos, datatype x) {assert (POS) before a non-head node in the headless single-linked list;//assert ( Pos->next) method of;//1 //datatype temp = 0;//slistnode *behind = pos;// Slistnode*prv =_buynode (x);//slistnode *next = behind->next;//pos->next = prv;//prv->next = next;//temp = pos->data;//pos->data = prv->data;// prv->data = temp;//2 method Slistnode*prv = _buynode (pos->data);p rv->next = pos->next;pos->next = prv;pos->data = x;} Find Intermediate node Slistnode *findmidnode (slistnode *phead) {slistnode *fast = phead; slistnode *slow = phead;while (Fast&&fast->next) {fast = fast-> Next->next; slOw = slow->next;} Return slow;} //Find the penultimate K-node Slistnode *findknode (slistnode *phead, int k) {SListNode *fast = phead; slistnode *slow = phead; /*for (int i = 1; i <=k-1; i++) { fast = fast->next;} while (fast->next) {slow = slow->next;fast = fast->next;} */while (fast&&k--) {fast= fast->next;if (Fast == null) Return NULL;} while (FAST) {slow = slow->next;fast = fast->next;} Return slow;} From tail to head print the list void printtailtohead (slistnode*phead) {if (phead) {printtailtohead (Phead->next);p rintf ("% d ", phead->data);}} Slistnode *reverse (Slistnode *phead)//reverse {slistnode *cur = phead; of single-linked list slistnode *newhead = null;while (cur) {Slistnode*tmp =cur;cur = cur->next =t+P->next =newhead;newhead = tmp;} Return newhead;} Void test1 () {slistnode*list = null; Pushback (list, 1); Pushback (list, 3); Pushback (LIST,&NBSP;2); Pushback (list, 4); Pushback (LIST,&NBSP;6); Printslist (list);} Void test2 () {slistnode*list = null; Pushback (list, 1); Pushback (LIST,&NBSP;2); Pushback (list, 3); Pushback (list, 4); Pushback (list, 5); Pushback (LIST,&NBSP;6); Pushback (LIST,&NBSP;7); Printslist (list);//find (list, 4);//insert (list,7, 8); Slistnode *pos = find (list, 1); Erase (list,pos ); Printslist (list);} Void test3 () {slistnode*list = null; Pushback (list, 1); Pushback (LIST,&NBSP;2); Pushback (list, 3); Pushback (list, 4); Pushback (list, 5); Pushback (LIST,&NBSP;6); Pushback (LIST,&NBSP;7); Printslist (list); Slistnode *pos = find (list ,7); Insert (POS,&NBSP;2); Printslist (list);} Void test4 () {slistnode*list = null; Pushback (list,&NBSP;1); Pushback (LIST,&NBSP;2); Pushback (list, 3); Pushback (list, 4); Pushback (list, 5); Pushback (LIST,&NBSP;6); Pushback (LIST,&NBSP;7); Printslist (list);/*slistnode *pos = list;delnonode (POS); */slistnode *pos = find ( list,1); Insertnonode (pos, 9); Printslist (list);} VOID&NBSP;TEST5 () {slistnode *list = null; Pushback (list, 1); Pushback (list, 8); Pushback (LIST,&NBSP;2); Pushback (list, 3); Pushback (list, 4); Pushback (list, 5); Printslist (list);//slistnode*pos = findmidnode (list); Slistnode*pos =findknode (list, 5);p rintf ("%d\n", pos->data);//printslist (list);} Void test6 () {slistnode*list = null; Pushback (list, 1); Pushback (list, 3); Pushback (LIST,&NBSP;2); Pushback (list, 4); Pushback (list, 6);// slistnode*pos=reverse (list); Printtailtohead (POS);} Int main () {//test1 (); Test6 (); System ("pause"); return 0;}
If you find the wrong place or have other suggestions, we hope to make valuable comments. Thank you!!!
This article is from the "Rookie Experience" blog, please be sure to keep this source http://10738432.blog.51cto.com/10728432/1763144
Single linked list of additions and deletions to check the inverse of the K-node and other issues