1. 程式人生 > >資料結構之鏈式表的實現--單鏈表(C語言)

資料結構之鏈式表的實現--單鏈表(C語言)

學習參考: 嚴蔚敏: 《資料結構-C語言版》

基本操作:

  1. 單鏈表的建立
  2. 新增結點(頭插法)
  3. 新增結點(尾插法)
  4. 單鏈表的輸出
  5. 單鏈表的修改
  6. 單鏈表的插入
  7. 單鏈表的刪除
  8. 單鏈表按序號查詢
  9. 單鏈表按值查詢 
  10. 單鏈表銷燬  

程式碼實現

鏈式表結點定義:

typedef struct node
{
	int data;
	struct node* next;
}Node,* pNode;

鏈式表定義:

typedef struct 
{
	int len;
	pNode node;
}LinkList,* pList;

單鏈表初始化:

int initList(pList list)
{
	if(!list)
		return -1;
	list->len = 0;
	list->node = NULL;
	return 1;
}

新增結點(頭插法)的實現:

int addDataHead(pList list, int data)
{
	pNode pre =NULL, p = NULL;
	if(!list)
		return 0;
	pre = list->node;
	p = (pNode) malloc(sizeof(int));
	p->data = data;
	p->next = pre;
	list->node = p;
	list->len ++;
	return 1;
} 

 新增結點(尾插法)的實現:

int addDataTail(pList list, int data)
{
	pNode pre = NULL, p = NULL;
	if(!list->node)
		return 0;
	p = list->node;
	while(p)
	{
		pre = p;
		p=p->next;
	}
	p = (pNode)malloc(sizeof(Node));
	if(!p)
		return 0;
	p->next = NULL;
	p->data = data;
	if(!pre)
		list->node = p;
	else
		pre->next = p;
	list->len ++;
	return 1;
}

單鏈表的輸出的實現:

int display(pList list)
{
	pNode p = NULL;
	if(!list->node)
		return 0;
	p = list->node;
	while(p)
	{
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
	return 1;
}

單鏈表的修改的實現: 

int updata(pList list, int pos, int data)
{
	pNode p = NULL;
	int count = 0;
	if(!list || !list->node || pos<1)
		return 0;
	p = list->node;
	while (p)
	{
		count++;
		if(count >= pos)
			break;
		p = p->next;
	}
	if(count >= pos)
		p->data = data;
	return 1;
}

單鏈表的插入實現:

int insertData(pList list, int pos, int data)
{
	pNode q = NULL, p = NULL;
	int count = 0;
	if(!list || pos<1 || !list->node)
		return 0;
	p = list->node;
	while (count<pos)
	{
		count++;
		if(count >= pos)
			break;
		p = p->next;
	}
	q = (pNode) malloc(sizeof(Node));
	q->next = q->next;
	q->data = data;
	q->next = q;
	list->len ++;
	return 1;
}

單鏈表的刪除程式碼實現:

int deleteData(pList list, int pos, int* e)
{
	pNode pre = NULL, p = NULL;
	int count = 0;
	if(!list || pos<1 || pos>list->len || !list->node)
		return 0;
	p = list->node;
	while(count<pos-1)
	{
		count ++;
		pre = p;
		p = p->next;
	}
	if( !pre)
		list->node = p->next;
	else
		pre->next = p->next;
	*e = p->data;
	free(p);
	p = NULL;
	list->len--;
	return 1;
}

單鏈表按序號查詢的程式碼實現:

int getData(pList list, int pos, int* data)
{
	pNode p = NULL;
	int count = 1;
	if(!list || pos<1 || pos >list->len || !list->node)
		return 0;
	p = list->node;
	while( count<pos)
	{
		count++;
		p = p->next;
	}
	*data = p->data;
	return 1;
}

單鏈表按值查詢的實現;

int getLocate(pList list, int data, int* index)
{
	pNode p = NULL;
	int count = 0;
	if(!list || !list->node)
		return 0;
	p = list->node;
	while(p)
	{
		count++;
		if(p->data == data)
			break;
		p = p->next;
	}
	if(!p)
	{
		*index = -1;
		return 0;
	}
	*index = count;
	return 1;
}

單鏈表銷燬的程式碼實現:

int destroy(pList list)
{
	pNode pre = NULL, p = NULL;
	if(!list || !list->node)
		return 0;
	p = list->node;
	while(p)
	{
		pre = p;
		p = pre->next;
		free(pre);
		pre = NULL;
	}
	list->len =0;
	free(list->node);
	list->node =NULL;
	return 1;
}

測試程式碼

#include <stdio.h>
#include "LinkList.h" 
int main()
{
	int val = -1;
	LinkList list;
	initList(&list);
	addDataHead(&list, 1);
	addDataHead(&list, 2);
	addDataHead(&list, 3);
	display(&list);
	addDataTail(&list ,4);
	display(&list);
	updata(&list, 2, 6);
	display(&list);
	deleteData(&list, 1, &val);
	printf("刪除的資料為:%d \n", val);
	display(&list);
	getData(&list, 2, &val);
	printf("第2位的值為:%d \n", val);
	getLocate(&list, 2, &val);
	printf("數字2的位置%d \n", val);
	destroy(&list);
	return 0;
}