我在 C 中的堆栈实现有问题
Problem with my stack implementation in C
我正在学习数据结构并在 C 上实现堆栈。代码编译正确但堆栈保持未编辑状态,即即使在推送操作之后也没有任何内容被推送到堆栈中并且堆栈保持为空。我不确定这段代码的问题出在哪里。请帮我解决一下这个。谢谢
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 101
//Define a struct
struct stack{
int A[MAX_SIZE]; //Array of size 101
int top; //Variable that stores the index position of the recently inserted element in the stack.
};
//Function to create a stack, set top to -1 and return it.
struct stack CreateStack(){
struct stack p;
p.top = -1;
return p;
}
//Function to insert a number at the end of the stack.
void Push(int x, struct stack p){
//If the array is full, return an error message.
if (p.top == MAX_SIZE - 1){
printf("Error: Stack Overflow!");
return;
}
//Increment top and set insert x at the last of A.
p.top++;
p.A[p.top] = x;
};
//Function to delete an element from the last in a stack.
void Pop(struct stack p){
//If stack is already empty, print a message.
if (p.top == -1){
printf("Empty Stack!");
return;
}
//Decrement top.
p.top--;
};
//Function to return the top element in the stack.
int Top(struct stack p){
return p.A[p.top];
};
//Function to check if the stack is empty.
int IsEmpty(struct stack p){
return p.top == -1;
};
//Function to display all the elements in the stack.
void Print(struct stack p)
{
printf("Stack: ");
for(int i = 0; i <= p.top; i++){
printf("%d", p.A[i]);
}
printf("\n");
};
int main(){
struct stack mystack = CreateStack(); //Creates a stack called mystack.
Push(22, mystack); //Pushes 22 on the stack.
Print(mystack);// Should display 22.
}
你按值传递struct stack p
(复制);而是通过指针传递它,以便修改函数可以更改状态:
void Push(int x, struct stack *p,) {
if (p->top == MAX_SIZE - 1){
printf("Error: Stack Overflow!");
return;
}
p->A[p->top++] = x;
};
惯例是将抽象数据类型(此处 struct stack *p
)作为第一个参数传递。还要考虑返回一个状态,以便调用者可以判断函数是否失败。最后,将 i/o(打印错误消息)与逻辑分开是个好主意,因为它可能取决于调用者的上下文(console/browser/gui,但如果堆栈已满)。例如:
struct stack *Push(struct stack *p, int x) {
if (p->top == MAX_SIZE - 1) return NULL;
p->A[p->top++] = x;
return p;
};
我正在学习数据结构并在 C 上实现堆栈。代码编译正确但堆栈保持未编辑状态,即即使在推送操作之后也没有任何内容被推送到堆栈中并且堆栈保持为空。我不确定这段代码的问题出在哪里。请帮我解决一下这个。谢谢
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 101
//Define a struct
struct stack{
int A[MAX_SIZE]; //Array of size 101
int top; //Variable that stores the index position of the recently inserted element in the stack.
};
//Function to create a stack, set top to -1 and return it.
struct stack CreateStack(){
struct stack p;
p.top = -1;
return p;
}
//Function to insert a number at the end of the stack.
void Push(int x, struct stack p){
//If the array is full, return an error message.
if (p.top == MAX_SIZE - 1){
printf("Error: Stack Overflow!");
return;
}
//Increment top and set insert x at the last of A.
p.top++;
p.A[p.top] = x;
};
//Function to delete an element from the last in a stack.
void Pop(struct stack p){
//If stack is already empty, print a message.
if (p.top == -1){
printf("Empty Stack!");
return;
}
//Decrement top.
p.top--;
};
//Function to return the top element in the stack.
int Top(struct stack p){
return p.A[p.top];
};
//Function to check if the stack is empty.
int IsEmpty(struct stack p){
return p.top == -1;
};
//Function to display all the elements in the stack.
void Print(struct stack p)
{
printf("Stack: ");
for(int i = 0; i <= p.top; i++){
printf("%d", p.A[i]);
}
printf("\n");
};
int main(){
struct stack mystack = CreateStack(); //Creates a stack called mystack.
Push(22, mystack); //Pushes 22 on the stack.
Print(mystack);// Should display 22.
}
你按值传递struct stack p
(复制);而是通过指针传递它,以便修改函数可以更改状态:
void Push(int x, struct stack *p,) {
if (p->top == MAX_SIZE - 1){
printf("Error: Stack Overflow!");
return;
}
p->A[p->top++] = x;
};
惯例是将抽象数据类型(此处 struct stack *p
)作为第一个参数传递。还要考虑返回一个状态,以便调用者可以判断函数是否失败。最后,将 i/o(打印错误消息)与逻辑分开是个好主意,因为它可能取决于调用者的上下文(console/browser/gui,但如果堆栈已满)。例如:
struct stack *Push(struct stack *p, int x) {
if (p->top == MAX_SIZE - 1) return NULL;
p->A[p->top++] = x;
return p;
};