Chained stacks
Stacklinknode.h#define datatype intstruct stacknode{int num;//number datatype data;//data struct Stacknode *pnext ;//pointer field};typedef struct Stacknode stacknode;//simplified Stacknode * INIT (Stacknode * phead);//Initialize Stacknode * PUSH (Stacknode * phead, int num, datatype data),//input stack Stacknode * POP (Stacknode * phead, Stacknode * poutdata);//Stack Stacknode * Freeall (STAC KNode * phead);//Empty 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)//Enter stack {stacknode *pnewnode = (Stacknode *) malloc (sizeof (Stacknode );//Create node Pnewnode->num = Num;pnewnode->data = Data;pnewnode->pnext = null;//node and assign if (Phead = = null)//Empty list, Direct connection {Phead = pnewnode;//connect a node}else{stacknode *p = Phead;while (p->pnext!=null) {p = p->pnext;//always forward}p->pnext = pnewnode;//Insert}return phead;//return head node}stacknode * Printfall (Stacknode * phead) {if (phead==null) {return NULL;} else{printf ("%d,%d,%p,%p\n", Phead->num, Phead->data,phead,phead->pnext);p Rintfall (phead->pNext);// Print}}stacknode * POP (Stacknode * phead, Stacknode * poutdata) {if (Phead = = NULL) {return null;//already has no element}else if (phead-> Pnext==null) {poutdata->num = Phead->num;poutdata->data = phead->data;//Take out data free (phead);//release Memory Phead = null;//only one node return phead;} Else{stacknode *p = Phead;while (p->pnext->pnext!=null) {p = p->pnext;//loop to the penultimate node}poutdata->num = p->pnext->num; Poutdata->data = p->pnext->data;//Take out data free (p->pnext);//Release p->pnext = Null;return Phead;}} Delete all nodes Stacknode * Freeall (Stacknode * phead) {if (Phead = = null) {return null;} Else{stacknode *p1=null, *p2=null;p1 = phead;//head node while (p1->pnext! = NULL) {P2 = p1->pnext;//save 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"/* Use chain stacks, Advanced post-out principles. Implement 10-digit conversion to 2-digit */void Main01 () {int num;scanf ("%d", &num);p rintf ("num=%d\n", num);//print data Stacknode *phead = null;/ /Create a chain stack of head node printf ("\ n"), while (num) {printf ("%d\n", num% 2);p head = push (Phead, num%2, 0); Num/= 2;} while (phead! = NULL) {Stacknode *pout = (Stacknode *) malloc (sizeof (Stacknode));p head = Pop (phead, pout);p rintf ("%d", pout ->num);//Stack}system ("Pause");} void Main () {Stacknode *phead=null;//creates a chain-Stack header phead = init (phead);//Set stack is empty phead = Push (Phead, 1, 1);p head = Push (Phead, 2 , one);p head = Push (Phead, 3, 111);p head = Push (Phead, 4, 1111);p head = Push (Phead, 5, 11111);p Rintfall (phead);p head = Free All (Phead);p rintf ("\ n release");p Rintfall (Phead),//while (phead!=null)//{////save Data//printf ("Stack \ n") of the stack;//stacknode * Pout = (Stacknode *) malloc (sizeof (Stacknode)),//phead = Pop (phead, pout),//printf ("after stack \ n");//printfall (phead);// printf ("\ nthe data%d,%d after the stack", Pout->num,Pout->data);//}system ("Pause");}
Linked list queues and priority queues
Queue.hstruct queue{int num;//represents data int high;//priority 1111struct Queue *pnext;//stores the address of the next node};typedef struct queue queue;//Simplified Queue * init (*QUEUEA);//Initialize queue * EnQueue (queue *queuea, int num, int high);//queue * DeQueue (queue * QueueA, queue *pout);//queue * Freeall (queue *queuea);//empty void sort (queue *queuea);//priority queue void Printfall (queue * QueueA);//print all data, recursive 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)//queued {Queue *pnewnode = (Queue *) malloc (sizeof (queue));//Allocate Memory pnewnode-& Gt;num = Num;pnewnode->high = High;pnewnode->pnext = Null;if (queuea==null)//list is empty {QueueA = Pnewnode;//sort (QueueA );//Queue return queuea;//return value}else{queue *p = queuea;//head node while (p->pnext!=null) {p = P->pnext;} Determine where to insert P->pnext = pnewnode;//Insert//sort (QUEUEA);//Queue return queuea;//return}}queue * DeQueue (queue *queuea, queue *pout )//sequence out of queue {if (QueueA = = null) {return null;} Else{pout->num = Queuea->num;pout->high = queuea->high;//Assignment Queue *ptemp = queuea;//Record the address to be deleted QueueA = queueA- >pnext;//Skip Queueafree (ptemp);//release node return QueueA;}} Queue * Freeall (queue *queuea)//Empty {}queue * Insertenqueue (queue *queuea, int num, int high)//queuing Insert {Queue *pnewnode = (Que UE *) malloc (sizeof (Queue));//Allocate memory Pnewnode->num = Num;pnewnode->high = High;if (quEuea = = null)//node is empty {Pnewnode->pnext = Null;queuea = Pnewnode;return QueueA;} Else{if (Pnewnode->high >queuea->high) {pnewnode->pnext = queuea;//header Insert QueueA = pnewnode;//point to this node return QueueA;} else {Queue *p = queuea;//head node while (p->pnext! = NULL) {p = P->pnext;} P Loop to tail if (Pnewnode->high <= p->high) {p->pnext = Pnewnode;pnewnode->pnext = Null;return QueueA;} Else{queue *p1, *p2;p1 = P2 = null;//Avoid also pointer p1 = queuea;//head node while (p1->pnext! = NULL) {P2 = p1->pnext;if (p1->high& Gt;=pnewnode->high && p2->high<pnewnode->high) {pnewnode->pnext = P2;p1->pnext = pnewnode;/ /insert break;} P1 = P1->pnext;} return QueueA;}}} void sort (Queue *queuea)//Priority queueing {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;//Interchange on node data//}//}//}}void Printfall (Queue *queuea)//recursive {if (queuea==null) {return;} else{printf ("%d,%d,%p,%p\n", Queuea->num, Queuea->high, QueueA, Queuea->pnext);p Rintfall (queueA->pNext );//Enter the next node}}
main.c#include<stdio.h> #include <stdlib.h> #include "Queue.h" void Main () {Queue *phead = null;// Create head node Phead = init (phead);//Initialize Phead = Insertenqueue (Phead, 1, 1);p rintf ("\ n");p Rintfall (phead);p head = Insertenqueue ( Phead, 2,;p rintf ("\ n");p Rintfall (phead);p head = Insertenqueue (Phead, 3, 3);p rintf ("\ n");p Rintfall (phead);p head = Insertenqueue (Phead, 4, +);p rintf ("\ n");p Rintfall (phead);p head = Insertenqueue (Phead, 5, 5);p rintf ("\ n");p Rintfall ( Phead);p head = Insertenqueue (Phead, 6, +);p rintf ("\ n");p Rintfall (phead);p head = Insertenqueue (phead, 6, 0);p rintf ("\ n ");p Rintfall (phead);p head = Insertenqueue (phead, 7, 0);p rintf (" \ n ");p Rintfall (phead);p head = Insertenqueue (phead, 8, 0 ;p rintf ("\ n");p Rintfall (phead);p head = Insertenqueue (Phead, 9, 1);p rintf ("\ n");p Rintfall (phead);p head = Insertenqueue (phead, 0);p rintf ("\ n");p Rintfall (phead);p head = Insertenqueue (Phead, one, ten);p rintf ("\ n"); Printfall (phead);p head = Insertenqueue (Phead, 111,);p rintf ("\ n");p Rintfall (phead);//while (PHead! = NULL)//Continue//{////to allocate memory//queue * ptemp = (queue *) malloc (sizeof (queue));//phead = DeQueue (Phead, ptemp);// printf ("//printfall");//printf ("Phead%d,%d", Ptemp->num, Ptemp->high);//}system ("pause"); ");}
Package Chain List Library
linknode.h#include<stdio.h> #include <stdlib.h> #define datatype intstruct node{int num;// Number datatype data;//stored data struct node *pnext;}; typedef struct Node node;//shorthand//function design idea//change a variable needs the address of the variable, change the pointer needs the address of the pointer//do not use a level two pointer, must be assigned with the return value//increment, delete, query, modify, sort, reverse void Backaddnode (node **ppnode, int num,datatype data);//Add node * Backaddnodea (nodes *pnode, int num, datatype data);//v OID Showallnode (node *pnode);//Display all nodes node * SEARCHFIRST (node *pnode, int num);//query int change (Node *pnode, int oldnum, int Newnum);//modify failed to return 0, successfully returned 1Node * REV (node *pnode);//List Reversal node * Delete (node *pnode, int num);//Delete node * INSERT (node *pnode, int findnum, int newnum, datatype data);//implement INSERT, front insert void sort (Node *pnode, char ch);//ch==> ch==<
Linknode.c#include "linknode.h" Node * Backaddnodea (node *pnode, int num, datatype data) {node *pnewnode = (node *) malloc ( sizeof (Node));p newnode->num = num;//Assignment pnewnode->data = data;//Assignment Pnewnode->pnext = null;//tail if (Pnode = = NULL) { Pnode = pnewnode;//Stores the address of the new node}else{node *p = pnode;//equals the head node while (p->pnext! = NULL) {p = p->pnext;//is looped to the address of the last node}p- >pnext = pnewnode;//tail inserted}return pnode;} void Backaddnode (node **ppnode, int num, datatype data)//Add Node {nodes *pnewnode = (node *) malloc (sizeof (node));p Newnode-> num = num;//Assignment pnewnode->data = data;//Assignment Pnewnode->pnext = null;//tail if (*ppnode = = NULL) {*ppnode = pnewnode;//store new section The address of the point}else{node *p = *ppnode;//equals the head node while (p->pnext! = NULL) {p = p->pnext;//always loops to the address of the last node}p->pnext = pnewnode;/ /tail Insert}}void Showallnode (node *pnode)//Show all nodes {printf ("\ n Print list \ n"), while (pnode! = NULL) {printf ("%p,%p", pnode,pnode- >pnext);p rintf ("%d,%d\n", Pnode->num, Pnode->data);p node = 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;//returns the address found break;}} return NULL;} int change (Node *pnode, int oldnum, int newnum) {aaa:if (pnode!=null) {if (Oldnum = = pnode->num)//Lookup {Pnode->num = newn um;//modify return 1;} Pnode = pnode->pnext;//cycle 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;//returns the head node}ELSE{P1 = PNODE;P2 = Pnode->pnext;while (P2! = NULL) {p3 = p2->pnext;//layout Third Point p2->pnext = p1;//address Turn P 1 = p2;//loop move p2 = p3;} Pnode->pnext = Null;pnode = p1;//Storage head node address return pnode;}} Node * Delete (node *pnode, int num) {node *p1=null, *p2=null;p1 = Pnode;while (p1! = NULL) {if (p1->num = = num) {//P1 saved the Delete the address of the node break;} 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 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 Saves the address of the node to be inserted break;} ELSE{P2 = P1;//p2 Save previous node P1 = p1->pnext;//forward loop}}node * Pnewnode = (node *) malloc (sizeof (node));p Newnode->num = Newnu M;pnewnode->data = data;//Assignment if (pnode = = p1) {pnewnode->pnext = Pnode;pnode = pnewnode;//Header Inserts a node}else{pnewnode-& Gt;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; p 2! = Null;p2=p2->pnext) {if (P1->num > P2->num) {struct node tnode;tnode.num = P1->num;p1->num = P2->n Um;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-&G T;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;//the head node of the linked list Backaddnode (&pnode, 1, one),//backaddnode (&pnode, 2, N),//backaddnode (&pnode, 3,);//backaddnode ( &pnode, 4, +);//backaddnode (&pnode, 5,);p node = Backaddnodea (Pnode, 1, 1);p node = Backaddnodea (Pnode, 12, 11); Pnode = Backaddnodea (Pnode, 3, 111);p node = Backaddnodea (Pnode, +, 1111);p node = Backaddnodea (Pnode, 5, 11111);p node = ba Ckaddnodea (pnode,16, 111111); Showallnode (Pnode);//change (Pnode, 155),//pnode = rev (pnode);//pnode = Delete (Pnode, 1);//pnode = Delete (Pnode, 3),//pnode = Delete (Pnode, 6);p node = insert (Pnode, 3, 3, 333);p node = 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 notice: This blog all articles are original, welcome to Exchange, welcome reprint, reprint do not tamper with the content, and indicate the source, thank you!
"C + + Academy" 0802-chain stack/list queue and Priority queue/package chain List Library