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

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

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

知识点

题目知识点
5820貌似都不能称之为完整的链表?
5821尾插法
5822头插法
5823尾插法,记录长度
5824链表数据查找

题目

emm直接背就完事了,后面会写总结篇

5820

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

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

int main()
{
    LNode *a,*b,*c,*Head,*D;
    int a1,a2,a3,a4;
    scanf("%d%d%d%d",&a1,&a2,&a3,&a4);
    Head=(LNode *)malloc(sizeof(LNode));Head->next=NULL;//开辟头节点所需空间,并且指向空
    a=(LNode *)malloc(sizeof(LNode));a->data=a1;//动态开辟节点,数据域赋值
    b=(LNode *)malloc(sizeof(LNode));b->data=a2;
    c=(LNode *)malloc(sizeof(LNode));c->data=a3;
    D=(LNode *)malloc(sizeof(LNode));D->data=a4;
    b->next=D;D->next=c;//把D插入到bc
    printf("Head:->%d->%d->%d->%d",a->data,b->data,D->data,c->data);
    return 0;
}

5821

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

void CreatList(Lnode * H ,int n)
{
    Lnode * p,* r;
    int x;
    r=H;
    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 PrintList(Lnode * H)
{
    Lnode * p;
    p=H->next;
    while(p!=NULL)
    {
        printf("%d  ",p->data);
        p=p->next;
    }
    puts("");
}
int n;
int main()
{
    Lnode * H;
    while(~scanf("%d",&n))
    {
        H=(Lnode *)malloc(sizeof (Lnode));H->next=NULL;
        CreatList(H,n);
        PrintList(H);
    }
    return 0;
}

5822

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

void CreatList2(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;
    }
    puts("");
}
int n;
int main()
{
    Lnode * H;
    while(~scanf("%d",&n))
    {
        H=(Lnode *)malloc(sizeof (Lnode));H->next=NULL;
        CreatList2(H,n);
        PrintList(H);
    }
    return 0;
}

5823

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

int CreatList(Lnode * H)
{
    Lnode *p,*r;
    r=H;
    int res=0;

    int x;
    while(~scanf("%d",&x)&&x)
    {
        ++res;
        p=(Lnode *)malloc(sizeof (Lnode));
        p->data=x;
        r->next=p;r=p;
    }
    r->next=NULL;
    return res;
}
void PrintList(Lnode * H)
{
    Lnode * p;
    p=H->next;
    while(p!=NULL)
    {
        printf("%d  ",p->data);
        p=p->next;
    }
}
int main()
{
    Lnode * H;

    H=(Lnode *)malloc(sizeof (Lnode));H->next=NULL;
    int len=CreatList(H);
    PrintList(H);
    printf("\n%d",len);

    return 0;
}

5824

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

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

void CreatList(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 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;
    int idx=0;
    p=H->next;
    while(p!=NULL&&p->data!=x)
    {
        ++idx;
        p=p->next;
    }
    if(p!=NULL)printf("%d %d",idx+1,p->data);
    else printf("0");
}

int main()
{
    Lnode * H;
    int n,x;
    scanf("%d",&n);
    H=(Lnode *)malloc(sizeof(Lnode));H->next=NULL;
    CreatList(H,n);
    scanf("%d",&x);
    SearchList(H,x);
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值