单链表
#include<iostream>
using namespace std;
const int defaultSize = 20;
typedef int TypeData;
typedef struct list{
TypeData data;
list *next;
}list;
class SeqList {
list *head;
public:
SeqList();
SeqList(int sz);
SeqList(SeqList & l);
~SeqList();
//遍历单链表
void TraverseList(void f(TypeData &));
// 计算链表的长度
int length();
//返回单链表中指定序号的结点值
TypeData GetElem(int pos);
//在表的i位置后插入元素x
void Insert( int i,TypeData & x);
//删除表中i位置的元素x
bool Delete( int addr, TypeData & x);
//表是否为空
bool IsEmpty();
};
/*
-----------链表实现部分-------------
------------------------------------
*/
//----------不带形参构造函数----------------
SeqList::SeqList()
{
head=new list;
head->next=NULL;
}
//-----------带一个形参构造函数--------------
SeqList::SeqList( int sz)
{
int i=0;
head=new list;
list *p=head,*q;
while(i<sz)
{
q= new list;
cin>>q->data;
p->next =q;
p=p->next;
i++;
}
p->next =NULL;
}
//===========复制构造函数===================
SeqList::SeqList(SeqList& HL)
{
head=new list;
head->next=NULL;
list *p2=HL.head->next,*p1=head,*q;
while( p2)
{
q= new list;
q->data=p2->data ;
p1->next =q;
p1=q;
p2=p2->next ;
}
p1->next =NULL;
}
//----------析构函数--------------
SeqList::~SeqList()
{
list *p=head->next ,*q;
while(p)
{
q=p->next;
free(p);
p=q;
}
}
//----------链表的长度-----------------------
int SeqList::length()
{
list*current=head->next;
int i=0;
while(current)
{
i++;
current=current->next;
}
return i;
}
//----------//返回单链表中指定序号的结点值-------------
TypeData SeqList:: GetElem(int pos)
{
if(pos<1||pos>length())
{cerr<<"无效位置!\n";}
else{
list*current=head->next;
int i=1;
while(current)
{
if(i++==pos)return current->data ;
current=current->next ;
}
return head->data;
}
}
//----------在链表中插入元素x----------------------
void SeqList::Insert( int i ,TypeData & x)
{
if(head==NULL||i==0)
{
list *newNode=new list;
newNode->data=x;
if(newNode==NULL){cerr<<"存储分配错误!\n";exit(1);}
newNode->next=head->next;head->next=newNode;
}else
{
list* current=head;
for(int k=1;k<=i;k++)
if(current==NULL)break;
else current=current->next;
if(current==NULL&&head!=NULL)
{cerr<<"无效的插入位置!\n";}
else
{
list* newNode=new list;
newNode->data=x;
if(newNode==NULL){cerr<<"存储分配错误!\n";exit(1);}
newNode->next=current->next;
current->next=newNode;
}
}
}
//------------删除表中i位置的元素x-------------------
bool SeqList:: Delete( int addr, TypeData & x)
{
list *current,*del;
if(addr<1)
{del=head;head=head->next;}
else
{
current=head->next;
for(int k=1;k<addr-1;k++)
if(current==NULL) break;
else current=current->next;
if(current==NULL||current->next==NULL)
{cerr<<"无效的删除位置!\n";return false;}
del=current->next;
current->next=del->next;
}
x=del->data;delete del;
return true;
}
//------------链表是否为空--------------------
bool SeqList::IsEmpty()
{
return length()==0;
}
//------------遍历链表--------------------
void SeqList::TraverseList(void f(TypeData &))
{
list*current=head->next;
while(current)
{
f(current->data);
current=current->next;
}
}
//------------遍历的函数--------------------
void f(int &a)
{
cout<<a<<" ";
}
//----------------main()---------------------
void main()
{
int n,u,num;
cout<<"构造单链表list1,请确定数组元素个数:";
cin>>n;
SeqList List1(n);
List1.TraverseList(f);
cout<<"链表的长度为:"<<List1.length()<<endl;
cout<<"你想得到哪个元素:";
int addrs;
cin>>addrs;
cout<<"你想得到的元素是:"<<List1.GetElem(addrs)<<endl;
cout<<"你想在链表的第几个元素后插入元素(若在第一个前插入请按'0'):";
cin>>u;
cout<<"插入的元素为";
cin>>num;
List1.Insert(u,num);
cout<<"新链表为:"<<endl;
List1.TraverseList(f);
int it,i;
cout<<"你想删除第几个数:"<<endl;
cin>>i;
List1.Delete(i,it);
cout<<"新链表为:"<<endl;
List1.TraverseList(f);
}
浙公网安备 33010602011771号