C++链式结构
来源:互联网 发布:室内平面图软件 编辑:程序博客网 时间:2024/06/06 00:57
各种形式的链表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
- C->数据结构->链式结构
- Stack-c单向链式结构
- 二叉树链式结构C代码实现
- C语言栈的链式存储结构
- 队列的链式结构C语言实现
- 栈-链式存储结构-c语言
- C语言--链式结构表1
- C语言链式结构表2
- 线性表-链表-链式结构-LinkedList.c
- 线性表——链式结构(c语言实现)
- 用C语言实现简单链式队列结构
- 线性表之单链表(链式存储结构)--C实现
- 数据结构C语言实现之二叉树链式结构
- 栈的链式存储结构及C实现
- C语言数据结构-2.线性表之链式存储结构
- C语言线性表(基于链式结构)
- 栈(C语言实现,基于链式结构)
- 队列(C语言实现,基于链式结构)
- 本人在最近用Hibernate的CallBack接口时发现问题
- List C++链式线性表模板
- 代码重构浅谈
- JSF自定义组件之五 JSF实现-Renderer
- struts中消息和错误的显示(转)
- C++链式结构
- 万万小心!不是闹着玩的!
- linux——vi编辑器基本使用方法
- SATA硬盘的安装指南
- linux下备份和恢复引导区
- 用C#编写ActiveX控件
- List-C++ template
- C#中的索引器——双重索引器
- 研发人员如何看待项目开发管理