顺序栈的入栈和出栈c
时间: 2023-11-12 16:12:19 浏览: 44
入栈:
```c
#define MAXSIZE 100 // 定义栈的最大长度
typedef struct Stack {
int data[MAXSIZE]; // 存放栈中元素的数组
int top; // 栈顶指针
} Stack;
// 初始化栈
void initStack(Stack *s) {
s->top = -1;
}
// 判断栈是否为空
int isEmpty(Stack *s) {
return s->top == -1;
}
// 判断栈是否已满
int isFull(Stack *s) {
return s->top == MAXSIZE - 1;
}
// 入栈
void push(Stack *s, int x) {
if (isFull(s)) {
printf("Error: Stack is full!\n");
return;
}
s->data[++s->top] = x;
}
```
出栈:
```c
// 出栈
int pop(Stack *s) {
if (isEmpty(s)) {
printf("Error: Stack is empty!\n");
return -1;
}
return s->data[s->top--];
}
```
以上是顺序栈的入栈和出栈的 C 语言代码实现。
相关问题
C语言实现顺序栈的入栈和出栈两个栈的基本操作函数
以下是C语言实现顺序栈的入栈和出栈两个栈的基本操作函数的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
typedef struct {
int data[MAX_SIZE];
int top1;
int top2;
} SeqStack;
void initStack(SeqStack *s) {
s->top1 = -1;
s->top2 = MAX_SIZE;
}
int isFull(SeqStack *s) {
return (s->top1 + 1) == s->top2;
}
int isEmpty1(SeqStack *s) {
return s->top1 == -1;
}
int isEmpty2(SeqStack *s) {
return s->top2 == MAX_SIZE;
}
void push1(SeqStack *s, int x) {
if (isFull(s)) {
printf("Stack 1 is full.\n");
return;
}
s->data[++s->top1] = x;
}
void push2(SeqStack *s, int x) {
if (isFull(s)) {
printf("Stack 2 is full.\n");
return;
}
s->data[--s->top2] = x;
}
int pop1(SeqStack *s) {
if (isEmpty1(s)) {
printf("Stack 1 is empty.\n");
return -1;
}
return s->data[s->top1--];
}
int pop2(SeqStack *s) {
if (isEmpty2(s)) {
printf("Stack 2 is empty.\n");
return -1;
}
return s->data[s->top2++];
}
int main() {
SeqStack s;
initStack(&s);
push1(&s, 1);
push1(&s, 2);
push1(&s, 3);
push2(&s, 4);
push2(&s, 5);
push2(&s, 6);
printf("%d\n", pop1(&s));
printf("%d\n", pop2(&s));
printf("%d\n", pop1(&s));
printf("%d\n", pop2(&s));
printf("%d\n", pop1(&s));
printf("%d\n", pop2(&s));
return 0;
}
```
在这个例子中,我们定义了一个 `SeqStack` 结构体,包含了两个整型数组和两个指向栈顶的指针 `top1` 和 `top2`。`initStack()` 函数用来初始化栈,将 `top1` 和 `top2` 初始化为 -1 和 `MAX_SIZE`,表示两个栈都为空。`isFull()` 函数判断两个栈是否已经满了,如果 `top1` 和 `top2` 相差为 1,就说明两个栈都已经满了。`isEmpty1()` 和 `isEmpty2()` 函数分别判断两个栈是否为空,如果 `top1` 或 `top2` 等于它们的初始值,则说明它们为空。
`push1()` 和 `push2()` 函数分别用来向两个栈中压入数据。它们都先判断栈是否已经满了,如果已经满了则打印一条提示信息并返回。否则,`push1()` 将数据压入第一个栈的栈顶,同时将 `top1` 加 1;`push2()` 将数据压入第二个栈的栈顶,同时将 `top2` 减 1。
`pop1()` 和 `pop2()` 函数分别用来从两个栈中弹出数据。它们都先判断栈是否为空,如果为空则打印一条提示信息并返回 -1。否则,`pop1()` 从第一个栈的栈顶弹出数据,同时将 `top1` 减 1 并返回弹出的数据;`pop2()` 从第二个栈的栈顶弹出数据,同时将 `top2` 加 1 并返回弹出的数据。
在 `main()` 函数中,我们首先调用 `initStack()` 函数初始化栈。然后,我们分别向两个栈中压入 1、2、3 和 4、5、6 这六个数。接着,我们依次从两个栈中弹出数据,并打印输出。最后,我们返回 0 表示程序正常结束。
用c语言建立顺序栈,并在顺序栈上实现入栈和出栈操作
好的,以下是用C语言建立顺序栈的示例代码,并实现了入栈和出栈操作:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
// 定义顺序栈结构体
typedef struct {
int data[MAX_SIZE]; // 存放栈元素
int top; // 栈顶指针
} SeqStack;
// 初始化顺序栈
void init(SeqStack *s) {
s->top = -1; // 初始时栈为空,栈顶指针设为-1
}
// 判断栈是否为空
int is_empty(SeqStack *s) {
return s->top == -1;
}
// 判断栈是否已满
int is_full(SeqStack *s) {
return s->top == MAX_SIZE - 1;
}
// 入栈操作
int push(SeqStack *s, int x) {
if (is_full(s)) {
printf("栈已满,不能再入栈!\n");
return 0; // 返回0表示入栈失败
}
s->data[++s->top] = x; // 先将栈顶指针加1,再将元素x存入栈中
return 1; // 返回1表示入栈成功
}
// 出栈操作
int pop(SeqStack *s, int *x) {
if (is_empty(s)) {
printf("栈已空,不能再出栈!\n");
return 0; // 返回0表示出栈失败
}
*x = s->data[s->top--]; // 先取出栈顶元素,再将栈顶指针减1
return 1; // 返回1表示出栈成功
}
int main() {
SeqStack s;
init(&s); // 初始化栈
// 入栈操作
push(&s, 1);
push(&s, 2);
push(&s, 3);
// 出栈操作
int x;
pop(&s, &x);
printf("出栈元素:%d\n", x);
pop(&s, &x);
printf("出栈元素:%d\n", x);
pop(&s, &x);
printf("出栈元素:%d\n", x);
return 0;
}
```
这样就可以建立一个顺序栈,并实现入栈和出栈操作了。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)