数据结构:顺序栈
实验目标:
(1)初始化栈
(2)将元素e入栈
(3)出栈,用e返回出栈元素
(4)返回栈顶元素
(5)清空栈
(6)遍历栈元素(从栈底到栈顶)
(7)进制转换函数,将十进制dec数转化为n进制数,并输出转换后结果
实验代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| #include <iostream> using namespace std;
#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10
typedef int Status; typedef int SElemType;
typedef struct{ SElemType *base; SElemType *top; int stacksize; }SqStack;
Status InitStack(SqStack &S) { S.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); if(!S.base) exit(OVERFLOW); S.top=S.base; S.stacksize=STACK_INIT_SIZE;
return OK; }
Status Push(SqStack &S,SElemType e) { if(S.top-S.base >= S.stacksize){ S.base = (SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base) exit(OVERFLOW);
S.top = S.base + S.stacksize; S.stacksize += STACKINCREMENT; }
*S.top++ =e; return OK; }
Status Pop(SqStack &S,int &e) { if(S.top == S.base) return ERROR; e=*--S.top; return OK; }
Status GetTop(SqStack &S) { if(S.top == S.base) return ERROR; int e = *(S.top-1); cout<<"栈顶元素为:"<<e; cout<<endl; return OK; }
void ClearStack(SqStack &S) { if(S.top == S.base) cout<<"清空失败,栈不存在!"<<endl;
else{ delete S.base; S.stacksize=0; S.base=S.top=NULL; cout<<"清空成功!"<<endl; } }
void Traverse(SqStack &S) { if(S.top == S.base) cout<<"遍历失败,栈不存在!"<<endl; for(int *p=S.base;p<S.top;*p++){ int e=*p; cout<<e<<" "; } cout<<endl; }
void Convert(int dec ,int n) { SqStack S; InitStack(S); int e; while(dec){ e=dec%n; dec/=n; Push(S,e); }
cout<<"转换后为:";
while(S.top!=S.base){ Pop(S,e); if(e>=10) cout<<(char)('A'+e-10); else cout<<e; } cout<<endl; }
int main() { SqStack S; int e; while(1){ cout<<endl; cout<<"**1,初始化栈"<<endl; cout<<"**2,将元素e入栈"<<endl; cout<<"**3,出栈,用e返回出栈元素"<<endl; cout<<"**4,返回栈顶元素"<<endl; cout<<"**5,清空栈"<<endl; cout<<"**6,遍历栈元素(从栈底到栈顶)"<<endl; cout<<"**7,进制转换函数,将十进制dec数转化为n进制数,并输出转换后结果"<<endl; cout<<"**0,退出系统"<<endl;
int choice; cout<<"请输入要进行的操作:"; cin>>choice; cout<<endl;
switch(choice){ case 1:InitStack(S);break; case 2:{ int n; cout<<"请输入本次入栈的元素个数:"; cin>>n;
cout<<endl<<"请输入要插入的元素:"; for(int i=1;i<=n;i++){ cin>>e; Push(S,e); } cout<<"入栈成功!"; cout<<endl<<"本次插入的元素如下:"; Traverse(S); break; } case 3:{ int n; cout<<"请输入本次出栈的元素个数:"; cin>>n;
cout<<endl; for(int i=n;i>0;i--) Pop(S,e); cout<<"出栈成功!"<<endl; cout<<"剩下的元素为:"; Traverse(S); break; } case 4:GetTop(S);break; case 5:ClearStack(S);break; case 6:Traverse(S);break; case 7:{ int dec,n; cout<<"请输入要转换的十进制数:"; cin>>dec; cout<<endl<<"请输入要转换到几进制数:"; cin>>n; cout<<endl;
if(n!=2 && n!=8 && n!=16) cout<<"输入错误!"<<endl; else Convert(dec,n); break; } case 0:exit(0);
} }
system("pause"); return 0; }
|