1. 程式人生 > >連結串列的基本操作

連結串列的基本操作

·什麼是連結串列

連結串列是一種物理儲存單元上非連續、非順序的儲存結構資料元素的邏輯順序是通過連結串列中的指標連結次序實現的。連結串列由一系列結點(連結串列中每一個元素稱為結點)組成,結點可以在執行時動態生成。每個結點包括兩個部分:一個是儲存資料元素的資料域,另一個是儲存下一個結點地址的指標域。 

如下圖:·程式碼及其實現

·Linklist.h

#ifndef __LINKLIST_H__ 
#define __LINKLIST_H__ 

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


typedef int DataType;
typedef struct Node Node;
typedef struct Node* pNode;
typedef struct Node List;
typedef struct Node* pList;
struct Node
{
	DataType data;
	struct Node* next;
};

void InitLinkList(pList* pplist);
pNode BuyNode(DataType d);
void DestroyLinkList(pList* pplist);
void PushBack(pList plist, DataType d);
void PopBack(pList* pplist);
void PushFront(pList* pplist, DataType d);
void PopFront(pList* pplist);
pNode Find(pList* pplist, DataType d);
void Print(pList* pplist);

//在指定位置之前插入一個值 
void Insert(pList* pplist, pNode pos, DataType d);
//指定位置刪除 
void Erase(pList* pplist, pNode pos);
void Remove(pList* pplist, DataType d);
void RemoveAll(pList* pplist, DataType d);
void EraseNotTailNode(pNode pos);
void PrintLinkList(pList plist);
int GetListLength(pList plist);
//連結串列面試題 

//1. 逆序列印單項鍊表 
void PrintReverse(pList plist);
void Reverse(pList plist, pList *newplist);

#endif //__LINKLIST_H__ 

·Linklist.c

#include "linklist.h"


void test()
{
	pList* pplist = NULL;
	pList* newpplist = NULL;
	InitLinkList(&pplist);
	PushFront(&pplist, 1);
	PushFront(&pplist, 2);
	PushFront(&pplist, 3);
	PushFront(&pplist, 4);
	Erase(&pplist, Find(&pplist, 3));
	Print(&pplist);
}

int main()
{
	test();
	system("pause");
	return 0;
}

int GetListLength(pList plist)
{
	assert(plist);
	pNode cur = plist;
	int count = 0;
	while (cur)
	{
		cur = cur->next;
		count++;
	}
	return count;
}

void Reverse(pList plist,pList *newplist)
{
	pNode cur = plist;
	while (cur)
	{
		PushFront(newplist, cur->data);
		cur = cur->next;
	}
	Print(newplist);
}

void PrintReverse(pList plist)
{
	pNode cur = plist;
	if (cur->next)
	{
		PrintReverse(cur->next);
		printf("%d->", cur->data);
	}
	else if (cur->next == NULL)
	{
		printf("%d->", cur->data);
	}
}

pNode BuyNode(DataType d)
{
	pNode newNode = (pNode)malloc(sizeof(Node));
	if (NULL == newNode)
	{
		perror("malloc");
		exit(EXIT_FAILURE);
	}
	newNode->data = d;
	newNode->next = NULL;
	return newNode;
}

void InitLinkList(pList* pplist)
{
	assert(pplist);
	*pplist = NULL;
}

void Print(pList* pplist)
{
	pNode cur = *pplist;
	while (cur)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

void PushBack(pList* pplist, DataType d)
{
	assert(pplist);
	pNode newNode = BuyNode(d);
	pNode cur = *pplist;
	if (NULL == *pplist)
	{
		*pplist = newNode;
		return;
	}
	else
	{
		while (cur->next != NULL)
		{
			cur = cur->next;
		}
		cur->next = newNode;
	}
}
void PopBack(pList* pplist)
{
	assert(pplist);
	pNode cur = *pplist;
	pNode pre = NULL;
	while (cur->next != NULL)
	{
		pre = cur;
		cur = cur->next;
	}
	pre->next = NULL;
	free(cur);
}

void PushFront(pList* pplist, DataType d)
{
	assert(pplist);
	pNode newNode = BuyNode(d);
	if (NULL == *pplist)
	{
		*pplist = newNode;
		return;
	}
	else
	{
		newNode->next = *pplist;
		*pplist = newNode;
	}
}

void PopFront(pList* pplist)
{
	assert(pplist);
	pNode cur = *pplist;
	*pplist=cur->next;
	free(cur);
}

pNode Find(pList *pplist, DataType d)
{
	pNode cur = *pplist;
	while (cur)
	{
		if (cur->data == d)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

void Insert(pList* pplist, pNode pos, DataType d)
{
	assert(pplist);
	pNode newNode = BuyNode(d);
	newNode->next = pos->next;
	pos->next = newNode;
}

void Erase(pList* pplist, pNode pos)
{
	assert(pplist);
	pNode cur = pos->next;
	pos->next = cur->next;
	free(cur);
	cur = NULL;
}