use*_*330 0 c stack struct pointers netbeans
#include<stdio.h>
#include<stdlib.h>
struct node{
int info;
node *link;
};
node *top = NULL;
void push();
void pop();
void display();
main()
{
int choice;
while(1)
{
printf("Enter your choice:\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
break;
default:
printf("Wrong choice");
}
// getch();
}
}
void push()
{
node *tmp;
int pushed_item;
tmp = new node;
printf("Enter the value to be pushed in the stack:");
scanf("%d",&pushed_item);
tmp->info=pushed_item;
tmp->link=top;
top=tmp;
}
void pop()
{
node *tmp;
if(top == NULL)
printf("Stack is empty");
else
{
tmp=top;
printf("Popped item is %d",tmp->info);
top=top->link;
delete tmp;
}
}
void display()
{
node *ptr;
ptr = top;
if(top==NULL)
printf("Stack is empty");
else
{
while(ptr != NULL)
{
printf("%d\n",ptr->info);
ptr=ptr->link;
}
}
}
Run Code Online (Sandbox Code Playgroud)
每当我尝试创建一个新节点时,我都会收到错误,例如
node *tmp;
tmp = new node;
Run Code Online (Sandbox Code Playgroud)
错误在第二行.它在其他IDE(如TurboC++)中正常工作,但在Netbeans中,它给出了错误:"无法找到其声明的标识符."
C中没有new(和delete)运算符.您必须使用标准函数malloc(和free)声明的un头<stdlib.h>.
考虑到这种结构定义
struct node{
int info;
node *link;
};
Run Code Online (Sandbox Code Playgroud)
在C中无效.您必须struct在链接的定义中使用标记.例如
struct node
{
int info;
struct node *link;
};
Run Code Online (Sandbox Code Playgroud)
C和C++中的函数main应具有返回类型int.
似乎TurboC++将您的代码编译为C++代码.