对实现堆栈的怀疑 [英] DOUBT ON IMPLEMENTATION OF STACK
本文介绍了对实现堆栈的怀疑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我运行编译好的文件时,它会显示分段错误.
这有什么不对?
请帮助!!!
WHEN I RUN THE COMPILED FILE ITS SHOWING SEGMENTATION FAULT.
WHAT''S WRONG WITH THIS?
PLEASE HELP!!!
#include<stdio.h>
#include<malloc.h>
struct node{
int data;
struct node *link;
}*top,*ptr,*start;
main()
{
top->link=NULL;
int choice,info;
printf("CHOOSE AN OPTION\n");
scanf("%d",&choice);
printf("1.PUSH AN ELEMENT\n");
printf("2.POP AN ELEMENT\n");
printf("3.DISPLAY\n");
printf("4.EXIT\n");
switch(choice)
{
case 1:
printf("ENTER THE ELEMENT YOU WANT TO PUSH\n");
scanf("%d",&info);
push(info);
break;
/*case 2:
pop(top->data);
break;*/
case 3:
display();
break;
}
}
push(int info)
{
if(top->link!=NULL)
{
ptr->link=top;
top=malloc(sizeof(struct node));
top->data=info;
top->link=NULL;
}
else
{
start->link=top;
top->data=info;
top->link=NULL;
}
}
display()
{ ptr=start;
while(ptr->link!=NULL)
{
int info;
info=(ptr->data);
printf("%d\t",info);
ptr=ptr->link;
}
}
当我运行编译好的文件时,它会显示分段错误.
这有什么不对?
请帮助!!!
WHEN I RUN THE COMPILED FILE ITS SHOWING SEGMENTATION FAULT.
WHAT''S WRONG WITH THIS?
PLEASE HELP!!!
推荐答案
请看main
的第一行.top
来自哪里?您从未为该指针分配内存.
—SA
Look at the very first line ofmain
. Wheretop
comes from? You never allocated memory for this pointer.
—SA
这篇关于对实现堆栈的怀疑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文