《C语言及程序设计》实践参考——改造链表

简介: 返回:贺老师课程教学链接【项目3 - 改造链表】 下面是一个建立动态链表的程序。阅读程序,然后按要求改造程序。#include <iostream> using namespace std; #include <stdio.h>#include <malloc.h>#define N 5typedef struct

返回:贺老师课程教学链接

【项目3 - 改造链表】
下面是一个建立动态链表的程序。阅读程序,然后按要求改造程序。

#include  <iostream>  
using namespace std;  
#include <stdio.h>
#include <malloc.h>
#define N 5
typedef struct NODE
{
    int data;            //结点的数据
    struct NODE *next;  //指向下一结点
} Node;
Node *head=NULL;    //将链表头定义为全局变量,以便于后面操作
void make_list();   //建立链表
void out_list();    //输出链表

int main( )
{
    make_list();
    out_list();
    return 0;
}
void make_list()
{
    int n;
    Node *p;
    printf("输入若干正数(以0或一个负数结束)建立链表:" );
    scanf("%d", &n);
    while(n>0)   //输入若干正数建立链表,输入非正数时,建立过程结束
    {
        p=(Node*)malloc(sizeof(Node));  //新建结点
        p->data=n;
        p->next=head;  //新建的结点指向原先的链表头
        head=p;    //链表头赋值为新建的节点,这样,新结点总是链表头
        scanf("%d", &n);      //输入下一个数,准备建立下一个结点
    }
    return;
}
void out_list()
{
    Node *p=head;
    printf("链表中的数据为:\n");
    while(p!=NULL)
    {
        printf("%d ", p->data);
        p=p->next;
    }
    printf("\n");
    return;
}

在上面的程序基础上定义下面的函数,实现相应的功能。
为简便起见,每编写一个函数,立刻在main函数中调用进行测试。
(1)编写make_list2()函数建立链表,使建立链表时,后输入的数据,将新输入的数字对应的结点放在链表末尾。若输入为3 5 2 9 4 7 0,建立的链表为:

[参考解答]

#include <stdio.h>
#include <malloc.h>
#define N 5
typedef struct NODE
{
    int data;            //结点的数据
    struct NODE *next;  //指向下一结点
} Node;
Node *head=NULL;
void make_list2();  //新结点始终在链表尾部
void out_list();    //输出链表

int main( )
{
    make_list2();
    out_list();
    return 0;
}
void make_list2()
{
    int n;
    Node *p,*q;  //p用于指向新建立的结点, q指向链表尾部
    printf("输入若干正数(以0或一个负数结束)建立链表:\n");
    scanf("%d", &n);
    while(n>0)
    {
        p=(Node*)malloc(sizeof(Node));
        p->data=n;
        p->next=NULL;
        if(head==NULL)
            head=p;
        else
            q->next=p;
        q=p;
        scanf("%d", &n);
    }
    return;
}
void out_list()
{
    Node *p=head;
    printf("链表中的数据为:\n");
    while(p!=NULL)
    {
        printf("%d ", p->data);
        p=p->next;
    }
    printf("\n");
    return;
}

(2)编写函数void search(int x),输出链表中是否有值为x的结点。
[参考解答]

#include <stdio.h>
#include <malloc.h>
#define N 5
typedef struct NODE
{
    int data;            //结点的数据
    struct NODE *next;  //指向下一结点
} Node;
Node *head=NULL;
void make_list2();  //新结点始终在链表尾部
void out_list();    //输出链表
void search(int x);  //查找是否有值为x的结点

int main( )
{
    int x;
    make_list2();
    out_list();
    scanf("%d", &x);  //测试中输入一个链表中有的数字
    search(x);
    scanf("%d", &x);  //测试中输入一个链表中没有的数字
    search(x);
    return 0;
}

void make_list2()
{
    int n;
    Node *p,*q;  //p用于指向新建立的结点, q指向链表尾部
    printf("输入若干正数(以0或一个负数结束)建立链表:\n");
    scanf("%d", &n);
    while(n>0)
    {
        p=(Node*)malloc(sizeof(Node));
        p->data=n;
        p->next=NULL;
        if(head==NULL)
            head=p;
        else
            q->next=p;
        q=p;
        scanf("%d", &n);
    }
    return;
}

