NEFU锐格实验九[链表2-基本访问操作]

NEFU锐格实验八[链表1-动态节点]

推荐:NEFU大一下C语言锐格实验与作业参考程序目录

知识点

题目知识点
5825查找前驱
5826查询变形
5827删除
5828链表插入排序
5829链表插入

题目

search操作的各种变形

5825

#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
    int data;
    struct node * next;
}Lnode;

void CreatList_Tail(Lnode * H,int n)//尾插法
{
    Lnode * p,*r;
    r=H;
    int x;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        p=(Lnode *)malloc(sizeof (Lnode));
        p->data=x;
        r->next=p;r=p;
    }
    r->next=NULL;
}

void CreatList_Head(Lnode * H,int n)//头插法
{
    Lnode *p;
    int x;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        p=(Lnode *)malloc(sizeof (Lnode));
        p->data=x;
        p->next=H->next;
        H->next=p;
    }
}

void PrintList(Lnode * H)//打印
{
    Lnode * p;
    p=H->next;
    while(p!=NULL)
    {
        printf("%d  ",p->data);
        p=p->next;
    }
}

void SearchList(Lnode * H ,int x)//查询值
{
    Lnode * p, * pre;//pre为前驱
    int idx=1;
    p=H->next;
    while(p!=NULL&&p->data!=x)
    {
        ++idx;
        pre=p;
        p=p->next;
    }
    if(p==NULL)printf("x不存在\n");
    else
    {
        if(idx==1)printf("没有前驱\n");
        else printf("%d\n",pre->data);
    }
}



int main()
{
    Lnode * H;
    int n,x;
    while(~scanf("%d",&n))
    {
        H=(Lnode *)malloc(sizeof(Lnode));H->next=NULL;
        CreatList_Tail(H,n);
        scanf("%d",&x);
        SearchList(H,x);
    }
    return 0;
}

5826

#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
    int data;
    struct node * next;
}Lnode;

void CreatList_Tail(Lnode * H,int n)//尾插法
{
    Lnode * p,*r;
    r=H;
    int x;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        p=(Lnode *)malloc(sizeof (Lnode));
        p->data=x;
        r->next=p;r=p;
    }
    r->next=NULL;
}

void CreatList_Head(Lnode * H,int n)//头插法
{
    Lnode *p;
    int x;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        p=(Lnode *)malloc(sizeof (Lnode));
        p->data=x;
        p->next=H->next;
        H->next=p;
    }
}

void PrintList(Lnode * H)//打印
{
    Lnode * p;
    p=H->next;
    while(p!=NULL)
    {
        printf("%d  ",p->data);
        p=p->next;
    }
}

void SearchList(Lnode * H )//查询
{
    Lnode * p;
    p=H->next;
    int cnt=0;
    while(p!=NULL)
    {
        if(p->data%2==0)cnt++;
        p=p->next;
    }
    printf("%d\n",cnt);
}



int main()
{
    Lnode * H;
    int n,x;
    while(~scanf("%d",&n))
    {
        H=(Lnode *)malloc(sizeof(Lnode));H->next=NULL;
        CreatList_Head(H,n);
        SearchList(H);
    }
    return 0;
}

5827

并不那么正规的remove操作(flag标记是否输出)

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct node
{
    int data;
    bool flag;
    struct node * next;
}Lnode;

void CreatList_Tail(Lnode * H,int n)//尾插法
{
    Lnode * p,*r;
    r=H;
    int x;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        p=(Lnode *)malloc(sizeof (Lnode));
        p->data=x;
        p->flag=1;
        r->next=p;r=p;
    }
    r->next=NULL;
}

void CreatList_Head(Lnode * H,int n)//头插法
{
    Lnode *p;
    int x;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        p=(Lnode *)malloc(sizeof (Lnode));
        p->data=x;
        p->next=H->next;
        H->next=p;
    }
}

void PrintList(Lnode * H)//打印
{
    Lnode * p;
    p=H->next;
    while(p!=NULL)
    {
        if(p->flag)printf("%d  ",p->data);
        p=p->next;
    }
}

void SearchList(Lnode * H )//查询
{
    Lnode * p;
    p=H->next;
    int cnt=0;
    while(p!=NULL)
    {
        if(p->data%2==0)cnt++;
        p=p->next;
    }
    printf("%d\n",cnt);
}

void RemoveList(Lnode * H,int x)//删除
{
    Lnode * p;
    p=H->next;
    while(p!=NULL)
    {
        if(p->data==x)p->flag=0;
        p=p->next;
    }
}


int main()
{
    Lnode * H;
    int n,x;
    while(~scanf("%d",&n))
    {
        H=(Lnode *)malloc(sizeof(Lnode));H->next=NULL;
        CreatList_Tail(H,n);
        scanf("%d",&x);
        RemoveList(H,x);
        PrintList(H);
        printf("\n");
    }
    return 0;
}

正规点的

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct node
{
    int data;
    struct node * next;
}Lnode;

