《 대화 데이터 구 조 》 의 단일 체인 표.

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

typedef int Status;
typedef int ElemType;

#define MAXSIZE 20
typedef struct 
{
	ElemType data [MAXSIZE];
	int length;
}SqList;

typedef struct Node
{
	ElemType data;
	struct Node *next;
};

typedef struct Node *LinkList;

// e  L  i       
Status GetElem(LinkList L,int i,ElemType *e)
{
	int j;
	LinkList p;
	p= L->next;
	j = 1;
	while(p && j<i)
	{
		p = p->next;
		++j;
	}
	if(!p || j > i)
	{
		return ERROR;
	}
	*e = p->data;
	return OK;
}

// L  i               e,L     1
Status ListInsert(LinkList L,int i,ElemType e)
{
	int j;
	LinkList p,s;
	p = L;
	j = 1;
	while(p && j < i)
	{
		p = p->next;
		++j;
	}
	if(!p || j > i)
	{
		return ERROR;
	}
	s = (LinkList)malloc(sizeof(Node));
	s->data = e;
	s->next = p->next;
	p->next = s;
	return OK;
}

//  L  i   ,  e    ,L     
Status ListDelete(LinkeList L,int i,ElemType e)
{
	int j;
	LinkList p,q;
	p = L;
	j = l;
	while(p->next && j < i)
	{
		p = p->next;
		++j;
	}
	if(!(p-next) || j > i)
	{
		return ERROR;
	}
	q = p->next;
	p->next = q->next;
	*e = q->data;
	free(q);
	return OK;
}

//    n     ,              L(        )
void CreateListHead(LinkList L,int n)
{
	LinkList p;
	int i;
	srand(time(0));
	L = (Node *)malloc(sizeof(Node));
	L->next = NULL;
	for(i=0;i<n;i++)
	{
		p = (LinkList)malloc(sizeof(Node));
		p->data = rand()%100 + 1;
		p->next = L->next;
		L->next = p;
	}
}

//    n     ,             L(          )
void CreateListTail(LinkList L,int n)
{
	LinkList p,r;
	int i;
	srand(time(0));
	L = (LinkList)malloc(sizeof(Node));
	r = L;
	for(i=0;i<n;i++)
	{
		p =(Node *)malloc(sizeof(Node));
		p->data = rand()%100 + 1;
		r->next = p;
		r = p;
	}
	r->next = NULL;
}

//     ,     ,      
Status ClearList(LinkList L)
{
	LinkList p,q;
	p = L->next;
	while(p)
	{
		q = p->next;
		free(p);
		p=q;
	}
	L->next = NULL;
	return OK;
}

좋은 웹페이지 즐겨찾기