将堆栈实现为数组时出现分段错误
Segmentation fault while implementing stack as an array
这是一个菜单驱动程序,使用 C 编程语言中的数组执行基本的堆栈操作。执行的函数有 push、pop、peep、isempty 和 isfull。
#include<stdio.h>
#include<stdlib.h>
struct stack
{
long int top;
long int size;
char* key;
};
int is_empty(struct stack *s) //check if its empty
{
if(s->top==-1)
{
return -1;
}
else
{
return 1;
}
}
int is_full(struct stack *s) //check if its full
{
if (s->top ==s->size-1)
{
return -1;
}
else
{
return 1;
}
}
void push(struct stack *s, char x) //pushes into stack
{
int check;
check = is_full(s);
if(check==-1)
{
printf("-1\n");
}
else
{
s->top = s->top+1;
s->key[s->top]=x;
}
}
void pop(struct stack *s) //deletes the last element
{
int check;
check = is_empty(s);
if(check==-1)
{
printf("-1\n");
}
else
{
char k;
k = s->key[s->top];
printf("%c\n",k);
s->top--;
}
}
void peep(struct stack *s) //prints the last element without deleting
{ int check;
char k;
check = is_empty(s);
if (check == -1)
{
printf("-1\n");
}
else
{
k = s->key[s->top];
printf("%c \n",k);
}
}
int main()
{
char ch;
char x;
long int n;
struct stack *s;
scanf("%ld ", &n);
s->size = n; //initialise the size
s->top = -1; //setting as -1 base case
s->key= (char *)malloc(n*sizeof(char)); //dynamic allocation of keys
while(1)
{
scanf("%c ",&ch);
switch(ch)
{
case 'i':
scanf("%c ",&x);
push(s,x);
break;
case 'd':pop(s);
break;
case 'p':peep(s);
break;
case 't':exit(0); //termination case
}
}
return 0;
}
这是一个 C 程序,我可以在一些在线编译器中使用它,但在 VScode 和其他编译器中,它显示了一个没有任何输出的分段错误。这是使用数组的堆栈实现。是否是任何 scanf 函数的问题?
您已经创建了一个指针变量 s
,然后访问该结构上的 size
字段。
struct stack *s;
scanf("%ld ", &n);
s->size = n; //initialise the size
Except s
此时实际上并未指向任何内容。您需要为该结构静态或动态分配内存。
struct stack s;
或:
struct stack *s = malloc(sizeof(struct stack));
这是一个菜单驱动程序,使用 C 编程语言中的数组执行基本的堆栈操作。执行的函数有 push、pop、peep、isempty 和 isfull。
#include<stdio.h>
#include<stdlib.h>
struct stack
{
long int top;
long int size;
char* key;
};
int is_empty(struct stack *s) //check if its empty
{
if(s->top==-1)
{
return -1;
}
else
{
return 1;
}
}
int is_full(struct stack *s) //check if its full
{
if (s->top ==s->size-1)
{
return -1;
}
else
{
return 1;
}
}
void push(struct stack *s, char x) //pushes into stack
{
int check;
check = is_full(s);
if(check==-1)
{
printf("-1\n");
}
else
{
s->top = s->top+1;
s->key[s->top]=x;
}
}
void pop(struct stack *s) //deletes the last element
{
int check;
check = is_empty(s);
if(check==-1)
{
printf("-1\n");
}
else
{
char k;
k = s->key[s->top];
printf("%c\n",k);
s->top--;
}
}
void peep(struct stack *s) //prints the last element without deleting
{ int check;
char k;
check = is_empty(s);
if (check == -1)
{
printf("-1\n");
}
else
{
k = s->key[s->top];
printf("%c \n",k);
}
}
int main()
{
char ch;
char x;
long int n;
struct stack *s;
scanf("%ld ", &n);
s->size = n; //initialise the size
s->top = -1; //setting as -1 base case
s->key= (char *)malloc(n*sizeof(char)); //dynamic allocation of keys
while(1)
{
scanf("%c ",&ch);
switch(ch)
{
case 'i':
scanf("%c ",&x);
push(s,x);
break;
case 'd':pop(s);
break;
case 'p':peep(s);
break;
case 't':exit(0); //termination case
}
}
return 0;
}
这是一个 C 程序,我可以在一些在线编译器中使用它,但在 VScode 和其他编译器中,它显示了一个没有任何输出的分段错误。这是使用数组的堆栈实现。是否是任何 scanf 函数的问题?
您已经创建了一个指针变量 s
,然后访问该结构上的 size
字段。
struct stack *s;
scanf("%ld ", &n);
s->size = n; //initialise the size
Except s
此时实际上并未指向任何内容。您需要为该结构静态或动态分配内存。
struct stack s;
或:
struct stack *s = malloc(sizeof(struct stack));