顺序栈和链栈的实现
这学期开了数据结构,刚开始以为代码应该会很简单,但是真正实现起来才发现有好多细节需要注意, 而且发现指针的姿势忘了好多好多((٩(//̀Д/́/)۶))
130h.h
1 #include <bits/stdc++.h> 2 #define MAX 100 3 struct node1 4 { 5 int *base; 6 int *top; 7 int Size; 8 }; 9 struct node2 10 { 11 int data; 12 node2 *next; 13 }; 14 15 void menu(); 16 bool init1(node1 *&s); 17 void input1(node1 *s, int x); 18 bool pop1(node1 *s); 19 int top1(node1 *s); 20 bool init2(node2 *&s); 21 void input2(node2 *s, int x); 22 bool pop2(node2 *s); 23 int top2(node2 *s);
main.cpp
1 #include "130h.h" 2 using namespace std; 3 4 int main() 5 { 6 int n, x, t, i; 7 node1 *s1; 8 node2 *s2; 9 while(1) 10 { 11 menu(); 12 cin>>t; 13 if(t==-1) 14 { 15 free(s1); 16 free(s2); 17 break; 18 } 19 switch(t) 20 { 21 case 1: 22 if(init1(s1)) puts("yes"); 23 else puts("no"); 24 break; 25 case 2: 26 puts("输入元素,-1结束"); 27 while(1) 28 { 29 scanf("%d", &x); 30 if(x == -1) break; 31 input1(s1, x); 32 } 33 puts("插入成功"); 34 break; 35 case 3: 36 puts("请输入n的值"); 37 cin>>n; 38 for(i = 1; i <= n; i++) 39 { 40 if(!pop1(s1)) 41 { 42 puts("栈空!"); 43 break; 44 } 45 } 46 if(i == n+1) puts("删除成功"); 47 break; 48 case 4: 49 x = top1(s1); 50 if(x == -1) puts("栈空!"); 51 else printf("%d\n", x); 52 break; 53 case 5: 54 if(init2(s2)) puts("yes"); 55 else puts("no"); 56 break; 57 case 6: 58 puts("输入元素,-1结束"); 59 while(1) 60 { 61 scanf("%d", &x); 62 if(x == -1) break; 63 input2(s2, x); 64 } 65 puts("插入成功"); 66 break; 67 case 7: 68 puts("请输入n的值"); 69 cin>>n; 70 for(i = 1; i <= n; i++) 71 { 72 if(!pop2(s2)) 73 { 74 puts("栈空"); 75 break; 76 } 77 } 78 if(i==n+1) puts("删除成功"); 79 break; 80 case 8: 81 x = top2(s2); 82 if(x == -1) puts("栈空!"); 83 else printf("%d\n", x); 84 break; 85 } 86 } 87 return 0; 88 }
function.cpp
1 #include "130h.h" 2 3 void menu() 4 { 5 puts(""); 6 puts("-------顺序栈操作-------"); 7 puts("1 初始化顺序栈"); 8 puts("2 插入栈顶元素, -1结束"); 9 puts("3 删除n个栈顶元素"); 10 puts("4 弹出栈顶元素"); 11 puts("-------链栈操作--------"); 12 puts("5 初始化链栈"); 13 puts("6 插入栈顶元素, -1结束"); 14 puts("7 删除n个栈顶元素"); 15 puts("8 弹出栈顶元素"); 16 puts("------------------------"); 17 puts("-1 终止程序"); 18 puts(""); 19 } 20 21 bool init1(node1 *&s)//初始化顺序栈 22 { 23 s = (node1*)malloc(sizeof(node1)); 24 s->base = (int*)malloc(MAX*sizeof(int)); 25 if(!s->base) return 0; 26 s->top = s->base; 27 s->Size = MAX; 28 return 1; 29 } 30 31 void input1(node1 *s, int x)//插入栈顶元素 32 { 33 if(s->top - s->base == MAX) 34 { 35 s->base = (int*)realloc(s->base, sizeof(int)); 36 s->top = s->base + MAX; 37 } 38 *s->top++ = x; 39 } 40 41 bool pop1(node1 *s)//删除栈顶元素 42 { 43 if(s->top == s->base) return 0; 44 s->top--; 45 return 1; 46 } 47 48 int top1(node1 *s)//弹出栈顶元素 49 { 50 if(s->top == s->base) return -1; 51 return *(s->top-1); 52 } 53 54 bool init2(node2 *&s)//初始化链栈 55 { 56 s = (node2*)malloc(sizeof(node2)); 57 if(!s) return 0; 58 s->next = NULL; 59 return 1; 60 } 61 62 void input2(node2 *s, int x)////插入栈顶元素 63 { 64 node2 *p; 65 p = (node2*)malloc(sizeof(node2)); 66 p->data = x; 67 p->next = s->next; 68 s->next = p; 69 } 70 71 bool pop2(node2 *s)////删除栈顶元素 72 { 73 if(s->next == NULL) return 0; 74 node2 *p = s->next; 75 s->next = p->next; 76 free(p); 77 return 1; 78 } 79 80 int top2(node2 *s)////弹出栈顶元素 81 { 82 if(!s->next) return -1; 83 return s->next->data; 84 }
加油~