void out_list()
{
    Node *p=head;
    printf("链表中的数据为:\n");
    while(p!=NULL)
    {
        printf("%d ", p->data);
        p=p->next;
    }
    printf("\n");
    return;
}

void search(int x)
{
    //查找是否有值为x的结点
    Node *p=head;
    while(p!=NULL&&p->data!=x)
    {
        p=p->next;
    }
    if(p!=NULL)  //退出上一层循环一定是因为p->data==x
        printf("在链表中有值为%d的结点\n", x);
    else
        printf("在链表中没有值为%d的结点\n", x);
    return;
}

补充:

这个版本的search,只判断是否找到,有兴趣的同学,可以实现能输出有几个的版本
void search(int x)还可以重定义为bool search(int x),让main函数根据返回值输出结果
对应能输出有几个的版本,重定义为int search(int x),返回值是存在的个数,为0没有找到


(3)编写函数delete_first_node(),删除链表中的第一个结点。
[参考解答]

#include <stdio.h>
#include <malloc.h>
#define N 5
typedef struct NODE
{
    int data;            //结点的数据
    struct NODE *next;  //指向下一结点
} Node;
Node *head=NULL;
void make_list2();  //新结点始终在链表尾部
void out_list();    //输出链表
void delete_first_node();  //删除第一个结点

int main( )
{
    make_list2();
    out_list();
    delete_first_node();
    out_list();
    return 0;
}

void make_list2()
{
    int n;
    Node *p,*q;  //p用于指向新建立的结点, q指向链表尾部
    printf("输入若干正数(以0或一个负数结束)建立链表:\n");
    scanf("%d", &n);
    while(n>0)
    {
        p=(Node*)malloc(sizeof(Node));
        p->data=n;
        p->next=NULL;
        if(head==NULL)
            head=p;
        else
            q->next=p;
        q=p;
        scanf("%d", &n);
    }
    return;
}

void out_list()
{
    Node *p=head;
    printf("链表中的数据为:\n");
    while(p!=NULL)
    {
        printf("%d ", p->data);
        p=p->next;
    }
    printf("\n");
    return;
}

void delete_first_node()
{
    Node *p=head;
    if (p!=NULL)
    {
        head = p->next;
        free(p);
        printf("删除了首结点.\n");
    }
    else
    {
        printf("空链表,不能删除.\n");
    }
    return;
}

(4)编写函数delete_node(int x),删除结点值为x的结点。
[参考解答]

#include <stdio.h>
#include <malloc.h>
#define N 5
typedef struct NODE
{
    int data;            //结点的数据
    struct NODE *next;  //指向下一结点
} Node;
Node *head=NULL;
void make_list2();  //新结点始终在链表尾部
void out_list();    //输出链表
void delete_node(int x);  //删除第一个结点

int main( )
{
    make_list2();
    out_list();
    delete_node(3);
    out_list();
    return 0;
}

void make_list2()
{
    int n;
    Node *p,*q;  //p用于指向新建立的结点, q指向链表尾部
    printf("输入若干正数(以0或一个负数结束)建立链表:\n");
    scanf("%d", &n);
    while(n>0)
    {
        p=(Node*)malloc(sizeof(Node));
        p->data=n;
        p->next=NULL;
        if(head==NULL)
            head=p;
        else
            q->next=p;
        q=p;
        scanf("%d", &n);
    }
    return;
}

void out_list()
{
    Node *p=head;
    printf("链表中的数据为:\n");
    while(p!=NULL)
    {
        printf("%d ", p->data);
        p=p->next;
    }
    printf("\n");
    return;
}

