SDUT数据结构pta-实验二 链表

7-1 两个有序链表序列的合并

#include<bits/stdc++.h>
using namespace std;
struct node{
    int data;
     node *next;
};
int main()
{
    int r;
    node *h1=new node;
    node *h2=new node;
    h1->next=NULL;
    h2->next=NULL;
    node *p1,*p2,*t1,*t2;
    t1=h1;
    t2=h2;
    while(true)
    {
        cin >>r;
        if(r==-1)break;
        p1=new node;
        p1->data=r;
        p1->next=NULL;
        t1->next=p1;
        t1=p1;
    }
    while(true)
    {
        cin >>r;
        if(r==-1)break;
        p2=new node;
        p2->data=r;
        p2->next=NULL;
        t2->next=p2;
        t2=p2;
    }
    node *h=new node;
    h->next=NULL;
    node *p,*t;
    t=h;
    p1=h1->next;
    p2=h2->next;
    while(p1!=NULL && p2!=NULL)
    {
        if(p1->data <= p2->data){
            p=new node;
            p->data=p1->data;
            p->next=NULL;
            t->next=p;
            t=p;
            p1=p1->next;
        }
        else {
            p=new node;
            p->data=p2->data;
            p->next=NULL;
            t->next=p;
            t=p;
            p2=p2->next;
        }
    }
    while(p1!=NULL)
    {
        p=new node;
        p->data=p1->data;
        p->next=NULL;
        t->next=p;
        t=p;
        p1=p1->next;
    }
    while(p2!=NULL)
    {
        p=new node;
        p->data=p2->data;
        p->next=NULL;
        t->next=p;
        t=p;
        p2=p2->next;
    }
    p=h->next;
    if(p==NULL){
    printf("NULL");
                return 0;}
    while(p!=NULL)
    {
        if(p->next==NULL)cout <<p->data;
        else cout <<p->data <<" ";
        p = p->next;
    }
    
}

7-2 两个有序链表序列的交集

#include<bits/stdc++.h>
using namespace std;
struct node{
    int data;
    node *next;
};
int main()
{
    int i,n,cnt=0;
    node *h1=new node;
    node *h2=new node;
    h1->next=NULL;
    h2->next=NULL;
    node *p1,*p2,*t1,*t2;
    t1=h1;
    t2=h2;
    while(true)
    {
        cin >> n;
        if(n==-1)break;
        p1=new node;
        p1->data=n;
        p1->next=NULL;
        t1->next=p1;
        t1=p1;
    }
    while(true)
    {
        cin >> n;
        if(n==-1)break;
        p2=new node;
        p2->data=n;
        p2->next=NULL;
        t2->next=p2;
        t2=p2;
    }
    p1=h1->next;
    p2=h2->next;
    while(p1!=NULL&&p2!=NULL)
    {
        if(p1->data==p2->data){
            if(cnt==0)cout <<p1->data;
            else cout <<" "<<p1->data;
            p2=p2->next;
            p1=p1->next;
            cnt++;
        }
        else {
            if(p1->data < p2->data) {
                p1 = p1->next;
            } else {
                p2 = p2->next;
            }
        }
    }
    if(cnt==0)cout <<"NULL";
}

7-3 重排链表

#include <stdio.h>
struct node
{
    int data,next;
}a[100001];
int start,n;
int paixu[100001];
int chongpai[100001];
int main()
{
    scanf("%d %d",&start,&n);
    for(int i=0;i<n;i++){
        int p,q,w;
        scanf("%d %d %d",&p,&q,&w);
        a[p].data=q;
        a[p].next=w;
    }
    int len=0;
    for(int i=start;i>=0;i=a[i].next){
        paixu[len++]=i;
    }
    int left=0,right=len-1;
    int t=0;
    while(left<=right){
        if(left<right){
            chongpai[t++]=paixu[right];
            right--;
            chongpai[t++]=paixu[left];
            left++;
        }
        else{
            chongpai[t++]=paixu[left];
            left++;
        }
    }
    for(int i=0;i<t;i++){
        if(i!=t-1){
            printf("%.5d %d %.5d\n",chongpai[i],a[chongpai[i]].data,chongpai[i+1]);
        }
        else printf("%.5d %d -1\n",chongpai[i],a[chongpai[i]].data);
    }
    return 0;
}

7-4 约瑟夫环

#include <stdio.h>
#include <stdlib.h>
struct node {
    int data;
    struct node *next;
};
int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    struct node *h = NULL, *t = NULL, *p = NULL;
    // 创建链表
    h=(struct node*)malloc(sizeof(struct node*));
    h->data=1;
    t=h;
    for (int i = 2; i <= n; i++) {
        p = (struct node*)malloc(sizeof(struct node));
        p->data = i;
        p->next = NULL;
            t->next = p; // 连接新的节点
            t = p; // 移动到新的最后节点
    }
    t->next = h; // 形成循环链表
    struct node *q = NULL;
    t = h;
    // 使用循环来执行删除操作
    while (n > 0) {
        for (int count = 1; count < m; count++) {
            q = t; // 更新前一个节点
            t = t->next; // 移动到下一个节点
        }
        // 输出被删除节点的数据
        printf("%d", t->data);
        n--; // 减少剩余节点计数
        if (n > 0) {
            printf(" "); // 输出空格分隔
        }
        // 处理删除逻辑
        if (q == NULL) {
            // 删除头节点
            h = t->next;
        } else {
            // 删除中间或尾节点
            q->next = t->next;
        }
        struct node *to = t;
        t = t->next; // 先保存下一个节点
        free(to); // 释放当前节点
    }
    return 0;
}

