• 不带头结点的单链表——数据结构课堂作业


    #include <stdio.h>
    #include <stdlib.h>
    /**************************************/
    /* 链表实现的头文件,文件名slnklist.h */
    /**************************************/

    #include <stdio.h>
    #include <stdlib.h>
    /**************************************/
    /* 链表实现的头文件,文件名slnklist.h */
    /**************************************/
    typedef int datatype;
    typedef struct link_node
    {
        datatype info;
        struct link_node *next;
    } node;
    typedef node *linklist;
    
    /**********************************/
    /*函数名称:creatbystack()              */
    /*函数功能:头插法建立单链表            */
    /**********************************/
    linklist creatbystack()
    {
        linklist  head,s;
        datatype x;
        head=NULL;
        printf("请输入若干整数序列:
    ");
        scanf("%d",&x);
        while (x!=0)        /*以0结束输入*/
        {
            s=(linklist)malloc(sizeof(node));  /*生成待插入结点*/
            s->info=x;
            s->next=head;            /*将新结点插入到链表最前面*/
            head=s;
            scanf("%d",&x);
        }
        return head;                /*返回建立的单链表*/
    }
    /**********************************/
    /*函数名称:creatbyqueue()              */
    /*函数功能:尾插法建立单链表            */
    /**********************************/
    linklist creatbyqueue()
    {
        linklist head,r,s;
        datatype x;
        head=r=NULL;
        printf("请输入若干整数序列:
    ");
        scanf("%d",&x);
        while (x!=0) /*以0结束输入*/
        {
            s=(linklist)malloc(sizeof(node));
            s->info=x;
            if (head==NULL)        /*将新结点插入到链表最后面*/
                head=s;
            else
                r->next=s;
            r=s;
            scanf("%d",&x);
        }
        if (r)  r->next=NULL;
        return head;                    /*返回建立的单链表*/
    }
    /**********************************/
    /*函数名称:print()                      */
    /*函数功能:输出不带头结点的单链表      */
    /**********************************/
    void print(linklist head)
    {
        linklist p;
        int i=0;
        p=head;
        printf("List is:
    ");
        while(p)
        {
            printf("%5d",p->info);
            p=p->next;
            i++;
            if (i%10==0) printf("
    ");
        }
        printf("
    ");
    }
    /**********************************/
    /*函数名称:delList()                  */
    /*函数功能:释放不带头结点的单链表      */
    /**********************************/
    void delList(linklist head)
    {
        linklist p=head;
        while (p)
        {
            head=p->next;
            free(p);
            p=head;
        }
    }
    View Code

    /*编写函数slnklist delx(linklist head, datatype x),删除不带头结点单链表head中第一个值为x 的结点。
    并构造测试用例进行测试。
    */
    /**********************************/
    /*文件名称:lab2_01.c */
    /**********************************/

    /*编写函数slnklist delx(linklist head, datatype x),删除不带头结点单链表head中第一个值为x 的结点。
    并构造测试用例进行测试。
    */
    /**********************************/
    /*文件名称:lab2_01.c             */
    /**********************************/
    
    #include "slnklist.h"
    /*请将本函数补充完整,并进行测试*/
    linklist delx(linklist head,datatype x)
    {
        linklist p = head;
        if(head->info==x) {
            head = head ->next;
        }
        else {
            linklist temp = head;
            p = head ->next;
            while(p) {
                if(p->info!=x) {
                    temp = p;
                    p = p->next;
                }
                else {
                    temp ->next = p->next;
                    break;
                }
            }
        }
        return head;
    }
    
    int main()
    {   datatype x;
        linklist head;
        head=creatbyqueue();        /*尾插入法建立单链表*/
        print(head);
        printf("请输入要删除的值:");
        scanf("%d",&x);
        head=delx(head,x);            /*删除单链表的第一个值为x的结点*/
        print(head);
        delList(head);                /*释放单链表空间*/
        return 0;
    }
    View Code

    /**********************************/
    /*文件名称:lab2_02.c */
    /**********************************/
    /*
    假设线性表(a1,a2,a3,…an)采用不带头结点的单链表存储,
    请设计算法函数linklist reverse1(linklist head)和
    void reverse2(linklist *head)将不带头结点的单链表head就地倒置,
    使表变成(an,an-1,…a3.a2,a1)。并构造测试用例进行测试。
    */

    /**********************************/
    /*文件名称:lab2_02.c                 */
    /**********************************/
    /*
    假设线性表(a1,a2,a3,…an)采用不带头结点的单链表存储,
    请设计算法函数linklist reverse1(linklist  head)和
    void reverse2(linklist *head)将不带头结点的单链表head就地倒置,
    使表变成(an,an-1,…a3.a2,a1)。并构造测试用例进行测试。
    */
    #include "slnklist.h"
    /*请将本函数补充完整,并进行测试*/
    linklist reverse1(linklist head)
    {
        linklist p,s;
        p = NULL;
        while(head) {
            datatype x = head->info;
            head = head ->next;
            s=(linklist)malloc(sizeof(node));  /*生成待插入结点*/
            s->info=x;
            s->next=p;            /*将新结点插入到链表最前面*/
            p=s;
        }
        return p;
    }
    
    void reverse2(linklist *head)
    {
    
        linklist p,s,tem;
        tem = *head;
        p = NULL;
        while(*head) {
            datatype x = tem->info;
            *head = (*head)->next;
            s=(linklist)malloc(sizeof(node));  /*生成待插入结点*/
            s->info=x;
            s->next=p;            /*将新结点插入到链表最前面*/
            tem=s;
        }
        head = &tem;
    
    }
    
    int main()
    {
        datatype x;
        linklist head;
        head=creatbystack();        /*头插入法建立单链表*/
        print(head);                /*输出原链表*/
        head= reverse1(head);        /*倒置单链表*/
        print(head);                /*输出倒置后的链表*/
        reverse2(&head);            /*倒置单链表*/
        print(head);
        delList(head);
        return 0;
    }
    View Code

    /*
    假设不带头结点的单链表head是升序排列的,设计算法函数linklist insert(linklist head,datatype x),
    将值为x的结点插入到链表head中,并保持链表有序性。
    分别构造插入到表头、表中和表尾三种情况的测试用例进行测试。
    */
    /**********************************/
    /*文件名称:lab2_03.c */
    /**********************************/

    /*
    假设不带头结点的单链表head是升序排列的,设计算法函数linklist insert(linklist head,datatype x),
    将值为x的结点插入到链表head中,并保持链表有序性。
    分别构造插入到表头、表中和表尾三种情况的测试用例进行测试。
    */
    /**********************************/
    /*文件名称:lab2_03.c                 */
    /**********************************/
    #include "slnklist.h"
    /*请将本函数补充完整,并进行测试*/
    linklist insert(linklist head,datatype x)
    {
        linklist  s,temp,p;
        if(x<=head->info) {
            s = (linklist)malloc(sizeof(node));
            s ->info = x;
            s ->next = head;
            head = s;
        }
        else {
            p = head;
            temp = head->next;
            int flag = 0;
            while(temp->info<x&&temp) {
                p = temp;
                temp = temp ->next;
                if(temp==NULL) {
                    flag = 1;
                    break;
                }
            }
            if(!flag) {
                s = (linklist)malloc(sizeof(node));
                s->info = x;
                s->next = temp;
                p->next = s;
            }
            else {
                s = (linklist)malloc(sizeof(node));
                p ->next = s;
                s->info = x;
                s->next = NULL;
            }
        }
    
        return head;
    }
    int main()
    {
        datatype x;
        linklist head;
        printf("输入一组升序排列的整数:
    ");
        head=creatbyqueue();                /*尾插入法建立单链表*/
        print(head);
        printf("请输入要插入的值:");
        scanf("%d",&x);
        head=insert(head,x);                /*将输入的值插入到单链表适当位置*/
        print(head);
        delList(head);
        return 0;
    }
    View Code

    /*
    编写算法函数linklist delallx(linklist head, int x),删除不带头结点单链表head中所有值为x的结点。
    */
    /**********************************/
    /*文件名称:lab2_04.c */
    /**********************************/

    /*
    编写算法函数linklist delallx(linklist head, int x),删除不带头结点单链表head中所有值为x的结点。
    */
    /**********************************/
    /*文件名称:lab2_04.c                 */
    /**********************************/
    #include "slnklist.h"
    /*请将本函数补充完整,并进行测试*/
    linklist delallx(linklist head,int x)
    {
        int flag = 1;
        while((head->info)==x&&head) {
            head = head->next;
            if(head==NULL) {
                flag = 0;
                break;
            }
    
        }
        if(flag==1) {
            printf("%d 
    ",head->info);
    
            linklist pre = head;
            linklist p = head->next;
            while(p) {
                if(p->info==x) {
                    pre ->next = p->next;
                    p = p->next;
                }
                else {
                    pre = p;
                    p = p->next;
                }
            }
        }
    
        return head;
    }
    
    int main()
    {
        datatype x;
        linklist head;
        head=creatbyqueue();                /*尾插入法建立单链表*/
        print(head);
        printf("请输入要删除的值:");
        scanf("%d",&x);
        head=delallx(head,x);
        print(head);
        delList(head);
        return 0;
    }
    View Code
  • 相关阅读:
    关于Design Complier/Library Compiler的跌坑(坑爹)记录
    博客暂时停更
    简单的Verilog测试模板结构
    存储器的设计/建模
    静态时序分析的三种分析模式(简述)
    Linux系统的基本使用
    Modelsim的使用——复杂的仿真
    Python-第三方库requests
    MySQL查询结果写入到文件总结
    MySQL创建函数报“ERROR 1418 ”错误,不能创建函数
  • 原文地址:https://www.cnblogs.com/TreeDream/p/6031548.html
Copyright © 2020-2023  润新知