LinkList(链式存储结构)

#include
#define MAXSIZE 100
typedef int ElemType
using namespace std;

typedef struct LNode
{
ElemType data;
struct LNode *next;
}LinkList;

bool InitLinkList(LinkList *L)
{
L=(LinkList *)malloc(sizeof(LinkList));
L->next=NULL;
return true;
}

int Length_of_LinkList(LinkList *L)
{
int i=0;
LinkList *p=L;
while(p->next!=NULL)
{
p=p->next;
i++;
}
return i;
}

bool EmptyLinkList(LinkList *L)
{
if(L->next==NULL)
return true;
else
return false;
}

void OutputLinkList(LinkList *L)
{
LinkList *p=L;
cout<<"The linklist is "<while(p->next!=NULL)
{
cout<data<<" ";
p=p->next;
}
cout<}
LinkList Create1LinkList(LinkList *L,ElemType a[MAXSIZE],int n)
{
LinkList *s;
L=(LinkList *)malloc(sizeof(LinkList));
L->next=NULL;
for(int i=0;i{
s=(LinkList *)malloc(sizeof(LinkList));
s->data=a[i];
s->next=L->next;
L->next=s;
}
return L;

}

LinkList Create2LinkList(LinkList *L,ElemType a[MAXSIZE],int n)
{
LinkList *s,*r;
L=(LinkList *)malloc(sizeof(LinkList));
r=L;
for(int i=0;i{
s=(LinkList)malloc(sizeof(LinkList));
s->data=a[i];
r->next=s;
r=s;
}
r->next=NULL;
return L;
}

void GetElementFromLinkList(LinkList *L,int i)
{
int j=0;
LinkList *p=L;
while(jnext!=NULL)
{
p=p->next;
j++;
}
if(p->next==NULL)
cout<<"Can't be found!"<else
cout<<"The element is "<data<}

void LocateLinkList(LinkList *L,ElemType x)
{
int i=0;
LinkList *p=L;
while(p->data!=x&&p->next!=NULL)
{
p=p->next;
i++;
}
if(p-next==NULL)
cout<<"No this element!"<else
cout<<"The element is located on "i<}

LinkList InsertLinkList(LinkList *L,ElemType x,int i)
{
LinkList *s,*p;
p=L;
int j=0;
while(jnext!=NULL)
{
p=p->next;
j++;
}
if(p->next==NULL)
cout<<"Insert failed!"<else
{
s=(LinkList *)malloc(sizeof(LinkList));
s->data=x;
s->next=p->next;
p->next=s;
}
cout<<"Insert success!"<return L;

}

LinkList DeleteLinkList(LinkList *L,int i)
{
LinkList *p,*q;
int j=0;p=L;
while(jnext!=NULL)
{
p=p->next;
j++;
}
if(p->next==NULL)
cout<<"Delete failed!"<else
{
q=p->next;
p-next=q->next;
free(p);
cout<<"Delete success!"<}
return L;
}

LinkList DestoryLinkList(LinkList *L)
{
LinkList *p,*q;
p=L; q=p->next;
while(q!=NULL)
{
free(p);
p=q;
q=p->next;

}
free(p);
cout<<"Destory success!"<return L;
}
void Menu()
{
cout<<" Please choose "<cout<<"1. InitLinkList"<cout<<"2. Create1LinkList"<cout<<"3. Create2LinkList"<cout<<"4. Length_of_LinkList"<cout<<"5. EmptyLinkList"<cout<<"6. OutputLinkList"<cout<<"7. GetElementFromLinkList"<cout<<"8. LocateLinkList"<cout<<"9. InsertLinkList"<cout<<"10. DeleteLinkList"<cout<<"11. DestoryLinkList"<cout<<"others. exit"<}

int main()
{
Menu()
int i,n,x;ElemType a[MAXSIZE];
cin>>i;
LinkList L;
switch(i)
{
case 1: InitLinkList(L);break;
case 2:
cout<<"please input the number of the linklist "<cin>>n;
cout<<"please input the element of the linklist "<for(int j=0;jcin>>a[j];
Create1LinkList(L,a,n);break;
case 3:
cout<<"please input the number of the linklist "<cin>>n;
cout<<"please input the element of the linklist "<for(int j=0;jcin>>a[j];
Create2LinkList(L,a,n);break;
case 4: cout<<"Length is "<case 5: if(EmptyLinkList(L))
cout<<"The linklist is empty !"<else
cout<<"The linklist is not empty !"<;break;
case 6: OutputLinkList(L);break;
case 7:
cout<<"please input the location "<cin>>i;
GetElementFromLinkList(L,i);break;
case 8:
cout<<"please input the element you want to find "<cin>>x;
LocateLinkList(L,x);break;
case 9:
cout<<"please input the element and its location "<cin>>x>>i;
InsertLinkList(L,x,i);break;
case 10: cout<<"please input the element's location "<cin>>i;
DeleteLinkList(L,i);break;
case 11: DestoryLinkList(L);break;
default :exit(0);
}
return 0;
}

相关文档
最新文档