void delete_node(int x)
{
    Node *p,*q;
    if (head==NULL)
        printf("空链表,不能删除\n");
    else
    {
        //要删除的恰是首结点(不排除连续有若干个结点值为x),
        while(head!=NULL&&head->data==x)
        {
            p=head;
            head=head->next;
            free(p);
        }
        if(head!=NULL)
        {
            p=head;
            q=p->next;
            while(q!=NULL)
            {
                if(q->data==x)//q就是该删除的结点
                {
                    p->next=q->next;
                    free(q);
                }
                else     //q不该删除,继续考察下一个
                {
                    p=q;
                }
                q=p->next;  //总是p的下一个结点
            }
        }
    }
    return;
}

为充分测试,该程序运行了4次,所用的测试输入数据分别为:
5 2 9 9 7 11 3 0
3 5 2 9 9 7 11 0
3 5 2 9 3 9 7 11 0
3 3 3 3 3 3 3 0


(5)编写make_list3()函数建立链表,使建立链表时,使结点中的数据呈现升序。若输入为3 5 2 9 4 7 0,建立的链表为:

[参考解答]

#include <stdio.h>
#include <malloc.h>
#define N 5
typedef struct NODE
{
    int data;            //结点的数据
    struct NODE *next;  //指向下一结点
} Node;
Node *head=NULL;
void make_list3();  //建立有序链表,各结点由小到大
void out_list();    //输出链表

int main( )
{
    make_list3();
    out_list();
    return 0;
}

void make_list3()
{
    int n;
    Node *t,*p,*q;  //p用于指向新建立的结点, q指向链表尾部
    printf("输入若干正数(以0或一个负数结束)建立链表:\n");
    scanf("%d", &n);
    while(n>0)
    {
        t=(Node*)malloc(sizeof(Node));
        t->data=n;
        t->next=NULL;
        if(head==NULL)   //是空链表,p作为第一个结点即可
            head=t;
        else             //插入p结点后,各结点应该保持有序
        {
            if(n<=head->data)  //新加入的结点应该为首结点
            {
                t->next=head;
                head=t;
            }
            //应该找到合适的位置后,将结点插入
            //此时,链表中至少已经有一个结点,且插入结点不是首结点
            else
            {
                p=head;
                q=p->next;   //p与q相邻,p更靠近q,插入位置将在p和q之间
                while(q!=NULL&&n>q->data)  //链表没有完且p结点比n小,一直往后找
                {
                    p=q;
                    q=p->next;
                }
                if(q==NULL) //q为null,作为最后一个结点直接插入到p后即可
                {
                    p->next = t;
                }
                else   //t插入到p和q之间
                {
                    t->next=q;
                    p->next=t;
                }
            }
        }
        scanf("%d", &n);
    }
    return;
}

void out_list()
{
    Node *p=head;
    printf("链表中的数据为:\n");
    while(p!=NULL)
    {
        printf("%d ", p->data);
        p=p->next;
    }
    printf("\n");
    return;
}

(6)编写函数void insert(int x),将值为x的结点插入到由make_list3建立起来的有序链表中。
[参考解答]

#include <stdio.h>
#include <malloc.h>
#define N 5
typedef struct NODE
{
    int data;            //结点的数据
    struct NODE *next;  //指向下一结点
} Node;
Node *head=NULL;
void make_list3();  //建立有序链表,各结点由小到大
void out_list();    //输出链表
void insert(int x); //将值为x的结点插入到有序链表中,使仍有序

int main( )
{
    make_list3();
    out_list();
    insert(5);
    out_list();
    return 0;
}

void make_list3()
{
    int n;
    Node *t,*p,*q;  //p用于指向新建立的结点, q指向链表尾部
    printf("输入若干正数(以0或一个负数结束)建立链表:\n");
    scanf("%d", &n);
    while(n>0)
    {
        t=(Node*)malloc(sizeof(Node));
        t->data=n;
        t->next=NULL;
        if(head==NULL)   //是空链表,p作为第一个结点即可
            head=t;
        else             //插入p结点后,各结点应该保持有序
        {
            if(n<=head->data)  //新加入的结点应该为首结点
            {
                t->next=head;
                head=t;
            }
            //应该找到合适的位置后,将结点插入
            //此时,链表中至少已经有一个结点,且插入结点不是首结点
            else
            {
                p=head;
                q=p->next;   //p与q相邻,p更靠近q,插入位置将在p和q之间
                while(q!=NULL&&n>q->data)  //链表没有完且p结点比n小,一直往后找
                {
                    p=q;
                    q=p->next;
                }
                if(q==NULL) //q为null,作为最后一个结点直接插入到p后即可
                {
                    p->next = t;
                }
                else   //t插入到p和q之间
                {
                    t->next=q;
                    p->next=t;
                }
            }
        }
        scanf("%d", &n);
    }
    return;
}

