栈逆序后输出
发布时间:2020-12-15 00:27:27 所属栏目:C语言 来源:网络整理
导读:table class="text" tr class="li1" td class="ln"pre class="de1"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
<table class="text"> |
<tr class="li1">
<td class="ln"><pre class="de1">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
#define MaxSize 100
//定义栈结构
typedef struct{
? ? int data[MaxSize];
? ? int top;
}Stack;
//定义队列结构
typedef struct {
? ? int data[MaxSize];
? ? int front,rear;
}Queue;
?
//初始化队列
void InitQueue(Queue &Q) {
? ? Q.front = Q.rear = 0;
? ? printf("循环队列初始化成功n");
}
?
//判空
bool QueueEmpty(Queue Q) {
? ? if(Q.front == Q.rear) {
? ? ? ? return true;
? ? }else {
? ? ? ? return false;
? ? }
}
//入队
?bool EnQueue(Queue &Q,int &e) {
? ? ?//判断队列是否满,可以通过队尾指针的下一个位置就是队头来判断
? ? ?if((Q.rear + 1) % MaxSize == Q.front) {
? ? ? ? ?printf("队列已经满了,%d不能再入队n",e);
? ? ? ? ?return false;
? ? ?}
? ? ?//在队列尾部入队
? ? ?Q.data[Q.rear] = e;
? ? ?printf("入队列的元素: %dn",e);
? ? ?Q.rear = (Q.rear + 1) % MaxSize; ? //队尾指针加1取模
? ? ?return true;
? ? ?
?}
? //出队
int DeQueue(Queue &Q,int &x) {
? ? if(Q.rear == Q.front) {
? ? ? ? return 0; ? ?//队列为空,不能再出队了 ?
? ? }
? ? x = Q.data[Q.front];
? ? Q.front = (Q.front + 1) % MaxSize;
? ? return x;
}
?
?//初始化栈
void InitStack(Stack &S) {
? ? S.top = -1;
? ? printf("顺序栈初始化成功n");
}
?
bool StackEmpty(Stack S) {
? ? if(S.top == -1) { ? ? ?//判断栈空
? ? ? ? return true;
? ? }else {
? ? ? ? return false;
? ? }
}
?
void Push(Stack &S,int &x) {
? ? if(S.top == MaxSize-1 ) {
? ? ? ? printf("顺序栈已经满了,无法进栈n");
? ? }
? ? printf("n入栈元素: %dn",x);
? ? S.data[++S.top] = x;
}
?
int Pop(Stack &S,int &x) {
? ? if(S.top == -1) {
? ? ? ? printf("栈已经空了,不能出栈n");
? ? }
? ? x = S.data[S.top--];
? ? printf("n出栈元素: %dn",x);
? ? return x;
}
//打印队列中的元素
void printQueue(Queue Q) {
? ? while(!QueueEmpty(Q)) {
? ? ? ? printf("%3d",Q.data[Q.front]);
? ? ? ? Q.front = (Q.front + 1) % MaxSize;
? ? }
}
?
int main() {
? ? Stack S;
? ? Queue Q;
? ? Queue InverserQ;
? ? InitQueue(Q);
? ? InitQueue(InverserQ);
? ? InitStack(S);
? ? if(QueueEmpty(Q)) { ?//返回true
? ? ? ? printf("该循环队列为空n");
? ? }else {
? ? ? ? printf("该循环队列不空n");
? ? }
? ? int e;
? ? printf("请输入想要入队的元素,以负数作为结束输入: ?");
? ? while(scanf("%d",&e)) {
? ? ? ? if(e < 0) {
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? EnQueue(Q,e);
? ? }
? ? printf("n输入的队列元素顺序为:n");
? ? printQueue(Q);
? ? //逆序
? ? if( Q.front == Q.rear || (Q.rear - Q.front + MaxSize) % MaxSize == 1) { ? ?
? ? ? ? printf("n不需要逆置n");
? ? ? ? return 0;
? ? }
? ? int x;
? ? while(!QueueEmpty(Q)) {
? ? ? ? DeQueue(Q,x); ? //将队列中的元素依次出队
? ? ? ? Push(S,x); ? ?//再全部Push进入栈中
? ? }
?
? ? while(!StackEmpty(S)) {
? ? ? ? Pop(S,x); ? ? ?//将栈中元素全部出栈
? ? ? ? EnQueue(InverserQ,x); ? ?//再将出栈的元素依次进队
? ? }
? ? ? ?printf("n逆序后队列元素顺序为:n");
? ? ? ? printQueue(InverserQ);
}
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!