双向循环链表
来源:互联网 发布:acm杨辉三角c语言程序 编辑:程序博客网 时间:2024/05/02 04:56
//头文件
#include<iostream>
using namespace std;
#include"CyclesList.h"
//CyclesList.h 定义
class SNode
{
public:
SNode *pNext;
SNode *pFront;
int iData;
SNode();
};
class CyclesList
{
public:
int len;
SNode *pHead;
CyclesList();
~CyclesList();
void CyclesListAdd(int data);
void CyclesListInsert(int data,int pos);
void CyclesListDelete(int data);
bool CyclesListIsEmpey();
int CyclesListGegLenth();
void CyclesListPrint();
void CyclesListFind();
};
////CyclesList.cpp 实现
#include"HeadMain.h"
SNode::SNode()
{
iData=0;
pFront=NULL;
pNext=NULL;
}
CyclesList::CyclesList()
{
len=0;
pHead=NULL;
}
CyclesList::~CyclesList()
{
}
void CyclesList::CyclesListAdd(int data)
{
SNode * newNode=new SNode();
newNode->iData=data;
len++;
if(pHead==NULL)
{
pHead=newNode;
newNode->pFront=pHead;
newNode->pNext=pHead;
}
else
{
SNode *temp=pHead;
while(temp->pNext!=pHead)
{
temp=temp->pNext;//赋值相当一指针的++操作
}
newNode->pNext=pHead;
pHead->pFront=newNode;
temp->pNext=newNode;
newNode->pFront=temp;
}
}
void CyclesList::CyclesListPrint()
{
if(pHead==NULL)
{
return ;
}
else
{
//正的打印
SNode *temp=pHead;
cout<<"链表中的数据是:"<<endl;
while(temp->pNext!=pHead)
{
cout<<temp->iData<<" ";
temp=temp->pNext;
}
cout<<temp->iData<<endl;
//倒的打印
//SNode *temp=pHead;
// while(temp->pFront!=pHead)
// { temp=temp->pFront;
// cout<<temp->iData<<endl;
//
// }
//cout<<pHead->iData<<endl;
}
}
void CyclesList::CyclesListInsert(int data,int pos)
{
if(pos<0)
{
return ;
}
else
{
len++;
SNode * newNode=new SNode();
newNode->iData=data;
SNode *temp=pHead;
int i=1;
while(temp->pNext!=pHead && i!=pos-1)
{
temp=temp->pNext;
i++;
}
newNode->pNext=temp->pNext;
temp->pNext->pFront=newNode;
newNode->pFront=temp;
temp->pNext=newNode;
}
}
void CyclesList::CyclesListDelete(int data)
{
if(NULL==pHead)
{
return;
}
else
{
SNode *pTemp=pHead;
SNode *pTemp1=pHead->pNext;
while(pTemp1!=pHead && pTemp1->iData!=data)
{ //cout<<"data "<<data<<endl;
pTemp=pTemp1;
pTemp1=pTemp1->pNext;
}
if(pTemp1==pHead)
{
cout<<"没有要删除的元素"<<endl;
}
else
{
pTemp1->pNext->pFront=pTemp;
pTemp->pNext=pTemp1->pNext;
len--;
delete pTemp1;
pTemp1=NULL;
}
}
}
bool CyclesList::CyclesListIsEmpey()
{ if(len>0)
{
return true;
}
else
{
return false;
}
}
int CyclesList::CyclesListGegLenth()
{
return len;
}
void CyclesList::CyclesListFind()
{
}
// 主函数调用
#include"HeadMain.h"
void main()
{
CyclesList *cyl =new CyclesList();
int input;
cout<<"1)添加元素"<<endl;
cout<<"2)插入元素"<<endl;
cout<<"3)删除元素"<<endl;
cout<<"4)查询信息"<<endl;
cout<<"5)打印"<<endl;
cout<<"6)返回链表长度"<<endl;
cout<<"7)链表是否为空"<<endl;
cout<<"8)退出"<<endl;
while((cin>>input)&& input!=8)
{ int data=0;
int pos=0;
switch(input)
{
case 1:cout<<"输入要添加的元素:"<<endl;cin>>data;cyl->CyclesListAdd(data);break;
case 2:cout<<"请输入要插入的元素和位置:"<<endl;cin>>data;cin>>pos;cyl->CyclesListInsert(data,pos);break;
case 3:cout<<"请输入要删除的元素:"<<endl;cin>>data;cyl->CyclesListDelete(data);break;
case 4:cout<<"请输入要查找的元素:"<<endl;cin>>data;break;
case 5:cout<<"当前表中的元素有:"<<endl;cyl->CyclesListPrint();break;
case 6:cout<<"链表长度是:"<<cyl->CyclesListGegLenth()<<endl;break;
case 7:if(cyl->CyclesListIsEmpey())cout<<"链表不为空"<<endl;else cout<<"链表为空"<<endl;break;
}
cout<<"1)添加元素"<<endl;
cout<<"2)插入元素"<<endl;
cout<<"3)删除元素"<<endl;
cout<<"4)查询信息"<<endl;
cout<<"5)打印"<<endl;
cout<<"6)返回链表长度"<<endl;
cout<<"7)链表是否为空"<<endl;
cout<<"8)退出"<<endl;
}
}
- 双向循环链表
- 双向循环链表
- 双向循环链表
- 双向循环链表
- 双向循环链表
- 双向循环链表
- 双向循环链表
- 双向循环链表
- 双向循环链表
- 双向循环链表
- 双向循环链表
- 双向循环链表
- 双向循环链表
- 双向循环链表
- 双向循环链表
- 双向循环链表
- 双向循环链表
- 循环双向链表
- 系统日期时间格式化与字符串替换函数
- JBossESB HelloWorld实例解析
- poj/pku 1051
- select, poll和epoll的区别
- 清除日常操作历史记录
- 双向循环链表
- makefile 的含义和语法
- #pragma once与 #ifndef的用法和区别
- Linux kernel thread lock
- Qt4-外部程序调用和输出内容再学习
- [笔试题]判断链表是否有环及环的位置问题
- 春游
- 技术管理中常见的几个问题(转载)
- c++ 虚函数的实现机制