栈的基本操作(详细)
生活随笔
收集整理的這篇文章主要介紹了
栈的基本操作(详细)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文章會詳細介紹棧的基本操作
目錄
1.本文章中全部實現的功能
2.建棧
3.輸入棧內元素(由于起初輸入棧不牽扯到棧的擴容,所以對此部分注釋)
4.進棧
5.彈棧,并且返回出彈棧元素
6.棧內元素的個數
7.按棧輸入的順序輸出棧里面的值
8.按棧彈出的順序輸出棧
9.判斷棧是否為空
10.獲取棧頂元素
11.清空一個棧
12.摧毀一個棧
13.switch功能語句
14.全部代碼
15.運行結果
1.本文章中全部實現的功能
????????棧的特點,先進后出。
void program() {printf("\t請輸入以下功能數字\n");printf("\t0.退出\n");printf("\t1.判斷棧是否為空\n");printf("\t2.按棧彈出的順序輸出棧\n");printf("\t3.按棧輸入的順序輸出棧里面的值\n");printf("\t4.獲取棧頂元素\n");printf("\t5.摧毀一個棧\n");printf("\t6.清空一個棧\n");printf("\t7.求棧的長度\n");printf("\t8.彈棧,并且返回出彈棧元素\n");printf("\t9.輸入棧內數據\n");printf("\t10.在清空的基礎下重新建立棧\n");printf("\t11.請輸入想要插入棧頂的元素\n");}2.建棧
Status InitStack(SqStack &S) {//建棧S.base=(int*)malloc(Stack_Init_Size*sizeof(int));if(!S.base) exit(OVERFLOW);S.top=S.base;S.stacksize=Stack_Init_Size;return OK; }3.輸入棧內元素(由于起初輸入棧不牽扯到棧的擴容,所以對此部分注釋)
SqStack InputStack(SqStack &S) { int a;/*scanf("%d",&a);while(a!=-1){ if(S->top-S->base>S->stacksize){S->base=(int *)realloc(S->base,(Stack_Init_Size+Stack_Increment)*sizeof(int));if(!S->base) printf("NoRealloc\n");S->top=S->base+S->stacksize;S->stacksize=S->stacksize+Stack_Increment;}*S->top++ = a;scanf("%d",&a);}*/InitStack(S);printf("\t");for(int i=0;;i++){scanf("%d",&a);if(a==-1)break;*S.top++ =a;}printf("\t寫入完成\n"); }4.進棧
? ? ? ? 如果棧的內容小于輸入內容不需要擴展直接*S.top++=e;但是當內容大于了棧的內容。就進入if語句。
Status Push(SqStack &S,int e) {//進棧if(S.top-S.base>S.stacksize){S.base=(int *)realloc(S.base,(Stack_Init_Size+Stack_Increment)*sizeof(int));if(!S.base) printf("NoRealloc\n");S.top=S.base+S.stacksize;S.stacksize=S.stacksize+Stack_Increment;}*S.top++=e;return OK; }5.彈棧,并且返回出彈棧元素
Status Pop(SqStack &S,int *e) { //彈棧,并且返回出彈棧元素*e=*--S.top;return OK; }6.棧內元素的個數
Status StackLength(SqStack S) { //棧長int Length=S.top-S.base;printf("\tlength = %d\n",Length);return Length; }7.按棧輸入的順序輸出棧里面的值
void PrintStack_int(SqStack S) {//按棧輸入的順序輸出棧里面的值int *p=S.base;while(p!=S.top){ printf("\t");printf(" %d ",*p);p++;}printf("\n"); }8.按棧彈出的順序輸出棧
void PrintStack_Pop(SqStack S) { //按棧彈出的順序輸出棧int *p=S.top-1;while(p!=S.base-1){ printf("\t");printf(" %d ",*p);p--;}printf("\n"); }9.判斷棧是否為空
void IsNullStack(SqStack S) {//判斷棧是否為空if(S.base==S.top||S.base==NULL)printf("\t棧為空棧\n");else printf("\t棧不為空\n"); }10.獲取棧頂元素
Status GetTop(SqStack S) { //獲取棧頂元素int e;e=*(S.top-1);printf("\t棧頂元素為%d\n",e); }11.清空一個棧
void ClearStack(SqStack &S) { //清空一個棧 S.top=S.base; }12.摧毀一個棧
void DestroyStack(SqStack &S) { //摧毀一個棧free(S.base);S.base=S.top;S.stacksize=0;S.top=NULL;S.base=NULL; }13.switch功能語句
void swi(SqStack S){int num;program();printf("\t輸入的元素是:");scanf("%d",&num);printf("\n\n");while(num){ switch(num){case 0:num=0;break;case 1:if(S.base==NULL){printf("\t在進行操作1之前需要操作功能9\n");}else{printf("\t判斷棧是否為空\n");IsNullStack(S);}break;case 2:if(S.base==NULL){printf("\t在進行操作2之前需要操作功能9\n");}else{printf("\t2.按棧彈出的順序輸出棧\n");PrintStack_Pop(S);}break;case 3:if(S.base==NULL){printf("\t在進行操作3之前需要操作功能9\n");}else{printf("\t3.按棧輸入的順序輸出棧里面的值\n");PrintStack_int(S);}break;case 4:if(S.base==NULL){printf("\t在進行操作4之前需要操作功能9\n");}else{printf("\t4.獲取棧頂元素\n");GetTop(S);}break;case 5:if(S.base==NULL){printf("\t在進行操作5之前需要操作功能9\n");}else{printf("\t5.摧毀一個棧\n");DestroyStack(S);printf("\t棧已經被摧毀\n");}break;case 6:if(S.base==NULL){printf("\t在進行操作6之前需要操作功能9\n");}else{printf("\t6.清空一個棧\n");ClearStack(S);printf("\t棧已經清空");}break;case 7:if(S.base==NULL){printf("\t在進行操作7之前需要操作功能9\n");}else{printf("\t7.求棧的長度\n");StackLength(S);}break;case 8:if(S.base==NULL){printf("\t在進行操作8之前需要操作功能9\n");}else{printf("\t彈棧,并且返回出彈棧元素\n");int a;Pop(S,&a);printf("\t8.彈棧彈出的元素是%d\n",a);}break;case 9:printf("\t9.輸入棧內數據\n");InputStack(S);break;case 10:if(S.base==NULL){printf("\t在進行操作10之前需要操作功能9\n");}else{printf("\t10.在清空的基礎下重新建立棧\n");DestroyStack(S);printf("\t請重新輸入棧內數據\n");InputStack(S);}break;case 11:if(S.base==NULL){printf("\t在進行操作11之前需要操作功能9\n");}else{printf("\t11.在棧頂插入元素\n");int x;printf("\t請輸入想要插入棧頂的元素:\n");scanf("%d",&x);Push(S,x);printf("\t插入完成\n");}break;default:printf("輸入有誤,請重新輸入\n");}printf("\n\n\n");program();printf("\t輸入的元素是:");scanf("%d",&num);printf("\n\n");} }14.全部代碼
//define區 #define Stack_Init_Size 100 #define Stack_Increment 10 #define OK 1 #define OVERFLOW -2 #define ERROR 0//預處理指令區 #include<stdio.h> #include<stdlib.h>//typedef typedef int Status; typedef struct {int *base;int *top;int stacksize; }SqStack;Status InitStack(SqStack &S); //建棧 SqStack InputStack(SqStack &S); //輸入棧內元素 Status Push(SqStack &S,int e); //進棧 Status Pop(SqStack &S,int *e); //彈棧,并且返回出彈棧元素 Status StackLength(SqStack S); //棧內元素的個數 void PrintStack_int(SqStack S); //按棧輸入的順序輸出棧里面的值 void PrintStack_Pop(SqStack S); //按彈棧順序輸出棧里面的值 void IsNullStack(SqStack S); //判斷是否為空棧 Status GetTop(SqStack S); //獲取棧頂元素 void ClearStack(SqStack &S); //清空棧 void DestroyStack(SqStack &S); //摧毀棧 void program(); //功能函數 void swi(SqStack S); //switchint main() {SqStack S;S.base=NULL;swi(S);printf("\t程序退出了,下次見\n"); }Status InitStack(SqStack &S) {//建棧S.base=(int*)malloc(Stack_Init_Size*sizeof(int));if(!S.base) exit(OVERFLOW);S.top=S.base;S.stacksize=Stack_Init_Size;return OK; }SqStack InputStack(SqStack &S) { int a;/*scanf("%d",&a);while(a!=-1){ if(S->top-S->base>S->stacksize){S->base=(int *)realloc(S->base,(Stack_Init_Size+Stack_Increment)*sizeof(int));if(!S->base) printf("NoRealloc\n");S->top=S->base+S->stacksize;S->stacksize=S->stacksize+Stack_Increment;}*S->top++ = a;scanf("%d",&a);}*/InitStack(S);printf("\t");for(int i=0;;i++){scanf("%d",&a);if(a==-1)break;*S.top++ =a;}printf("\t寫入完成\n"); }Status Push(SqStack &S,int e) {//進棧if(S.top-S.base>S.stacksize){S.base=(int *)realloc(S.base,(Stack_Init_Size+Stack_Increment)*sizeof(int));if(!S.base) printf("NoRealloc\n");S.top=S.base+S.stacksize;S.stacksize=S.stacksize+Stack_Increment;}*S.top++=e;return OK; }Status Pop(SqStack &S,int *e) { //彈棧,并且返回出彈棧元素*e=*--S.top;return OK; }Status StackLength(SqStack S) { //棧長int Length=S.top-S.base;printf("\tlength = %d\n",Length);return Length; }void PrintStack_int(SqStack S) {//按棧輸入的順序輸出棧里面的值int *p=S.base;while(p!=S.top){ printf("\t");printf(" %d ",*p);p++;}printf("\n"); }void PrintStack_Pop(SqStack S) { //按棧彈出的順序輸出棧int *p=S.top-1;while(p!=S.base-1){ printf("\t");printf(" %d ",*p);p--;}printf("\n"); }void IsNullStack(SqStack S) {//判斷棧是否為空if(S.base==S.top||S.base==NULL)printf("\t棧為空棧\n");else printf("\t棧不為空\n"); }Status GetTop(SqStack S) { //獲取棧頂元素int e;e=*(S.top-1);printf("\t棧頂元素為%d\n",e); }void ClearStack(SqStack &S) { //清空一個棧 S.top=S.base; }void DestroyStack(SqStack &S) { //摧毀一個棧free(S.base);S.base=S.top;S.stacksize=0;S.top=NULL;S.base=NULL; } void program() {printf("\t請輸入以下功能數字\n");printf("\t0.退出\n");printf("\t1.判斷棧是否為空\n");printf("\t2.按棧彈出的順序輸出棧\n");printf("\t3.按棧輸入的順序輸出棧里面的值\n");printf("\t4.獲取棧頂元素\n");printf("\t5.摧毀一個棧\n");printf("\t6.清空一個棧\n");printf("\t7.求棧的長度\n");printf("\t8.彈棧,并且返回出彈棧元素\n");printf("\t9.輸入棧內數據\n");printf("\t10.在清空的基礎下重新建立棧\n");printf("\t11.請輸入想要插入棧頂的元素\n");}void swi(SqStack S){int num;program();printf("\t輸入的元素是:");scanf("%d",&num);printf("\n\n");while(num){ switch(num){case 0:num=0;break;case 1:if(S.base==NULL){printf("\t在進行操作1之前需要操作功能9\n");}else{printf("\t判斷棧是否為空\n");IsNullStack(S);}break;case 2:if(S.base==NULL){printf("\t在進行操作2之前需要操作功能9\n");}else{printf("\t2.按棧彈出的順序輸出棧\n");PrintStack_Pop(S);}break;case 3:if(S.base==NULL){printf("\t在進行操作3之前需要操作功能9\n");}else{printf("\t3.按棧輸入的順序輸出棧里面的值\n");PrintStack_int(S);}break;case 4:if(S.base==NULL){printf("\t在進行操作4之前需要操作功能9\n");}else{printf("\t4.獲取棧頂元素\n");GetTop(S);}break;case 5:if(S.base==NULL){printf("\t在進行操作5之前需要操作功能9\n");}else{printf("\t5.摧毀一個棧\n");DestroyStack(S);printf("\t棧已經被摧毀\n");}break;case 6:if(S.base==NULL){printf("\t在進行操作6之前需要操作功能9\n");}else{printf("\t6.清空一個棧\n");ClearStack(S);printf("\t棧已經清空");}break;case 7:if(S.base==NULL){printf("\t在進行操作7之前需要操作功能9\n");}else{printf("\t7.求棧的長度\n");StackLength(S);}break;case 8:if(S.base==NULL){printf("\t在進行操作8之前需要操作功能9\n");}else{printf("\t彈棧,并且返回出彈棧元素\n");int a;Pop(S,&a);printf("\t8.彈棧彈出的元素是%d\n",a);}break;case 9:printf("\t9.輸入棧內數據\n");InputStack(S);break;case 10:if(S.base==NULL){printf("\t在進行操作10之前需要操作功能9\n");}else{printf("\t10.在清空的基礎下重新建立棧\n");DestroyStack(S);printf("\t請重新輸入棧內數據\n");InputStack(S);}break;case 11:if(S.base==NULL){printf("\t在進行操作11之前需要操作功能9\n");}else{printf("\t11.在棧頂插入元素\n");int x;printf("\t請輸入想要插入棧頂的元素:\n");scanf("%d",&x);Push(S,x);printf("\t插入完成\n");}break;default:printf("輸入有誤,請重新輸入\n");}printf("\n\n\n");program();printf("\t輸入的元素是:");scanf("%d",&num);printf("\n\n");} }15.運行結果
在沒有建立棧的條件下如果輸入別的數據
?1.建棧
2.按棧彈出的順序輸出棧
?3.按棧輸入的順序輸出棧里面的值
?4.獲取棧頂元素
7.求棧的長度
?8.彈棧,并且返回出彈棧元素
驗證:
?11.請輸入想要插入棧頂的元素
6.清空
驗證:
?
10.在清空的狀態下重新輸入棧
驗證:
5.摧毀棧
?
0.退出
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的栈的基本操作(详细)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信支付开发(1) JS API支付
- 下一篇: iOS 音频开发