1. 程式人生 > >資料結構之連結串列操作(c++實現)

資料結構之連結串列操作(c++實現)

1、單向連結串列(頭結點不含資料,不佔長度),C++實現:

#include <iostream>
#include <stack>
using namespace std;

/*****定義節點****/
typedef struct node{
    int value;
    struct node *next;
}NODE;

class LinkList{
private:
    NODE *Head;
public:
    LinkList();
    ~LinkList();
    void creatlist(NODE *Head,int n);
    bool bianli(NODE *Head);
    bool bianli_convert(NODE *Head);
    bool getlenth(NODE *Head);
    void Insert(NODE *Head,int n,int value);
    void InsertHead(NODE *Head,int value);
    bool Delete(NODE *Head,int n);
    NODE* convert(NODE *Head);                 //翻轉連結串列
};

/*****初始化頭節點******/
LinkList::LinkList(){

}

/*******建立大小為n的單向連結串列******/
void LinkList::creatlist(NODE *Head,int n){
    NODE *p = Head;
    NODE *tmp;
    cout<<"輸入"<<n<<"個連結串列的vaule"<<endl;
    for(int i=1;i<=n;i++){
        tmp = new NODE;
        cin>>tmp->value;
        p->next = tmp;
        tmp->next = NULL;
        p = tmp;
    }
}

/*******遍歷連結串列*******/
bool LinkList::bianli(NODE *Head){
    NODE *p = Head;
    if(p==NULL||p->next==NULL)
        return false;
    cout<<"遍歷:";
    while(p->next!=NULL){
        p = p->next;
        cout<<p->value<<" ";
    }
    cout<<endl;
    return true;
}

/*******反向遍歷連結串列(stack實現)*******/
bool LinkList::bianli_convert(NODE *Head){
    NODE *p = Head;
    if(p==NULL||p->next==NULL)
        return false;
    cout<<"反向遍歷:";
    stack<NODE*>nodestack;
    while(p->next!=NULL){
        p = p->next;
        nodestack.push(p);
    }
    while(!nodestack.empty()){
        p = nodestack.top();
        cout<<p->value<<" ";
        nodestack.pop();
    }
    cout<<endl;
    return true;
}

/*******反轉連結串列*******/
NODE* LinkList::convert(NODE *Head){
    NODE *p = Head->next;
    NODE *pre = NULL;
    NODE *next = NULL;
    while(p->next!=NULL){
        next = p->next;
        p->next = pre;
        pre = p;
        p = next;
    }
    p->next = pre;
    NODE* p1 = new NODE;
    p1->next = p;
    return p1;
}

/*********獲取單向連結串列長度*********/
bool LinkList::getlenth(NODE *Head){
    NODE *p = Head;
    if(p==NULL||p->next==NULL)
        return false;
    int cnt = 0;
    while(p->next!=NULL){
        cnt++;
        p = p->next;
    }
    cout<<"連結串列長度為:"<<cnt<<endl;
    return true;
}

/******在第n個節點後加入新節點,其值為value******/
void LinkList::Insert(NODE *Head,int n,int value){
    NODE *p = Head;
    while(n!=0){
        p = p->next;
        n--;
    }
    NODE *Tmp = new NODE;
    Tmp->value = value;
    Tmp->next = p->next;   //先後順序不能亂
    p->next =Tmp;
}

/**********頭部插入節點***********/
void LinkList::InsertHead(NODE *Head,int value){
    NODE *p = Head;
    NODE *Tmp = new NODE;
    Tmp->value = value;
    Tmp->next = p->next;
    p->next = Tmp;
}

/*******刪除第n個節點********/
bool LinkList::Delete(NODE *Head,int n){
    NODE *p = Head;
    if(p==NULL||p->next==NULL)
        return false;
    while(n>1){
        p = p->next;
        n--;
    }
    p->next = p->next->next;
    return true;
}

/******解構函式*******/
LinkList::~LinkList(){
    delete Head;
}


int main()
{
    int n;
    LinkList L1;
    NODE *node1 = new NODE;
    cout<<"輸入建立連結串列的個數"<<endl;
    cin>>n;
    L1.creatlist(node1,n);
    L1.Insert(node1,4,66);
    L1.InsertHead(node1,88);
    L1.Delete(node1,2);
    L1.bianli(node1);
    L1.bianli_convert(node1);
    L1.getlenth(node1);

    NODE *node2 = new NODE;
    node2  = L1.convert(node1);
    L1.getlenth(node2);
    L1.bianli(node2);
    return 0;
}

測試:

 2、雙向迴圈列表程式碼:

#include <iostream>
#include <stack>
using namespace std;

/*****定義節點****/
typedef struct node{
    int value;
    struct node *next;
    struct node *pre;
}NODE;

class LinkList{
private:
    NODE *Head;
public:
    LinkList();
    ~LinkList();
    void creatlist(NODE *Head,int n);
    bool bianli(NODE *Head);
    bool getlenth(NODE *Head);
    void Insert(NODE *Head,int n,int value);
    bool Delete(NODE *Head,int n);
};

/*****初始化頭節點******/
LinkList::LinkList(){

}

/*******建立大小為n的雙向迴圈連結串列******/
void LinkList::creatlist(NODE *Head,int n){
    NODE *p = Head;
    NODE *tmp;
    cout<<"輸入"<<n<<"個連結串列的vaule"<<endl;
    for(int i=1;i<=n;i++){
        tmp = new NODE;
        cin>>tmp->value;
        p->next = tmp;
        Head->pre = tmp;
        tmp->pre = p;
        tmp->next = Head;
        p = tmp;
    }
}

/*******遍歷雙向迴圈連結串列*******/
bool LinkList::bianli(NODE *Head){
    NODE *p = Head;
    if(p==NULL||p->next==Head)
        return false;
    cout<<"遍歷:";
    while(p->next!=Head){
        p = p->next;
        cout<<p->value<<" ";
    }
    cout<<endl;
    return true;
}

/*********獲取雙向迴圈連結串列長度*********/
bool LinkList::getlenth(NODE *Head){
    NODE *p = Head;
    if(p==NULL||p->next==Head)
        return false;
    int cnt = 0;
    while(p->next!=Head){
        cnt++;
        p = p->next;
    }
    cout<<"連結串列長度為:"<<cnt<<endl;
    return true;
}

/******在第n個節點後加入新節點,其值為value******/
void LinkList::Insert(NODE *Head,int n,int value){
    NODE *p = Head;
    while(n!=0){
        p = p->next;
        n--;
    }
    NODE *Tmp = new NODE;
    Tmp->value = value;
    Tmp->next = p->next;   //先後順序不能亂
    p->next->pre = Tmp;
    Tmp->pre = p;
    p->next =Tmp;
}

/*******刪除第n個節點********/
bool LinkList::Delete(NODE *Head,int n){
    NODE *p = Head;
    if(p==NULL||p->next==Head)
        return false;
    while(n>1){
        p = p->next;
        n--;
    }
    p->next = p->next->next;
    p->next->next->pre = p;
    return true;
}

/******解構函式*******/
LinkList::~LinkList(){
    delete Head;
}

int main()
{
    int n;
    LinkList L1;
    NODE *node1 = new NODE;
    cout<<"輸入建立雙向迴圈連結串列的個數"<<endl;
    cin>>n;
    L1.creatlist(node1,n);
    L1.Insert(node1,2,66);
    L1.Delete(node1,1);
    L1.bianli(node1);
    L1.getlenth(node1);
    return 0;
}

 測試: