考研數據結構複習——線性表(鏈表)

/**
***@Title   :考研數據結構複習
***@Subject :線性表(鏈表結構)
***@Author  :lxfhahaha
***@language: C語言
***@Time    : 2018/9/25 20:55
*****/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#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;i<n;i++){
		q=(LNode*)malloc(sizeof(LNode));
		if(!q){
			printf("Malloc Error!\n");
			exit(0);
		}
		q->next=(*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;i<n;i++){
		q=(LNode*)malloc(sizeof(LNode));
		if(!q){
			printf("Malloc Error!\n");
			exit(0);
		}
		q->data=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;	
}

效果截圖

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章