#include<stdio.h>
#include<stdlib.h>
#define LISTCREMENT 12
#define OK 1
#define ERROR -1
#define OVERFLOW -1
#define TRUE 1
#define FALSE -1
typedef int Status;
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}*Link,*Position;
typedef struct
{
Link head,tail; //·Ö±ðÖ¸ÏòÏßÐÔÁ´±íµÄÍ·½áµãºÍβ½áµã
int len;
}LinkList,*link;
Status InitList(link &L)
{
L=(link)malloc(sizeof(LinkList));
L->head=NULL;
L->len=0;
return OK;
}
Status DestroyList(link &L)
{
if(!L) return ERROR;
free(L);
return OK;
}
Status CreatList(link &L,ElemType a[],int n)
{
Link p;
for(int i=n;i>0;--i)
{
p=(Link)malloc(sizeof(LNode));
if(i==n) L->tail=p;
p->data=a[i-1];
p->next=L->head;
L->head=p;
}
L->len=n;
return OK;
}
Status prt(link &L)
{
Link p;
p=L->head;
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->next;
}
return OK;
}
Status InsFirst(link h,Link s)
{
s->next=h->head;
h->head=s;
++h->len;
return OK;
}
Status DelFirst(link h,Link &q) //ɾ³ýÁ´±íµÄµÚÒ»¸ö½áµã£¬²¢ÓÃq·µ»ØµÚÒ»¸ö½áµã
{
q=h->head;
h->head=h->head->next;
--h->len;
return OK;
}
Status Append(link &L,link s) //½«Á½¸öÁ´±íÁ¬½ÓÆðÀ´£¬LµÄβ½Ó×ÅsµÄÍ·
{
L->tail->next=s->head;
L->tail=s->tail;
L->len+=s->len;
return OK;
}
Status Remove(link &L,Link &q)
{
Link p;
p=L->head;
q=L->tail;
while(p->next->next!=NULL)
p=p->next;
p->next=NULL;
--L->len;
return OK;
}
Status InsBefore(link L,Link &p,Link s)
{
Link q=L->head;
while(q->next!=p)
q=q->next;
if(p==L->head)
{
InsFirst(L,s);
}
else
{
s->next=p;
q->next=s;
++L->len;
}
return OK;
}
Status InsAfter(link L,Link &p,Link s)
{
s->next=p->next;
if(p->next==NULL)
s->next=NULL;
else
s->next=p->next;
p->next=s;
++L->len;
return OK;
}
Status SetCurElem(Link &p,ElemType e)
{
p->data=e;
return OK;
}
ElemType GetCurElem(Link p)
{
return p->data;
}
Status ListEmpty(link L)
{
if(!L->head) return FALSE;
return TRUE;
}
int ListLength(link L)
{
return L->len;
}
Position GetHead(link L)
{
return L->head;
}
Position GetLast(link L)
{
return L->tail;
}
Position PriorPos(link L,Link p)
{
Link q;
q=L->head;
while(q->next!=p)
q=q->next;
return q;
}
Position NextPos(link L,Link p)
{
return p->next;
}
Status LocatePos(link L,int i,Link &p)
{
Link q=L->head;
for(int n=0;n<i;++n)
q=q->next;
if(q!=p) return FALSE;
else return OK;
}
Position LocateElem(link L,ElemType e,Status(*compare)(ElemType,ElemType))
{
int z=0;
int i=0;
Link p=L->head;
while(i<L->len)
{
z=compare(p->data,e);
p=p->next;
++i;
if(z==1) break;
if(i+1==L->len) return NULL;
}
return p;
}
Status equal(int x,int y)
{
if(x==y) return 1;
return 0;
}
Status ListTraverse(link L,Status(*visit)(ElemType &x))
{
Link p=L->head;
for(int i=0;i<L->len;++i)
{
if(!visit(p->data))
return ERROR;
p=p->next;
}
return OK;
}
Status zero(ElemType &x)
{
x=0;
return OK;
}
void main()
{
Status InitList(link &L);
Status DestroyList(link &L);
Status CreatList(link &L,ElemType a[],int n);
Status prt(link &L);
Status InsFirst(link h,Link s);
Status DelFirst(link h,Link &q);
Status Append(link &L,link s);
Status Remove(link &L,Link &q);
Status InsBefore(link L,Link &p,Link s);
Status InsAfter(link L,Link &p,Link s);
Status SetCurElem(Link &p,ElemType e);
ElemType GetCurElem(Link p);
Status ListEmpty(link L);
int ListLength(link L);
Position GetHead(link L);
Position GetLast(link l);
Position PriorPos(link L,Link p);
Position NextPos(link L,Link p);
Status LocatePos(link L,int i,Link &p);
Position LocateElem(link l,ElemType e,Status(*compare)(ElemType,ElemType));
Status equal(int x,int y);
Status ListTraverse(link L,Status(*visit)(ElemType &x));
Status zero(ElemType &x);
link L; //linkÀàÐ͵ÄÖ¸Õ룬ָÏòLinkList
InitList(L);
//DestroyList(L);
ElemType a[5]={1,2,3,4,5};
CreatList(L,a,5);
LNode newL; //нáµã£¬ÓÃÓÚ²âÊÔInsFirst
newL.data=100; //нáµãdataÊý¾Ý
Link s;
s=&newL;
InsFirst(L,s);
Link q; //ÐÂÖ¸ÏòLNodeµÄÖ¸Õ룬ÓÃÓÚ²âÊÔDelFitst
DelFirst(L,q);
printf("ɾ³ýµÄµÚÒ»¸ö½áµãµÄdata:%d\n",q->data);
printf("×îºóÒ»¸ö½áµãµÄdata:%d\n",L->tail->data);
//prt(L);
link L1; //´´½¨Ò»¸öL1È«ÐÂÁ´±í£¬ÓÃÓÚ²âÊÔAppend
InitList(L1);
ElemType b[5]={101,102,103,104,105};
CreatList(L1,b,5);
//Append(L,L1);
//Remove(L,q);
//printf("ɾ³ýµÄ×îºóÒ»¸ö½áµãµÄdata:%d\n",q->data);
Link p;
p=L->head->next->next->next;
//InsBefore(L,p,s);
//InsAfter(L,p,s);
ElemType e=100;
//SetCurElem(p,e);
//printf("%d\n",GetCurElem(p));
//printf("%d\n",ListEmpty(L));
//printf("%d\n",ListLength(L));
//printf("%d\n",GetHead(L)==L->head);
//printf("%d\n",PriorPos(L,p)==L->head);
//printf("%d\n",LocatePos(L,1,p));
//printf("%d\n",LocateElem(L,3,equal)==p);
//printf("%d\n",ListTraverse(L,zero));
prt(L);
}
02-01
5990
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)