永发信息网

用链表仿真堆栈

答案:1  悬赏:0  手机版
解决时间 2021-05-06 03:30

#include "stdio.h"
#include "stdlib.h"

struct stack
{
int data;
struct stack *next;
};
typedef struct stack stack;
typedef stack *link;
link pointer = NULL;

void printStack();
void push(int value);
int pop(); //返回值等于1,则堆栈中没有元素。否则,返回堆栈顶端元素

int main()
{
int value;
enum choose { push, pop, print, quit } choice; //存放--0,取出--1,打印--2,退出--3
printf("push is 0, pop is 1, print is 2, quit is 3. please select one choice : ");
scanf("%d",&choice);
do{
switch(choice)
{
case 0:
printf("Input a value : ");
scanf("%d",&value);
push(value);
break;
case 1:
value = pop();
if(value == -1)
printf("Have not data!\n");
else
printf("The data is : %d \n",value);
break;
case 2:
printStack();
break;
default:
break;
}
printf("push is 0, pop is 1, print is 2, quit is 3. please select one choice : ");
scanf("%d",&choice);
}while(choice != 3);
return 0;
}

void printStack()
{
link head;
head = pointer;
printf("Output the stack data :\n");
while(head != NULL)
{
printf("%d/t",head->data);
head = head->next;
}
printf("\n");
}

void push(int value)
{
link top = NULL;
top = (link)malloc(sizeof(stack));
if(top == NULL)
printf("memory allocate failure!\n");
else
{
top->data = value;
top->next = pointer;
pointer = top;
}
}

int pop()
{
int temp;
link top;
top = pointer;
if(top != NULL)
{
temp = pointer->data;
pointer = pointer->next;
free(top);
return temp;
}
return -1;
}

错误信息:C:\Program Files\Microsoft Visual Studio\MyProject\tt\a.cpp(29) : error C2064: term does not evaluate to a function
C:\Program Files\Microsoft Visual Studio\MyProject\tt\a.cpp(32) : error C2064: term does not evaluate to a function
执行 cl.exe 时出错.

请高手帮我找出错误 急急急

最佳答案

只是一个小毛病,你在enum中定义了push,pop,函数名与他们冲突了,我给改为了void push1(int value); int pop1(); 在试验一下就没有错误了。


#include <stdio.h>
#include <stdlib.h>


struct stack
{
int data;
struct stack *next;
};
typedef struct stack stack;
typedef stack *link;
link pointer = NULL;


void printStack();
void push1(int value);
int pop1(); //返回值等于1,则堆栈中没有元素。否则,返回堆栈顶端元素
int main()
{
int value;
enum choose { push, pop, print, quit } choice; //存放--0,取出--1,打印--2,退出--3
printf("push is 0, pop is 1, print is 2, quit is 3. please select one choice : ");
scanf("%d",&choice);
do{
switch(choice)
{
case 0:
printf("Input a value : ");
scanf("%d",&value);
push1(value);
break;
case 1:
value=pop1();
if(value == -1)
printf("Have not data!\n");
else
printf("The data is : %d \n",value);
break;
case 2:
printStack();
break;
default:
break;
}
printf("push is 0, pop is 1, print is 2, quit is 3. please select one choice : ");
scanf("%d",&choice);
}while(choice != 3);
return 0;
}


void printStack()
{
link head;
head = pointer;
printf("Output the stack data :\n");
while(head != NULL)
{
printf("%d/t",head->data);
head = head->next;
}
printf("\n");
}


void push1(int value)
{
link top = NULL;
top = (link)malloc(sizeof(stack));
if(top == NULL)
printf("memory allocate failure!\n");
else
{
top->data = value;
top->next = pointer;
pointer = top;
}
}


int pop1()
{
int temp;
link top;
top = pointer;
if(top != NULL)
{
temp = pointer->data;
pointer = pointer->next;
free(top);
return temp;
}
else
return -1;
}

我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
DNF灵犀之心几多钱?
面对自己舍不得放手,又必须放手的爱人该怎么
我知道好友的名字,怎么加他?
有没有什么软件可以看见自己电脑Adminstrator
改装的车过期了再买改装的还有吗
一个曾经热爱体育的人为什么会对体育产生恐惧
如何破解N86手机证书?
磨砂玻璃怎么调,静电玻璃贴膜的使用方法
来者汽车影音地址有知道的么?有点事想过去
有一部异形电影,死得只剩最后一个战士
释迦摩尼和阿弥陀佛两者是什么关系?
晚上穿袜子睡觉有什么利害吗
到底是赚谁的钱?
女朋友过生日,送什么比较特殊?
学院路在什么地方啊,我要过去处理事情
推荐资讯
萌牛鲜奶吧这个地址在什么地方,我要处理点事
不喝酒就没有朋友句子,关于友谊的饮酒诗句
化妆品品牌排名
貌似1个月的奶猫怎么养法?
为什么我的丝路英雄声望达到了丞相长史可是官
“不得不做”用英文怎么说
DNF做任务才给的首饰怎么附魔的?级数没有60
最新全本修仙小说 推荐个
恋人信息上说的话是不是都是其心里话?
有什么节目?
帮忙啊!谁能帮我戒掉炫舞,谢了。
怎样更改登录模式
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?