线性表的链式存储结构

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

typedef int Elemtype;

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

typedef struct Node* LinkList;

void InitLinkList(LinkList *L);
void DestoryList(LinkList* L);
void ClearList(LinkList *L);
void HeadCreateList(LinkList*,int);
void TailCreateList(LinkList*,int);
void DisplayList(LinkList);

//void InitLinkList(Node** L){
//	*L = (LinkList)malloc(sizeof(struct Node));
//	(*L)->next = NULL;
//	//alternation 
//}
void InitList(LinkList *L){
	*L = (LinkList)malloc(sizeof(struct Node));
	(*L)->next = NULL;

void DestoryList(LinkList* L){
/*
	LinkList p = *L,q;
	printf("%d %d\n",p,*L); //p == *L == 7279912
	while(p){
		q = p->next;
		free(p);
		p = q;	       //last step p = NULL
	}
	printf("%d %d",p,*L);  //p = 0;*L = 7279912
	free(*L);
	printf("%d ",*L);      //*L = 7279912
analise the reason for error by the result of "printf"
free(p) is release the block of memory pointed by P,
dose not change pointer p itself.  NULL == 0
*/
	LinkList q;
	while(*L){
		q = (*L)->next;
		free(*L);
		*L = q;
	}
}
void ClearList(LinkList *L){
	//ClearList(&L);
//	LinkList p = *L;
//	LinkList q;
//
//	TailCreateList(&L,4);
//	DisplayList(L);
//	ClearList(&L);
//	TailCreateList(&L,4);

	LinkList p = (*L)->next;
	LinkList q;
	while(p){
		q = p -> next;
		free(p);
		p = q;
	}
	(*L)->next = NULL;
	printf("LinkList has been clear!\n");
}
void ListEmpty(LinkList L){
	if(L->next == NULL)
		printf("List empty!\n");
	else 
		printf("Exit Element in List\n");
}
int ListLength(LinkList L){
	LinkList p = L->next;
	int count = 0;
	while(p){
		count+=1;
		p = p->next;
	}
	return count;
}
int GetElem(LinkList L,int index,int *r){
	int length = ListLength(L);
	if(index < 1 || index > length){
		return 0; //failed
	}else{
		int j = 0;
		while(j<index){
			L = L->next;
			j++;
		}
		*r = L->data;
		return 1;
	}
}
void GetPriorElem(LinkList L,int current_elem,int *priorElement){
/*
	LinkList p = L->next;
	LinkList q;
	if(p == NULL){
		printf("List is Empty!\n");
		exit(1);
	}
	if(p->data == current_elem){
		printf("the current element is first!\n");
		exit(1);
	}
	q = p->next;
	while(q && q->data != current_elem){
		p = q;q = q->next;
	}
	if(q == NULL){
		printf("there is no current in List!\n");
		exit(1);
	}
	else
		*priorElement = p->data;
*/
/*
the promise of the following code is that the 
current_element is not the first element of the
list and does not consider the case of the list
for NULL
*/
	LinkList p = L->next; //p point first node
	LinkList q;
	while(p->next){
		q = p->next;
		if(q->data == current_elem){
			*priorElement = p->data;
			break;
		}
		p = q;
	}
	if(p->next == NULL)
		printf("there is no current_element in List\n");
}
void GetNextElem(LinkList L,int current_elem,int* next_elem){
	LinkList p = L->next;
	LinkList q;
	if(p == NULL){
		printf("List is Empty!\n");
		exit(1);
	}
	while(p->next){
		q = p->next;
		if(p->data == current_elem){
			*next_elem = q->data;
			return;
		}
		p = q;
	}
	printf("GetNextElement is Failed!\n");
}
void HeadCreateList(LinkList* L,int n){
	LinkList s;
	int i;
	int e;
	for(i = 1;i <= n; i++){
		printf("enter %d integer: ",i);
		scanf("%d",&e);
		s = (LinkList)malloc(sizeof(struct Node));
		s->data = e;
		s->next = (*L)->next;
		(*L)->next = s;
	}
	printf("\n");
}
void TailCreateList(LinkList* L,int n){
	LinkList tail = *L;
	LinkList s;
	int i;
	int e;
	for(i = 1;i <= n; i++){
		printf("enter %d integer: ",i);
		scanf("%d",&e);
		s = (LinkList)malloc(sizeof(struct Node));
		s->data = e;
		tail->next = s;
		s->next = NULL;
		tail = s;
	}
	printf("\n");
}
void ListInsert(LinkList* L,int index,int e){
/*
//insert element before index
	LinkList q;
	LinkList p = *L;
	LinkList s;
	int count = 0;
	while(p->next){
		q = p->next;
		count += 1;
		if(count == index){
			s = (LinkList)malloc(sizeof(struct Node));
			s -> data = e;
			p->next = s;
			s->next = q;
			break;
		}
		p = q;
	}
	if(p->next == NULL){
		printf("InsertList Index Error!\n");
	}
*/
//options
	LinkList p = *L;
	LinkList s;
	int count = 0;
	while(p && count<index-1){
		p = p->next;
		count += 1;
	}
	if(p){
		s = (LinkList)malloc(sizeof(struct Node));
		s->data = e;
		s->next = p->next;
		p->next = s;
	}
	if(p == NULL){
		printf("InsertList Index Error!\n");
	}
}
void ListDelete(LinkList *L,int index,int* e){
	LinkList p = *L;
	int count = 0;
	while(p && count < index - 1){
		p = p->next;
		count += 1;
	}
	if(p){
		if(p->next){
			*e = p->next->data;
			p->next = p->next->next;
		}
		else
			printf("DeleteList Index Error\n");
	}
	if(p == NULL)
		printf("DeleteList Index Error\n");
}
void DisplayList(LinkList L){
	if(L == NULL){
		printf("List has been destory!\n");
	}else
	{
		LinkList p = L;
		while(p -> next){
			printf("%d ",p->next->data);
			p = p->next;
		}
		printf("\n********displayList executed!\n");
	}
}
void main(){
//	Node L;
//	LinkList L1 = &L;
//	InitLinkList(&L1);

	LinkList L;
	InitList(&L);
	TailCreateList(&L,4);
	DisplayList(L);
	ClearList(&L);
	ListEmpty(L);
	DisplayList(L);
	DestoryList(&L);
	DisplayList(L);

	InitList(&L);
	TailCreateList(&L,5);
	printf("Length of List is: %d\n",ListLength(L));
	DisplayList(L);

	int r0;
	if(GetElem(L,5,&r0)){
		printf("getelement index=5 is : %d\n",r0);
	}
	else
		printf("getelement index=5 is Failed\n");

	if(GetElem(L,6,&r0)){
		printf("getelement index=6 is : %d\n",r0);
	}
	else
		printf("getelement index=6 is Failed\n");

	int r1;
	GetPriorElem(L,3,&r1);
	printf("the result of GetPriorElement(3) is: %d\n",r1);

	int r2;
	GetNextElem(L,3,&r2);
	printf("the result of GetNextElement(3) is: %d\n",r2);
	ListInsert(&L,5,999);
	DisplayList(L);

	int r3;
	ListDelete(&L,2,&r3);
	DisplayList(L);
	printf("execute ListDelete index=2 is %d\n",r3);
}

你可能感兴趣的:(数据结构,链表,C语言)