一个简单链表的C++实现(二)

/*  LList.cpp
*   Author: Qiang Xiao
*   Time: 2015-07-12
*/

#include<iostream>
using namespace std;

class Node{
    public:
        int data;
    Node* ptr;
    Node(int elem= 0, Node* node= NULL){this->data= elem; this->ptr= node;}
};

class LList{
    private:
    Node* head;
    Node* tail;
    int length;
    public:
    LList();
    ~LList();
    bool append(Node*);
    bool insert(int, Node*);
    void print();
    int getLength(){return this->length;}
    int getElementByPos(int);
    void pop();
    int getLast() const;
    bool deleteElementByPos(int);
};

int LList::getLast() const{
    return this->tail->data;
}


bool LList::deleteElementByPos(int pos){
    if(pos< 0|| pos> this->getLength()- 1){
    cout<<"Out of range!"<<endl;
    return false;
    }

    Node* tmp= this->head;
    int k= -1;
    while(k< pos- 1){
    tmp= tmp->ptr;
    k++;
    }
    Node* del= tmp->ptr;
    tmp->ptr= tmp->ptr->ptr;
    delete del;
    this->length--;
    return true;
}

void LList::pop(){

    Node* tmp= this->head;
    int i= 0;
    while(i< this->getLength()- 1){
    tmp= tmp->ptr;
    i++;
    }
    Node* t= this->tail;
    this->tail= tmp;
    this->length--;
    delete t, tmp;
}

int LList::getElementByPos(int pos){
    if(pos< 0 || pos> this->getLength()- 1){
    cout<<"Out of range!"<<endl;
    return -200;
    }
    Node* p= this->head->ptr;
    int k= 0;
    while(k<pos){
    p= p->ptr;
    k++;
    }
    int m= p->data;
    return m;
}

LList::LList(){
    Node* init= new Node(4);
    this->head= init;
    this->tail= init;
    this->length= 0;
}

LList::~LList(){
    while(head){
        Node* tmp= new Node(0,head);
    tmp= head;
    head= head->ptr;
    delete tmp;
    }
    delete head;
    delete tail;
}

bool LList::insert(int pos, Node* node){
    if(pos>this->getLength()){
    cout<<"Out of range!"<<endl;
    return false;
    }
    int i= 0;
    Node* fence= new Node();
    fence= this->head;
    while(i< pos){
    fence= fence->ptr;
    i++;
    }
    node->ptr= fence->ptr;
    fence->ptr= node;
    this->length++;
    return true;
}

bool LList::append(Node* node){
    this->tail->ptr= node;
    this->tail= node;
    this->length++;
    return true;
}

void LList::print(){
    Node* p= this->head->ptr;
    int k= 0;
    while(k< this->getLength()){
    cout<<p->data<<"\t";
    p= p->ptr;
    k++;
    }
    cout<<endl;
}

int main(){
    cout<<"\n******************Begin Test**********************\n";
    Node* node1= new Node(1);
    Node* node2= new Node(2);
    Node* node3= new Node(3);
    Node* node4= new Node(4);
    LList* list= new LList();
    cout<<"\n******************Empty List**********************\n";
    list->print();
    list->append(node1);
    list->append(node2);
    list->append(node3);
    list->append(node4);
    cout<<"\n******************After Append********************\n";
    list->print();
    cout<<"\n\n";
    Node* node5= new Node(10);
    int pos= 2;
    list->insert(pos,node5);
    Node* node6= new Node(30);
    pos= 4;
    list->insert(pos,node6);
    Node* nod1= new Node(60);
    pos= 7;
    list->insert(pos,nod1);

    cout<<"\n\n*****************After Insert*******************\n";
    list->print();
    
    cout<<"\n\n****************Print one-by-one****************\n";
    for(int i= 0; i< list->getLength(); i++){
    cout<<"The "<<i<<" element of list is:  "<<list->getElementByPos(i)<<endl;
    }
    cout<<"\n\n*********************POP3***********************\n";
    list->pop();
    list->print();

    cout<<"\n\n*******************Get Last*********************\n";
    cout<<list->getLast()<<endl;
/*
    Node* node7= new Node(7);
    list->append(node7);
    cout<<"\n******************After Append********************\n";
    list->print();
*/
    cout<<"\n******************After Delete********************\n";
    int k2= 2;
    list->deleteElementByPos(k2);
    list->print();

    return 0;
}

 

比上一版本(http://www.cnblogs.com/ruchicyan/p/4640665.html)仅仅是多了两个操作:pop() 和 deleteElementByPos(int pos)。

还是没有把指针给完全弄懂,这两天得花一些时间,好好看一下书中现成的代码。

敬请指正。

欢迎交流!

posted @ 2015-07-12 18:29  TACHIA  阅读(266)  评论(0编辑  收藏  举报