zoukankan      html  css  js  c++  java
  • 数据结构实验之链表七:单链表中重复元素的删除(SDUT 2122)

    #include <bits/stdc++.h>
    using namespace std;
    typedef struct node
    {
        int data;
        struct node* next;
    } Node;
    Node* Createlist(int n)
    {
        Node* head,*p;
        int i,d;
        head=(Node *)malloc(sizeof(Node));
        head->next=NULL;
        for(i=1; i<=n; i++)
        {
            p=(Node*)malloc(sizeof(Node));
            scanf("%d",&d);
            p -> data = d;
            p -> next = head -> next;
            head -> next = p;
        }
        return head;
    };
    int main()
    {
        Node* head,*p,*t,*q;  
        int n;
        scanf("%d",&n);
        head = Createlist(n); 
        q = head -> next;  
        printf("%d
    ",n);
        for(p = head -> next; p !=NULL ; p = p->next) 
            if(p == head -> next)
                printf("%d", p -> data);
            else
                printf(" %d",p -> data);
        printf("
    ");
        while(q)
        {
            t = q;  
            p = q -> next;
            while(p)
            {
                if(q -> data == p -> data) 
                {
                    t -> next = p -> next;
                    n --;
                    p = p -> next;
                }
                else
                {
                    t = t -> next;
                    p = p -> next;
                }
            }
            q = q -> next;
        }
        printf("%d
    ",n);
        for(p = head -> next; p != NULL; p = p -> next)
        {
            if(p == head -> next)
                printf("%d",p -> data);
            else
                printf(" %d",p -> data);
        }
        return 0;
    }
    
    
    
  • 相关阅读:
    [蓝桥杯2017初赛]青蛙跳杯子 BFS
    第十一章 进程和信号
    第七章 数据管理
    特殊符号大全
    第四章 Linux环境
    (十六)异常
    (十五)代理
    (十四)内部类
    第三章 文件操作
    (十三)对象克隆
  • 原文地址:https://www.cnblogs.com/lcchy/p/10139495.html
Copyright © 2011-2022 走看看