c语言链表问题
答案:1 悬赏:10 手机版
解决时间 2021-01-25 17:20
- 提问者网友:鐵馬踏冰河
- 2021-01-24 23:54
c语言链表问题
最佳答案
- 五星知识达人网友:蕴藏春秋
- 2021-01-25 01:18
#include
#include
struct link
{
int data;
struct link *next;
};
struct link *add(struct link *head);
void display(struct link *head);
struct link *dele(struct link *head,int a);
int main()
{
char c;
struct link *head=NULL;
printf("要创建一个链表吗?");
scanf(" %c",&c);
while(c=='y'||c=='Y')
{
head=add(head);
printf("要继续创建节点吗?");
scanf(" %c",&c);
}
display(head);
int a;
printf("要删除哪项数据?");
scanf(" %d",&a);
dele(head,a);
display(head);
}
struct link *add(struct link *head)
{
int data;
struct link *p=(struct link*)malloc(sizeof(struct link));
if(head==NULL)
{
head=p;
}
else
{
struct link *pr=head;//一个临时指针pr先保存下head的地址
while(pr->next !=NULL)
{
pr=pr->next ;
}
pr->next =p;
}
printf("输入数据");
scanf("%d",&p->data );
p->next =NULL;
return head;
}
void display(struct link *head)
{
struct link *pr = head;
while(pr!=NULL)
{
printf("%d
",pr->data ); //不要写成&pr->
pr=pr->next ;
}
}
struct link *dele(struct link *head,int a)
{
struct link *p=head;
struct link *pr=NULL;
while(p!= NULL&&a!=p->data )
{
pr=p;//pr临时指针,把p先保存下来
p=p->next ;
}
if(p==NULL)
{
printf("没有找到数据");
}
else
{
if(p==head)
{
head=p->next ;
printf("%d",head->data );
free(p);
}
else
{
pr->next =p->next ;
free(p);
}
}
return head;
}代码在这里,提问的上面代码不知道为什么挤在在一起
#include
struct link
{
int data;
struct link *next;
};
struct link *add(struct link *head);
void display(struct link *head);
struct link *dele(struct link *head,int a);
int main()
{
char c;
struct link *head=NULL;
printf("要创建一个链表吗?");
scanf(" %c",&c);
while(c=='y'||c=='Y')
{
head=add(head);
printf("要继续创建节点吗?");
scanf(" %c",&c);
}
display(head);
int a;
printf("要删除哪项数据?");
scanf(" %d",&a);
dele(head,a);
display(head);
}
struct link *add(struct link *head)
{
int data;
struct link *p=(struct link*)malloc(sizeof(struct link));
if(head==NULL)
{
head=p;
}
else
{
struct link *pr=head;//一个临时指针pr先保存下head的地址
while(pr->next !=NULL)
{
pr=pr->next ;
}
pr->next =p;
}
printf("输入数据");
scanf("%d",&p->data );
p->next =NULL;
return head;
}
void display(struct link *head)
{
struct link *pr = head;
while(pr!=NULL)
{
printf("%d
",pr->data ); //不要写成&pr->
pr=pr->next ;
}
}
struct link *dele(struct link *head,int a)
{
struct link *p=head;
struct link *pr=NULL;
while(p!= NULL&&a!=p->data )
{
pr=p;//pr临时指针,把p先保存下来
p=p->next ;
}
if(p==NULL)
{
printf("没有找到数据");
}
else
{
if(p==head)
{
head=p->next ;
printf("%d",head->data );
free(p);
}
else
{
pr->next =p->next ;
free(p);
}
}
return head;
}代码在这里,提问的上面代码不知道为什么挤在在一起
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