void insert(int x) //将值为x的结点插入到有序链表中,使仍有序
{
    Node *t,*p,*q;  //p用于指向新建立的结点, q指向链表尾部
    t=(Node*)malloc(sizeof(Node));;
    t->data=x;
    t->next=NULL;
    if(head==NULL)   //是空链表,p作为第一个结点即可
        head=t;
    else             //插入p结点后,各结点应该保持有序
    {
        if(x<=head->data)  //新加入的结点应该为首结点
        {
            t->next=head;
            head=t;
        }
        //应该找到合适的位置后,将结点插入
        //此时,链表中至少已经有一个结点,且插入结点不是首结点
        else
        {
            p=head;
            q=p->next;   //p与q相邻,p更靠近q,插入位置将在p和q之间
            while(q!=NULL&&x>q->data)  //链表没有完且p结点比n小,一直往后找
            {
                p=q;
                q=p->next;
            }
            if(q==NULL) //q为null,作为最后一个结点直接插入到p后即可
            {
                p->next = t;
            }
            else   //t插入到p和q之间
            {
                t->next=q;
                p->next=t;
            }
        }
    }
    return;
}

void out_list()
{
    Node *p=head;
    printf("链表中的数据为:\n");
    while(p!=NULL)
    {
        printf("%d ", p->data);
        p=p->next;
    }
    printf("\n");
    return;
}

(6)另解
实际上,从程序整体上看,makelist3()可以直接调用insert(int x)实现,这样写出的程序合乎工程上的原则。这启示我们,用函数组织程序结构,应该成为一种意识。

#include <stdio.h>
#include <malloc.h>
#define N 5
typedef struct NODE
{
    int data;            //结点的数据
    struct NODE *next;  //指向下一结点
} Node;
Node *head=NULL;
void make_list3();  //建立有序链表,各结点由小到大
void out_list();    //输出链表
void insert(int x); //将值为x的结点插入到有序链表中,使仍有序

int main( )
{
    make_list3();
    out_list();
    insert(5);
    out_list();
    return 0;
}

void make_list3()
{
    int n;
    printf("输入若干正数(以0或一个负数结束)建立链表:\n");
    scanf("%d", &n);
    while(n>0)
    {
        insert(n);
        scanf("%d", &n);
    }
    return;
}

void insert(int x) //将值为x的结点插入到有序链表中,使仍有序
{
    Node *t,*p,*q;  //p用于指向新建立的结点, q指向链表尾部
    t=(Node*)malloc(sizeof(Node));;
    t->data=x;
    t->next=NULL;
    if(head==NULL)   //是空链表,p作为第一个结点即可
        head=t;
    else             //插入p结点后,各结点应该保持有序
    {
        if(x<=head->data)  //新加入的结点应该为首结点
        {
            t->next=head;
            head=t;
        }
        //应该找到合适的位置后,将结点插入
        //此时,链表中至少已经有一个结点,且插入结点不是首结点
        else
        {
            p=head;
            q=p->next;   //p与q相邻,p更靠近q,插入位置将在p和q之间
            while(q!=NULL&&x>q->data)  //链表没有完且p结点比n小,一直往后找
            {
                p=q;
                q=p->next;
            }
            if(q==NULL) //q为null,作为最后一个结点直接插入到p后即可
            {
                p->next = t;
            }
            else   //t插入到p和q之间
            {
                t->next=q;
                p->next=t;
            }
        }
    }
    return;
}

