转载:http://www.diybl.com/course/3_program/c/csuanfa/2007213/21577.html
#include "iostream.h"
#include "iomanip.h"
typedef int ElemType;
typedef struct ADTList
{
ElemType Elem;
struct ADTList *next;
}ADTList;
//链表功能函数
bool InitList(ADTList *&L);
void DestroyList(ADTList *L);
void ClearList(ADTList *L);
bool ListEmpty(ADTList *L);
long ListLength(ADTList *L);
bool GetElem(ADTList *L,long index,ElemType& e);
long LocateElem(ADTList *L,ElemType e);
int compare(ElemType elem1,ElemType elem2);
bool PriorElem(ADTList *L,ElemType cur_e,ElemType& pre_e);
bool NextElem(ADTList *L,ElemType cur_e,ElemType& next_e);
bool ListInsert(ADTList *L,long index,ElemType e);
bool ListDelete(ADTList *L,long index,ElemType& e);
bool visit(ElemType elem);
bool ListTraverse(ADTList *L);
///
//测试用函数
bool create(ADTList *L);
bool create1(ADTList *L);
bool deleteTest(ADTList *L);
///
int main(int argc,char **argv)
{
ADTList *list;
list=NULL;
cout<<"链表实验程序"<///
cout<<"List Init:"< if(!InitList(list))
return 1;
///
if(!create(list))
return 1;
cout< if(!ListTraverse(list))
return 1;
///
cout< ClearList(list);
cout< if(!ListTraverse(list))
return 1;
///
if(!create1(list))
return 1;
cout< if(!ListTraverse(list))
return 1;
///
deleteTest(list);
///
return 0;
}
//以下是验证链表功能的函数
bool deleteTest(ADTList *L)
{
ElemType e;
cout< cout<<"delete the 1th node:"< ListDelete(L,1,e);
cout<<"e="< cout<<"The list:"< ListTraverse(L);
cout< ListDelete(L,2,e);
cout<<"e="< cout<<"The list:"< ListTraverse(L);
cout< ListDelete(L,ListLength(L),e);
cout<<"e="< cout<<"The list:"< ListTraverse(L);
return true;
}
bool create1(ADTList *L)
{
int i;
cout< for(i=1;i<=100;i++)
{
if(!ListInsert(L,i,i))
return false;
}
return true;
}
bool create(ADTList *L)
{
ElemType cur;
int i,num;
cout<<"新建一个链表"< do
{
cout<<"请输入链表的结点数:";
cin>>num;
}while(num<1);
cout< for(i=1;i<=num;i++)
{
cout<<"请输入"< cin>>cur;
if(!ListInsert(L,i,cur))
return false;
}
return true;
}
//以下是链表功能的实现函数
bool InitList(ADTList *&L)
{
ADTList *p;
p=NULL;
if((p=new(ADTList))==NULL)
return false;
p->next=NULL;
L=p;
return true;
}
void DestroyList(ADTList *L)
{
ADTList *p;
while(L->next)
{
p=L;
L=L->next;
delete(p);
}
}
void ClearList(ADTList *L)
{
ADTList *p,*q;
p=L->next;
while(p)
{
q=p;
p=p->next;
delete(q);
}
L->next=NULL;
}
bool ListEmpty(ADTList *L)
{
if(L->next)
return true;
else
return false;
}
long ListLength(ADTList *L)
{
long length=0;
ADTList *p;
p=L->next;
while(p)
{
length++;
p=p->next;
}
return length;
}
bool GetElem(ADTList *L,long index,ElemType& e)
{
if(index<1 || index>ListLength(L))
return false;
long i;
ADTList *p=L;
for(i=1;i<=index;i++)
p=p->next;
e=p->Elem;
return true;
}
int compare(ElemType elem1,ElemType elem2)
{
if(elem1==elem2)
return 0;
else if(elem1>elem2)
return 1;
else
return -1;
}
long LocateElem(ADTList *L,ElemType e)
{
ADTList *p;
long locate=0;
p=L->next;
while(p)
{
locate++;
if(compare(e,p->Elem)==0)
{
return locate;
}
p=p->next;
}
return 0;
}
bool PriorElem(ADTList *L,ElemType cur_e,ElemType& pre_e)
{
ADTList *p;
long location;
location=LocateElem(L,cur_e);
if(location<=1)
return false;
p=L->next;
location--;
while(location)
{
location--;
p=p->next;
}
pre_e=p->Elem;
return true;
}
bool NextElem(ADTList *L,ElemType cur_e,ElemType& next_e)
{
ADTList *p;
long location;
location=LocateElem(L,cur_e);
if(location>=ListLength(L)-1)
return false;
location++;
while(location)
{
location--;
p=p->next;
}
next_e=p->Elem;
return true;
}
bool ListInsert(ADTList *L,long index,ElemType e)
{
if(index<1 || index>ListLength(L)+1)
return false;
ADTList *p,*q;
long i;
if((q=new(ADTList))==NULL)
return false;
q->Elem=e;
q->next=NULL;
p=L;
for(i=1;i p=p->next;
q->next=p->next;
p->next=q;
return true;
}
bool ListDelete(ADTList *L,long index,ElemType& e)
{
if(index<1 || index>ListLength(L))
return false;
long i;
ADTList *p,*q;
p=L;
for(i=1;i p=p->next;
e=p->next->Elem;
q=p->next;
p->next=p->next->next;
delete(q);
return true;
}
bool visit(ElemType elem)
{
cout< return true;
}
bool ListTraverse(ADTList *L)
{
ADTList *p;
p=L->next;
while(p)
{
if(!visit(p->Elem))
return false;
p=p->next;
}
return true;
}