【數據結構】棧的順序存儲(整型棧、字符棧)
- 一、棧的結構定義
- 二、字符棧的初始化、入棧、出棧、判斷是否棧為空、獲取棧頂元素、獲取棧的當前元素個數等操作
- 三、整型棧的初始化、入棧、出棧、判斷是否棧為空、獲取棧頂元素、獲取棧的當前元素個數等操作
一、棧的結構定義
MaxSize: 順序棧的長度
s.top: 指針,指向棧頂元素。當棧為空時,指向-1。
s.data[MaxSize]: 順序棧,用數組表示
#define MaxSize 50
typedef struct SqStack{char data[MaxSize]; // 數據 int top; // 指針
}SqStack;
棧空 | s.top = -1 |
棧滿 | s.top = MaxSize - 1 |
棧頂元素 | s.data[s.top] |
棧的當前元素個數 | s.top + 1(因為元素從索引0開始,所以元素個數是:索引值+1) |
二、字符棧的初始化、入棧、出棧、判斷是否棧為空、獲取棧頂元素、獲取棧的當前元素個數等操作
#include <stdio.h>
using namespace std;#define MaxSize 50
typedef struct SqStack{char data[MaxSize]; // 數據 int top; // 指針
}SqStack;//InitStack(&s):初始化一個空棧 S。
void InitStack(SqStack &s){s.top = -1;
}//StackEmpty(s):判斷一個棧是否為空,若棧s為空則返回 true,否則返回 false。
bool StackEmpty(SqStack s){if(s.top == -1) return true;return false;
}//Push(&s,x): 入棧,若棧s未滿,則將x加入使之成為新棧頂。
bool Push(SqStack &s, char x){if(s.top == MaxSize - 1) return false;s.data[++s.top] = x;return true;
}//Pop(&S,&x):出棧,若棧s非空,則彈出棧頂元素,并用x返回。
bool Pop(SqStack &s, char &x){if(s.top == -1) return false;x = s.data[s.top--];return true;
}//GetTop(s,&x):讀棧頂元素,但不出棧,若棧s非空,則用x返回頂元素。
bool GetTop(SqStack s, char &x){if(s.top == -1) return false;x = s.data[s.top];return true;
}//Destroystack(&s):銷毀棧,并釋放棧s占用的存儲空間(“&”表示引用調用)。//getNum(s): 獲取棧的當前元素個數
int GetNum(SqStack s){if(s.top == -1) return 0;return s.top + 1;
}
int main() {SqStack s;InitStack(s); bool flag = true;int choose;while(flag){printf("\n=======================\n");printf("1.入棧\n");printf("2.出棧\n");printf("3.判斷是否棧為空\n");printf("4.讀棧頂元素\n");printf("5.獲取棧的當前元素個數\n");printf("6.退出\n");printf("=======================\n");printf("請選擇:");scanf("%d", &choose);switch(choose) {case 1:char x;printf("輸入要新入棧的字符:");scanf(" %c", &x); // 注意,此時%c前面需要加個空格,使得scanf跳過任何空白字符(包括換行符),然后讀取下一個非空白字符。 if(Push(s, x)) printf("成功把%c入棧!\n", x);else printf("入棧失敗!\n");break;case 2:char x2;if(Pop(s, x2)) printf("%c出棧成功!\n", x2);else printf("出棧失敗!\n");break;case 3:if(StackEmpty(s)) printf("棧為空!\n");else printf("棧暫時不為空!\n");break;case 4:char x3;if(GetTop(s, x3)) printf("當前棧的棧頂元素是:%c\n", x3);else printf("出錯!\n");break;case 5:printf("棧的當前元素個數是:%d", GetNum(s));break;case 6:flag = false;break; default:printf("非法輸入!\n");break; } }
}
三、整型棧的初始化、入棧、出棧、判斷是否棧為空、獲取棧頂元素、獲取棧的當前元素個數等操作
很簡單,只是把data數組及參數x的數據類型從char改為int類型就好了。
#include <bits/stdc++.h>
using namespace std;/*
棧空: top = -1
棧滿: top = MaxSize - 1
棧頂元素: SqStack[top]
*/
#define MaxSize 50
typedef struct SqStack{int data[MaxSize]; // 數據 int top; // 指針
}SqStack;//InitStack(&s):初始化一個空棧 S。
void InitStack(SqStack &s){s.top = -1;
}//StackEmpty(s):判斷一個棧是否為空,若棧s為空則返回 true,否則返回 false。
bool StackEmpty(SqStack s){if(s.top == -1) return true;return false;
}//Push(&s,x): 入棧,若棧s未滿,則將x加入使之成為新棧頂。
bool Push(SqStack &s, int x){if(s.top == MaxSize - 1) return false;s.data[++s.top] = x;return true;
}//Pop(&S,&x):出棧,若棧s非空,則彈出棧頂元素,并用x返回。
bool Pop(SqStack &s, int &x){if(s.top == -1) return false;x = s.data[s.top--];return true;
}//GetTop(s,&x):讀棧頂元素,但不出棧,若棧s非空,則用x返回頂元素。
bool GetTop(SqStack s, int &x){if(s.top == -1) return false;x = s.data[s.top];return true;
}//Destroystack(&s):銷毀棧,并釋放棧s占用的存儲空間(“&”表示引用調用)。//getNum(s): 獲取棧的當前元素個數
int GetNum(SqStack s){if(s.top == -1) return 0;return s.top + 1;
}
int main() {SqStack s;InitStack(s); bool flag = true;int choose;while(flag){printf("\n=======================\n");printf("1.入棧\n");printf("2.出棧\n");printf("3.判斷是否棧為空\n");printf("4.讀棧頂元素\n");printf("5.獲取棧的當前元素個數\n");printf("6.退出\n");printf("=======================\n");printf("請選擇:");scanf("%d", &choose);switch(choose) {case 1:int x;printf("輸入要新入棧的字符:");scanf("%d", &x);if(Push(s, x)) printf("成功把%d入棧!\n", x);else printf("入棧失敗!\n");break;case 2:int x2;if(Pop(s, x2)) printf("%d出棧成功!\n", x2);else printf("出棧失敗!\n");break;case 3:if(StackEmpty(s)) printf("棧為空!\n");else printf("棧暫時不為空!\n");break;case 4:int x3;if(GetTop(s, x3)) printf("當前棧的棧頂元素是:%d\n", x3);else printf("出錯!\n");break;case 5:printf("棧的當前元素個數是:%d", GetNum(s));break;case 6:flag = false;break; default:printf("非法輸入!\n");break; } }
}