日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【数据结构】顺序栈与链表栈

發(fā)布時(shí)間:2025/3/20 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【数据结构】顺序栈与链表栈 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

順序棧

頭文件

1 //@ author 成鵬致遠(yuǎn) 2 //@ net http://infodown.tap.cn 3 //@ qq 552158509 4 //@ blog lcw.cnblogs.com 5 6 //順序棧 7 8 #ifndef __SQSTACK_H 9 #define __SQSTACK_H 10 11 12 #include <stdio.h> 13 #include <stdlib.h> 14 #include <stdbool.h> 15 16 #define MAXSIZE 1024 17 18 typedef int datatype; 19 20 typedef struct stack 21 { 22 datatype data[MAXSIZE]; 23 int top;//指向棧頂 24 }sqstack,*p_sqstack; 25 26 extern void sqstack_init(p_sqstack *pstack);//棧的初始化 27 extern bool is_empty(p_sqstack pstack); 28 extern bool is_full(p_sqstack pstack); 29 extern bool sqstack_push(p_sqstack pstack, datatype data);//壓棧操作 30 extern bool sqstack_pop(p_sqstack pstack, datatype *result);//出棧操作 31 extern void sqstack_show(p_sqstack pstack);//顯示棧元素 32 33 #endif View Code

順序棧文件

1 //@ author 成鵬致遠(yuǎn) 2 //@ net http://infodown.tap.cn 3 //@ qq 552158509 4 //@ blog lcw.cnblogs.com 5 6 //輸入數(shù)字則入棧 7 //輸入字符則出棧 8 9 #include "sqstack.h" 10 11 void sqstack_init(p_sqstack *pstack)//棧的初始化 12 { 13 *pstack = (p_sqstack)malloc(sizeof(sqstack)); 14 if(NULL == *pstack) 15 { 16 perror("malloc"); 17 exit(1); 18 } 19 (*pstack)->top = -1;//初始化棧頂 20 } 21 22 bool is_empty(p_sqstack pstack) 23 { 24 if(-1 == pstack->top) 25 { 26 printf("Empty !\n"); 27 return true; 28 } 29 else 30 return false; 31 } 32 33 bool is_full(p_sqstack pstack) 34 { 35 if(MAXSIZE-1 == pstack->top) 36 { 37 printf("Full !\n"); 38 return true; 39 } 40 else 41 return false; 42 } 43 44 bool sqstack_push(p_sqstack pstack, datatype data)//壓棧操作 45 { 46 if(is_full(pstack)) 47 return false; 48 else 49 { 50 pstack->top++;//先棧頂加1,再賦值 51 pstack->data[pstack->top] = data; 52 return true; 53 } 54 } 55 56 bool sqstack_pop(p_sqstack pstack, datatype *result)//出棧操作 57 { 58 if(is_empty(pstack)) 59 return false; 60 else 61 { 62 *result = pstack->data[pstack->top]; 63 pstack->top--;//先出棧,再減棧頂指針 64 return true; 65 } 66 } 67 68 69 void sqstack_show(p_sqstack pstack)//顯示棧元素 70 { 71 int i; 72 73 for(i=0; i<pstack->top+1; i++) 74 { 75 printf("%d\t",pstack->data[i]); 76 } 77 printf("\n"); 78 } View Code

主文件

1 //@ author 成鵬致遠(yuǎn) 2 //@ net http://infodown.tap.cn 3 //@ qq 552158509 4 //@ blog lcw.cnblogs.com 5 6 //輸入數(shù)據(jù)則入棧 7 //輸入字符則出棧 8 9 #include "sqstack.h" 10 11 int main() 12 { 13 p_sqstack pstack; 14 datatype data; 15 datatype result;//用來存放出棧的元素 16 17 sqstack_init(&pstack);//初始化棧 18 19 while(1) 20 { 21 printf("Pls input data: "); 22 if(1 == scanf("%d",&data))//輸入數(shù)據(jù),入棧 23 { 24 sqstack_push(pstack,data);//入棧操作 25 sqstack_show(pstack);//顯示棧內(nèi)內(nèi)容 26 } 27 else//輸入字符,出棧 28 { 29 if(sqstack_pop(pstack,&result))//元素出棧 30 { 31 printf("The pop is %d\n",result); 32 sqstack_show(pstack); 33 } 34 while('\n' != getchar());//緩沖區(qū)處理 35 } 36 } 37 38 return 0; 39 } View Code

