您的位置:首页 > 编程语言 > C语言/C++

链式存储结构类的实现 (实现函数)(包括插入,删除等操作的实现)

2013-10-15 00:06 113 查看
//程序名:类定义.h
//      程序功能:链式存储结构类的实现(包括插入,删除等操作的实现)
//          作者:吴雨羲
//          日期:2013.9.30
//          版本:1.0
//      修改内容:无
//      修改日期:
//      修改作者:
//
#include<iostream>
using namespace std;
//定义单链表的结点结构
struct Node
{
int data;
Node *next;
};
//定义单链表类
class List
{
public:
List();//定义构造函数
void H_Insert();//头插入建链
void T_Insert();//尾插入建链
void Insert(int i,int val);//插入
void Delete(int i);//删除
void Search(List list);//查找
int Lenth();//链表长度
void Reverse();//逆置
void Print();//输出

int Testcin(int i,int len);//检验输入
bool Testlist();//测试链表是否为空
~List();//析构函数
private:
Node *first;//表头指针
};


//程序名:类实现.cpp
//      程序功能:链式存储结构类的实现(包括插入,删除等操作的实现)
//          作者:吴雨羲
//          日期:2013.9.30
//          版本:1.0
//      修改内容:无
//      修改日期:
//      修改作者:
//
#include<iostream>
#include"类定义.h"
using namespace std;
//////////////////////////////////////////////////////////////////////////////
//  构造函数
//  函数功能:定义一个表头节点无初值
//函数参数:无
//参数返回值:无
List::List()
{
first=new Node;
first->next=0;
}
//////////////////////////////////////////////////////////////////////////////
// 析构函数
// 函数功能:将链表空间释放
//函数参数:无
//参数返回值:无
List::~List()
{
Node *p;
while(first)
{
p=first;
first=first->next;
delete p;
}

}
//////////////////////////////////////////////////////////////////////////////
//  头插入建链函数
//  函数功能:以头插入方式建立有n个节点的链表
//函数参数:无
//
//参数返回值:无
//
void List::H_Insert()
{
int n;
cout<<"输入链表元素个数"<<endl;
cin>>n;
cout<<"输入元素"<<endl;

Node *p,*head=0;
int i;
for(i=0;i<n;i++)
{
p=new Node;
cin>>p->data;
p->next=head;
head=p;
}
first->next=head;
}
//////////////////////////////////////////////////////////////////////////////
//  尾插入建链函数
//  函数功能:以尾插入方式建立有n个节点的链表
//函数参数:无
//
//参数返回值:无
//
void List::T_Insert()
{
int n;
cout<<"输入链表元素个数"<<endl;
cin>>n;
cout<<"输入元素"<<endl;
Node *tail=new Node;
tail=first;
Node *p;
int i;
for(i=0;i<n;i++)
{
p=new Node;
cin>>p->data;
p->next=0;
tail->next=p;
tail=p;
}

}
//////////////////////////////////////////////////////////////////////////////
//  插入元素函数
//  函数功能:将新值插入到指定位置
//函数参数:
//       i   第i位
//      val  新值
//参数返回值:无
//
void List::Insert(int i,int val)
{

Node *head=first;
Node *r,*p;
int t;
for(t=0;t<i;t++)
{
r=head;
head=head->next;
}
p=new Node;
p->data=val;
r->next=p;
p->next=head;
}
//////////////////////////////////////////////////////////////////////////////
//  删除元素函数
//  函数功能:将指定位置元素删除
//函数参数:
//       i   第i位
//参数返回值:无
//
void List::Delete(int i)
{

Node *head=first;
Node *r;
int t;
for(t=0;t<i;t++)
{
r=head;
head=head->next;
}
r->next=head->next;
delete head;
}
//////////////////////////////////////////////////////////////////////////////
//  查找函数
//  函数功能:查找有无相应元素
//函数参数:
//       list 类对象
//参数返回值:
//       无
//
void List::Search(List list)
{

int val;
cout<<"输入要查找的元素"<<endl;
cin>>val;
Node *head=first->next;
while(head)
{
if(head->data==val)
{
cout<<"查找成功"<<endl;
return ;
}
head=head->next;
}
if(head==0)
{
cout<<"没有该元素"<<endl;
cout<<"请核对";
list.Print();
}

}
//////////////////////////////////////////////////////////////////////////////
//  链表长度函数
//  函数功能:输出表长
//函数参数:无
//参数返回值:
//       count 表长
//
int List::Lenth()
{
int count=0;
Node *head=first->next;
while(head)
{
head=head->next;
count++;
}
return count;

}
//////////////////////////////////////////////////////////////////////////////
//  元素逆置输出函数
//  函数功能:元素逆置输出
//函数参数:无
//参数返回值:无
//
void List::Reverse()
{
Node*head=first->next;
Node*h=0,*p;
while(head)
{
p=head->next;
head->next=h;
h=head;
head=p;
}
first->next=h;
}
//////////////////////////////////////////////////////////////////////////////
//  输出函数
//  函数功能:元素输出
//函数参数:无
//参数返回值:无
//
void List::Print()
{
cout<<"当前元素序列"<<endl;
Node*head=first->next;
while(head)
{
cout<<head->data<<" ";
head=head->next;
}
cout<<endl;
}

