c++チェーンテーブル重複データの削除

20037 ワード

//List.h
#include <iostream>

typedef int dataType;



struct Node{

    Node():data(0),pNextNode(NULL){} //      

    dataType data;

    Node* pNextNode;

};

class List{

private:

    Node *head; //          

    int size;   //    

public:

    List(){head=new Node;size=0;}

    bool isEmpty();                //      

    bool InsertList(int i,dataType elem);  //i    1  ,    0  

    void PushList(dataType elem);          //         

    bool DeleteList(int i);        //         

    void ClearList();            //      

    void DeleteRepetitiveData();//      

    void PrintList();            //       

    int GetSize();    

    Node* Fine(int i);            //   i            

    Node* FinePre(int i);        //   i       ,    

};

 
//List.cpp
#include "List.h"

#include <iostream>

#include <vector>

using namespace std;

//    

bool List::isEmpty(){

    if(head==NULL)

        return false;

    else

        return true;

}

//  i     

bool List::InsertList(int i,dataType elem){

    if (i<1)

        return false;

    else if(head==NULL||i==1)//     

    {

        head->data=elem;

        size++;

        return true;

    }

    else if (i>size)        //

    {

        PushList(elem);

        return true;

    }

    else

    {

        Node *pre=Fine(i-1);

        Node *follow=Fine(i);

        Node *s=new Node;        //        

        pre->pNextNode=s;        //     

        s->pNextNode=follow;    //     

        s->data=elem;

        size++;

        return true;

    }

}

//       

void List::PushList(dataType elem){

    if(head==NULL)

    {

        head=new Node;

        head->data=elem;

        size++;

    }

    else

    {

        Node *cur=head;

        while(cur->pNextNode)

            cur=cur->pNextNode;

        Node *s=new Node;

        cur->pNextNode=s;

        s->data=elem;

        size++;

    }

}

//    

void List::PrintList(){

    Node *cur=head;

    while (cur!=NULL)

    {

        cout<<cur->data<<"  ";

        cur=cur->pNextNode;

    }

    cout<<endl;

}

//size

int List::GetSize(){

    return size;

}

//   i   

Node* List::Fine(int i){

    if(i==1)

        return head;

    else

    {

        Node *cur=head;

        for (int pos=1;pos<i;pos++)

            cur=cur->pNextNode;

        return cur;

    }    

}

//  i       

Node* List::FinePre(int i){

    if(i<2)

    {

        cout<<"        2!"<<endl;

        return NULL;

    }

    else if(i==2)

        return head;

    else

        return Fine(i-1);

}

//   i   

bool List::DeleteList(int i){

    if (i<1||i>size)        //  i   

        return false;

    else if(i==1)

    {

        Node *temp=head;

        head=head->pNextNode;

        delete temp;

        size--;

        return true;

    }

    else

    {

        Node *cur=this->Fine(i);

        Node *pre=this->FinePre(i);

        pre->pNextNode=cur->pNextNode; //      

        delete cur;

        size--;

        return true;

    }

}

//      

void List::ClearList(){

    Node *temp=head;

    while (head!=NULL)

    {

        temp=head;

        head=head->pNextNode;

        delete temp;

    }

    size=0;

}

//      

void List::DeleteRepetitiveData(){

    int flag=0;        //      ,0    

    if(size==1)        //         

        return;

    Node *stand=head;//                       

    Node *cursor;    //  

    vector<int>storge;

    for (int i=0;i<size;i++)

    {            // for                

                //       ,         O(n^2)

        cursor=stand->pNextNode;

        flag=0;

        while(cursor!=NULL)

        {

            if(stand->data==cursor->data)

            {

                stand=stand->pNextNode;

                flag=1;

                //               

                storge.insert(storge.begin(),i+1);

                break;

            }

            else

                cursor=cursor->pNextNode;

        }    

        if(!flag)

            stand=stand->pNextNode;

    }

    int itemp=storge.size();

    while (itemp--)        

    {

        this->DeleteList(storge.at(0));//

        storge.erase(storge.begin());

    }

    storge.clear();    //  vector  

}

 
//main.cpp
#include "List.h"

#include <iostream>

using namespace std;



void main(){

    //List     

    List list;

    list.InsertList(1,1);

    list.InsertList(5,3);

    list.InsertList(2,2);

    list.InsertList(4,4);    

    list.PrintList();        //   4   

    cout<<""<<list.GetSize()<<endl;

    cout<<"     2   "<<endl;

    list.DeleteList(2);        

    list.PrintList();        

    cout<<"      "<<endl;

    list.ClearList();        //    

    cout<<""<<endl;

    list.PrintList();        

    

    list.PushList(4);

    list.PushList(1);

    list.PushList(7);

    list.PushList(1);

    list.PushList(1);

    list.PushList(2);

    list.PushList(2);        //  4 1 7 1 1 2 2

    cout<<""<<endl;    

    list.PrintList();

    cout<<""<<endl;    

    list.DeleteRepetitiveData();

    list.PrintList();

}



/*

  :

1.       Fine() FindPre(),       

2.                   。     

          ,          ,

          ,        。
 
void List::DeleteRepetitiveData()
  , vector
*/