请大家帮帮我 2

ML20 2002-04-13 11:42:12
我是一名会计专业的大二学生,今年初对计算机产生了浓厚的兴趣,刚刚学完C语言,现在正在学数据结构,不知怎么学.只知道把书中的伪代码改成C代码上机执行.可是改后的总是出毛病,这不,又错了.我想用showlist()显示表中的值,用getelem()获得第N个值,可是不对.请大家帮我看看.
#include <stdio.h>
#include <stdlib.h>

struct lnode{
int data;
struct lnode * next;
};

void getelem(struct lnode *li,int i,int *e);
void showlist(struct lnode *li);
void createlist(struct lnode *li,int n);

int main(){
struct lnode lin;
int n;
int e;
int c;

printf("\nHow many members do you want:");
scanf("%d",&n);

createlist(&lin,n);
showlist(&lin);

printf("\nWhich value you want to got:");
scanf("%d",&c);

getelem(&lin,c,&e);
printf("\nThe value is %d.",e);

getchar();
getchar();

return 0;
}

void getelem(struct lnode *li,int i,int *e){
struct lnode *pl;
register int j;

pl=li->next;
j=1;

while(pl&&j<i){
pl=pl->next;
++j;
}

*e=pl->data;
}

void showlist(struct lnode *li){
struct lnode * se;
se=li->next;

printf("\n");
while(se){
printf("%d\t",se->data);
se=se->next;
}
} /*showlist*/

void createlist(struct lnode *li,int n){
register int i;
struct lnode *p;

li=(lnode *)malloc(sizeof(lnode));
li->next=NULL;

for(i=n;i>0;--i){
p=(lnode *)malloc(sizeof(lnode));
printf("\nNow enter the value of this member:");
scanf("%d",&(p->data));
p->next=li->next;
li->next=p;
}

} /*createlist*/
...全文
44 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mthcy 2002-04-13
  • 打赏
  • 举报
回复
介绍一本书,《数据结构(c语言版)》清华大学出版社的,这样你就
不用看伪码了。
Ordie 2002-04-13
  • 打赏
  • 举报
回复
如果用C的编译器编译你的代码会出现错误,因为createlist函数中几处
lnode*前面都没有加上struct关键字。我把代码改成了可以运行的版本,
在vc上通过了,可能对你有用。还有几点应该注意:
1 分配内存的操作一定要判断返回值。
2 对于象getelem这样的函数只要返回一个int值就可以,传递e的地址
这种方法比较麻烦。


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

struct lnode{
int data;
struct lnode * next;
};

void getelem(struct lnode *li,int i,int *e);
void showlist(struct lnode *li);
struct lnode *createlist(int n);

int main()
{
struct lnode *lin;
int n;
int e;
int c;

printf("\nHow many members do you want:");
scanf("%d",&n);

lin = createlist(n);
showlist(lin);

printf("\nWhich value you want to got:");
scanf("%d",&c);

getelem(lin,c,&e);
printf("\nThe value is %d.",e);

getchar();
getchar();

return 0;
}

void getelem(struct lnode *li,int i,int *e)
{
struct lnode *pl=li;
/* register int j;

pl=li->next;
j=1;

while(pl&&j<i){
pl=pl->next;
++j;
}

*e=pl->data;*/
int j=0;
while (pl!=NULL)
{
j++;
if (i==j) *e=pl->data;
pl = pl->next;
}
}

void showlist(struct lnode *li)
{
struct lnode *se=NULL;
for (se=li; se!=NULL; se=se->next)
printf("%d\t", se->data);
} /*showlist*/

struct lnode *createlist(int n)
{
register int i;
struct lnode *p=NULL, *li=NULL, *pCur=NULL;

for(i=n;i>0;--i)
{
p=(struct lnode *)malloc(sizeof(struct lnode));
if (p==NULL)
{
printf("memory allocate error!");
return NULL;
}
printf("\nNow enter the value of this member:");
scanf("%d",&(p->data));
p->next=NULL; //到这里新分配的p节点已经准备就绪
if (i==n) {li = pCur = p;} //如果是第一个则将它
//赋给li(头指针)
else
{//如果不是第一个则把p赋给pCur(当前指针,总是指向
//最后一个结点)的指针域,这时p成为最后一个结点,
//所以再把pCur指向p即可。
pCur->next = p;
pCur = p;
}
}
return li;
} /*createlist*/
rty 2002-04-13
  • 打赏
  • 举报
回复
这不是线性链表吗,我去试试

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