//////////////////////////////////////////////////////////////////////////////
//  检验输入函数
//  函数功能:检验插入或删除指定位置是否合法
//函数参数:
//       i   第i位
//       len 序列长度
//参数返回值:i 合法位置
//
int List::Testcin(int i,int len)
{

while(i>len||i<=0)
{
if(i>len)
cout<<"插入位置太靠后了,当前序列只有 "<<len<<" 位"<<endl;
else
if (i<=0)
cout<<"输入的位置错误,位置应该大于0。"<<endl;
cout<<"请重新输入合适的位置"<<endl;
cin>>i;
}
return i;
}
//////////////////////////////////////////////////////////////////////////////
//  检验链表函数
//  函数功能:检验检验链表是否为空
//函数参数:
//       i   第i位
//       len 序列长度
//参数返回值:
//       true 链表不为空
//       false 链表为空
//
bool List::Testlist()
{
if(first->next==0)
{
cout<<"        错误!	请先进行链表初始化,选择 选项 1        "<<endl;
cout<<endl;
return false;
}
else
return true;
}


//程序名:链式存储操作.cpp
//      程序功能:链式存储操作主程序(包括插入,删除等操作的实现)
//          作者:吴雨羲
//          日期:2013.9.30
//          版本:1.0
//      修改内容:无
//      修改日期:
//      修改作者:
//
#include<iostream>
#include"类定义.h"
using namespace std;
void main()
{
List list;
int choice,finish=1;
int n,val,real_n;
while(finish)
{
cout<<endl;
cout<<"             ************************菜单*****************************"<<endl;
cout<<"             **     1:链表初始化   2:尾插入建链    3:头插入建链     **"<<endl;
cout<<"             **     4:插入元素     5:删除元素      6:查找元素       **"<<endl;
cout<<"             **     7:输出元素个数 8:元素逆置输出  9:退出           **"<<endl;
cout<<"             *********************************************************"<<endl;
cout<<"请选择你要执行的选项(1-9):";
cin>>choice;
switch (choice)
{
case 1:
case 2:
list.T_Insert();
list.Print();
break;
case 3:
list.H_Insert();
list.Print();
break;
case 4:
if(!list.Testlist())
break;
list.Print();
cout<<"输入插入位置"<<endl;
cin>>n;
real_n=list.Testcin(n,list.Lenth());
cout<<"输入插入元素值"<<endl;
cin>>val;
list.Insert(real_n,val);
list.Print();
break;
case 5:
if(!list.Testlist())
break;
list.Print();
cout<<"请输入要删除的位置"<<endl;
cin>>n;
real_n=list.Testcin(n,list.Lenth());
list.Delete(real_n);
cout<<"当前元素序列"<<endl;
list.Print();
break;
case 6:
if(!list.Testlist())
break;
list.Search(list);
break;
case 7:
if(!list.Testlist())
break;
cout<<"表长为"<<list.Lenth()<<endl;
break;
case 8:
if(!list.Testlist())
break;
list.Print();
list.Reverse();
list.Print();
break;
case 9:
finish=0;
break;
default:
cout<<"没有此选项"<<endl;
break;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  链表 存储 c++
相关文章推荐