#include<stdio.h>
#include<stdlib.h>
#define ERROR 0
#define OK 1
typedef int ElemType;
typedef int Status;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
Status InitList_L(LinkList &L)
{
L=(LinkList)malloc(sizeof(LNode));
if(!L)
return ERROR;
L->next=L;
}
Status ClearList_L(LinkList &L)
{
struct LNode *p,*q;
if(L->next==L)
return OK;
p=L->next;
while(p!=L)
{
q=p->next;
free(p);
p=q;
}
L->next=L;
}
Status DestroyList_L(LinkList &L)
{
struct LNode *p,*q;
if(L==NULL)
return OK;
p=L->next;
while(p!=L)
{
q=p->next;
free(p);
p=q;
}
L->next=L;
free(L);
L=0;
return OK;
}
Status CreatList_LT(LinkList &L,int n)
{
L=(LinkList)malloc(sizeof(sizeof(LNode)));
L->next=L;
LinkList s=L;
for(int i=0;i<n;i++)
{
LinkList p=(LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
s->next=p;
s=p;
}
s->next=L;
return OK;
}
Status CreatList_LH(LinkList &L,int n)
{
L=(LinkList)malloc(sizeof(LNode));
L->next=L;
for(int i=n;i>0;i--)
{
LinkList p=(LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next=L->next;
L->next=p;
}
return OK;
}
bool ListEmpty_L(LinkList L)
{
if(L->next==L)
return true;
return false;//return(L->next==L)
}
Status ListLength_L(LinkList L)
{
int i=0;
LinkList p=L->next;
while(p!=L)
{
i++;
p=p->next;
}
return i;
}
Status GetElem_L(LinkList L,int i,ElemType &e)
{
LinkList p=L->next;
int j=1;
while(p!=L&&j<i)
{
p=p->next;
j++;
}
if(j>i||p==L)
return ERROR;
e=p->data;
return OK;
}
bool compare(ElemType e1,ElemType e2)
{
if(e1==e2)
return true;
return false;
}
Status LocateElem_L(LinkList L,ElemType e,bool(*compare)(ElemType e1,ElemType e2))
{
int i=0;
LinkList p=L->next;
while(p!=L)
{
i++;
if(compare(p->data,e))
return i;
p=p->next;
}
return OK;
}
Status PriorElem_L(LinkList L,ElemType cur_e,ElemType &pre_e)
{
LinkList q,p=L->next;
while(p->next!=L)
{
q=p->next;
if(q->data==cur_e)
{
pre_e=p->data;
return OK;
}
p=q;
}
return ERROR;
}
Status NextElem_L(LinkList L,ElemType cur_e,ElemType &next_e)
{
LinkList p=L->next;
while(p->next!=L)
{
if(p->data==cur_e)
{
next_e=p->next->data;
return OK;
}
p=p->next;
}
return ERROR;
}
Status ListInsert_L(LinkList &L,int i,ElemType e)
{
LinkList p=L->next;
int j=1;
while(p!=L&&j<i-1)
{
p=p->next;
j++;
}
if(p==L||j>i-1)
return ERROR;
LinkList s=(LinkList)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;
return OK;
}
Status ListDelete_L(LinkList &L,int i,ElemType &e)
{
LinkList p=L;
int j=0;
while(p->next!=L&&j<i-1)
{
p=p->next;
j++;
}
if(p->next==L||j>i-1)
return ERROR;
LinkList q=p->next;
p->next=q->next;
e=q->data;
free(q);
return OK;
}
Status ListTraverse(LinkList L,Status(*visit)(ElemType))
{
LinkList p=L->next;
while(p!=L)
{
visit(p->data);
p=p->next;
}
return OK;
}
Status ListPrint_L(LinkList L)
{
LinkList p=L->next;
while(p!=L)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
int main()
{
LinkList L;
ElemType pre_e,next_e,e;
InitList_L(L);
if(ListEmpty_L(L))
printf("線性表L是空表。\n");
else
printf("線性表L不是空表。\n");
printf("線性表L的長度:%d\n",ListLength_L(L));
CreatList_LT(L,6);
ListPrint_L(L);
if(ListEmpty_L(L))
printf("線性表L是空表。\n");
else
printf("線性表L不是空表。\n");
printf("線性表L的長度:%d\n",ListLength_L(L));
/* ClearList_L(L);
if(ListEmpty_L(L))
printf("線性表L是空表。\n");
else
printf("線性表L不是空表。\n");
printf("線性表L的長度:%d\n",ListLength_L(L));
printf("銷燬前:");
L?printf("L存在。\n"):printf("L不存在。\n");
DestroyList_L(L);
printf("銷燬後:");
L?printf("L存在。\n"):printf("L不存在。\n");*/
int i=4;
GetElem_L(L,i,e);
printf("線性表L中第%d個元素是:%d\n",i,e);
e=8;
printf("線性表L中第一個與%d滿足compae()的數據元素的位序爲:%d\n",e,LocateElem_L(L,e,compare));
PriorElem_L(L,e,pre_e);
printf("線性表L中元素%d的前驅是:%d\n",e,pre_e);
NextElem_L(L,e,next_e);
printf("線性表L中元素%d的後繼是:%d\n",e,next_e);
ListInsert_L(L,5,7);
ListPrint_L(L);
ListDelete_L(L,4,e);
ListPrint_L(L);
printf("刪除的元素爲:%d\n",e);
return 0;
}