2021-05-20

链表实验题目:

链表结点的结构定义为:
struct node{
    int value;
    struct node *next;
};
编写下列函数:
struct node *add_to_list(struct node *head, int n)  //向链表首部插入一个值为n的结点
struct node *build_list()  //建立链表,调用函数struct node *add_to_list(struct node *head, int n)
struct node *search_list(struct node *head, int n)  //搜索链表
struct node *delete_from_list(struct node *head, int n)  //删除链表中值为n的结点,并释放该结点
void print_list(struct node *head)  //打印链表


测试程序为:
#include <stdio.h>
#include <stdlib.h>

struct node{
    int value;
    struct node *next;
};

struct node *add_to_list(struct node *head, int n);
struct node *build_list();
struct node *search_list(struct node *head, int n);
struct node *delete_from_list(struct node *head, int n);
void print_list(struct node *head);

int main()
{
     struct node *first = NULL;
     int n;

     first = build_list();
     print_list(first);
     scanf("%d", &n);
     if(search_list(first, n) == NULL)
     {
         printf("NO\n");
     }
     else
     {
         printf("YES\n");
     }
      first = delete_from_list(first, n);
     print_list(first);

     return 0;
}
/* 你的代码将被嵌在这里 */

Input Description

输入的第一行是若干整数,为建立链表结点的值,当输入-1时结束。
输入的第二行是一个整数n,为搜索和删除的结点的值。

Output Description

第一行打印出链表结点的值,每个整数以","分隔,最后一个后面没有逗号。
第二行输出搜索值为n的结点的结果,找到了输出YES,否则输出NO。
第三行输出删除值为n的结点后的链表结点的值,每个整数以","分隔,最后一个后面没有逗号。

Sample Input

10 20 30 40 50 60 70 80 90 100 -1
60

Sample Output

100,90,80,70,60,50,40,30,20,10
YES
100,90,80,70,50,40,30,20,10

代码

struct node *add_to_list(struct node *head, int n)
{
    head=malloc(sizeof(struct node));
    head->value=n;
    return head;
}
struct node *build_list()
{
    int n;
    struct node *first=NULL;
    struct node *head;
    do
    {
        scanf("%d",&n);
        if(n==-1)
        {
            break;
        }
        head=add_to_list(head,n);
        head->next=first;
        first=head;
 
    }while(n!=-1);
    return head;
}
struct node *search_list(struct node *head, int n)
{
    struct node *p;
    for(p=head;p!=NULL ;p=p->next)
    {
        if(p->value==n)
        {
            return p;
        }
    }
    return p;
}
struct node *delete_from_list(struct node *head, int n)
{
    struct node *cur,*prue;
    for(cur=head,prue=NULL;cur!=NULL&&cur->value!=n;prue=cur,cur=cur->next)
    ;
if (cur==NULL)
{
    return head;
}
if(prue==NULL)
    {
        head=head->next;
       delete_from_list(head,n);
    }
    else
    {
        prue->next=cur->next;
        delete_from_list(head,n);
    }
    free(cur);
    return head;
 
}
 void print_list(struct node *head)
 {
      struct node *q,*t;
for(t=head,q=head->next;q!=NULL;t=q,q=q->next)
{
    if(q!=NULL)
    {
    printf("%d,",t->value);
    }
}
printf("%d\n",t->value);
 }

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安全天天学

你的鼓励是对我最大的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值