| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 611 人关注过本帖
标题:[求助]关于顺序链表的问题!
只看楼主 加入收藏
liebaorun
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2005-9-15
收藏
 问题点数:0 回复次数:0 
[求助]关于顺序链表的问题!

下面这个链表是可以运行的,但结果不对啊!我是个菜鸟,请教各位大虾了! #include <stdio.h>

struct zm

{

char c;

struct zm *next;

};

extern struct zm *insert(struct zm *,char);

extern struct zm *delc(struct zm *,char);

extern void print(struct zm *);

main()

{

char choice,c;

struct zm *head;

head=NULL;

printf("Enter a choice:\n1.Insert a character!\n2.Delete a character!\n3.End of run!\n");

printf("?");

scanf("%c",&choice);

while(choice!='3')

{

switch(choice)

{

case '1':

printf("Insert a character:\n");

scanf("%c",&c);

head=insert(head,c);

print(head);

break;

case '2':

printf("Input the character you want to deleted:\n");

scanf("%c",&c);

head=delc(head,c);

print(head);

break;

default:

printf("Invalid choice!");

}

printf("?");

scanf("%c",&choice);

}

printf("End of run!\n");

return 0;

}

struct zm *insert(struct zm *head,char c)

{

struct zm *p1,*p2,*new;

new=(struct zm *)malloc(sizeof(struct zm));

p1=head;p2=head;

new->c=c;new->next=NULL;

if(head==NULL){head=new;}

else

{

while((new->c>p1->c)&&(p1->next!=NULL))

{

p2=p1;p1=p1->next;

}

if(new->c<=p1->c)

{

if(p1==head)

{new->next;head=new;}

else

{p2->next=new;new->next=p1;}

}

else

{p1->next=new;}

}

return (head);

}

struct zm *delc(struct zm *head,char c)

{

struct zm *p1,*p2,*temp;

p1=head;p2=head;

if(head==NULL){printf("The list is empty!Can't delete any character!\n");}

else

{

while((p1->c!=c)&&(p1->next!=NULL))

{

p2=p1;p1=p1->next;

}

if(p1->c==c)

{

if(head->c==c){head=p1->next;}

else{p2->next=p1->next;}

}

else

{

printf("The character is not exsit!\n");

}

}

return(head);

}

void print(struct zm *head)

{

struct zm *p;

if(head==NULL)

{printf("The list is:\nNULL\n");}

else

{

p=head;

printf("The list is:\n");

while(p!=NULL)

{

printf("%c-->",p->c);

p=p->next;

}

printf("NULL\n");

}

}

[此贴子已经被作者于2005-9-21 15:44:40编辑过]

搜索更多相关主题的帖子: 链表 顺序 
2005-09-21 11:14
快速回复:[求助]关于顺序链表的问题!
数据加载中...
 
   



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

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