| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 811 人关注过本帖
标题:[求助]链栈操做出了点问题
只看楼主 加入收藏
柒兲
Rank: 1
等 级:新手上路
威 望:1
帖 子:126
专家分:0
注 册:2007-9-26
收藏
 问题点数:0 回复次数:5 
[求助]链栈操做出了点问题

#include <stdio.h>
#include <malloc.h>
# define null 0
typedef struct stacknode
{ int data;
struct stacknode *next;
}stacklink;
typedef struct
{
stacklink *top;
int stacksize;
}stackk;

void initlink(stackk *s)
{s->top=(stacklink *)malloc(sizeof(stacklink));
s->top->data=0;
s->top->next=null;
}

int poplink(stackk *s)
{
stacklink *p;int v;
if(s->top->next==null) printf("the stackis empty\n");
else
{
v=s->top->next->data;
p=s->top->next;
s->top=s->top->next;
free(p);

}
return v;
}

void pushlink(stackk *s,int x)
{stacklink *p;
p=(stacklink *)malloc(sizeof(stacklink));
p->data=x;
p->next=s->top->next;
s->top->next=p;
}

int gettop(stackk *s)
{int e;
if(s==null) printf("the stack is empty!\n");
e=s->top->next->data;
return e;
}
void display(stackk *s)
{stacklink *p;
p=s->top->next;
printf("display the stacklink:\n");
if (s->top=null) printf("the stacklink is empty!\n");
else {while(p)
{printf("->%d",p->data);
p=p->next;}
}
}
void main()
{int n,k,i,select,h,x1,x2;
stackk *p ; initlink(p);
printf("create a empty stacklink!\n");

printf("input a stacklink length:\n");
scanf("%d",&n);
for (i=1;i<=n;i++)
{printf("input a stacklink value:\n");
scanf("%d",&k);
pushlink(p,k);
}
printf("select 1:display()\n");
printf("select 2:pushlink()\n");
printf("select 3:poplink()\n");
printf("select 4:gettop()\n");
printf("input a your select(1-4):\n");
scanf("%d",&select);
switch(select)
{case 1:{display(p);break;}
case 2:{printf("input a push a value :\n");
scanf("%d",&h);
pushlink(p,h);
display(p);
break;}
case 3:{x1=poplink(p);printf("x1->%d\n",x1);
display(p);
break;}
case 4: {x2=gettop(p);printf("x2->%d",x2);
break;}
}
}

希望那个大哥帮个忙! 这个程序 在C-Free `3.5里能够通过编译``并结果正确,但在vc中那么通不过啊主要问题在了
这里
void initlink(stackk *s)
{s->top=(stacklink *)malloc(sizeof(stacklink));
s->top->data=0;
s->top->next=null;
}
通过调式出现了`内存分配有问题
Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
First-chance exception in huiwenpanduan.exe: 0xC0000005: Access Violation.


搜索更多相关主题的帖子: include null 
2007-10-14 13:16
ondy
Rank: 1
等 级:新手上路
威 望:1
帖 子:88
专家分:0
注 册:2007-9-4
收藏
得分:0 
指针操作有误

2007-10-14 13:19
柒兲
Rank: 1
等 级:新手上路
威 望:1
帖 子:126
专家分:0
注 册:2007-9-26
收藏
得分:0 
回复:(ondy)指针操作有误

拜托师兄说清楚点`` 谢谢拉`!
帮我改下


2007-10-14 13:31
柒兲
Rank: 1
等 级:新手上路
威 望:1
帖 子:126
专家分:0
注 册:2007-9-26
收藏
得分:0 

那个师兄`` 帮个忙啊` ` 急啊``1
在线等哦`!

2007-10-14 13:38
zxc1998
Rank: 1
等 级:新手上路
威 望:1
帖 子:133
专家分:0
注 册:2007-3-21
收藏
得分:0 

void main()
{int n,k,i,select,h,x1,x2;
stackk *p ; initlink(p); //看这个,指针p没有申请空间
.....
void initlink(stackk *s) //s是一个随机指针
{s->top=(stacklink *)malloc(sizeof(stacklink));
s->top->data=0;
s->top->next=null;
}

2007-10-19 00:11
柒兲
Rank: 1
等 级:新手上路
威 望:1
帖 子:126
专家分:0
注 册:2007-9-26
收藏
得分:0 

谢谢大家` 我问题已经解决了!


2007-10-27 22:33
快速回复:[求助]链栈操做出了点问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015053 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved