0
点赞
收藏
分享

微信扫一扫

SDUT——程序设计基础||——链表

五殳师兄 2022-03-16 阅读 22

 

目录

7-1 数据结构实验之链表一:顺序建立链表 (20 分)

7-2 数据结构实验之链表二:逆序建立链表 (20 分)

7-3 数据结构实验之链表三:链表的逆置 (20 分)

7-4 数据结构实验之链表四:有序链表的归并 (20 分)

7-5 数据结构实验之链表五:单链表的拆分 (20 分)

7-6 数据结构实验之链表七:单链表中重复元素的删除 (20 分)

7-7 师--链表的结点插入 (20 分)

7-8 约瑟夫问题 (20 分)

7-9 数据结构实验之链表九:双向链表 (20 分)

7-10 不敢死队问题 (20 分)


7-1 数据结构实验之链表一:顺序建立链表 (20 分)

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

node *creat(int n)
{
    node *head, *tail, *p;
    head = (node*)malloc(sizeof(node));
    head -> next = NULL;
    tail = head;
    for(int i = 0; i < n; i ++)
    {
        p = (node*)malloc(sizeof(node));
        scanf("%d", &p -> data);
        p -> next = NULL;
        tail -> next = p;
        tail = p;
    }
    return head;
}

int main()
{
    int n;
    scanf("%d", &n);
    node *head, *p;
    head = creat(n);
    p = head -> next;
    while(p)
    {
        if(p -> next == NULL)
            printf("%d", p -> data);
        else printf("%d ", p -> data);
        p = p -> next;
    }
    return 0;
}

7-2 数据结构实验之链表二:逆序建立链表 (20 分)

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node *Creat(int n)
{
    struct node *head, *tail;
    head = (struct node*)malloc(sizeof(struct node));
    head -> next = NULL;
    int i;
    for(i = 0; i < n; i ++)
    {
        tail = (struct node*)malloc(sizeof(struct node));
        scanf("%d", &tail -> data);
        tail -> next = head -> next;
        head -> next = tail;
    }
    return head;
}
int main()
{
    int n;
    scanf("%d", &n);
    int i;
    struct node *head = Creat(n);
    struct node *p;
    p = head -> next;
    while(p)
    {
        if(p -> next == NULL) printf("%d", p -> data);
        else printf("%d ", p -> data);
        p = p -> next;
    }
    return 0;
}

7-3 数据结构实验之链表三:链表的逆置 (20 分)

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
int main()
{
    int x;
    struct node *head, *tail;
    head = (struct node *)malloc(sizeof(struct node));
    head->next = NULL;
    while(~scanf("%d", &x) && x != -1)
    {
        tail = (struct node *)malloc(sizeof(struct node));
        tail -> data = x;
        tail -> next = head -> next;
        head -> next = tail;
    }
    struct node *y;
    y = head -> next;
    int flag = 0;
    while(y)
    {
        if(flag == 0)
        {
            printf("%d", y -> data);
            flag = 1;
        }
        else printf(" %d", y -> data);
        y = y -> next;
    }
    return 0;
}

7-4 数据结构实验之链表四:有序链表的归并 (20 分)

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
int main()
{
    int n, m;
    scanf("%d %d", &m, &n);
    struct node *head1, *tail1, *p1;
    head1 = (struct node*)malloc(sizeof(struct node));
    tail1 = (struct node *)malloc(sizeof(struct node));
    head1 -> next = NULL;
    tail1 = head1;
    int i;
    for(i = 0; i < m; i ++)
    {
        p1 = (struct node*)malloc(sizeof(struct node));
        scanf("%d", &p1 -> data);
        tail1 -> next = p1;
        p1->next = NULL;
        tail1 = p1;
    }
    struct node *head2, *tail2, *p2;
    head2 = (struct node *)malloc(sizeof(struct node));
    tail2 = (struct node *)malloc(sizeof(struct node));
    head2->next = NULL;
    tail2 = head2;
    for (i = 0; i < n; i++) 
    {
        p2 = (struct node *)malloc(sizeof(struct node));
        scanf("%d", &p2->data);
        tail2->next = p2;
        p2->next = NULL;
        tail2 = p2;
    }
    struct node *a, *b, *c;
    a = head1;
    b = head1 -> next;
    c = head2 -> next;
    free(head2);
    while(b && c)
    {
        if(b -> data > c -> data)
        {
            a -> next = c;
            a = c;
            c = c -> next;
        }
        else
        {
            a -> next = b;
            a = b;
            b = b -> next;
        }
    }
    if(b) a -> next = b;
    if(c) a -> next = c;
    struct node *x;
    x = head1 -> next;
    int flag = 0;
    while(x)
    {
        if(flag == 0)
        {
            printf("%d", x->data);
            flag = 1;
        }
        else printf(" %d", x -> data);
        x = x -> next;
    }
    printf("\n");
    return 0;
}

7-5 数据结构实验之链表五:单链表的拆分 (20 分)

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
int main()
{
    struct node *head, *tail, *p;
    head = (struct node*)malloc(sizeof(struct node));
    tail = (struct node*)malloc(sizeof(struct node));
    head -> next = NULL;
    tail = head;
    struct node *head1, *tail1, *p1;
    head1 = (struct node*)malloc(sizeof(struct node));
    tail1 = (struct node*)malloc(sizeof(struct node));
    head1 -> next = NULL;
    tail1 = head1;
    int n;
    scanf("%d", &n);
    int i;
    int x, cnt1 = 0, cnt2 = 0;
    for(i = 0; i < n; i ++)
    {
        scanf("%d", &x);
        if(x%2 == 0)
        {
            cnt1 ++;
            p = (struct node*)malloc(sizeof(struct node));
            p -> data = x;
            tail -> next = p;
            p -> next = NULL;
            tail = p;
        }
        else
        {
            cnt2 ++;
            p1 = (struct node*)malloc(sizeof(struct node));
            p1 -> data = x;
            tail1 -> next = p1;
            p1 -> next = NULL;
            tail1 = p1;
        }
    }
    struct node *a = head -> next;
    struct node *b = head1 -> next;
    printf("%d %d\n", cnt1, cnt2);
    int flag = 0;
    while(a)
    {
        if(flag == 0)
        {
            printf("%d", a -> data);
            flag = 1;
        }
        else printf(" %d", a -> data);
        a = a -> next;
    }
    printf("\n");
    flag = 0;
    while(b)
    {
        if(flag == 0)
        {
            printf("%d", b -> data);
            flag = 1;
        }
        else printf(" %d", b -> data);
        b = b -> next;
    }
    printf("\n");
    return 0;
}

7-6 数据结构实验之链表七:单链表中重复元素的删除 (20 分)

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
int main()
{
    struct node *head, *p;
    head = (struct node*)malloc(sizeof(struct node));
    head -> next = NULL;
    int n;
    scanf("%d", &n);
    int i;
    for(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);
    struct node *q;
    q = head -> next;
    int flag = 0;
    while(q)
    {
        if(flag == 0)
        {
            printf("%d", q -> data);
            flag = 1;
        }
        else printf(" %d", q -> data);
        q = q -> next;
    }
    printf("\n");
    struct node *pre, *u, *v;
    pre = (struct node*)malloc(sizeof(struct node));
    u = (struct node*)malloc(sizeof(struct node));
    v = (struct node *)malloc(sizeof(struct node));
    u = head;
    while(u)
    {
        pre = u;
        v = u -> next;
        while(v)
        {
            if(v -> data == u -> data)
            {
                n --;
                pre -> next = v -> next;
                free(v);
                v = pre -> next;
            }
            else 
            {
                pre = v;
                v = v -> next;
            }
        }
        u = u -> next;
    }
    printf("%d\n", n);
    struct node *x;
    x = head -> next;
    flag = 0;
    while(x)
    {
        if(flag == 0)
        {
            printf("%d", x -> data);
            flag = 1;
        }
        else printf(" %d", x -> data);
        x = x -> next;
    }
    printf("\n");
    return 0;
}

7-7 师--链表的结点插入 (20 分)

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
int main()
{
    int n, a, b;
    int i, j;
    while(~scanf("%d", &n))
    {
        struct node *head, *tail, *p;
        head = (struct node *)malloc(sizeof(struct node));
        tail = (struct node *)malloc(sizeof(struct node));
        head->next = NULL;
        tail = head;
        for (i = 0; i < n; i++) 
        {
            scanf("%d %d", &a, &b);
            tail = head;
            for (j = 0; j < a; j++) 
            {
                if (tail->next != NULL) 
                {
                    tail = tail->next;
                }
            }
            p = (struct node *)malloc(sizeof(struct node));
            p->data = b;
            p->next = tail->next;
            tail->next = p;
        }
        struct node *q;
        q = head->next;
        int flag = 0;
        while (q) 
        {
            if (flag == 0) 
            {
                printf("%d", q->data);
                flag = 1;
            } 
            else
                printf(" %d", q->data);
            q = q->next;
        }
        printf("\n");
    }
    return 0;
}

