您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“C語言中棧實現(xiàn)方法有哪些”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
一、順序棧
二、鏈式棧
#include<stdio.h> #include<stdlib.h> #define maxsize 64 //定義棧 typedef struct { int data[maxsize]; int top; }sqstack,*sqslink; //設置??? void Clearstack(sqslink s) { s->top=-1; } //判斷??? int Emptystack(sqslink s) { if (s->top<0) return 1; else return 0; } //進棧 int Push(sqslink s, int x) { if (s->top>=maxsize-1) return 0; else { s->top++; s->data[s->top]=x; return 1; } } // 出棧 int Popstack(sqslink s) { int n; if (Emptystack(s)>0) return NULL; else { n=s->data[s->top]; s->top--; return n; } } void main() { sqslink s1; s1 =(sqslink)malloc(sizeof(sqstack)); Clearstack(s1); printf("%d\n",s1->top); for(int i=0; i<=10;i++) { Push(s1, i); printf("%d is pushed into stack\n",i); } printf("top is point to %d\n",s1->top); printf("\n"); int n; n = Popstack(s1); printf("number %d is poped\n",n); printf("top is point to %d\n",s1->top); }
#include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node * next; }snode,*slink; struct Node { slink i; slink n; }; // 清空棧 void Clearstack(slink top) { top=NULL; } //判斷棧是否為空 int Emptystack(slink top) { if (top==NULL) return 1; else return 0; } // 進棧 slink Push(slink top, int x) { slink node = NULL; node = (slink)malloc(sizeof(snode)); node->data = x; node->next = top; top = node; printf("*************************\n"); printf("%d",top->data); printf("*************************\n"); return top; } // 出棧 struct Node Pop(slink top) { slink node = NULL; struct Node result; if (Emptystack(top)) { result.i=node; } else { int n; node = top; top = node->next; result.i = top; result.n = node; return result; } } void main() { slink top_ = NULL; for(int i =0; i<10;i++) { top_ = Push(top_, i); printf("%d is pushed in to the stack\n",i); } int e; e = top_->data; printf("top is pointint to %d\n",e); printf("\n"); printf("\n"); printf("\n"); slink node =NULL; printf("*************************\n"); struct Node result = Pop(top_); if ((result.i)!=NULL) { top_ = result.i; node = result.n; e = top_->data; printf("top is pointint to %d\n",e); int e_node; e_node = node->data; printf("the node Poped 's data is pointint to %d\n",e_node); free(node); } else { printf("stack is empty"); } }
“C語言中棧實現(xiàn)方法有哪些”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。