[C/C ++ school] 0802-chain stack/linked list queue and priority queue/encapsulated linked list library, 0802-chain
Chain Stack
// Stacklinknode. h # define datatype intstruct stacknode {int num; // number datatype data; // data struct stacknode * pNext; // pointer field}; typedef struct stacknode StackNode; // simplify StackNode * init (StackNode * phead); // initialize StackNode * push (StackNode * phead, int num, datatype data ); // stack StackNode * pop (StackNode * phead, StackNode * poutdata); // stack StackNode * freeall (StackNode * phead ); // clear StackNode * printfall (StackNode * phead); // print
// Stacklinknode. c # include "stacklinknode. h "# include <stdio. h> # include <stdlib. h> StackNode * init (StackNode * phead) // initialize {return NULL;} StackNode * push (StackNode * phead, int num, datatype data) // stack {StackNode * pnewnode = (StackNode *) malloc (sizeof (StackNode); // create a node pnewnode-> num = num; pnewnode-> data = data; pnewnode-> pNext = NULL; // open the node and assign the value if (phead = NULL) // empty linked list to directly connect to {phead = pnewnode; // connect to a node} else {Sta CkNode * p = phead; while (p-> pNext! = NULL) {p = p-> pNext; // always forward} p-> pNext = pnewnode; // insert} return phead; // return header node} StackNode * printfall (StackNode * phead) {if (phead = NULL) {return NULL;} else {printf ("% d, % d, % p, % p \ n ", phead-> num, phead-> data, phead, phead-> pNext); printfall (phead-> pNext ); // print} StackNode * pop (StackNode * phead, StackNode * poutdata) {if (phead = NULL) {return NULL; // No element} else if (phead-> pNext = NULL) {poutdata-> num = phead-> num; p Outdata-> data = phead-> data; // get data free (phead); // release the memory phead = NULL; // only one node returns phead ;} else {StackNode * p = phead; while (p-> pNext! = NULL) {p = p-> pNext; // loop to the second to last node} poutdata-> num = p-> pNext-> num; poutdata-> data = p-> pNext-> data; // retrieve data free (p-> pNext); // release p-> pNext = NULL; return phead ;}} // Delete All node StackNode * freeall (StackNode * phead) {if (phead = NULL) {return NULL;} else {StackNode * p1 = NULL, * p2 = NULL; p1 = phead; // header node while (p1-> pNext! = NULL) {p2 = p1-> pNext; // save the next node p1-> pNext = p2-> pNext; // skip p2free (p2 ); // release node} free (phead); return NULL ;}}
# Define _ CRT_SECURE_NO_WARNINGS # include <stdio. h> # include <stdlib. h> # include "stacklinknode. h"/* uses the chain stack and advanced post-release principles. Convert a 10-digit number to a 2-digit number */void main01 () {int num; scanf ("% d", & num ); printf ("num = % d \ n", num); // print the data StackNode * phead = NULL; // create a chain stack header node printf ("\ n"); while (num) {printf ("% d \ n", num % 2 ); phead = push (phead, num % 2, 0); num/= 2;} while (phead! = NULL) {StackNode * pout = (StackNode *) malloc (sizeof (StackNode); phead = pop (phead, pout); printf ("% d ", pout-> num); // output stack} system ("pause");} void main () {StackNode * phead = NULL; // create a chain stack header node phead = init (phead); // set the stack to empty phead = push (phead, 1, 1); phead = push (phead, 2, 11); phead = push (phead, 3,111); phead = push (phead, 4, 1111); phead = push (phead, 5, 11111 ); printfall (phead); phead = freeall (phead); printf ("\ n release "); Printfall (phead); // while (phead! = NULL) // {// Save the data of the output stack // printf ("output stack \ n"); // StackNode * pout = (StackNode *) malloc (sizeof (StackNode); // phead = pop (phead, pout); // printf ("\ n"); // printfall (phead ); // printf ("\ n % d, % d", pout-> num, pout-> data after stack output ); //} system ("pause ");}
Linked List queue and priority queue
// Queue. hstruct queue {int num; // indicates the data int high; // priority 1111 struct queue * pNext; // store the address of the next node}; typedef struct queue; // simplify the Queue * init (Queue * queueA); // initialize the Queue * EnQueue (Queue * queueA, int num, int high ); // Queue * DeQueue (Queue * queueA, Queue * pout); // Queue * freeall (Queue * queueA); // clear void sort (Queue * queueA ); // Queue void printfall (Queue * queueA) with priority; // print all data, recursively returning Queue * insertEnQueue (Queue * queueA, int num, int high );
// Queue. c # include "Queue. h "# include <stdio. h> # include <stdlib. h> Queue * init (Queue * queueA) // initialize {return NULL;} Queue * EnQueue (Queue * queueA, int num, int high) // Queue in sequence {Queue * pnewnode = (Queue *) malloc (sizeof (Queue); // allocate the memory pnewnode-> num = num; pnewnode-> high = high; pnewnode-> pNext = NULL; if (queueA = NULL) // The linked list is empty {queueA = pnewnode; // sort (queueA); // The queue returns queueA; // return value} else {Queue * p = queueA; // header node while (p-> PNext! = NULL) {p = p-> pNext;} // determine the position to insert p-> pNext = pnewnode; // insert // sort (queueA ); // Queue return queueA; // return} Queue * DeQueue (Queue * queueA, Queue * pout) // queues in sequence {if (queueA = NULL) {return NULL ;} else {pout-> num = queueA-> num; pout-> high = queueA-> high; // assign a value to Queue * ptemp = queueA; // record the address queueA = queueA-> pNext; // skip queueAfree (ptemp); // release the node return queueA;} Queue * freeall (Queue * queueA) // clear {} Queue * insertEnQueue (Queue * q UeueA, int num, int high) // Queue inserts {Queue * pnewnode = (Queue *) malloc (sizeof (Queue); // allocates the memory pnewnode-> num = num; pnewnode-> high = high; if (queueA = NULL) // The node is NULL {pnewnode-> pNext = NULL; queueA = pnewnode; return queueA ;} else {if (pnewnode-> high> queueA-> high) {pnewnode-> pNext = queueA; // insert queueA = pnewnode in the header; // point to this node return queueA ;} else {Queue * p = queueA; // header node while (p-> pNext! = NULL) {p = p-> pNext;} // p loop to the end if (pnewnode-> high <= p-> high) {p-> pNext = pnewnode; pnewnode-> pNext = NULL; return queueA;} else {Queue * p1, * p2; p1 = p2 = NULL; // avoid pointer p1 = queueA; // header node while (p1-> pNext! = NULL) {p2 = p1-> pNext; if (p1-> high> = pnewnode-> high & p2-> high <pnewnode-> high) {pnewnode-> pNext = p2; p1-> pNext = pnewnode; // insert break;} p1 = p1-> pNext;} return queueA ;}}}} void sort (Queue * queueA) // Queues with priority {if (queueA = NULL | queueA-> pNext = NULL) {return ;} // for (Queue * p1 = queueA; p1! = NULL; p1 = p1-> pNext) // {// for (Queue * p2 = queueA; p2! = NULL; p2 = p2-> pNext) // {// if (p1-> high> p2-> high) // {// Queue temp; // temp. num = p1-> num; // p1-> num = p2-> num; // p2-> num = temp. num; // temp. high = p1-> high; // p1-> high = p2-> high; // p2-> high = temp. high; // exchange node data //} void printfall (Queue * queueA) // recursion {if (queueA = NULL) {return ;} else {printf ("% d, % d, % p, % p \ n", queueA-> num, queueA-> high, queueA, queueA-> pNext ); printfall (queueA-> pNext); // enter the next node }}
// Main. c # include <stdio. h> # include <stdlib. h> # include "Queue. h "void main () {Queue * phead = NULL; // create the header node phead = init (phead); // initialize phead = insertEnQueue (phead, 1, 1 ); printf ("\ n"); printfall (phead); phead = insertEnQueue (phead, 2, 12); printf ("\ n"); printfall (phead ); phead = insertEnQueue (phead, 3, 3); printf ("\ n"); printfall (phead); phead = insertEnQueue (phead, 4, 14 ); printf ("\ n"); printfall (phead); phead = insertE NQueue (phead, 5, 5); printf ("\ n"); printfall (phead); phead = insertEnQueue (phead, 6, 16 ); printf ("\ n"); printfall (phead); phead = insertEnQueue (phead, 6, 0); printf ("\ n"); printfall (phead ); phead = insertEnQueue (phead, 7, 0); printf ("\ n"); printfall (phead); phead = insertEnQueue (phead, 8, 0 ); printf ("\ n"); printfall (phead); phead = insertEnQueue (phead, 9, 1); printf ("\ n"); printfall (phead ); phead = insertEnQueue (ph Ead, 10, 0); printf ("\ n"); printfall (phead); phead = insertEnQueue (phead, 11, 16); printf ("\ n "); printfall (phead); phead = insertEnQueue (phead, 111, 19); printf ("\ n"); printfall (phead); // while (phead! = NULL) // if it is not NULL, continue // {// allocate memory // Queue * ptemp = (Queue *) malloc (sizeof (Queue )); // phead = DeQueue (phead, ptemp); // printf ("\ n pull once \ n"); // printfall (phead ); // printf ("\ n pulled out is % d, % d", ptemp-> num, ptemp-> high); //} system ("pause ");}
Encapsulate linked list Library
// Linknode. h # include <stdio. h> # include <stdlib. h ># define datatype intstruct node {int num; // number datatype data; // the stored data struct node * pNext ;}; typedef struct node; // abbreviation // function design idea // change the address of the variable required by a variable, change the address of the pointer required by the pointer // no second-level pointer, must use the return value to assign a value // Add, delete, query, modify, sort, and reverse void backaddnode (Node ** ppnode, int num, datatype data); // Add Node * backaddnodeA (Node * pnode, int num, datatype data); // void showallnode (Node * pnode); // display all nodes * searchfirst (Node * pnode, int num ); // query int change (Node * pnode, int oldnum, int newnum); // if the modification fails, 0 is returned, and 1 Node * rev (Node * pnode) is returned ); // reverse Node * delete (Node * pnode, int num) of the linked list; // delete Node * insert (Node * pnode, int findnum, int newnum, datatype data ); // implement insert, with void sort (Node * pnode, char ch) inserted before; // ch ==> ch ==<
// Linknode. c # include "linknode. h "Node * backaddnodeA (Node * pnode, int num, datatype data) {Node * pnewnode = (Node *) malloc (sizeof (Node); pnewnode-> num = num; // value pnewnode-> data = data; // value pnewnode-> pNext = NULL; // if (pnode = NULL) {pnode = pnewnode; // store the address of the new Node} else {Node * p = pnode; // equal to the header Node while (p-> pNext! = NULL) {p = p-> pNext; // until the last Node Address} p-> pNext = pnewnode; // Insert at the end} return pnode ;} void backaddnode (Node ** ppnode, int num, datatype data) // Add Node {Node * pnewnode = (Node *) malloc (sizeof (Node )); pnewnode-> num = num; // assign a value to pnewnode-> data = data; // assign a value to pnewnode-> pNext = NULL; // end if (* ppnode = NULL) {* ppnode = pnewnode; // store the address of the new Node} else {Node * p = * ppnode; // equal to the header Node while (p-> pNext! = NULL) {p = p-> pNext; // continues until the address of the last node} p-> pNext = pnewnode; // tail insert} void showallnode (Node * pnode) // display all nodes {printf ("\ n print linked list \ n"); while (pnode! = NULL) {printf ("% p, % p", pnode, pnode-> pNext); printf ("% d, % d \ n", pnode-> num, pnode-> data); pnode = pnode-> pNext;} Node * searchfirst (Node * pnode, int num) {for (Node * p = pnode; p! = NULL; p = p-> pNext) // for Loop {if (num = p-> num) {return p; // return the address break found ;}} return NULL;} int change (Node * pnode, int oldnum, int newnum) {AAA: if (pnode! = NULL) {if (oldnum = pnode-> num) // query {pnode-> num = newnum; // modify return 1;} pnode = pnode-> pNext; // The loop tends to terminate goto AAA;} return 0;} Node * rev (Node * pnode) {Node * p1, * p2, * p3; p1 = p2 = p3 = NULL; // avoid wild pointer if (pnode = NULL | pnode-> pNext = NULL) {return pnode; // return header node} else {p1 = pnode; p2 = pnode-> pNext; while (p2! = NULL) {p3 = p2-> pNext; // layout third point p2-> pNext = p1; // address to p1 = p2; // cyclically move p2 = p3;} pnode-> pNext = NULL; pnode = p1; // return pnode;} Node * delete (Node * pnode, int num) {Node * p1 = NULL, * p2 = NULL; p1 = pnode; while (p1! = NULL) {if (p1-> num = num) {// p1 saves the address break of the node to be deleted;} else {p2 = p1; // p2 Save the previous node p1 = p1-> pNext; // forward loop} if (p1 = pnode) {pnode = p1-> pNext; // skip this node free (p1); // Delete the node} else {p2-> pNext = p1-> pNext; // skip p1free (p1);} return pnode ;} node * insert (Node * pnode, int findnum, int newnum, datatype data) {Node * p1, * p2; p1 = p2 = NULL; p1 = pnode; while (p1! = NULL) {if (p1-> num = findnum) {// p1 stores the address break of the node to be inserted;} else {p2 = p1; // p2 Save the previous Node p1 = p1-> pNext; // forward loop} Node * pnewnode = (Node *) malloc (sizeof (Node )); pnewnode-> num = newnum; pnewnode-> data = data; // assign the value if (pnode = p1) {pnewnode-> pNext = pnode; pnode = pnewnode; // insert a Node in the header} else {pnewnode-> pNext = p1; p2-> pNext = pnewnode;} return pnode;} void sort (Node * pnode, char ch) {if (ch = '<') {for (Node * p1 = pnode; p1! = NULL; p1 = p1-> pNext) {for (Node * p2 = pnode; p2! = NULL; p2 = p2-> pNext) {if (p1-> num> p2-> num) {struct node tnode; tnode. num = p1-> num; p1-> num = p2-> num; p2-> num = tnode. num; // exchange data tnode. data = p1-> data; p1-> data = p2-> data; p2-> data = tnode. data; // exchange data }}} else {for (Node * p1 = pnode; p1! = NULL; p1 = p1-> pNext) {for (Node * p2 = pnode; p2! = NULL; p2 = p2-> pNext) {if (p1-> num <p2-> num) {struct node tnode; tnode. num = p1-> num; p1-> num = p2-> num; p2-> num = tnode. num; // exchange data tnode. data = p1-> data; p1-> data = p2-> data; p2-> data = tnode. data; // exchange data }}}}}
// Main. c # include <stdio. h> # include <stdlib. h> # include "linknode. h "void main () {Node * pnode = NULL; // head Node of the linked list // backaddnode (& pnode, 1, 11); // backaddnode (& pnode, 2, 12); // backaddnode (& pnode, 3, 13); // backaddnode (& pnode, 4, 14); // backaddnode (& pnode, 5, 15 ); pnode = backaddnodeA (pnode, 1, 1); pnode = backaddnodeA (pnode, 12, 11); pnode = backaddnodeA (pnode, 3,111); pnode = backaddnodeA (pnode, 14,111 1); pnode = backaddnodeA (pnode, 5, 11111); pnode = backaddnodeA (pnode, 16,111 15,155); showallnode (pnode); // change (pnode ); // pnode = rev (pnode); // pnode = delete (pnode, 1); // pnode = delete (pnode, 3); // pnode = delete (pnode, 6); pnode = insert (pnode, 3, 3,333); pnode = insert (pnode, 1, 13,133 3); showallnode (pnode); sort (pnode, '> '); showallnode (pnode); sort (pnode, '<'); showallnode (pnode);/* Node * pfind = searchfirst (pnode, 5); if (pfind = NULL) {printf ("not found");} else {printf ("% p, % d, % d, % p", pfind, pfind-> num, pfind-> data, pfind-> pNext);} */system ("pause ");}
Copyright Disclaimer: all the articles in this blog are original articles. You are welcome to share them with us. repost them. Do not tamper with the content and indicate the source. Thank you!