关闭

关于单链表的一些基本操作

69人阅读 评论(0) 收藏 举报

1.首先创建一个包含数据和指向该结构体的结构体即链表的节点

#pragma one
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef int DataType;
typedef struct ListNode
{
	DataType _data;
	struct ListNode* _next;
}ListNode;<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
2.链表的基本操作函数

//创造一个存储x的新节点
ListNode* BuyNode(DataType x)
{
	ListNode* newpHead = NULL;
	newpHead = (ListNode*)malloc(sizeof(ListNode));
	assert(newpHead);
	newpHead->_data = x;
	newpHead->_next = NULL;//避免newpHead->_next指向某个未知位置
	return newpHead;
}
//打印链表
void PrintListNode(ListNode* pHead)
{
	ListNode* tmp = pHead;
	while (tmp)
	{
		printf("%d->", tmp->_data);
		tmp = tmp->_next;
	}
	printf("NULL\n");
}
//摧毁链表
void DestroyList(ListNode* &pHead)
{
	ListNode* tmp = pHead;
	if (pHead == NULL)
	{
		printf("ListNode is empty\n");
		free(pHead);
		return;
	}
		while (pHead)
		{
			pHead = pHead->_next;
			free(tmp);
			tmp = pHead;
		}
		pHead = tmp;
		free(pHead);
}
//尾部插入一个节点
void PushBack(ListNode*& pHead, DataType x)
{
	if (pHead == NULL)
	{
		pHead = BuyNode(x);
	}
	else
	{
		ListNode *tail = pHead;
     	while (tail->_next != NULL)
		{
			tail = tail->_next;
		}
		tail->_next = BuyNode(x);
	}
}
//尾部删除一个节点
void PopBack(ListNode*& pHead)
{
	if (pHead == NULL)
	{
		printf("List is empty\n");
		return;
	}
	else if (pHead->_next == NULL)
	{
		free(pHead);
		pHead = NULL;
	}
	else
	{
		ListNode* prevTail = NULL, *tail = pHead;
		while (tail->_next != NULL)
		{
			prevTail = tail;
			tail = tail->_next;
		}
		prevTail->_next = NULL;
		free(tail);
	}
 }
//删除头部节点
void PopFront(ListNode* pHead)
{
	if (NULL == pHead->_next)
	{
		pHead = NULL;
	}
	else
		pHead = pHead->_next;
	return;
}
//寻找一个值为x的节点位置
ListNode* Find(ListNode* pHead, DataType x)
{
	ListNode* pos = pHead;
	while (pHead != NULL)
	{
		if (pHead->_data == x)
		{
			return pos;
		}
		else
		{
			pHead = pHead->_next;
			pos = pHead;
		}
	}
	return NULL;
}
//在链表中部的节点后插入新节点
void Insert(ListNode* pos, DataType x)
{
	assert(pos);
	ListNode* newNode = BuyNode(x);
	ListNode* tmp = pos->_next;
	newNode->_next = tmp;
	pos->_next = newNode;
	
}
//删除某个位置的节点
void Erase(ListNode*& pHead, ListNode* pos)
{
	ListNode* tmp = pHead;
	if (pHead == pos)
	{
		pHead = pHead->_next;
		return;
	}
	while (tmp)
	{
		if (tmp->_next == pos)
		{
			tmp->_next = tmp->_next->_next;
			free(pos);
			break;
		}
		tmp = tmp->_next;
	}
}
//删除链表中值为x的节点
void Remove(ListNode*& pHead, DataType x)
{
	ListNode* tmp = Find(pHead, x);
	Erase(pHead, tmp);
}
//逆置节点
ListNode* Reverse1(ListNode* pHead)
{
	ListNode* NewHead = NULL;
	ListNode* tmp = NULL;
	if (!pHead || !pHead->_next)
		return;
	while (pHead)
	{
		NewHead = pHead->_next;
		pHead->_next = tmp;
		tmp = pHead;
		pHead = NewHead;
	}
	return tmp;
}
//逆置节点
void Reverse(ListNode*& pHead)
{
	ListNode* Next = NULL;
	ListNode* Prev = NULL;
	if (!pHead || !pHead->_next)
       return;
	while (pHead)
	{
		Next = pHead->_next;
		pHead->_next = Prev;
		Prev = pHead;
		pHead = Next;
	}
	pHead = Prev;
}//删除无头单链表的非尾节点
void PopNoheadList(ListNode* pos)
{
	if (!pos&&!pos->_next)
		return;
	pos->_data = pos->_next->_data;
	pos->_next = pos->_next->_next;
}
//在无头单链表的一个非头节点插入一个节点
void PushNohead(ListNode* pos, DataType x)
{
	ListNode* NewNode = BuyNode(x);
	DataType tmp = 0;
	if (!pos)
		return;
	NewNode->_next = pos->_next;
	pos->_next = NewNode;
	tmp = NewNode->_data;
	NewNode->_data = pos->_data;
	pos->_data = tmp;
}
//查找单链表的中间节点,要求只能遍历一次
ListNode* FindMidNode(ListNode* pHead)
{
	ListNode* pos = NULL;
	ListNode* Fast = pHead, *Slow = pHead;
	while (Fast->_next!=NULL)
	{
		if (Fast->_next->_next != NULL)
		{
			Slow = Slow->_next;
			Fast = Fast->_next->_next;
		}
		else
			Fast = Fast->_next;
	}
	pos = Slow;
	return pos;
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1518次
    • 积分:126
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:5篇
    • 译文:0篇
    • 评论:0条