标签:结点 return int top next 数据结构 data
1.栈
https://blog.csdn.net/CSDN___CSDN/article/details/82918436
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct link_node 5 { 6 int info; 7 struct link_node *next; 8 }N; 9 10 /*创建一个空的链式栈*/ 11 N *init() 12 { 13 return NULL; 14 } 15 16 /*对链式栈进行初始化*/ 17 N *creat(N *top) 18 { 19 int x; 20 N *p; 21 printf("以输入-1为结束\n"); 22 scanf("%d",&x); 23 while(x!=-1) 24 { 25 p=(N*)malloc(sizeof(N)); 26 p->info=x; 27 p->next=top; 28 top=p; 29 scanf("%d",&x); 30 } 31 return top; 32 } 33 34 /*取得链式栈的栈顶结点值*/ 35 int read(N *top) 36 { 37 if(!top) 38 { 39 printf("\n该链式栈是空的\n"); 40 } 41 return top->info; 42 } 43 44 /*输出链式栈中各结点的值*/ 45 void display(N *top) 46 { 47 N *p=top; 48 if(!top) 49 { 50 printf("该链式栈是空的\n"); 51 } 52 else 53 { 54 while(p) 55 { 56 printf("%d ",p->info); 57 p=p->next; 58 } 59 printf("\n"); 60 } 61 } 62 63 /*链式栈的插入操作*/ 64 N *insert(N *top,int x) 65 { 66 N *q=top,*p; 67 p=(N*)malloc(sizeof(N)); 68 p->info=x; 69 p->next=top; 70 top=p; 71 return top; 72 } 73 74 /*链式栈的删除操作*/ 75 N *dele(N *top) 76 { 77 N *p=top; 78 if(!top) 79 { 80 printf("\n该链式栈是空的,无法进行删除\n");return NULL; 81 } 82 top=top->next; 83 free(p); 84 return top; 85 } 86 87 int main() 88 { 89 N* top = init(); 90 top = creat(top); 91 display(top); 92 int i = read(top); 93 printf("%d\n",i); 94 top = insert(top,5); 95 display(top); 96 top = dele(top); 97 display(top); 98 99 return 0; 100 }
2.队列
https://blog.csdn.net/LiuBo_01/article/details/80412290
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct Node 5 { 6 int data; 7 struct Node* next; 8 }N; 9 10 typedef struct 11 { 12 N* front; 13 N* rear; 14 }Q; 15 16 //初始化队列 17 void Init(Q* q) 18 { 19 //创建一个头结点 20 N* n = (N*)malloc(sizeof(N)); 21 q->front = q->rear = n; //队头和队尾指向头结点 22 q->front->next = NULL; 23 } 24 25 //判断队列是否为空 26 int IsEmpty(Q* q) 27 { 28 if (q->front->next == NULL) 29 { 30 return 1; 31 } 32 return 0; 33 } 34 35 //入队操作 36 void Enter(Q* q, int data) 37 { 38 //创建一个新结点 39 N* n = (N*)malloc(sizeof(N)); 40 n->data = data; //将数据元素赋值给结点的数据域 41 n->next = NULL; //将结点的指针域置空 42 q->rear->next = n; //将原来队列的队尾指针指向新结点 43 q->rear = n; //将队尾指针指向新结点 44 } 45 46 //出队操作 47 void Delete(Q* q,int* data) 48 { 49 if (IsEmpty(q)) 50 { 51 printf("队列为空!\n"); 52 return; 53 } 54 //pDel指向队头元素,由于队头指针front指向头结点,所以pDel指向头结点的下一个结点 55 N* n = q->front->next; 56 *data = n->data; //将要出队的元素赋给data 57 q->front->next = n->next; //使指向头结点的指针指向pDel的下一个结点 58 //如果队列中只有一个元素,将队列置空 59 if (q->rear = n) 60 { 61 q->rear = q->front; 62 } 63 free(n); //释放pDel指向的空间 64 } 65 66 //取队头元素 67 int GetHead(Q* q, int* data) 68 { 69 if (IsEmpty(q)) 70 { 71 printf("队列为空!\n"); 72 return 0; 73 } 74 N* n; 75 n = q->front->next; //pCur指向队列的第一个元素,即头结点的下一个结点 76 *data = n->data; //将队头元素值赋给data 77 return *data; //返回队头元素值 78 } 79 80 //打印队列中的元素 81 void Print(Q* q) 82 { 83 N* n; 84 n = q->front->next; 85 while (n) 86 { 87 printf("%d ", n->data); 88 n = n->next; 89 } 90 printf("\n"); 91 } 92 93 int main() 94 { 95 Q q; 96 int x; 97 Init(&q); 98 Enter(&q,1); 99 Enter(&q,2); 100 Enter(&q,3); 101 Enter(&q,4); 102 Print(&q); 103 Delete(&q,&x); 104 printf("%d\n",x); 105 GetHead(&q,&x); 106 printf("%d\n",x); 107 Print(&q); 108 109 return 0; 110 }
3.单链表
标签:结点,return,int,top,next,数据结构,data 来源: https://www.cnblogs.com/xiaogudexuexibiji/p/16429981.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。