用类实现单链表操作
来源:互联网 发布:乐视网络电视报价 编辑:程序博客网 时间:2024/06/05 17:23
/*—————————————————————————————————
Node.h
Declearation of Class Node.
—————————————————————————————————*/
#ifndef _NODE
#define _NODE
class Nodes
{
public:
Nodes(void);
void NodesCreat(void); //创建链表
void NodesPrint(void); //遍历输出链表
void NodesDelete(void); //删除链表节点
void NodesInsert(void); //插入节点
void NodesReverse(void); //链表逆置
private:
struct List
{
int index;
List *next;
} *head,*end; //首尾指针
int iListLen; //链表长度
};
#endif
/*—————————————————————————————————
Node.cpp
Source File of Class Node.
—————————————————————————————————*/
#include "iostream.h"
#include "Node.h"
Nodes::Nodes(void) //构造函数实现节点空,长度零
{
head=end=NULL;
iListLen=0;
}
void Nodes::NodesCreat(void)
{
int iListNum,iCount=0;
List *ptemp=NULL;
cout<<" Input The ListNum You want: ";
cin>>iListNum; //创建的节点数
cout<<" Input Every List: ";
while (iCount<iListNum)
{
ptemp=new List;
cin>>ptemp->index;
if (head==NULL)
{
head=end=ptemp;
end->next=NULL;
}
else //链表不为空
{
end->next=ptemp;
end=ptemp;
end->next=NULL;
}
iCount++;iListLen++;
}
cout<<" Creat Or Add sucess... ";
}
void Nodes::NodesPrint(void)
{
List *pListIndex;int iLen=0;
if (head==NULL)
{
cout<<" No List Now ";
return;
}
else //链表不为空
{
cout<<" Total List Num:"<<iListLen<<endl;
iLen=1;
pListIndex=head;
cout<<iLen<<"# "<<pListIndex->index<<endl;
while (pListIndex->next)
{
iLen++;
pListIndex=pListIndex->next;
cout<<iLen<<"# "<<pListIndex->index<<endl;
}
}
}
void Nodes::NodesDelete(void)
{
int iListPos,n=1;List *pListIndex,*pListTemp;
if (head==NULL)
{
cout<<" No list Now ";
return;
}
else //链表不为空
{
cout<<" Input the Delete Position: ";
cin>>iListPos;
if (iListPos>iListLen) //删除的节点超出总长度
{
cout<<" Error!";
cout<<" The Total ListNum is:"<<iListLen<<endl;
return;
}
if (iListPos==1||iListPos==iListLen) //删除的节点在头和尾
{
if (iListPos==1) //删除头节点
{
pListIndex=head;
head=head->next;
delete pListIndex;
}
else //删除尾节点
{
for (pListIndex=head;pListIndex->next!=end;)
pListIndex=pListIndex->next;
delete end;
end=pListIndex;
end->next=NULL;
}
}
else //删除节点在非特殊位置
{
for (pListIndex=head;n<iListPos-1;n++)
pListIndex=pListIndex->next;
pListTemp=pListIndex->next;
pListIndex->next=pListTemp->next;
delete pListTemp;
}
iListLen--;
}
cout<<" The List has been deleted... ";
}
void Nodes::NodesInsert(void)
{
int iListPos,n=1;
List *pListIndex=NULL,*pListInsert=NULL;
pListInsert=new List;
cout<<" Insert List Information: ";
cin>>pListInsert->index;
cout<<" Input The The Insert Positon: ";
cin>>iListPos;
pListInsert->next=NULL;
if (head==NULL) //如果链表为空则创建插入点
{
cout<<" No List Now ,Insert you List as head. ";
head=end=pListInsert;
end->next=NULL;
NodesPrint();
return;
}
if (iListPos==1 || iListPos==iListLen) //插入位置在头和尾
{
if (iListPos==1)
{
pListInsert->next=head;head=pListInsert;
}
if(iListPos==iListLen)
{
end->next=pListInsert;end=pListInsert;
}
}
else //非特殊位置的插入操作
{
if (iListPos>iListLen)
{
cout<<" Error ! Beyond the Num! ";
return;
}
for (pListIndex=head;n<iListPos-1;n++)
pListIndex=pListIndex->next;
pListInsert->next=pListIndex->next;
pListIndex->next=pListInsert;
}
iListLen++;
cout<<" The List has been Inserted... ";
}
void Nodes::NodesReverse(void)
{
List *pListIndex,*pListTemp;
if (head!=NULL)
{
pListIndex=head;
while (pListIndex->next!=NULL)
{
pListTemp=pListIndex->next;
pListIndex->next=pListTemp->next;
pListTemp->next=head;
head=pListTemp;
pListTemp=NULL;
}
end=pListIndex;
}
else
{
cout<<" No List Now .You can't Reverse... ";
return;
}
}
/*——————————————————————————————————
main.cpp
The main function
——————————————————————————————————*/
#include "iostream.h"
#include "Node.h"
void Screen(Nodes &obj)
{
char op;
while (1)
{
cout<<" ";
cout<<" ———————————————— ";
cout<<" | Node List Operation | ";
cout<<" ———————————————— ";
cout<<" | 1.Creat Or Add The List | ";
cout<<" | 2.Delete The List | ";
cout<<" | 3.Insert The List | ";
cout<<" | 4.Reverse The List | ";
cout<<" | 5.Print All The List | ";
cout<<" | 0.Exit | ";
cout<<" ———————————————— ";
cout<<" Input The Option Please: ";
cin>>op;
switch(op)
{
case '0':return;
case '1':obj.NodesCreat();break;
case '2':obj.NodesDelete();break;
case '3':obj.NodesInsert();break;
case '4':obj.NodesReverse();break;
case '5':obj.NodesPrint();break;
default:cout<<" Error Option.Input Again... ";
}
}
}
int main(int argc,char *argv[])
{
Nodes object;
Screen(object);
return 0;
}
- 用类实现单链表操作
- 用二级指针实现单链表插入操作
- 单链表的操作实现
- C++实现单链表操作
- 单链表操作java实现
- 单链表操作的实现
- 单链表若干操作实现
- 单链表的操作实现
- java实现单链表操作
- php实现单链表操作
- 单链表基本操作实现
- 单链表基本操作实现
- 用位操作实现+
- 数据库操作类实现
- 用栈操作实现队列的操作
- c#实现单链表的操作
- 单链表的基本操作实现
- Javascript实现单链表简单操作
- 分享:半年游戏测试的思考
- update 1
- 眼球
- Nginx 0.7.x + PHP 5.2.9(FastCGI)搭建胜过Apache十倍的Web服务器
- 开始练习用RichFaces实现进销存系统前台界面
- 用类实现单链表操作
- PLINQ -> Parallel Extensions
- WHY PEOPLE WOULD HELP YOU?
- asp access 时间段
- memset ,memcpy 和strcpy 的根本区别?
- sql 数据库全文索引的总结
- MFC程序运行机制
- VC中Tab控件的用法
- VC中如何调用DLL中的函数