7-8 约瑟夫问题 (20 分)

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node *creat(int n)
{
    struct node *head, *tail, *p;
    head = (struct node*)malloc(sizeof(struct node));
    tail = (struct node*)malloc(sizeof(struct node));
    p = (struct node*)malloc(sizeof(struct node));
    p -> data = 1;
    p -> next = NULL;
    tail = p;
    head = p;
    int i;
    for(i = 2; i <= n; i ++)
    {
        p = (struct node*)malloc(sizeof(struct node));
        p -> data = i;
        p -> next = NULL;
        tail -> next = p;
        tail = p;
    }
    tail -> next = head;
    return head;
}
int del(struct node *head, int n, int m)
{
    int cnt = 0, s = 0;
    struct node *tail, *p;
    tail = head;
    while(tail -> next != head)
    {
        tail = tail -> next;
    }
    while(cnt < n - 1)
    {
        p = tail -> next;
        s ++;
        if(s == m)
        {
            s = 0;
            tail -> next = p -> next;
            free(p);
            p = tail -> next;
            cnt ++;
        }
        else tail = p;
    }
    return tail -> data;
}
int main()
{
    struct node *head;
    int n, m;
    scanf("%d %d", &n, &m);
    head = creat(n);
    printf("%d\n", del(head, n, m));
    return 0;
}

7-9 数据结构实验之链表九:双向链表 (20 分)

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
    struct node *pre;
};
int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    struct node *head, *tail, *p;
    head = (struct node*)malloc(sizeof(struct node));
    tail = (struct node*)malloc(sizeof(struct node));
    head -> next = NULL;
    tail = head;
    int i, x;
    for(i = 0; i < n; i ++)
    {
        p = (struct node*)malloc(sizeof(struct node));
        scanf("%d", &p -> data);
        tail -> next = p;
        p -> pre = tail;
        p -> next = NULL;
        tail = p;
    }
    for(i = 0; i < m; i ++)
    {
        scanf("%d", &x);
        struct node *q;
        q = (struct node*)malloc(sizeof(struct node));
        q = head -> next;
        while(q)
        {
            if(q -> data == x && q -> next != NULL && q -> pre != head)
            {
                printf("%d %d\n", q -> pre -> data, q -> next -> data);
            }
            else if(q -> data == x && q -> next != NULL && q -> pre == head)
            {
                printf("%d\n", q -> next -> data);
            }
            else if(q -> data == x && q -> next == NULL && q -> pre != head)
            {
                printf("%d\n", q -> pre -> data);
            } 
            q = q -> next;
        }
    }
    return 0;
}

7-10 不敢死队问题 (20 分)

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node *creat(int n)
{
    struct node *head, *tail, *p;
    head = (struct node*)malloc(sizeof(struct node));
    tail = (struct node*)malloc(sizeof(struct node));
    p = (struct node*)malloc(sizeof(struct node));
    p -> data = 1;
    p -> next = NULL;
    tail = p;
    head = p;
    int i;
    for(i = 2; i <= n; i ++)
    {
        p = (struct node*)malloc(sizeof(struct node));
        p -> data = i;
        p -> next = NULL;
        tail -> next = p;
        tail = p;
    }
    tail -> next = head;
    return head;
}
int del(struct node *head, int n)
{
    int cnt = 0, s = 0;
    struct node *tail, *p;
    tail = head;
    while(tail -> next != head)
    {
        tail = tail -> next;
    }
    while(cnt < n)
    {
        p = tail -> next;
        s ++;
        if(s == 5)
        {
            if(p -> data == 1)
            {
                cnt ++;
                break;
            }
            else 
            {
                s = 0;
                tail -> next = p -> next;
                free(p);
                p = tail -> next;
                cnt ++;
            }
        }
        else tail = p;
    }
    return cnt;
}
int main()
{
    struct node *head;
    int n;
    while(~scanf("%d", &n) && n)
    {
        head = creat(n);
        printf("%d\n", del(head, n));
    }
    return 0;
}
举报

相关推荐

0 条评论