C语言学习记录(13)链表实验题目 && 有序链表

C语言学习记录

前言

一直自己没有学习做笔记的习惯,所以为了加强自己对知识的深入理解,决定将学习笔记写下来,希望向各位大牛们学习交流!

不当之处请斧正!在此感谢!这边就先从学习C语言写起,自己本身对程序语言方面不擅长,所以决定对此从基础开始学习,

大牛们对此文可以忽略!

学校OJ实验

链表实验题目

Problem Description
链表结点的结构定义为:
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

Hint

解题:

struct node *add_to_list(struct node *head, int n)  //向链表首部插入一个值为n的结点
{
    struct node *p = head;
    struct node *newNode = (struct node *)malloc(sizeof(struct node)); //动态申请内存

    if(newNode == NULL)  //内存不足即退出
    {
        exit(1);
    }
    if(head == NULL)  //添加结点
    {
        head = newNode;
        newNode->value = n;
        newNode->next = NULL;
    }
    else
    {
        head = newNode;
        newNode->value = n;
        newNode->next = p;
    }

    return head;
};

struct node *build_list()//建立链表,调用函数struct node *add_to_list(struct node *head, int n)
{
    int n;
    struct node *head;

    head = NULL;

    while(scanf("%d", &n) && n != -1)  //每输入一次增加一个结点
    {
        head = add_to_list(head, n);
    }


    return head;
};

struct node *search_list(struct node *head, int n)  //搜索链表
{
    struct node *s = head;

    while(s->value != n && s->next != NULL)  //遍历链表直到搜索到该值或结尾
    {
        s = s->next;
    }

    if(s->value != n)
    {
        return NULL;
    }
    else
    {
        return s;
    }
};

struct node *delete_from_list(struct node *head, int n) //删除链表中值为n的结点,并释放该结点
{
    struct node *t, *w;

    t = head;


    if( NULL == head )  //空链表则返回
	{
		return head;
	}


    while(t->value != n && t->next != NULL)
    {
        w = t;
        t = t->next;
    }
    if( n == t->value )
    {
        if( t == head )
		{
			*head = *t->next;
			//free(t);
		}
		else
		{
			w->next = t->next;
			free(t);
		}
    }
	else
    {
        return head;
    }


    return head;

};

void print_list(struct node *head)   //打印链表
{
    struct node *p = head;
	int flag = 1;

    do    //遍历链表
    {
		if(flag)  //格式化输出
		{
			printf("%d",p->value);
			flag = 0;
		}
		else
		{
			printf(",%d",p->value);
		}

        p = p->next;
    }while( p );
	putchar('\n');
}






/*
若需要有序链表,只需要把struct node *add_to_list(struct node *head, int n)函数
替换为下列函数即可
struct node *insert(struct node *head, int n)
{
    struct node *s, *p = head;
    struct node *newNode = (struct node *)malloc(sizeof(struct node));

    s = p;

    if(newNode == NULL)
    {
        exit(1);
    }
    if(head == NULL)
    {
        head = newNode;
        newNode->value = n;
        newNode->next = NULL;
    }
    else
    {
        while( p->value <= n && p->next != NULL)
        {
            s = p;
            p = p->next;
        }
        if( p->next == NULL )
        {
            if(p == head)
            {
                if( p->value >= n )
                {
                    head = newNode;
                    newNode->value = n;
                    newNode->next = p;
                }
                else
                {
                    p->next = newNode;
                    newNode->value = n;
                    newNode->next = NULL;
                }
            }
            else
            {
                if( p->value >= n )
                {
                    s->next = newNode;
                    newNode->value = n;
                    newNode->next = p;
                }
                else
                {
                    p->next = newNode;
                    newNode->value = n;
                    newNode->next = NULL;
                }
            }

        }
        else
        {
            if(p == head)
            {
                head = newNode;
                newNode->value = n;
                newNode->next = p;
            }
            else
            {
                if( s==p )
                {
                    s->next = newNode;
                    newNode->value = n;
                    newNode->next = p->next;
                }
                else
                {
                    s->next = newNode;
                    newNode->value = n;
                    newNode->next = p;
                }

            }

        }

    }

    return head;
};
*/
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值