【單鏈表】   c語言的單鏈表

單鏈表:是一種線性表,每個節點裏面存儲着下一個節點的指針,把存儲的數據元素連起來。

        本文中應用了C++的引用來傳參數。

wKiom1bw00qj4tk8AAAXPfwFDNQ807.png

存儲結構:

typedef int DataType;
typedef struct SListNode
{
	DataType _data;
	struct SListNode*  _next; //指向下一個節點
}SListNode;

鏈表中節點的建立與銷燬:

SListNode* _BuyNode(DataType x)   //建立節點

{

SListNode* tmp =(SListNode*) malloc(sizeof(SListNode));

tmp->_data = x;

tmp->_next = NULL;

return tmp;

}

void DestorySList(SListNode*& pHead)  //銷燬節點

{

SListNode* cur = pHead;

while (cur)

{

SListNode* tmp = cur;

cur = cur->_next;

free(tmp);

}

pHead = NULL;

}



//////////////////   單鏈表的 增 刪 查   ///////////////////

#include<malloc.h>
#include<assert.h>

typedef int DataType;
typedef struct SListNode
{
	DataType _data;
	struct SListNode*  _next; //指向下一個節點
}SListNode;

//void PushBack(SListNode*& pHead, DataType x);
//void PopBack(SListNode*& pHead);
//void PushFront(SListNode*& pHead, DataType x);
//void PopFront(SListNode*& pHead);
//SListNode* Find(SListNode* pHead, DataType x);
//void Insert(SListNode* pos, DataType x);
//void Erase(SListNode*& pHead, SListNode* pos);
//void DestorySList(SListNode*& pHead);
//void PrintSlist(SListNode* pHead);
    
 SListNode* _BuyNode(DataType x)   //建立節點
{
	SListNode* tmp =(SListNode*) malloc(sizeof(SListNode));
	tmp->_data = x;
	tmp->_next = NULL;
	return tmp;
}    
void DestorySList(SListNode*& pHead)  //銷燬節點
{
	SListNode* cur = pHead;
	while (cur)
	{
		SListNode* tmp = cur;
		cur = cur->_next;
		free(tmp);
	}
	pHead = NULL;
}
void PrintSlist(SListNode* pHead)
{
	SListNode* cur = pHead;
	while (cur)
	{
		printf("%d->", cur->_data);
		cur = cur->_next;
	}
	printf("NULL\n");
}


void PushBack(SListNode*& pHead, DataType x)  //尾插
{
	//1.空 2.不爲空
	if (pHead == NULL)
	{
		pHead = _BuyNode(x);
	}
	else
	{  //找尾節點
		SListNode* tail = pHead;
		while (tail->_next)
		{
			tail = tail->_next;
		}
		tail->_next = _BuyNode(x);
	}
}

void PopBack(SListNode*& pHead)
{
	//空;一個節點;兩個及以上節點
	if (pHead == NULL)
	{
		return;
	}
	else if (pHead->_next == NULL)
	{
		free(pHead);
		pHead = NULL;
	}
	else
	{
		SListNode* tail = pHead;
		SListNode* prev = NULL;
		while (tail->_next)
		{
		    prev = tail;
			tail = tail->_next;
		}
		free(tail);
		prev->_next = NULL;
	}
}
void PushFront(SListNode*& pHead, DataType x)  //頭插
{
	if (pHead == NULL)
		pHead = _BuyNode(x);
	else
	{
		SListNode* tmp = _BuyNode(x);
		tmp->_next = pHead;
		pHead = tmp;
	}
}
void PopFront(SListNode*& pHead)
{
	if (pHead== NULL)
		return;
	else if (pHead->_next  == NULL)
	{
		free(pHead);
		pHead = NULL;
	}
	else
	{
		SListNode*tmp = pHead;
		pHead = pHead->_next;

		free(tmp);
	}
}

SListNode* Find(SListNode* pHead, DataType x)  //查
{
	SListNode* cur = pHead;
	while (cur)
	{
		if (cur->_data == x)
			return cur;
		cur = cur->_next;
	}
	printf("鏈表中無此數據\n");
	return NULL;
}
void Insert(SListNode* pos, DataType x)  //增加節點
{
	assert(pos);
	SListNode* tmp = _BuyNode(x);
	tmp->_next = pos->_next;
	pos->_next = tmp;
}
void Erase(SListNode*& pHead, SListNode* pos) //刪除
{
	assert(pos);
	if (pos == pHead)
	{
		pHead = pHead->_next;
		free(pos);
		return; 
	}

	SListNode* prev = pHead;
	while (prev)
	{
		if (prev->_next == pos)
		{
			prev->_next = pos->_next;
			free(pos);
			break;
		}
		prev = prev->_next;
	}
}


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