#include<iostream>
#include<stdlib.h>
#include<cstdlib>
#include<math.h>
#define ElemType int
#define ERROR 0
#define OK 1
using namespace std;
typedef struct Lnode
{
ElemType element;
struct Lnode* next;
}Lnode, *List;
class LList
{
protected:
List L;
public:
int Inite_Link();//初始化链表
int Input_Link();//输入元素
int Refer_Link(int location);//查找元素
int Delete_Link(int location);//删除元素
int Insert_Link(int location, ElemType elem);//插入元素
int Clear_Link();//清空链表
int Show_Link();//输出链表
int Transfer_Link(int n);//转化链表
};
int LList::Inite_Link()//初始化链表
{
L = NULL;
if(L)
exit(OVERFLOW);
return OK;
}
int LList::Input_Link()//输入元素
{
List p, q;
ElemType e;
p = (List) malloc (sizeof(Lnode));
L = p;
p->next = NULL;
int length;
cout<<"您要输入的元素个数:";
cin>>length;
cout<<"请依次输入元素:";
for(int i=0; i<length; i++)
{
q = (List) malloc (sizeof(Lnode));
p->next = q;
p = q;
cin>>e;
p->element = e;
}
p->next = NULL;
Show_Link();
return OK;
}
int LList::Refer_Link(int location)//查找元素
{
int i = 0;
List p;
p = L;
if(!p->next)
{
cout<<"空链表一个!"<<endl;
return ERROR;
}
while(p&&i<location)
{
p = p->next;
i++;
}
if(!p||location<i)
{
cout<<"输入有误"<<endl;
return ERROR;
}
cout<<"第"<<location<<"元素为"<<p->element<<endl;
return OK;
}
int LList::Delete_Link(int location)//删除元素
{
int i = 0;
List p, q;
p = L;
if(!p->next)
{
cout<<"空链表一个!"<<endl;
return ERROR;
}
while(p->next&&i<location-1)
{
p = p->next;
i++;
}
if(!p->next||location<i)
{
cout<<"输入有误"<<endl;
return ERROR;
}
q = p->next;
p->next = q->next;
free(q);
Show_Link();
return OK;
}
int LList::Insert_Link(int location, ElemType elem)//插入元素
{
int i = 0;
List p, q;
p = L;
while(p->next&&i<location-1)
{
p = p->next;
i++;
}
if(location>i+1)
{
cout<<"输入有误!"<<endl;
return ERROR;
}else
if(!p->next)
{
q = (List)malloc(sizeof(Lnode));
q->element = elem;
p->next = q;
q->next = NULL;
Show_Link();
return OK;
}
else
{
cout<<"qqqq";
q = (List)malloc(sizeof(Lnode));
q->element = elem;
q->next = p->next;
p->next = q;
}
Show_Link();
return OK;
}
int LList::Clear_Link()//清空链表
{
List p, q;
p = L;
if(!p->next)
{
cout<<"链表本为空!"<<endl;
return OK;
}
p = p->next;
L->next = NULL;
while(p)
{
q = p;
p = p->next;
free(q);
}
cout<<"BINGO!"<<endl;
Show_Link();
return OK;
}
int LList::Show_Link()//输出链表
{
List p;
p = L;
if(!p->next)
{
cout<<"链表为空!"<<endl;
return OK;
}
cout<<"链表元素:";
p = p->next;
while(p)
{
cout<<p->element<<" ";
p = p->next;
}
return OK;
}
int Transfer_Link(int n)//转化链表
{
ElemType *e;
e = new ElemType[n];
cout<<"请输入顺序表元素:";
for(int i=0;i<n;i++)
{
cin>>e[i];
}
cout<<"顺序表输出:";
for(int i=0;i<n;i++)
cout<<e[i]<<" ";
cout<<endl;
List p, q, LL;
LL = NULL;
p=(List)malloc(sizeof(Lnode));
LL = p;
for(int j=0;j<n;j++)
{
q=(List)malloc(sizeof(Lnode));
q->element=e[j];
p->next=q;
p=q;
}
p->next=NULL;
p=LL;
p=LL->next;
cout<<"链表输出:";
while(p)
{
cout<<p->element<<" ";
p=p->next;
}
cout<<endl<<endl<<endl;
return OK;
}
int Display()
{
LList L;
int i;
ElemType e;
cout<<"转换..."<<endl;
cout<<"请输入顺序表元素个数:";
cin>>i;
Transfer_Link(i);
cout<<"创建链表ing"<<endl;
L.Inite_Link();
L.Input_Link();
cout<<"创建成功!"<<endl<<endl;
cout<<"请输入你所要查找元素的位置:";
cin>>i;
L.Refer_Link(i);
cout<<endl;
cout<<"请输入你所要删除元素的位置:";
cin>>i;
L.Delete_Link(i);
cout<<endl;
cout<<"请输入你所要插入元素的位置:";
cin>>i;
cout<<"请输入插入元素的值:";
cin>>e;
L.Insert_Link(i,e);
cout<<endl;
cout<<"是否清空链表:(1->是|其他->否):";
cin>>i;
if(i == 1)
L.Clear_Link();
return 0;
}
int main()
{
Display();
}