/* 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<<" "; p= p->ptr; k++; } cout<<endl; } int main(){ cout<<" ******************Begin Test********************** "; 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<<" ******************Empty List********************** "; list->print(); list->append(node1); list->append(node2); list->append(node3); list->append(node4); cout<<" ******************After Append******************** "; list->print(); cout<<" "; 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<<" *****************After Insert******************* "; list->print(); cout<<" ****************Print one-by-one**************** "; for(int i= 0; i< list->getLength(); i++){ cout<<"The "<<i<<" element of list is: "<<list->getElementByPos(i)<<endl; } cout<<" *********************POP3*********************** "; list->pop(); list->print(); cout<<" *******************Get Last********************* "; cout<<list->getLast()<<endl; /* Node* node7= new Node(7); list->append(node7); cout<<" ******************After Append******************** "; list->print(); */ cout<<" ******************After Delete******************** "; int k2= 2; list->deleteElementByPos(k2); list->print(); return 0; }
比上一版本(http://www.cnblogs.com/ruchicyan/p/4640665.html)仅仅是多了两个操作:pop() 和 deleteElementByPos(int pos)。
还是没有把指针给完全弄懂,这两天得花一些时间,好好看一下书中现成的代码。
敬请指正。
欢迎交流!