sdut 程序设计二 链表题目汇总(c语言)

A - 数据结构实验之链表一:顺序建立链表
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int main()
{
    int n;
    struct node *head, *tail, *p;
    head = (struct node*)malloc(sizeof (struct node));
    tail = head;

    scanf("%d", &n);

    for (int i = 0; i < n; i ++)
    {   
        p = (struct node*)malloc(sizeof (struct node));
        scanf("%d", &p->data);

        tail->next = p;
        tail = p;
    }

    p = head->next;

    while (p)
    {
        if (p->next == NULL)
            printf("%d", p->data);
        else 
            printf("%d ", p->data);

        p = p->next;
    }
    return 0;
}
B - 数据结构实验之链表二:逆序建立链表
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int main()
{
    int n;
    struct node *head, *p;
    head = (struct node*)malloc(sizeof (struct node));
    head = NULL;

    scanf("%d", &n);

    for (int i = 0; i < n; i ++)
    {
        p = (struct node*)malloc(sizeof (struct node));
        scanf("%d", &p->data);

        p->next = head;
        head = p;
    }

    while (head)
    {
        if (head->next == NULL)
            printf("%d", head->data);
        else
            printf("%d ", head->data);

        head = head->next;
    }
    return 0;
}
数据结构实验之链表三:链表的逆置
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int main()
{
    int x;
    struct node *head, *tail, *p, *head1, *p1;
    head = (struct node*)malloc(sizeof (struct node));
    tail = head;

    while (scanf("%d", &x) && x != -1)
    {
        p = (struct node*)malloc(sizeof (struct node));
        p->data = x;
        tail->next = p;
        tail = p;
    }

    p = head->next;
    head1 = (struct node*)malloc(sizeof (struct node));
    head1 = NULL;

    while (p)
    {
        p1 = (struct node*)malloc(sizeof (struct node));
        p1->data = p->data;

        p1->next = head1;
        head1 = p1;

        p = p->next;
    }

    while (head1)
    {
        if (head1->next == NULL)
            printf("%d", head1->data);
        else
            printf("%d ", head1->data);

        head1 = head1->next;
    }
    return 0;
}
数据结构实验之链表四:有序链表的归并
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int main()
{
    int n, m;
    struct node *head1, *tail1, *p1, *head2, *tail2, *p2;
    head1 = (struct node*)malloc(sizeof (struct node));
    head2 = (struct node*)malloc(sizeof (struct node));
    tail1 = head1;
    tail2 = head2;

    scanf("%d%d", &n, &m);

    for (int i = 0; i < n; i ++)
    {
        p1 = (struct node*)malloc(sizeof (struct node));
        scanf("%d", &p1->data);
        tail1->next = p1;
        tail1 = p1;
    }

    for (int i = 0; i < m; i ++)
    {
        p2 = (struct node*)malloc(sizeof (struct node));
        scanf("%d", &p2->data);

        tail2->next = p2;
        tail2 = p2;
    }

    p1 = head1->next;
    p2 = head2->next;

    while (p1 != NULL && p2 != NULL)
    {
        if ((p1->data) < (p2->data))
        {
            printf("%d ", p1->data);
            p1 = p1->next;
        }
        else
        {
            printf("%d ", p2->data);
            p2 = p2->next;
        }
    }

    while (p1 != NULL)
    {
        if (p1->next == NULL)
            printf("%d", p1->data);
        else
            printf("%d ", p1->data);
        
        p1 = p1->next;
    }

    while (p2 != NULL)
    {
        if (p2->next == NULL)
            printf("%d", p2->data);
        else
            printf("%d ", p2->data);
        
        p2 = p2->next;
    }
    return 0;
}

数据结构实验之链表五:单链表的拆分
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int main()
{
    int n;
    struct node *head, *tail, *p, *head1, *tail1, *p1, *head2, *tail2, *p2;
    head = (struct node*)malloc(sizeof (struct node));
    head1 = (struct node*)malloc(sizeof (struct node));
    head2 = (struct node*)malloc(sizeof (struct node));
    
    tail = head;
    tail1 = head1;
    tail2 = head2;
    
    scanf("%d", &n);
    
    int sum1 = 0, sum2 = 0;  //sum1 记录奇数,sum2记录偶数
    for (int i = 0; i < n; i ++)
    {
        p = (struct node*)malloc(sizeof (struct node));
        scanf("%d", &p->data);
        
        if (p->data & 1)
            sum1 ++;
        else
            sum2 ++;
        
        tail->next = p;
        tail = p;
    }
    
    printf("%d %d\n", sum2, sum1);
    
    p = head->next;
    
    while (p)
    {
        if (!(p->data & 1))
        {
            sum2 --;
            
            if (sum2 == 0)
                printf("%d\n", p->data);
            else
                printf("%d ", p->data);
        }
        
        p = p->next;
    }
    
    p = head->next;
    
    while (p)
    {
        if (p->data & 1)
        {
            sum1 --;
            
            if (sum1 == 0)
                printf("%d\n", p->data);
            else
                printf("%d ", p->data);
        }
        
        p = p->next;
    }
    return 0;
}