单链表的创建及遍历

#include<bits/stdc++.h>
using namespace std;
struct node {
    int data;
    node *next;
};

int main()
{
    int n,i;
    node *h = new node;
    h->next = NULL;
    node *p, *t;
    t = h;
    cin >> n;
    for(i = 0; i < n; i++) {
        p = new node;
        cin >> p->data; // 修正输入操作符
        p->next = NULL;
        t->next = p;
        t = p;
    }
    p = h->next;
    for(i = 0; i < n; i++) {
        if(i == 0) {
            cout << p->data;
        } else {
            cout << " " << p->data;
        }
        p = p->next;
    }
    return 0;
}

7-6 链表去重

#include <stdio.h>
struct node
{
    int key,next;
}a[100001];
int start;
int diyige[100001];
int dierge[100001];
int repeat[100001];
int main()
{
    int n;
    scanf("%d %d",&start,&n);
    for(int i=0;i<n;i++){
        int p,q,w;
        scanf("%d %d %d",&p,&q,&w);
        a[p].key=q;
        a[p].next=w;
    }
    int len1=0,len2=0;
    for(int i=start;i>-1;i=a[i].next){
        int key=abs(a[i].key);
        if(repeat[key]==0){
            repeat[key]=1;
            diyige[len1++]=i;
        }
        else{
            dierge[len2++]=i;
        }
    }
    for(int i=0;i<len1;i++){
        printf("%.5d %d ",diyige[i],a[diyige[i]].key);
        if(i==len1-1)
            printf("-1\n");
        else
            printf("%.5d\n",diyige[i+1]);
    }
    for(int i=0;i<len2;i++){
        printf("%.5d %d ",dierge[i],a[dierge[i]].key);
        if(i==len2-1)
            printf("-1\n");
        else
            printf("%.5d\n",dierge[i+1]);
    }
    return 0;
}

7-7单链表就地逆置

#include<stdio.h>
#include<stdlib.h>
typedef struct
{
    int data;
    struct node *next;
}Lnode,*LinkList;
LinkList createLinkedList()
{
    LinkList head=(LinkList)malloc(sizeof(Lnode));
    head->next=NULL;
    LinkList r=head;
    while(1){
        int value;
        scanf("%d",&value);
        if(value==-1){break;}
        LinkList p=(LinkList)malloc(sizeof(Lnode));
        p->data=value;
        p->next=NULL;
        r->next=p;
        r=p;
    }
    return head;
}
void nizhi(LinkList head)
{
    LinkList p,q;
    p=head->next;
    head->next=NULL;
    while(p){
        q=p;
        p=p->next;
        q->next=head->next;
        head->next=q;
    }
}
void Print(LinkList head)
{
    LinkList p=head->next;
    while(p){
        if(p->next!=NULL){
            printf("%d ",p->data);
            p=p->next;
        }
        else {
            printf("%d",p->data);
            p=p->next;
        }
    }
    printf("\n");
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int i=0;i<T;i++)
    {
        LinkList L=NULL;
        L=createLinkedList();
        nizhi(L);
        Print(L);
    }
    return 0;
}

7-8 带头节点的双向循环链表操作

#include<bits/stdc++.h>
using namespace std;
struct node{
    int data;
    node * next;
    node *pre;
};
void puf(node *h, node *p)
{
    if (h->next == NULL) {
        h->next = p;
        p->next = h;
        p->pre = h;
    } else {
        p->next = h->next;
        h->next->pre = p;
        h->next = p;
        p->pre = h;
    }
}
void pub(node *h, node *p)
{
    if (h->next == NULL) {
        h->next = p;
        p->next = h;
        p->pre = h;
    } else {
        node *t = h->next;
        while (t->next != h) {
            t = t->next;
        }
        t->next = p;
        p->pre = t;
        p->next = h;
        h->pre = p;
    }
}
void shun(node *h)
{
    node *p = h->next;
    if (p != h) { // 添加判断条件,避免只有一个节点时的输出问题
        printf("%d", p->data);
        p = p->next;
    }
    while (p != h) {
        printf(" %d", p->data);
        p = p->next;
    }
    printf("\n");
}
void ni(node *h)
{
    node *p = h->pre;
    if (p != h) { // 添加判断条件,避免只有一个节点时的输出问题
        cout << p->data;
        p = p->pre;
    }
    while (p != h) {
        printf(" %d", p->data);
        p = p->pre;
    }
}
int main()
{
    int n, i = 0;
    node *h = new node;
    h->next = h->pre = h;
    while (~scanf("%d", &n) && n != -1)
    {
        node *p = new node;
        p->data = n;
        if (i % 2 == 0) {
            puf(h, p);
        } else {
            pub(h, p);
        }
        i++;
    }
    shun(h);
    ni(h);
    return 0;
}

7-9 头插法创建单链表、遍历链表、删除链表

#include<bits/stdc++.h>
using namespace std;
struct node{
    int data;
     node * next;
};
void deletelist(node *h) {
    node *t;
    while (h != NULL) {
        t = h;
        h = h->next;
        delete t;
    }
}
int main()
{
    int k;
    scanf("%d",&k);
    while(k)
    {
        int n,i,j;
        node * h=new node;
        h->next=NULL;
        node *p;
        while(scanf("%d",&n)!=EOF&&n!=-1)
        {
            p=new node;
            p->data=n;
            p->next=h->next;
            h->next=p;
        }
        p=h->next;
        while(p!=NULL)
        {
            cout <<p->data<<" ";
            p=p->next;
        }
        cout << endl;
        k--;
        deletelist(h);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值