void CreatList_Tail(Lnode * H,int n)//尾插法
{
    Lnode * p,*r;
    r=H;
    int x;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        p=(Lnode *)malloc(sizeof (Lnode));
        p->data=x;
        r->next=p;r=p;
    }
    r->next=NULL;
}

void CreatList_Head(Lnode * H,int n)//头插法
{
    Lnode *p;
    int x;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        p=(Lnode *)malloc(sizeof (Lnode));
        p->data=x;
        p->next=H->next;
        H->next=p;
    }
}

void PrintList(Lnode * H)//打印
{
    Lnode * p;
    p=H->next;
    while(p!=NULL)
    {
        printf("%d  ",p->data);
        p=p->next;
    }
}

void SearchList(Lnode * H )//查询
{
    Lnode * p;
    p=H->next;
    int cnt=0;
    while(p!=NULL)
    {
        if(p->data%2==0)cnt++;
        p=p->next;
    }
    printf("%d\n",cnt);
}

void RemoveList(Lnode * H,int x)//删除
{
    Lnode * p,* pre,*tmp;
    pre=H;p=H->next;
    while(p!=NULL)
    {
        if(p->data==x)
        {
            tmp=p;//直接用p释放后影响后续循环
            pre->next=p->next;
            p=p->next;//pre不用更新,p更新为下一个节点
            free(tmp);//释放
        }
        else
        {
            pre=p;
            p=p->next;
        }
    }
}


int main()
{
    Lnode * H;
    int n,x;
    while(~scanf("%d",&n))
    {
        H=(Lnode *)malloc(sizeof(Lnode));H->next=NULL;
        CreatList_Tail(H,n);
        scanf("%d",&x);
        RemoveList(H,x);
        PrintList(H);
        printf("\n");
    }
    return 0;
}

5828

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct node
{
    int data;
    struct node * next;
}Lnode;

void CreatList_Tail(Lnode * H,int n)//尾插法
{
    Lnode * p,*r;
    r=H;
    int x;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        p=(Lnode *)malloc(sizeof (Lnode));
        p->data=x;
        r->next=p;r=p;
    }
    r->next=NULL;
}

void CreatList_Head(Lnode * H,int n)//头插法
{
    Lnode *p;
    int x;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        p=(Lnode *)malloc(sizeof (Lnode));
        p->data=x;
        p->next=H->next;
        H->next=p;
    }
}

void PrintList(Lnode * H)//打印
{
    Lnode * p;
    p=H->next;
    while(p!=NULL)
    {
        printf("%d  ",p->data);
        p=p->next;
    }
}

void SearchList(Lnode * H )//查询
{
    Lnode * p;
    p=H->next;
    int cnt=0;
    while(p!=NULL)
    {
        if(p->data%2==0)cnt++;
        p=p->next;
    }
    printf("%d\n",cnt);
}

void InsertList(Lnode * H,int x)//插入
{
    Lnode * p,* pre,* idx;
    pre=H;p=H->next;
    idx=(Lnode *)malloc(sizeof (Lnode));
    idx->data=x;
    while(p!=NULL&&p->data<x)
    {
        pre=p;
        p=p->next;
    }
    idx->next=p;
    pre->next=idx;   
}
int main()
{
    Lnode * H;
    int n,x;
    while(~scanf("%d",&n))
    {
        H=(Lnode *)malloc(sizeof(Lnode));H->next=NULL;
        CreatList_Tail(H,n);
        scanf("%d",&x);
        InsertList(H,x);
        PrintList(H);
        printf("\n");
    }
    return 0;
}

5829

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct node
{
    int data;
    struct node * next;
}Lnode;

void CreatList_Tail(Lnode * H,int n)//尾插法
{
    Lnode * p,*r;
    r=H;
    int x;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        p=(Lnode *)malloc(sizeof (Lnode));
        p->data=x;
        r->next=p;r=p;
    }
    r->next=NULL;
}

void CreatList_Head(Lnode * H,int n)//头插法
{
    Lnode *p;
    int x;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        p=(Lnode *)malloc(sizeof (Lnode));
        p->data=x;
        p->next=H->next;
        H->next=p;
    }
}

void PrintList(Lnode * H)//打印
{
    Lnode * p;
    p=H->next;
    while(p!=NULL)
    {
        printf("%d  ",p->data);
        p=p->next;
    }
}

void SearchList(Lnode * H )//查询
{
    Lnode * p;
    p=H->next;
    int cnt=0;
    while(p!=NULL)
    {
        if(p->data%2==0)cnt++;
        p=p->next;
    }
    printf("%d\n",cnt);
}

void InsertList(Lnode * H,int x)//插入
{
    Lnode * p,* pre,* idx;
    pre=H;p=H->next;
    idx=(Lnode *)malloc(sizeof (Lnode));
    idx->data=x;
    while(p!=NULL&&p->data<x)
    {
        pre=p;
        p=p->next;
    }
    idx->next=p;
    pre->next=idx;   
}
int main()
{
    Lnode * H;
    int n,x;
    while(~scanf("%d",&n))
    {
        H=(Lnode *)malloc(sizeof(Lnode));H->next=NULL;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&x);
            InsertList(H,x);
        }
        PrintList(H);
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值