#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Snode)
struct Snode
{
int data;
struct Snode *next;
};
struct Snode *top=NULL; //指向栈顶的指针
void Print_list()
{struct Snode *p;
p=top;
if(p==NULL)
{printf("the top is empty");}
else
{while(p!=NULL)
printf("%d \n",p->data);
p=p->next;
}
}
void push(int value) //push a record to the top
{ struct Snode *newp;
newp=(struct Snode *)malloc(LEN);
newp->data=value;
newp->next=top;
top=newp;
}
int pop()
{struct Snode *temp;
if(top==NULL)
printf("the top is empty\n");
else
{temp=top;
top=top->next;
free(temp);
}
}
main()
{ int value;
while(1)
{ printf("please input the number");
scanf("%d",&value);
if(value==-1)
break;
else
push(value);
}
printf("the stack now is\n");
Print_list();
}
程序用一个链表模拟堆栈,当输入的数字为-1时输出堆栈
现在的问题是输出出错,程序总是将栈顶元素循环输出,
怀疑push函数或者print函数出错,
望大家不吝赐教!
以前不重视数据结构的学习,现在吃苦头了,得重头开始了。
[此贴子已经被作者于2006-8-10 23:30:32编辑过]
----------------解决方案--------------------------------------------------------
程序用一个链表模拟堆栈,
what mean?
----------------解决方案--------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Snode)
struct Snode
{
int data;
struct Snode *next;
};
struct Snode *top=NULL; //指向栈顶的指针
void Print_list()
{struct Snode *p;
p=top;
if(p==NULL)
{printf("the top is empty");}
else
{while(p!=NULL)
printf("%d \n",p->data);
p=p->next;
}
}
void push(int value) //push a record to the top
{ struct Snode *newp;
newp=(struct Snode *)malloc(LEN);
newp->data=value;
newp->next=top;
top=newp;
}
int pop()
{struct Snode *temp;
if(top==NULL)
printf("the top is empty\n");
else
{temp=top;
top=top->next;
free(temp);
}
}
main()
{ int value;
while(1)
{ printf("please input the number");
scanf("%d",&value);
if(value==-1)
break;
else
push(value);
}
printf("the stack now is\n");
Print_list();
}
程序用一个链表模拟堆栈,当输入的数字为-1时输出堆栈
现在的问题是输出出错,程序总是将栈顶元素循环输出,
怀疑push函数或者print函数出错,
望大家不吝赐教!
以前不重视数据结构的学习,现在吃苦头了,得重头开始了。
用链表实现堆栈
[此贴子已经被作者于2006-8-10 23:27:41编辑过]
----------------解决方案--------------------------------------------------------
that call 链栈
----------------解决方案--------------------------------------------------------
我真佩服楼主您,您的马虎可是前无古人,后无来者,我刚上线的时候,看了您的程序半天,也没找到错误,后来竟然在一个不可思仪的角落里看到了,楼主这错误您实在不该犯,所以我要罚您一些钱,等您看了之后把它复制一份再跟贴一下,免得我让世人唾骂哈哈哈....
----------------解决方案--------------------------------------------------------
[UseMoney=50]
[/UseMoney]
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Snode)
struct Snode
{
int data;
struct Snode *next;
};
struct Snode *top=NULL;
void Print_list()
{struct Snode *p;
p=top;
if(p==NULL)
{printf("the top is empty");}
else
{while(p!=NULL)
{
printf("%d \n",p->data);
p=p->next;
} //{ }不可丢!!!!
}
}
void push(int value)
{ struct Snode *newp;
newp=(struct Snode *)malloc(LEN);
newp->data=value;
newp->next=top;
top=newp;
}
int pop()
{struct Snode *temp;
if(top==NULL)
printf("the top is empty\n");
else
{temp=top;
top=top->next;
free(temp);
}
}
main()
{ int value;
while(1)
{ printf("please input the number");
scanf("%d",&value);
if(value==-1)
break;
else
push(value);
}
printf("the stack now is\n");
Print_list();
getch();
}
----------------解决方案--------------------------------------------------------
呵呵,惭愧惭愧。多谢几位的指点!
主要刚才一直在算法上面查错,没想到是最基本的东西。
以后得吸取教训了.
----------------解决方案--------------------------------------------------------
哈哈,不要说我赚你金币啊,哈哈,一对大括号50块不算太贵(本来它默认的是1000呢,我怕您付不起,哈哈哈哈),反正您也不用,我还得拿着以后去看美女的照片去
----------------解决方案--------------------------------------------------------
其实没买之前已经可以看见代码了,为了表示谢意。。。
哈哈哈
千金散尽还复来
----------------解决方案--------------------------------------------------------
我还因为这写钱没的用呢.
链栈就是带头结点的单链表做头插法(进栈),删除头结点(出栈).
----------------解决方案--------------------------------------------------------