# 共享栈

## 数据结构

```typedef struct shareStack{
int data[MAXSIZE];
int top1;
int top2;
}shareStack;```

## 出栈操作

```int Pop(shareStack *ss,int flag){
if(flag == 1){
if(ss->top1 == -1)
return -1;
return ss->data[ss->top1--];
}else if(flag == 2){
if(ss->top2 == MAXSIZE)
return -1;
return ss->data[ss->top2++];
}
return -1;
}```

## 入栈操作

```int Push(shareStack *ss,int num,int flag){
if(ss->top1+1 == ss->top2)
return 0;
if(flag == 1){
ss->data[++ss->top1] = num;
return 1;
}else if( flag == 2){
ss->data[--ss->top2] = num;
return 1;
}
return 0;
}```

## 示例代码

```  1 #include <stdio.h>
2 #include <stdlib.h>
3 #define MAXSIZE 20
4
5 typedef struct shareStack{
6     int data[MAXSIZE];
7     int top1;
8     int top2;
9 }shareStack;
10
11 void createStack(shareStack * ss,int n,int m);
12 void showStack(shareStack *ss);
13 int Push(shareStack *ss,int num,int flag);
14 int Pop(shareStack *ss,int flag);
15
16 int main()
17 {
18     shareStack * ss = (shareStack *)malloc(sizeof(shareStack));
19
20     createStack(ss,3,4);
21     showStack(ss);
22
23     if(Push(ss,6,1))
24         showStack(ss);
25
26     if(Push(ss,4,2))
27         showStack(ss);
28
29     int n;
30     n=Pop(ss,1);
31     if(n>=0)
32         printf("the pop num is:%d\n",n);
33     n=Pop(ss,2);
34     if(n>=0)
35         printf("the pop num is:%d\n",n);
36     n=Pop(ss,1);
37     if(n>=0)
38         printf("the pop num is:%d\n",n);
39     n=Pop(ss,1);
40     if(n>=0)
41         printf("the pop num is:%d\n",n);
42     n=Pop(ss,1);
43     if(n>=0)
44         printf("the pop num is:%d\n",n);
45     n=Pop(ss,1);
46     if(n>=0)
47         printf("the pop num is:%d\n",n);
48     n=Pop(ss,1);
49     if(n>=0)
50         printf("the pop num is:%d\n",n);
51
52     showStack(ss);
53
54     return 0;
55 }
56
57 void createStack(shareStack * ss,int n,int m){
58     int i;
59     ss->top1 = -1;
60     ss->top2 = MAXSIZE;
61     for(i=0;i<n;i++){
62         ss->top1++;
63         ss->data[ss->top1] = 2*i+1;
64     }
65     for(i=0;i<m;i++){
66         ss->top2--;
67         ss->data[ss->top2] = 2*i+1;
68     }
69 }
70
71 void showStack(shareStack *ss){
72     int i;
73     for(i=0;i<ss->top1+1;i++){
74         printf("%d->",ss->data[i]);
75     }
76     printf("top1-----top2");
77     for(i=ss->top2;i<MAXSIZE;i++){
78         printf("<-%d",ss->data[i]);
79     }
80     printf("\n");
81 }
82
83 int Push(shareStack *ss,int num,int flag){
84     if(ss->top1+1 == ss->top2)
85         return 0;
86     if(flag == 1){
87         ss->data[++ss->top1] = num;
88         return 1;
89     }else if( flag == 2){
90         ss->data[--ss->top2] = num;
91         return 1;
92     }
93     return 0;
94 }
95
96 int Pop(shareStack *ss,int flag){
97     if(flag == 1){
98         if(ss->top1 == -1)
99             return -1;
100         return ss->data[ss->top1--];
101     }else if(flag == 2){
102         if(ss->top2 == MAXSIZE)
103             return -1;
104         return ss->data[ss->top2++];
105     }
106     return -1;
107 }```

797 篇文章74 人订阅

0 条评论

## 相关文章

### textFile构建RDD的分区及compute计算策略

1,textFile A),第一点，就是输入格式，key，value类型及并行度的意义。 def textFile( path: String, mi...

2557

### 聊聊eureka server的response cache

eureka-core-1.8.8-sources.jar!/com/netflix/eureka/resources/ApplicationResource....

1143

4857

4068

### 走进 RxSwift 之观察者模式

RxSwift 是 ReactiveX 系列的 Swift 版本，如果你之前用过 ReactiveCocoa(RAC) 的话，想必对 Functional Re...

2092

4505

1451

2573

### dubbo消费方服务调用过程源码分析

dubbo PRC服务调用过程很复杂，这里准备通过分析一个典型rpc方法调用的调用栈来说明调用过程。说它典型，是因为本次分析的调用场景很典型简单 先定义一个接...

2.7K8

### 聊聊sentinel的SimpleHttpCommandCenter

sentinel-transport-simple-http-0.1.1-sources.jar!/com/alibaba/csp/sentinel/trans...

911