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

时间:2022-12-09 21:57:36
/*  LList.cpp
* Author: Qiang Xiao
* Time: 2015-07-12
*/ #include<iostream>
using namespace std; class Node{
public:
int data;
Node* ptr;
Node(int elem= , 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< || pos> this->getLength()- ){
cout<<"Out of range!"<<endl;
return false;
} Node* tmp= this->head;
int k= -;
while(k< pos- ){
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= ;
while(i< this->getLength()- ){
tmp= tmp->ptr;
i++;
}
Node* t= this->tail;
this->tail= tmp;
this->length--;
delete t, tmp;
} int LList::getElementByPos(int pos){
if(pos< || pos> this->getLength()- ){
cout<<"Out of range!"<<endl;
return -;
}
Node* p= this->head->ptr;
int k= ;
while(k<pos){
p= p->ptr;
k++;
}
int m= p->data;
return m;
} LList::LList(){
Node* init= new Node();
this->head= init;
this->tail= init;
this->length= ;
} LList::~LList(){
while(head){
Node* tmp= new Node(,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= ;
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= ;
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();
Node* node2= new Node();
Node* node3= new Node();
Node* node4= new Node();
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();
int pos= ;
list->insert(pos,node5);
Node* node6= new Node();
pos= ;
list->insert(pos,node6);
Node* nod1= new Node();
pos= ;
list->insert(pos,nod1); cout<<"\n\n*****************After Insert*******************\n";
list->print(); cout<<"\n\n****************Print one-by-one****************\n";
for(int i= ; 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= ;
list->deleteElementByPos(k2);
list->print(); return ;
}

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

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

敬请指正。

欢迎交流!