仅供参考!
#include
#include
#include
using namespace std;
#define ok 1;
#define error 0;
typedef int Status;
typedef int ElemType;
typedef struct List{
ElemType elem;
struct List* next;
}List;
List* Initial()
{
List *p,*head;
head=(List*)malloc(sizeof(List));
head->next=NULL;
return head;
}
Status Destroy( List *head )
{//销毁线性表即将链表中的所有元素释放
List *p;
while( head )
{
p=head->next;
free(head);
head=p;
}
return ok;
}
Status Clear( List *head )
{//清空线性表即保留头指针,释放其余元素
List *p,*pre;
pre=head->next;
while( pre!=NULL )
{
p=pre;
pre=pre->next;
free(p);
}
head->next=NULL;
return ok;
}
int ListLength( List *head )
{
int count=0;
List *p;
p=head;
while( p->next!=NULL )
{
p=p->next;
count++;
}
return count;
}
int GetelemByIndex( List *head,int x )
{
List *p;
int count=1,flag=0;
p=head;
while( p!=NULL )
{
p=p->next;
if( count==x )
{
flag=1;
count=p->elem;
break;
}
count++;
}
if( flag==0 )
count=-1;
return count;
}
int GetIndexByelem( List *head,int x )
{
List *p;
int index=-1,flag=0;
//index用来以循环的次数来标记元素的位置,flag用来标记找没找到该元素
p=head;
while( p->next!=NULL )
{
p=p->next;
index++;
if( p->elem==x )
{
flag=1;
break;
}
}
if( flag==0 )
//若未找到该元素,则继续令index=-1
index=-1;
return index+1;
}
int Getfront( List *head,int x )
{
List *p;
int Elem=-1,flag=0;
//Elem来记录前驱元素,flag用来标记找没找到该元素
p=head;
while( p->next!=NULL )
{
p=p->next;
if( p->elem==x )
{
flag=1;
break;
}
Elem=p->elem;
}
if( flag==0 )
//若未找到该元素,则令Elem=-2,Elem=-1为该元素无前驱元素
Elem=-2;
return Elem;
}
int Getbehind( List *head, int x )
{
List *p;
int Elem=-1,flag=0;
//Elem来记录后继元素,flag用来标记找没找到该元素
p=head->next;
while( p->next!=NULL )
{
if( p->elem==x )
{
flag=1;
Elem=p->next->elem;
break;
}
p=p->next;
}
if( flag==0 )
//若未找到该元素,则令Elem=-2,Elem=-1为该元素无后继元素
{
Elem=-2;
return Elem;
}
if( p->elem!=x )
{
Elem=-1;
return Elem;
}
return Elem;
}
Status Insert( List *head,int Il,int El )
{
List *p,*pre;
int count=0;
pre=head;
while( pre!=NULL )
{
count++;
if( count==Il )
break;
pre=pre->next;
}
p=(List*)malloc(sizeof(List));
p->next=pre->next;
p->elem=El;
pre->next=p;
return ok;
}
Status Delete( List *head,int Il )
{
List *p,*pre;
int count=0;
pre=head;
while( pre!=NULL )
{
count++;
if( count==Il )
break;
pre=pre->next;
}
p=pre->next;
pre->next=p->next;
free(p);
return ok;
}
Status Output( List *head )
{
List *p;
p=head->next;
while( p!=NULL )
{
cout<elem<<" ";
p=p->next;
}
cout<next=NULL;
pre=head;
while( 1 )
{
cin>>x;
if( x==-1 )
break;
p=(List*)malloc(sizeof(List));
p->elem=x;
p->next=NULL;
pre->next=p;
pre=p;
}
return head;
}
Status Deserialized( List *head )
{
List *pre,*p,*q;
pre=head;
p=head->next;//找到"第一个"节点 ,并记录
while( p->next!=NULL )
{
q=p->next;
p->next=q->next;
q->next=pre->next;
pre->next=q;
}
return ok;
}
int main()
{
int n,x; //x为要进行操作的位置
int Ll; //即Listlength,用来记录通过 Listlength()方法调用以后的链表的长度
int El; //即Elem,用来记录通过 GetelemByIndex()方法调用以后得到的元素值
int Il; //即存在元素的位置,若链表中存在则返回位置
List *head=NULL;
while( 1 )
{
cout<<"可执行的操作有:"<>n;
if( n<1 || n>13 )
cout<<"请输入合理的功能选项!"<next==NULL )
{
cout<<"链表为空,不用进行清空操作!"<next==NULL )
{
cout<<"链表为空,长度为0!"<next==NULL )
{
cout<<"链表为空,不能进行查找操作!"<>x;
if( x<1 || x>ListLength(head) )
{
cout<<"输入的位置不在线性表的范围之内!"<next==NULL )
{
cout<<"链表为空,不能进行查找操作!"<>x;
Il=GetIndexByelem(head,x);
if( Il>0 )
cout<<"该元素在整个链表的第"<next==NULL )
{
cout<<"链表为空,不能进行查找操作!"<>x;
El=Getfront(head,x);
if( El==-1 )
cout<<"该元素没有前驱元素!"<next==NULL )
{
cout<<"链表为空,不能进行查找操作!"<>x;
El=Getbehind(head,x);
if( El==-1 )
cout<<"该元素没有后继元素!"<>Il;
if( Il<1 || Il>ListLength(head)+1 )
{
cout<<"输入的位置有误,请重新操作!"<>El;
Insert(head,Il,El);
cout<<"插入成功!"<next==NULL )
{
cout<<"链表为空,不能进行删除操作!"<>Il;
if( Il<1 || Il>ListLength(head) )
{
cout<<"输入的位置有误,请重新操作!"<next==NULL )
{
cout<<"链表为空,没有可以输出的元素!"<next==NULL )
{
cout<<"链表为空,无法实现链表的逆序存放!"<