考研数据结构复习——线性表(链表)

/**
***@Title   :考研数据结构复习
***@Subject :线性表(链表结构)
***@Author  :lxfhahaha
***@language: C语言
***@Time    : 2018/9/25 20:55
*****/
#include 
#include 
#include 
#define Inital_Size 50
#define MAX_SIZE 100
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

typedef int Bool;
typedef int ElemType;
typedef int Status;

//单链表节点类型
//LNode是普通节点,*LinkList用来定义头节点
typedef struct LNode{ 
	ElemType data;
	struct LNode *next;	
}LNode,*LinkList;

//静态链表类型(在不能使用指针的情况下的替代品)
typedef struct SLNode{ 
	ElemType data;
	int next;	
}component,SLinkList[MAX_SIZE];

//双链表节点类型
typedef struct DuLNode{
	ElemType data;
	struct DuLNode *prior;
	struct DuLNode *next;
}DuLNode,*DoublyLinkList0;

//初始化一个单链表,具有头指针,头结点,头结点->next=NULL,data->length
Status InitList(LinkList *head){
	*head=(LinkList)malloc(sizeof(LNode));
	//*head=(LinkList*)malloc(sizeof(LinkList));
	if(!(*head)){
		printf("System Error!\n");
		exit(0);	
	}
	(*head)->data=0;
	(*head)->next=NULL;
	return OK;
}

//头插法创建一个链表,长度为n
Status CreateListHead(LinkList *list,int n){
	LNode *p,*q;
	int i;
	for(i=0,q=NULL;inext=(*list)->next;
		q->data=i;
		(*list)->next=q;
		(*list)->data++;			
	}
	return OK;
}


//尾插法创建一个链表,长度为n
Status CreateListTail(LinkList *list,int n){
	LNode *p,*q;
	int i;
	for(i=0,p=*list;idata=i;
		q->next=NULL;
		p->next=q;
		p=p->next;
	}
	return OK;
}

//获得链表长度
int GetLength(LinkList list){
	LNode *p=list;
	int length=0;
	while(p=p->next)
		length++;
	return length;
}


//打印list
Status ListPrint(LinkList list){
	LinkList itr=list;
	if(!itr){
		printf("Error!! Please inital first!!\n");
		exit(0);
	}
	printf("list_length: %d \n",GetLength(list));
	printf("[");
	for(itr=itr->next;itr!=NULL;itr=itr->next){
		printf("%d",itr->data);
		if(itr->next!=NULL)
			printf(",");
	}
	printf("]\n");
	return OK;
}

//获取链表中第i个位置处节点的数据元素
Status GetElem(LinkList list,int i,ElemType *e){
	LNode *p,*q;
	if(!list){
		printf("Error!! Please inital first!!\n");
		exit(0);
	}
	if(i<1||i>GetLength(list)){
		printf("index out\n");
		return ERROR;
	}
	for(p=list;i>0;p=p->next,i--);
	*e=p->data;
	return OK;
}

// 在链表的指定位置(第i个节点)插入一个节点
Status InsertList(LinkList *list, int i, ElemType data)
{
	LNode *n_node,*pri,*itr;
	if(!(*list)){
		printf("Error!! Please inital first!!\n");
		exit(0);
	}
	if(i<1||i>GetLength(*list)+1){
		printf("index out \n");
		return ERROR;
	}
	for(itr=(*list)->next,pri=*list;i>1;i--){
		pri=itr;
		itr=itr->next;
	}		
	n_node=(LNode*)malloc(sizeof(LNode));
	if(!n_node){
		printf("Malloc Error!\n");
		exit(0);
	}
	n_node->data=data;
	pri->next=n_node;
	n_node->next=itr;
	return OK;
}

//给链表追加一个节点,在最末尾处增加
Status InsertListTail(LinkList *list, ElemType data){
	LNode *n_node,*pri,*itr;
	if(!(*list)){
		printf("Error!! Please inital first!!\n");
		exit(0);
	}
	for(itr=*list;itr->next!=NULL;itr=itr->next);
	n_node=(LNode*)malloc(sizeof(LNode));
	if(!n_node){
		printf("Malloc Error!\n");
		exit(0);
	}
	n_node->data=data;
	n_node->next=NULL;
	itr->next=n_node;
	return OK;
}
 
//删除指定位置(第i个节点)处的节点
Status DeleteList(LinkList *list, int i, ElemType *data){
	LNode *n_node,*pri,*itr;
	if(!(*list)){
		printf("Error!! Please inital first!!\n");
		exit(0);
	}
	if(i<1||i>GetLength(*list)){
		printf("index out\n");
		return ERROR;
	}
	for(pri=*list,itr=pri->next;i>1;i--){
		itr=itr->next;
		pri=pri->next;
	}
	pri->next=itr->next;
	*data=itr->data;
	free(itr);
	return OK;
}

// 清空整个链表
Status ClearList(LinkList *list){
	LNode *p,*q;
	q=(LNode*)malloc(sizeof(LNode));
	if(!q){
		printf("Malloc Error!\n");
		exit(0);
	}
	for(p=*list;p->next!=NULL;){
		q=p->next;
		p->next=q->next;	
		free(q);
	}
	p->data=0;
	p->next=NULL;
	return OK;
}
int main()
{
	
	LinkList list;
	ElemType e;
	int i,j;
	InitList(&list);
   	CreateListTail(&list, 10);
	ListPrint(list);
	printf("length:%d\n",GetLength(list));
	GetElem(list,5,&e);
	printf("Get %d :%d\n",5,e);
	InsertList(&list, 7, 80);
	ListPrint(list);
	InsertListTail(&list,9999);
	ListPrint(list);
	DeleteList(&list,1,&j);
	ListPrint(list);
	
	DeleteList(&list,10,&j);
	
	ListPrint(list);
	ClearList(&list);
	ListPrint(list);
	
	return 0;	
}

考研数据结构复习——线性表(链表)_第1张图片

你可能感兴趣的:(数据结构)