void out_list()
{
    Node *p=head;
    printf("链表中的数据为:\n");
    while(p!=NULL)
    {
        printf("%d ", p->data);
        p=p->next;
    }
    printf("\n");
    return;
}
目录
相关文章
|
5月前
|
存储 C语言
【C语言程序设计——函数】递归求斐波那契数列的前n项(头歌实践教学平台习题)【合集】
本关任务是编写递归函数求斐波那契数列的前n项。主要内容包括: 1. **递归的概念**:递归是一种函数直接或间接调用自身的编程技巧,通过“俄罗斯套娃”的方式解决问题。 2. **边界条件的确定**:边界条件是递归停止的条件,确保递归不会无限进行。例如,计算阶乘时,当n为0或1时返回1。 3. **循环控制与跳转语句**:介绍`for`、`while`循环及`break`、`continue`语句的使用方法。 编程要求是在右侧编辑器Begin--End之间补充代码,测试输入分别为3和5,预期输出为斐波那契数列的前几项。通关代码已给出,需确保正确实现递归逻辑并处理好边界条件,以避免栈溢出或结果
296 16
|
5月前
|
存储 编译器 C语言
【C语言程序设计——函数】分数数列求和2(头歌实践教学平台习题)【合集】
函数首部:按照 C 语言语法,函数的定义首部表明这是一个自定义函数,函数名为fun,它接收一个整型参数n,用于指定要求阶乘的那个数,并且函数的返回值类型为float(在实际中如果阶乘结果数值较大,用float可能会有精度损失,也可以考虑使用double等更合适的数据类型,这里以float为例)。例如:// 函数体代码将放在这里函数体内部变量定义:在函数体中,首先需要定义一些变量来辅助完成阶乘的计算。比如需要定义一个变量(通常为float或double类型,这里假设用float。
146 3
|
5月前
|
存储 算法 安全
【C语言程序设计——函数】分数数列求和1(头歌实践教学平台习题)【合集】
if 语句是最基础的形式,当条件为真时执行其内部的语句块;switch 语句则适用于针对一个表达式的多个固定值进行判断,根据表达式的值与各个 case 后的常量值匹配情况,执行相应 case 分支下的语句,直到遇到 break 语句跳出 switch 结构,若没有匹配值则执行 default 分支(可选)。例如,在判断一个数是否大于 10 的场景中,条件表达式为 “num> 10”,这里的 “num” 是程序中的变量,通过比较其值与 10 的大小关系来确定条件的真假。常量的值必须是唯一的,且在同一个。
138 2
|
5月前
|
存储 编译器 C语言
【C语言程序设计——函数】回文数判定(头歌实践教学平台习题)【合集】
算术运算于 C 语言仿若精密 “齿轮组”,驱动着数值处理流程。编写函数求区间[100,500]中所有的回文数,要求每行打印10个数。根据提示在右侧编辑器Begin--End之间的区域内补充必要的代码。如果操作数是浮点数,在 C 语言中是不允许直接进行。的结果是 -1,因为 -7 除以 3 商为 -2,余数为 -1;注意:每一个数据输出格式为 printf("%4d", i);的结果是 1,因为 7 除以 -3 商为 -2,余数为 1。取余运算要求两个操作数必须是整数类型,包括。开始你的任务吧,祝你成功!
118 1
【移除链表元素】LeetCode第203题讲解
【移除链表元素】LeetCode第203题讲解
|
存储 SQL 算法
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
|
存储 SQL 算法
LeetCode 题目 86:分隔链表
LeetCode 题目 86:分隔链表
|
存储 算法 Java
【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
102 2
<数据结构>五道LeetCode链表题分析.环形链表,反转链表,合并链表,找中间节点.
<数据结构>五道LeetCode链表题分析.环形链表,反转链表,合并链表,找中间节点
136 1
【经典LeetCode算法题目专栏分类】【第7期】快慢指针与链表
【经典LeetCode算法题目专栏分类】【第7期】快慢指针与链表