各种形式的链表C++实现方式
1.单向链表链表
2.单向循环链表
3.双向链表
/******************************************************************************
* 单向链表的实现
*****************************************************************************/
#include <stdlib.h>
#include <iostream>
using namespace std ;
class Node
{
private:
Node * next;
int data;
public:
Node()
{
this->data=0;
this->next=NULL;
cout<<"Node::Node()"<<endl;
}
virtual ~Node()
{
cout<<"Node::~Node()"<<endl;
}
void setData(int data)
{
this->data=data;
}
int getData()
{
return this->data;
}
Node * getNext()
{
return this->next;
}
void setNext(Node * node)
{
this->next=node;
}
};
class NodeManager
{
private:
static Node * head;
static Node * tail;
public:
NodeManager()
{
this->head=NULL;
this->tail=NULL;
cout<<"NodeManager::NodeManager()"<<endl;
}
virtual ~NodeManager()
{
while (head)
{
Node * pTemp ;
pTemp = head ;
head = head->getNext() ;
cout << "pTemp --> " << pTemp->getData() << endl ;
delete pTemp ;
}
cout<<"NodeManager::~NodeManager()"<<endl;
}
void addNode(int data)
{
cout<<"now enter the function of addNode! "<<endl;
Node *node=new Node();
cout<<"initiate a node"<<endl;
node->setData(data);
node->setNext(NULL);
cout<<"node->getData() = "<<node->getData()<<endl;
if( (head == NULL) && ( tail == NULL ) )
{
cout<<"the list is NULL!"<<endl;
head=node;
tail=node;
cout<<"set the next of the node NULL;"<<endl;
cout<<"node->getNext() = "<<node->getNext()<<endl;
}
else
{
tail->setNext(node);
tail=node;
}
//delete node;
}
void printNode()
{
Node * node = head;
while(node!=NULL)
{
cout<<"node->getData()= "<<node->getData()<<" ";
node=node->getNext();
}
cout<<endl;
}
void deleteNode(int data)
{
Node * node = head;
Node * pre = head;
while(node!=NULL)
{
if(node->getData()!=data)
{
pre = node;
node = node -> getNext();
}
else
{
if(head == tail)
{
head = tail = NULL;
}
else
{
if ( head == node )
{
head = head -> getNext();
pre = node = head;
}
if ( tail == node )
{
tail = pre;
pre ->setNext(NULL);
}
if ( head != node && tail !=node)
{
pre -> setNext ( node -> getNext() );
node = pre -> getNext();
}
}//else
}//else
}//while
}
};
Node* NodeManager::head=NULL;
Node* NodeManager::tail=NULL;
void main()
{
int addNum=0,dtNum=0;
NodeManager *nm=new NodeManager;
while(1)
{
char choice ;
cout<<"**************************************************************************"<<endl;
cout<<" 0. quit ; 1. create the list ; 2. print the list; 3. delete the node ; "<<endl;
cout<<"**************************************************************************"<<endl;
cout<<"input your choice : ";cin>>choice;
cout<<endl;
switch(choice)
{
case '0': return;
case '1': cout<<"please input the data: ";
cin>>addNum;
nm->addNode(addNum);
cout<<"********"<<endl;break;
case '2': nm->printNode();
cout<<"********"<<endl;break;
case '3': cout<<"please input the data you want to delete: ";
cin>>dtNum;
nm ->deleteNode(dtNum);
default: cout<<"the num doesn't exist!"<<endl;break;
}
cout<<endl;
}
}
/*********************************************************
* 单向循环链表
**********************************************************/
#include <stdlib.h>
#include <iostream>
using namespace std ;
class Node
{
private:
Node * next;
int data;
public:
Node()
{
this->data=0;
this->next=NULL;
cout<<"Node::Node()"<<endl;
}
virtual ~Node()
{
cout<<"Node::~Node()"<<endl;
}
void setData(int data)
{
this->data=data;
}
int getData()
{
return this->data;
}
Node * getNext()
{
return this->next;
}
void setNext(Node * node)
{
this->next=node;
}
};
class NodeManager
{
private:
static Node * head;
static Node * tail;
public:
NodeManager()
{
this->head=NULL;
this->tail=NULL;
cout<<"NodeManager::NodeManager()"<<endl;
}
virtual ~NodeManager()
{
while (head)
{
Node * pTemp ;
pTemp = head ;
head = head->getNext() ;
cout << "pTemp --> " << pTemp->getData() << endl ;
delete pTemp ;
}
cout<<"NodeManager::~NodeManager()"<<endl;
}
void addNode(int data)
{
cout<<"now enter the function of addNode! "<<endl;
Node *node=new Node();
cout<<"initiate a node"<<endl;
node->setData(data);
node->setNext(NULL);
cout<<"node->getData() = "<<node->getData()<<endl;
if( (head == NULL) && ( tail == NULL ) )
{
cout<<"the list is NULL!"<<endl;
head=node;
tail=node;
cout<<"set the next of the node NULL;"<<endl;
cout<<"node->getNext() = "<<node->getNext()<<endl;
}
else
{
tail->setNext(node);
tail=node;
}
//delete node;
tail->setNext(head);
}
void printNode()
{
Node * node = head;
if(node!=NULL)
{
do
{
cout<<"node->getData()= "<<node->getData()<<" ";
node=node->getNext();
}while(node!=head);
}
cout<<endl;
}
void deleteNode(int data)
{
Node * node = head;
Node * pre = head;
if(node!=NULL)
{
do
{
if(node->getData()!=data)
{
pre = node;
node = node -> getNext();
}
else
{
if(head == tail)
{
head = tail = NULL;
}
else
{
if ( head == node )
{
head = head -> getNext();
tail -> setNext(head);
pre = node = head;
}
if ( tail == node )
{
tail = pre;
pre ->setNext(head);
}
if ( head != node && tail !=node)
{
pre -> setNext ( node -> getNext() );
node = pre -> getNext();
}
}//else
}//else
}while(node!=head);
}//if
}
};
Node* NodeManager::head=NULL;
Node* NodeManager::tail=NULL;
void main()
{
int addNum=0,dtNum=0;
NodeManager *nm=new NodeManager;
while(1)
{
char choice ;
cout<<"**************************************************************************"<<endl;
cout<<" 0. quit ; 1. create the list ; 2. print the list; 3. delete the node ; "<<endl;
cout<<"**************************************************************************"<<endl;
cout<<"input your choice : ";cin>>choice;
cout<<endl;
switch(choice)
{
case '0': return;
case '1': cout<<"please input the data: ";
cin>>addNum;
nm->addNode(addNum);
cout<<"********"<<endl;break;
case '2': nm->printNode();
cout<<"********"<<endl;break;
case '3': cout<<"please input the data you want to delete: ";
cin>>dtNum;
nm ->deleteNode(dtNum);break;
default: cout<<"the num doesn't exist!"<<endl;break;
}
cout<<endl;
}
}
/********************************************
* 双向链表
*********************************************/
#include <stdlib.h>
#include <iostream>
using namespace std ;
class Node
{
private:
Node * next;
Node * prior;
int data;
public:
Node()
{
this->data=0;
this->prior=NULL;
this->next=NULL;
cout<<"Node::Node()"<<endl;
}
virtual ~Node()
{
cout<<"Node::~Node()"<<endl;
}
void setData(int data)
{
this->data=data;
}
int getData()
{
return this->data;
}
void setPrior(Node * pNode)
{
this->prior=pNode;
}
Node * getPrior()
{
return this->prior;
}
void setNext(Node * nNode)
{
this->next=nNode;
}
Node * getNext()
{
return this->next;
}
};
class NodeManager
{
private:
static Node * head;
static Node * tail;
public:
NodeManager()
{
this->head=NULL;
this->tail=NULL;
cout<<"NodeManager::NodeManager()"<<endl;
}
virtual ~NodeManager()
{
while (head)
{
Node * pTemp ;
pTemp = head ;
head = head->getNext() ;
cout << "pTemp --> " << pTemp->getData() << endl ;
delete pTemp ;
}
cout<<"NodeManager::~NodeManager()"<<endl;
}
void addNode(int data)
{
Node *node=new Node();
node->setData(data);
node->setPrior(NULL);
node->setNext(NULL);
if( (head == NULL) && ( tail == NULL ) )
{
head=node;
tail=node;
}
else
{
node->setPrior(tail);
tail->setNext(node);
tail=node;
}
//delete node;
}
void printNode()
{
Node * node = head;
cout<<"the data are: ";
while(node!=NULL)
{
cout<<node->getData()<<" ";
node=node->getNext();
}
cout<<endl;
}
void deleteNode(int data)
{
Node * node = head;
Node * pre = head;
while(node!=NULL)
{
if(node->getData()!=data)
{
pre = node;
node = node -> getNext();
}
else
{
if(head == tail)
{
head = tail = NULL;
}
else
{
if( tail == node )
{
tail = tail -> getPrior();
tail -> setNext(NULL);
delete node;
break;
}
Node * temp = new Node();
temp = node;
if ( head == node )
{
head = head -> getNext();
head -> setPrior(NULL);
pre = node = head;
}
if ( head != node && tail !=node)
{
node = node -> getNext();
pre -> setNext (node);
node ->setPrior(pre);
}
temp -> setNext(NULL);
temp -> setPrior(NULL);
delete temp;
}//else
}//else
}//while
}
};
Node* NodeManager::head=NULL;
Node* NodeManager::tail=NULL;
void main()
{
int addNum=0,dtNum=0;
NodeManager *nm=new NodeManager;
while(1)
{
char choice ;
cout<<"**************************************************************************"<<endl;
cout<<" 0. quit ; 1. create the list ; 2. print the list; 3. delete the node ; "<<endl;
cout<<"**************************************************************************"<<endl;
cout<<"input your choice : ";cin>>choice;
cout<<endl;
switch(choice)
{
case '0': return;
case '1': cout<<"please input the data: ";
cin>>addNum;
nm->addNode(addNum);break;
case '2': nm->printNode();break;
case '3': cout<<"please input the data: ";
cin>>dtNum;
nm ->deleteNode(dtNum);break;
default: cout<<"choose error!"<<endl;break;
}
cout<<endl;
}
}
//end