??


?

鏈表?xiàng)?/h2>

頭文件

1 //@ author 成鵬致遠(yuǎn) 2 //@ net http://infodown.tap.cn 3 //@ qq 552158509 4 //@ blog lcw.cnblogs.com 5 6 //鏈表?xiàng)?/span> 7 8 #ifndef __LINKSTACK_H 9 #define __LINKSTACK_H 10 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <stdbool.h> 14 15 typedef int datatype; 16 17 typedef struct stack 18 { 19 datatype data; 20 struct stack *next; 21 }linkstack,*p_linkstack; 22 23 extern void linkstack_init(p_linkstack *top);//鏈表?xiàng)5某跏蓟?/span> 24 extern bool is_empty(p_linkstack top); 25 extern bool linkstack_push(p_linkstack *top, datatype data);//壓棧,因?yàn)樾枰淖冾^結(jié)點(diǎn),所以需要傳*top 26 extern bool linkstack_pop(p_linkstack *top,datatype *result);//出棧 27 extern void linkstack_show(p_linkstack top);//顯示鏈棧元素 28 29 #endif View Code

?鏈表?xiàng)N募?/h3> 1 //@ author 成鵬致遠(yuǎn) 2 //@ net http://infodown.tap.cn 3 //@ qq 552158509 4 //@ blog lcw.cnblogs.com 5 6 //輸入數(shù)字入棧 7 //輸入字符出棧 8 9 #include "linkstack.h" 10 11 void linkstack_init(p_linkstack *top)//鏈表?xiàng)5某跏蓟?/span> 12 { 13 *top = NULL;//為空 14 } 15 16 bool is_empty(p_linkstack top) 17 { 18 if(NULL == top) 19 { 20 printf("Empty \n"); 21 return true; 22 } 23 else 24 return false; 25 } 26 27 bool linkstack_push(p_linkstack *top, datatype data)//壓棧,因?yàn)樾枰淖冾^結(jié)點(diǎn),所以需要傳*top 28 { 29 p_linkstack new; 30 new = (p_linkstack)malloc(sizeof(linkstack)); 31 if(NULL == new) 32 { 33 perror("malloc"); 34 return false; 35 } 36 else//壓棧 37 { 38 new->data = data; 39 new->next = *top; 40 *top = new;//top指向新申請的結(jié)點(diǎn) 41 return true; 42 } 43 } 44 45 bool linkstack_pop(p_linkstack *top,datatype *result)//出棧 46 { 47 p_linkstack tmp;//指向?qū)⒈怀鰲5脑?/span> 48 if(is_empty(*top)) 49 return false; 50 else//出棧 51 { 52 tmp = *top; 53 *top = (*top)->next;//出棧 54 *result = tmp->data; 55 free(tmp);//釋放結(jié)點(diǎn) 56 57 return true; 58 } 59 } 60 61 void linkstack_show(p_linkstack top)//顯示鏈棧元素 62 { 63 while(NULL != top) 64 { 65 printf("%d\t",top->data); 66 top = top->next; 67 } 68 printf("\n"); 69 } View Code

主文件

1 //@ author 成鵬致遠(yuǎn) 2 //@ net http://infodown.tap.cn 3 //@ qq 552158509 4 //@ blog lcw.cnblogs.com 5 6 7 //輸入數(shù)字則入棧 8 //輸入字符則出棧 9 10 #include "linkstack.h" 11 12 int main() 13 { 14 p_linkstack top; 15 datatype data;//用于保存輸入的數(shù) 16 datatype result;//用于保存出棧的結(jié)果 17 18 linkstack_init(&top);//初始化鏈表?xiàng)?/span> 19 20 while(1) 21 { 22 printf("Pls input data: "); 23 if(1 == scanf("%d",&data))//輸入數(shù)字,入棧 24 { 25 linkstack_push(&top,data);//入棧 26 linkstack_show(top); 27 } 28 else//輸入字符,出棧 29 { 30 if(linkstack_pop(&top,&result))//出棧成功 31 printf("The top is %d\n",result); 32 linkstack_show(top); 33 34 while('\n' != getchar());//緩沖區(qū)處理 35 } 36 } 37 38 return 0; 39 } View Code

?

?

總結(jié)

以上是生活随笔為你收集整理的【数据结构】顺序栈与链表栈的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。