一、数据结构---两栈共享空间
实现简单的两栈共享空间的数据结构
栈1满和栈2满的四种情况要分析清楚

1 #include<stdio.h>
2 #define MAXSIZE 8
3 typedef struct spare_stack{
4 int top1;
5 int top2;
6 int data[MAXSIZE];
7 }ss;
8 struct spare_stack * stack_init(struct spare_stack *spare){
9 spare = (struct spare_stack *)malloc(sizeof(struct spare_stack));
10 spare->top2 = MAXSIZE-1;
11 spare->top1 = -1;
12 return spare;
13 }
14 void push(struct spare_stack *spare,int stacknum){
15 char a;
16 int stack_num = stacknum;
17 if(stack_num == 1)
18 {
19 printf("stack 1 push data!\n");
20 }
21 printf("input data!\n");
22 while((spare->top2)-(spare->top1) != 1)
23 {
24
25 scanf(" %c",&a);
26 if(a == 'q')
27 {
28 break;
29 }
30 if(stack_num == 1)
31 {
32 spare->data[++spare->top1] = a;
33 }
34 else
35 {
36 spare->data[spare->top2--] = a;
37 }
38
39 }
40 if((spare->top2)-(spare->top1) == 1)
41 {
42 printf("******栈满!!!******\n");
43 }
44
45
46 }
47 void pop(struct spare_stack *spare){
48 while(spare->top1 != -1)
49 {
50 printf("top1=%d,data=%c\n",spare->top1,spare->data[(spare->top1)--]);
51 }
52 }
53 void stack_status(struct spare_stack *spare){
54 if(spare->top1 == -1 && spare->top2 != MAXSIZE-1)
55 {
56 printf("stack1 empty!\n");
57 }
58 else if(spare->top2 == MAXSIZE-1 && spare->top1 != -1){
59 printf("stack2 empty!\n");
60 }
61 else if(spare->top1 == -1 && spare->top2 == MAXSIZE-1)
62 {
63 printf("stack empty!\n");
64 }
65 }
66 void main()
67 {
68 struct spare_stack *spare;
69 int topnum1,topnum2;
70 spare = stack_init(spare);
71
72 push(spare,1);
73 stack_status(spare);
74 pop(spare);
75 stack_status(spare);
76 }

浙公网安备 33010602011771号