数据结构(c语言版)(第三版)实验2

1、实验2 不带头节点的单链表

运行环境:Dev-C++

一、实验目的

1.熟练掌握动态链表结构及有关算法的设计方法。

2.理解不带头节点的单链表的特点,掌握其基本操作。

3.熟练掌握运用不带头节点链表表示特定形式的数据的方法,并设计出有关算法。

二、实验内容

已知不带头节点的链表结构定义及头插法建表,尾插法建表和打印链表等函数定义如下(详见slnklist.h文件),基于该内容完成实验题1~实验4。

#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("请输入若干整数序列:\n");
    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("请输入若干整数序列:\n");
    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:\n");
    while(p)
    {
        printf("%5d",p->info);
        p=p->next;
         i++;
         if (i%10==0) printf("\n");
    }
    printf("\n");
}
/**********************************/
/*函数名称:delList()                 */
/*函数功能:释放不带头结点的单链表      */
/**********************************/
void delList(linklist head)
{ linklist p=head;
  while (p)
  { head=p->next;
    free(p);
    p=head;
  }
}
​

实验2.1

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

/*编写函数slnklist delx(linklist head, datatype x),删除不带头结点单链表head中第一个值为x 的结点。
并构造测试用例进行测试。
*/
/**********************************/
/*文件名称:lab2_01.c             */
/**********************************/
​
#include "slnklist.h"
/*请将本函数补充完整,并进行测试*/
linklist delx(linklist head,datatype x)
{
    if(head->info==x){
        return head->next;
    } 
    else{
        node* p=head;
        node* q=head->next;
        while(q->info!=x){
            p=p->next;
            q=q->next;
        }
        p->next=q->next;
    }
    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;
}

实验2.2

假设线性表(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"
#include<iostream>
#include<vector> 
using namespace std; 
/*请将本函数补充完整,并进行测试*/
linklist reverse1(linklist head)
{
    vector <int> a;
    node* p=head;
    while(p){
        a.push_back(p->info);
        p=p->next;
    }
    p=head;
    while(p){
        p->info=a.back();
        a.pop_back();
        p=p->next;
    }
    return head;
}
void reverse2(linklist *head)
{
    vector <int> a;
    node* p=*head;
    while(p){
        a.push_back(p->info);
        p=p->next;
    }
    p=*head;
    while(p){
        p->info=a.back();
        a.pop_back();
        p=p->next;
    }
}
​
int main()
{   datatype x;
    linklist head;
    head=creatbystack();        /*头插入法建立单链表*/
    print(head);                /*输出原链表*/
    head= reverse1(head);       /*倒置单链表*/
    print(head);                /*输出倒置后的链表*/
    reverse2(&head);            /*倒置单链表*/
    print(head);
    delList(head);
    return 0;
}

实验2.3

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

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

实验2.4

编写算法函数linklist delallx(linklist head, int x),删除不带头结点单链表head中所有值为x的结点。

/*
编写算法函数linklist delallx(linklist head, int x),删除不带头结点单链表head中所有值为x的结点。
*/
/**********************************/
/*文件名称:lab2_04.c                 */
/**********************************/
#include "slnklist.h"
/*请将本函数补充完整,并进行测试*/
linklist delallx(linklist head,int x)
{
    while(head->info==x){
        head=head->next;
    }
    if(head==NULL){
        return head;
    }
    node* p=head;
    node* q=p->next;
    while(q){
        if(q->info==x){
            p->next=p->next->next;
            q=q->next;
        }
        else{
            p=p->next;
            q=q->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;
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

fanlangke

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值