SDUT 链表专题

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

///顺序建表
//node *creat(int n)
//{
//    node *head, *p;
//    head = new node;
//    head->next = NULL;
//    for(int i = 0; i < n; i++)
//    {
//        p = new node;
//        cin >> p->data;
//        p->next = head->next;
//        head->next = p;
//    }
//    return head;
//}

///逆序建表
node *creat(int n)
{
    node *head, *p;
    head = new node;
    head->next = NULL;
    for(int i = 0; i < n; i++)
    {
        p = new node;
        cin >> p->data;
        p->next = head->next;
        head->next = p;
    }
    return head;
}

///逆置
//node *Rev(node *head)
//{
//    node *p, *q;
//    p = head->next;
//    q = p->next;
//    head->next = NULL;
//    while(p)
//    {
//        p->next = head->next;
//        head->next = p;
//        p = q;
//        if(q)
//            q = q->next;
//    }
//    return head;
//}

///合并
//node *Merge(node *head1, node *head2)
//{
//    node *p, *q, *tail;
//    tail = head1;
//    p = head1->next;
//    q = head2->next;
//    free(head2);
//    while(p && q)
//    {
//        if(p->data < q->data)
//        {
//            tail->next = p;
//            tail = p;
//            p = p->next;
//        }
//        else
//        {
//            tail->next = q;
//            tail = q;
//            q = q->next;
//        }
//    }
//    if(q)
//        tail->next = q;
//    if(p)
//        tail->next = p;
//    return head1;
//}

///输出
void show(node *head)
{
    node *p;
    p = head->next;
    while(p)
    {
        if(p->next)
            printf("%d ", p->data);
        else
            printf("%d\n", p->data);
        p = p->next;
    }
}
///拆分
//void di(node *head)
//{
//    int n, m;
//    n = m = 0;
//    node *head1, *head2, *tail1, *tail2, *p;
//    head1 = new node;
//    head2 = new node;
//    head1->next = NULL;
//    head2->next = NULL;
//    tail1 = head1;
//    tail2 = head2;
//    p = head->next;
//    while(p)
//    {
//        if(p->data % 2 == 0)
//        {
//            n++;
//            tail1->next = p;
//            tail1 = p;
//            p = p->next;
//        }
//        else
//        {
//            m++;
//            tail2->next = p;
//            tail2 = p;
//            p = p->next;
//        }
//    }
//    tail1->next = NULL;
//    tail2->next = NULL;
//    printf("%d %d\n", n, m);
//    show(head1);
//    show(head2);
//}

///有序链表建立
//node *creat(int n)
//{
//    int x;
//    node *head, *p, *q, *nod;
//    head = new node;
//    head->next = NULL;
//    for(int i = 0; i < n; i++)
//    {
//        cin >> x;
//        p = head;
//        q = p->next;
//        while(q && q->data < x)
//        {
//            p = p->next;
//            q = q->next;
//        }
//        nod = new node;
//        nod->data = x;
//        p->next = nod;
//        nod->next = q;
//    }
//    return head;
//}

///删重
int Del(node *head)
{
    node *p, *q, *r;
    int n = 0;
    p = head->next;
    while(p)
    {
        if(p->next)///老毛病了,搞什么鬼!!
        q = p;
        r = q->next;
        while(r)
        {
            if(r->data == p->data)
            {
                q->next = r->next;
                r = r->next;
                n++;
            }
            else
            {
                q = q->next;
                r = r->next;
            }
        }
        p = p->next;
    }
    return n;
}


int main()
{
    ios::sync_with_stdio(false);
    node *head;
    int n;
    cin >> n;
    head = creat(n);
    printf("%d\n", n);
    show(head);
    printf("%d\n", n - Del(head));
    show(head);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值