C语言将值1,3,5依次入栈
时间: 2024-05-12 22:18:09 浏览: 15
可以使用以下代码将值1、3、5依次入栈:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
int stack[MAX_SIZE];
int top = -1;
void push(int x) {
if (top == MAX_SIZE - 1) {
printf("Error: Stack overflow\n");
exit(1);
}
stack[++top] = x;
}
int main() {
push(1);
push(3);
push(5);
printf("Values pushed to stack: 1, 3, 5\n");
return 0;
}
```
这段代码定义了一个栈,使用 push 函数将值1、3、5依次入栈,并输出了已入栈的值。
相关问题
c语言代码实现输入若干个整数,依次入栈,依次出栈并输出元素值,以空格分隔
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
int data[MAX_SIZE];
int top;
} Stack;
void init(Stack *s) {
s->top = -1;
}
int push(Stack *s, int value) {
if (s->top == MAX_SIZE - 1) {
return 0; // 栈满,入栈失败
}
s->top++;
s->data[s->top] = value;
return 1; // 入栈成功
}
int pop(Stack *s, int *value) {
if (s->top == -1) {
return 0; // 栈空,出栈失败
}
*value = s->data[s->top];
s->top--;
return 1; // 出栈成功
}
int main() {
Stack s;
init(&s);
int n, value;
printf("请输入整数个数:");
scanf("%d", &n);
printf("请输入%d个整数:", n);
for (int i = 0; i < n; i++) {
scanf("%d", &value);
push(&s, value);
}
printf("出栈序列为:");
while (s.top != -1) {
pop(&s, &value);
printf("%d ", value);
}
printf("\n");
return 0;
}
```
c语言实现顺序栈的创建、入栈、出栈、取栈顶元素、遍历的完整代码
以下是C语言实现顺序栈的完整代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100 // 定义栈的最大容量
typedef int ElemType; // 栈元素类型
typedef struct {
ElemType data[MAXSIZE]; // 存储栈元素
int top; // 栈顶指针
} SqStack;
// 初始化栈
void InitStack(SqStack *s) {
s->top = -1;
}
// 判断栈是否为空
int IsEmpty(SqStack s) {
return s.top == -1;
}
// 判断栈是否已满
int IsFull(SqStack s) {
return s.top == MAXSIZE - 1;
}
// 入栈
int Push(SqStack *s, ElemType e) {
if (IsFull(*s)) // 栈已满,无法入栈
return 0;
s->data[++s->top] = e; // 栈顶指针加1,将元素e入栈
return 1;
}
// 出栈
int Pop(SqStack *s, ElemType *e) {
if (IsEmpty(*s)) // 栈为空,无法出栈
return 0;
*e = s->data[s->top--]; // 将栈顶元素赋值给e,栈顶指针减1
return 1;
}
// 取栈顶元素
int GetTop(SqStack s, ElemType *e) {
if (IsEmpty(s)) // 栈为空,无栈顶元素
return 0;
*e = s.data[s.top]; // 将栈顶元素赋值给e
return 1;
}
// 遍历栈
void TraverseStack(SqStack s) {
int i;
printf("栈中元素为:");
for (i = s.top; i >= 0; i--) // 从栈顶到栈底依次输出元素
printf("%d ", s.data[i]);
printf("\n");
}
int main() {
SqStack s;
ElemType e;
int i;
InitStack(&s); // 初始化栈
printf("入栈操作:\n");
printf("请输入要入栈的元素,以-1结束:");
scanf("%d", &e);
while (e != -1) {
if (!Push(&s, e)) // 入栈失败
printf("栈已满,%d未入栈!\n", e);
else
printf("%d已入栈\n", e);
scanf("%d", &e);
}
printf("\n出栈操作:\n");
while (!IsEmpty(s)) {
if (!Pop(&s, &e)) // 出栈失败
printf("栈已空,无法出栈!\n");
else
printf("%d已出栈\n", e);
}
printf("\n遍历栈操作:\n");
for (i = 1; i <= 5; i++)
Push(&s, i); // 入栈操作
TraverseStack(s); // 遍历栈
return 0;
}
```
以上代码实现了顺序栈的创建、入栈、出栈、取栈顶元素和遍历等基本操作。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)