数据结构实验之链表七:单链表中重复元素的删除
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int main()
{
    int n;
    struct node *head, *tail, *p;
    
    head = (struct node*)malloc(sizeof (struct node));
    head->next = NULL;
    
    scanf("%d", &n);
    
    for (int i = 0; i < n; i ++)
    {
        p = (struct node*)malloc(sizeof (struct node));
        scanf("%d", &p->data);
        
        p->next = head->next;
        head->next = p;
    }
    
    printf("%d\n", n);
    
    p = head->next;
    while (p)
    {
        if (p->next == NULL)
            printf("%d\n", p->data);
        else
            printf("%d ", p->data);
        
        p = p->next;
    }
    
    p = head->next;
    struct node *q, *g;
    while (p)
    {
        g = p;
        q = p->next;
        while (q)
        {
            if (q->data == p->data)
            {
                n --;
                g->next = q->next;
                free(q);
                q = g->next;
            }
            else
            {
                g = q;
                q = q->next;
            }
        }
        
        p = p->next;
    }
    
    printf("%d\n", n);
    
    p = head->next;
    while (p)
    {
        if (p->next == NULL)
            printf("%d\n", p->data);
        else
            printf("%d ", p->data);
        
        p = p->next;
    }
    return 0;
}

师–链表的结点插入
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int main()
{
    int n;
    while (~scanf("%d", &n))
    {
        struct node *head, *tail, *p;
        head = (struct node*)malloc(sizeof (struct node));
        tail = head;
        
        for (int i = 0; i < n; i ++)
        {
            tail = head;
            p = (struct node*)malloc(sizeof (struct node));
            int m;
            
            scanf("%d%d", &m, &p->data);
            while (m -- && tail->next)
            {
                tail = tail->next;
            }
            
            p->next = tail->next;
            tail->next = p;
        }
        
        p = head->next;
        while (p)
        {
            if (p->next == NULL)
                printf("%d\n", p->data);
            else
                printf("%d ", p->data);
            
            p = p->next;
        }
    }
    return 0;
}

约瑟夫问题
#include <stdio.h>

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

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    struct node *tail, *head, *p, *q;
    head = (struct node*)malloc(sizeof(struct node));
    
    head->next = NULL;
    tail = head;
    
    for (int i = 1; i <= n; i ++)
    {
        p = (struct node*)malloc(sizeof(struct node));
        p->data = i;
        tail->next = p;
        tail = p;
    }
    p->next = head->next;
    
    p = head->next;
    while (p->next != p)
    {
        int k = m;
        while (k > 1) {
            q = p;
            p = p->next;
        }
        
        q->next = p->next;
        free(p);
        p = q->next;
    }
    
    printf("%d", p->data);
    return 0;
}
不敢死队问题
#include <stdio.h>

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

int main()
{
    int n, m, ans = 0;
    while (~scanf("%d", &n) && n)
    {
        ans = 0;
        struct node *tail, *head, *p, *q;
        head = (struct node*)malloc(sizeof(struct node));
        head->next = NULL;
        tail = head;

        for (int i = 1; i <= n; i ++)
        {
            p = (struct node*)malloc(sizeof(struct node));
            p->data = i;
            tail->next = p;
            tail = p;
        }
        p->next = head->next;

        p = head->next;
        while (p->next != p) 
        {
            int k = 5;
            while (k -- > 1)
            {
                q = p;
                p = p->next;
            }

            if (p->data == 1) break;
            q->next = p->next;
            free(p);
            p = q->next;
            ans ++;
        }

        printf("%d\n", ans + 1);
    }
    return 0;
}
数据结构实验之链表九:双向链表
#include <stdio.h>

typedef struct node
{
    int data;
    struct node *pre, *next;
};

int main()
{
    int n,m,i;
    scanf("%d%d", &n, &m);
    struct node *head, *tail, *p;
    head = (struct node*)malloc(sizeof (struct node));
    head->next = NULL;
    tail = head;
    
    for (int a=0; a<n; a++)
    {
        p = (struct node*)malloc(sizeof (struct node));
        scanf("%d", &p->data);
        p->pre = tail;
        tail->next = p;
        tail = p;
    }
    
    while (m --)
    {
        scanf("%d", &i);
        p = (struct node*)malloc(sizeof (struct node));
        p = head->next;
        
        while (p)
        {
            if(p->data == i) break;
            p = p->next;
        }
        
        if (p->next != NULL && p->pre != head)
            printf("%d %d\n", p->pre->data, p->next->data);
        else if (p->pre!=head)
            printf("%d\n", p->pre->data);
        else
            printf("%d\n", p->next->data);
    }
    return 0;
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值