SDUT 链表专题

来源:互联网 发布:禅道部署到linux 编辑:程序博客网 时间:2024/05/18 11:46
#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;}

原创粉丝点击