数据结构实验之链表

数据结构实验之链表一:顺序建立链表

Time Limit: 1000MS  Memory Limit: 65536KB

Problem Description
输入N个整数,按照输入的顺序建立单链表存储,并遍历所建立的单链表,输出这些数据。
Input
第一行输入整数的个数N;
第二行依次输入每个整数。
Output
输出这组整数。
Example Input
8
12 56 4 6 55 15 33 62
Example Output
12 56 4 6 55 15 33 62
Hint
不得使用数组!
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data; //数据域,用来记录数据
    struct node *next; //指针域,用来记录下一节点的地址
};
struct node *p,*tail,*head;
int main()
{
    int n,i;
    scanf("%d",&n);
    head=(struct node *)malloc(sizeof(struct node)); //开辟内存,创建头结点
    head->next=NULL; //初始状态下只有head链节,他没有后继结点,所以他的next值为NULL(空)
    tail=head; //tail 游动的指针,建立链表的时候始终指向最后一个链节
    for(i=0;i<n;i++) //初始状态下只有一个head链节,那么最后一个链节也是head,tail指向head
    {
        p=(struct node *)malloc(sizeof(struct node)); //p 开辟新的内存,存储新的链节
        scanf("%d",&p->data);
        p->next=NULL; //新链节是链表的最后一个链节,后面没有后继,那么,就把他的next值设为NULL(空)
        tail->next=p; //把p挂在tail后面
        tail=p; //之后,p成为最后一个链节
    } //循环结束,链表建立完成,head是头结点
    p=head->next; //从头遍历一遍
    while(p) //while(p!=NULL)当p访问不到NULL的时候,就会一直循环,NULL是链表结束的标志
    {
       if(p->next!=NULL)
            printf("%d ",p->data);
       else
            printf("%d\n",p->data);
       p=p->next; //p访问本节点的next值,p就指向下一个节点,相当于向后移动一个节点
    }
    return 0;
}

数据结构实验之链表二:逆序建立链表

Time Limit: 1000MS  Memory Limit: 65536KB

Problem Description
输入整数个数N,再输入N个整数,按照这些整数输入的相反顺序建立单链表,并依次遍历输出单链表的数据。
Input
第一行输入整数N;;
第二行依次输入N个整数,逆序建立单链表。
Output
依次输出单链表所存放的数据。
Example Input
10
11 3 5 27 9 12 43 16 84 22 
Example Output
22 84 16 43 12 9 27 5 3 11 
Hint
不能使用数组!

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node *p,*head;
int main()
{
    int n,i;
    scanf("%d",&n);
    head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    for(i=0;i<n;i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        p->next=NULL;
        scanf("%d",&p->data);
        p->next=head->next;
        head->next=p;
    }
    while(p)
    {
        if(p->next!=NULL)
            printf("%d ",p->data);
        else
            printf("%d\n",p->data);
        p=p->next;
    }
    return 0;
}
                               数据结构实验之链表三:链表的逆置
    
    
Time Limit: 1000MS  Memory Limit: 65536KB
Problem Description
输入多个整数,以-1作为结束标志,顺序建立一个带头结点的单链表,之后对该单链表的数据进行逆置,并输出逆置后的单链表数据。
Input
输入多个整数,以-1作为结束标志。
Output
输出逆置后的单链表数据。
Example Input
12 56 4 6 55 15 33 62 -1
Example Output
62 33 15 55 6 4 56 12
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node *q,*p,*r,*head;
int main()
{
    int n,i=0;
    head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    p=(struct node *)malloc(sizeof(struct node));
    p->next=NULL;
    r=(struct node *)malloc(sizeof(struct node));
    r->next=NULL;
    while(scanf("%d",&n)!=EOF&&n!=-1)
    {
        if(i==0)
        {
            head->data=n;
            p=head;
        }
        else
        {
            q=(struct node *)malloc(sizeof(struct node));
            q->next=NULL;
            q->data=n;
            p->next=q;
            p=p->next;
        }
        i++;
    }
    p=head->next;
    q=head;
    head->next=NULL;
    while(p)
    {
        r=p->next;
        p->next=q;
        q=p;
        head=p;
        p=r;
    }
    p=head;//遍历打印逆置后的链表
    while(p!=NULL)
    {
        printf("%d",p->data);
        if(p->next!=NULL)
            printf(" ");
        p=p->next;
    }
    printf("\n");
    return 0;
}

数据结构实验之链表四:有序链表的归并

Time Limit: 1000MS  Memory Limit: 65536KB
Problem Description
分别输入两个有序的整数序列(分别包含M和N个数据),建立两个有序的单链表,将这两个有序单链表合并成为一个大的有序单链表,并依次输出合并后的单链表数据。
Input
第一行输入M与N的值; 
第二行依次输入M个有序的整数;
第三行依次输入N个有序的整数。
Output
输出合并后的单链表所包含的M+N个有序的整数。
Example Input
6 5
1 23 26 45 66 99
14 21 28 50 100
Example Output
1 14 21 23 26 28 45 50 66 99 100
Hint
不得使用数组!

#include<stdio.h>
struct node
{
    int data;
    struct node *next;
};
struct node *q,*p,*head1,*head2,*r,*tail;//指针p,r,tail,游动的
void set(int);
void pai(void);
void set(int k)
{
    int a;
    while(k--)
    {
        scanf("%d",&a);
        q=(struct node *)malloc(sizeof(struct node));
        q->next=NULL;
        q->data=a;
        p->next=q;
        p=p->next;
    }
    return ;
}
void pai(void)
{
    r=head2->next;
    while(r!=NULL)
    {
        tail=(struct node *)malloc(sizeof(struct node));
        tail->data=r->data;
        tail->next=NULL;
        p=head1->next;
        q=head1;
        while(p!=NULL)
        {
            if(tail->data<p->data)
            {
                q->next=tail;
                tail->next=p;
                break;
            }
            q=p;
            p=p->next;
        }
        if(p==NULL)
        {
            tail->next=NULL;
            q->next=tail;
        }
        r=r->next;
    }
    p=head1->next;
    while(p!=NULL)
    {
        printf("%d",p->data);
        if(p->next!=NULL)
            printf(" ");
        p=p->next;
    }
    printf("\n");
}
int main()
{
    int n,m;
    head1=(struct node *)malloc(sizeof(struct node));
    head2=(struct node *)malloc(sizeof(struct node));
    head1->next=NULL;
    head2->next=NULL;
    p=(struct node *)malloc(sizeof(struct node));
    r=(struct node *)malloc(sizeof(struct node));
    scanf("%d %d",&m,&n);
    p=head1;
    set(m);//建立链表用的一个反法,节省时间空间
    p=head2;
    set(n);
    pai();
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值