C++链表实现

单个节点头文件

 1 //Node.h
 2 #ifndef NODE_H
 3 #define NODE_H
 4 
 5 #include<iostream>
 6 using namespace std;
 7 
 8 class Node{
 9 public:
10     //生成节点
11     Node();
12     //删除节点
13     ~Node();
14     //设置数据
15     void setData(int d){*data=d;};
16     //得到节点数据
17     int getData(){return *data;};
18     //链接下一节点
19     void setNext(Node *r){next=r;};
20     //得到节点的下一节点
21     Node *getNext(){return next;} ;
22     //打印数据
23     void PrintData(){cout<<*data;};
24 private:
25     int *data;
26     Node *next;
27 };
28 
29 Node::Node(){
30     data=new int;
31     next=NULL;
32 }
33 
34 Node::~Node(){
35     delete data;
36     next=NULL;
37 }
38 
39 #endif

链表头文件

#ifndef LINKLIST_H
#define LINKLIST_H

#include<iostream>
#include"Node.h"
using namespace std;

class LinkList{
public:
    LinkList();
    ~LinkList();
    //是否为空
    bool  isEmpty();
    //清除链表
    bool  ClearList();
    //得到链表长度
    int  GetListLength() const {return length;}
    //得到元素的位置
    int  GetElementIndex(int &elem);
    //得到链表指定位置的元素
    bool  GetListElement(int index,int *elem);
    //链表插入
    bool  ListInsert(int index,int &elem);
    //链表删除
    bool  ListDelete(int index,int *elem);
    //打印链表
    void  ListPrint();
private:
    Node *list;
    int  length;
};

LinkList::LinkList(){
    list=new Node();
    list->setData(0);
    list->setNext(NULL);
    length=0;
}

LinkList::~LinkList(){
    Node *nextNode=list;
    while(nextNode->getNext()!=NULL){
        nextNode=list->getNext();
        delete list;
        list=nextNode;
    }
    delete list;
    list=NULL;
}

bool  LinkList::isEmpty(){
    if(length==0){
        return true;
    }else{
        return false;
    }
}

bool  LinkList::ClearList(){
    if(isEmpty()){
        cout<<"List empty clear fail"<<endl;
        return false;
    }
    Node *nowNode=list->getNext();
    Node *nextNode=list->getNext();
    while(nextNode->getNext()!=NULL){
        nextNode=nextNode->getNext();
        delete nowNode;
        nowNode=nextNode;
    }
    delete nowNode;
    length=0;
    list->setNext(NULL);
    return true;
}

int LinkList::GetElementIndex(int &elem){
    Node *tempNode=list;
    for(int i=0;i<length;++i){
        tempNode=tempNode->getNext();
        if(elem==tempNode->getData()){
            return ++i;
        }
    }
    return -1;
}

bool LinkList::GetListElement(int index,int  *elem){
    if(index<0 || index>=length){
        return false;
    }
    Node *tempNode=list;
    for(int i=0;i<=index;++i){
        tempNode=tempNode->getNext();
    }
    *elem=tempNode->getData();
    return true;
}

bool LinkList::ListInsert(int index,int &elem){
    if(index<0 || index>length){
        return false;
    }
    Node *tempProNode=list;
    for(int i=0;i<index;++i){
        tempProNode=tempProNode->getNext();
    }
    Node *nowNode=new Node();
    if(nowNode==NULL){
        cout<<"new node create fail"<<endl;
        return false;
    }
    Node *temp=tempProNode->getNext();
    tempProNode->setNext(nowNode);
    nowNode->setNext(temp);
    nowNode->setData(elem);
    ++length;
    return true;
}

bool LinkList::ListDelete(int index,int *elem){
    if(index<0 || index>=length){
        return false;
    }
    Node *tempProNode=list;
    for(int i=0;i<index;++i){
        tempProNode=tempProNode->getNext();
    }
    Node *tempNode=tempProNode->getNext();
    tempProNode->setNext(tempNode->getNext());
    *elem=tempNode->getData();
    delete tempNode;
    --length;
    return true;
}

void LinkList::ListPrint(){
    if(isEmpty()){
        cout<<"List empty"<<endl;
        return ;
    }
    Node *tempNode=list->getNext();
    while(tempNode->getNext()!=NULL){
        tempNode->PrintData();
        tempNode=tempNode->getNext();
    }
    tempNode->PrintData();
    cout<<endl<<"end"<<endl;
}
#endif

调用代码

#include<iostream>
#include<string>
#include"LinkList.h"
using namespace std;

int main(){
    int data[10]={0,1,2,3,4,5,6,7,8,9};
    LinkList *linkList=new LinkList();
    for(int i=0;i<=5;++i){
        linkList->ListInsert(i,data[i]);
    }
    linkList->ListPrint();
    cout<<"getElementIndex:"<<linkList->GetElementIndex(data[4])<<endl;
    int getData;
    linkList->GetListElement(2,&getData);
    cout<<"getListElement:"<<getData<<endl;
    int deleteData;
    linkList->ListDelete(0,&deleteData);
    cout<<"delete data:"<<deleteData<<endl;
    linkList->ListPrint();

    delete linkList;
    linkList=NULL;
    system("pause");
    return 0;
}

 

posted @ 2019-07-26 15:27  羽吢  阅读(980)  评论(0编辑  收藏  举报