Header file #pragma once#include <stdio.h> #include <assert.h> #include <malloc.h > #include <stdlib.h>typedef int DateType;typedef struct LinkNode{ Datetype _data; struct linknode* _next;} linknode;void printlist (linknode* phead) //Print { linknode* begin = pHead; while (begin != null) { printf ("%d->", begin->_ Data) begin = begin->_next; //here to note, do not write the reverse:begin->_next=begin; } printf ("null\n");} Linknode* buynode (DATETYPE&NBSP;X) //initialization Node { linknode* tmp = (linknode*) malloc (sizeof (Linknode)); if (tmp == null) { printf (" Malloc faile "); exit ( -1); } tmp->_next = null; tmp->_data = x; return tmp;} void Pushfront (linknode*& phead, datetype x)//head Insert { //empty list if (phead == NULL) //proves to be empty linked list, need to open node { phead = buynode (x); } else { linknode* nownode = buynode (x); nownode->_next = phead; //the head pointer to my _next, I point to you, realize the fork phead = nownode; }}void popfront ( Linknode*& phead)//First Delete, there are three kinds of situation:{ if (phead == null) // Air { return; } else if (PHead->_next == null) // one node { free (phead); phead = null; } else // two nodes and above { LinkNode*del = pHead; //Save the back node first, then the previous delete phead = phead->_next; free (DEL); } Void pushback (linknode*& phead, datetype x)//tail plug { // if (phead == null) //CertificateMing is the empty list, need to open up the node { phead = buynode (x); } else { linknode* tail = pHead; while (tail->_next != null) // Note that the conditions of the loop cannot be written tail != NULL { tail = tail->_next; } tail->_next = buynode (x); }}void popback (LinkNode*& pHead) //tail { if (phead == null) //No Nodes { return; } else if (phead->_next == null) //a node { free (pHead); pHead = NULL; } else // Two nodes and above &NBSP;{&NBSP;&NBSP;//LINKNODE*&Nbsp;cur = phead; //This requires two pointers, one after the other, since deleting a node requires the previous node // linknode* prev = null; //while (cur->_next != null) //Note that the conditions of the cycle cannot be written cur != null //{ // prev = cur; // cur = cur->_next; //} //prev->_next = null; //free (cur); //or the following method: linknode* prevtail = phead; while (prevtail->_next->_next!=null) { prevtail = prevtail->_next; } free (prevTail->_ Next); prevtail->_next = null; }}linknode* find (LinkNode* pHead, DATETYPE&NBSP;X)// Find a number { linknode* cur = phead; while (cur) { if (CUR->_DATA&NBSP;==&NBSP;X) { return cur; } cur = cur->_next; } return null;} Void insert (linknode* pos, datetype x) //arbitrary position Insert { linknode* tmp = buynode (x); assert (POS); tmp->_next = pos->_next; //this way, pay special attention to, . Pos->_next = tmp; /*linknode* tmp = buynode (x); LinkNode* next = null; assert (POS); next = pos->_next; pos->_next = tmp; tmp->_next = next;*/}void erase (linknode*& phead,linknode* POS) //Delete a node { assert (POS); //pos is the head node //pos is the intermediate node if (phead == POS) //only one node { LinkNode* del = pos; Phead = phead->_next; free (del); } else //There are multiple node conditions { //pos not in the list LinkNode* prev = pHead; while (Prev && prev->_next != pos) { prev = prev->_next; } if (prev) { Prev->_next = pos->_next; free (POS); } }}linknode* Reverse (linknode*& phead) //reverse chain list { linknode* cur = phead; linknode* newhead = null; linknode* tmp; while (cur) { tmp = cur; cur = cur->_next; tmp->_next = newhead; newhead = tmp; } return newhead;} Linknode* findmidnode (linknode* phead) //Find middle Node { linknode* first = phead; linknode* second = phead; while (second->_next != null) { if (second->_next->_next == null) { return first; break; } else { first = first->_next; second = second->_next->_next; } } return first;} Linknode* findnodefromback (linknode* phead, int x)//Find the penultimate x node { int i = 0; linknode*first = phead; linknode*second = phead; while (first && (i < x) { first = first->_next; i++; } if (i < x) { return NULL; } while (first) { first = first->_next; second = second->_next; } return second;} Delete a non-tailed node of a headless single-linked list Int geTlistlength (Linknode* phead) //get the chain table length { int count = 0; linknode* begin = pHead; while (BEGIN) { count++; begin = begin->_next; } return count;} Void destorylist (linknode*& phead) //Delete all nodes { linknode* begin = pHead; while (BEGIN) { LinkNode* del = begin; begin = begin->_next; free (del); } phead = null;} Source file #include "Slist.h" void test1 ()//Head action { linknode* phead = null; //head pointer pushfront (phead, 1); pushfront (phead, 2); pushfront (phead, 3); PushFront ( PHEAD,&NBSP;4); printlist (phead);//Print popfront (phead); popfront (Phead); popfront (PHead); popfront (Phead); popfront (Phead); printlist (phead);//Print}void tesT2 ()//tail operation { linknode* phead = null; pushback (phead, 1); pushback (PHead, &NBSP;2); pushback (phead, 3); pushback (phead, 4); printlist (pHead);//Print popback ( Phead); popback (Phead); popback (Phead); popback (Phead); popback (Phead); PrintList ( Phead)///print}VOID&NBSP;TEST3 ()//Get the list length { linknode* phead = null; pushback (pHead, 1 ); pushback (phead, 2); pushback (phead, 3); pushback (phead, 4); PrintList ( Phead);//Print int ret = getlistlength (phead); printf ("%d\n", ret);} VOID&NBSP;TEST4 ()//delete entire list { linknode* phead = null; pushback (phead, 1); Pushback (phead, 2); pushback (phead, 3); pushback (phead, 4); PrintList (pHead);// Print destorylist (phead); printlist (Phead);} VOID&NBSP;TEST5 ()//Find a number to find this number after inserting a number { linknode* phead = null; pushback (Phead, 1); pushback (phead, 2); pushback (phead, 3); pushback (pHead, 4); Printlist (Phead); linknode* ret = find (phead, 2); printf ("%d\n", ret->_ data); insert (ret, 8); //after finding 2, insert a 8 printlist (Phead) in the back of 2; ret = Find (phead, 7); //7 does not exist printf ("ret=null:%p\n", ret);} Void test6 () //deletes the number of the specified position { Linknode* phead = null; pushback (phead, 1); pushback (pHead, 2); Pushback (phead, 3); pushback (phead, 4); printlist (phead); linknode* ret = find (PHEAD,&NBSP;2); erase (Phead,ret); //after finding 2, remove 2 printlist (phead);} VOID&NBSP;TEST7 () //Find the middle node { Linknode* phead = null; pushback (phead, 1); Pushback (phead, 2); pushback (phead, 3); pushback (phead, 4); PushBack (PHead, &NBSP;5); pushback (phead, 6); //pushback (phead, 7); printlist (pHead); LinkNode* ret=reverse (Phead); printlist (ret); /*linknode*ret=findmidnode (Phead); printf ("%d\n", ret->_data); */}void test8 () //returns the penultimate K node { linknode* phead = null; pushback (phead, 1); PushBack ( PHEAD,&NBSP;2); pushback (phead, 3); pushback (phead, 4); pushback (pHead, 5); pushback (phead, 6); printlist (Phead); linknode* ret = findnodefromback ( phead,3) printf ("%d\n", ret->_data); /*linknode*ret=findmidnode (Phead); printf ("%d\n", ret->_data); */}int main () { //test1 (); //test2 (); //test3 (); //test4 (); Test5 (); //test6 (); //test7 (); test8 (); return 0;}
Single linked list basic operation