链表

有关于链表的操作哦 ......

头文件

#pragma once

#include<stdio.h>
#include<assert.h>

typedef int Datatype;
typedef struct ListNode{
	Datatype _data;
	struct ListNode* _pNext;
}ListNode,*pNode;

void listNodeInit(pNode* pHead);//初始化
pNode BuyNode(Datatype data);//新建结点

void listPushBack(pNode* pHead, int data);//尾插
void listPopBack(pNode *pHead);//尾删

void ListPushFront(pNode* pHead, Datatype data);    //头插
void ListPopFront(pNode* pHead);//头删

void ListInsert(pNode* pHead, pNode pos, Datatype data);    // 在pos位置插入值为data的结点
void ListErase(pNode* pHead, pNode pos);    // 删除pos位置的结点

pNode ListFind(pNode pHead, Datatype data); // 在链表中查找值为data的元素,找到后返回值为data的结点
pNode FindNode(pNode pHead, int pos);   //查找已经存在的链表内是否存在第pos位结点
int SListSize(pNode pHead); // 获取链表中结点的个数

int ListEmpty(pNode pHead); //判断链表是否为空
void SListDestroy(pNode* pHead);    // 销毁聊表

void printlist(pNode pHead);//打印

相关代码

void listNodeInit(pNode* pHead)
{
	assert(pHead);
	*pHead = NULL;
}

pNode BuyNode(Datatype data)
{
	pNode NewNode = (pNode)malloc(sizeof(ListNode));

	if (NULL == NewNode)
		return NULL;

	NewNode->_data = data;
	NewNode->_pNext = NULL;
	return NewNode;
}

void listPushBack(pNode* pHead, int data)
{
	assert(pHead);
	if (NULL == *pHead)
	{
		*pHead = BuyNode(data);  
	}
	else
	{
		pNode pCur = *pHead;

		while (pCur->_pNext != NULL)  //找最后一个元素的位置
			pCur = pCur->_pNext;
		
		pCur->_pNext = BuyNode(data);
	}
}

void listPopBack(pNode *pHead) //尾删
{
	pNode Del = *pHead;
	pNode pPre = NULL;

	assert(pHead);
	if (NULL == *pHead)
		return;

	if (NULL == *pHead)
	{
		free(*pHead);
		*pHead = NULL;
	}
	else
	{
		while (Del->_pNext)
		{
			pPre = Del;
			Del = Del->_pNext;
		}

		free(Del);
		pPre->_pNext = NULL;
	}
}

void ListPushFront(pNode* pHead, Datatype data)   //头插
{
	pNode NewNode = NULL;

	assert(pHead);
	if (NULL == *pHead)
	{
		NewNode = BuyNode(data);
		NewNode->_pNext = NULL;
		*pHead = NewNode;
	}
	else
	{
		NewNode = BuyNode(data);
		NewNode->_pNext = *pHead;
		*pHead = NewNode;
	}
}
void ListPopFront(pNode* pHead)//头删
{
	pNode Del = *pHead;

	assert(pHead);
	if (NULL == pHead)
	{
		return;
	}

	if (*pHead)
	{
		free(Del);
		*pHead = NULL;
	}
	else if ((*pHead)->_pNext)
	{
		*pHead = (*pHead)->_pNext;
		free(Del);
		Del = NULL;

	}
}


void ListInsert(pNode* pHead, pNode pos, Datatype data)     // 在pos位置插入值为data的结点
{
	assert(pHead);

	if (NULL == pHead || pos == 0)   //若链表为空或者要在头插,直接头插
		ListPushFront(pHead, data);

	else if (pos->_pNext == NULL)  //尾插
		listPushBack(pHead, data);
	else{
		pNode pCur = NULL;
		pNode NewNode = BuyNode(data);
		Datatype temp;

		NewNode->_pNext = pos->_pNext;
		pos->_pNext = NewNode;

		temp = NewNode->_data;
		NewNode->_data = pos->_data;
		pos->_data = temp;	
	}


}
void ListErase(pNode* pHead, pNode pos)    // 删除pos位置的结点
{
	pNode Del = *pHead;
	pNode pPre = NULL;

	assert(pHead);
	if (NULL == *pHead || NULL == pos)
		return;

	while (Del && Del != pos) //del存在 并且还没找到pos的位置
	{
		pPre = Del;
		Del = Del->_pNext;
	}

	if (Del->_pNext != NULL)
	{
		pPre->_pNext = Del->_pNext;
		free(Del);
		Del = NULL;
	}
	else{
		free(Del);
		pPre->_pNext = NULL;
	}
}


pNode ListFind(pNode pHead, Datatype data) // 在链表中查找值为data的元素,找到后返回值为data的结点
{
	pNode pCur = pHead;

	assert(pHead);
	if (NULL == pHead)
		return NULL;

	while (pCur)
	{
		if (pCur->_data == data)
			return pCur;
		pCur = pCur->_pNext;
	}
	return NULL;

}
pNode FindNode(pNode pHead, int pos)   //查找已经存在的链表内是否存在第pos位结点
{
	pNode pCur = pHead;
	pNode pPre = NULL;

	assert(pHead);
	if (NULL == pHead)
		return NULL;

	while (pCur && pos) 
	{
		pPre = pCur;
		pCur = pCur->_pNext;
		pos--;
	}

	if (pos == 0)
		return pPre;
	return NULL;

}
int SListSize(pNode pHead) // 获取链表中结点的个数
{
	pNode pCur = pHead;
	int count = 0;

	assert(pHead);
	if (NULL == pHead)
		return 0;

	while (pCur)
		count++;

	return count;
}


int ListEmpty(pNode pHead) //判断链表是否为空
{
	if (NULL == pHead)
	{
		printf("list is empty\n");
		return 0;
	}

	printf("list is not empty\n");
	return 1;
}
void SListDestroy(pNode* pHead)    // 销毁链表
{
	pNode Des;

	assert(pHead);
	if (NULL == (pHead))
		return;

	Des = *pHead;
	while (Des)
	{
		Des = (*pHead)->_pNext;
		free(*pHead);
		if (Des)
			(*pHead) = Des;
	}
	*pHead = NULL;
}


void printlist(pNode pHead)  //打印
{
	pNode pCur = pHead;
	if (NULL == pHead)
		return;
	while (pCur)
	{
		printf("%d -->", pCur->_data);
		pCur = pCur->_pNext;
	}
	printf("NULL\n");

}

测试代码

void testInitPushbackPopback()
{
	pNode pHead;

	listNodeInit(&pHead);

	listPushBack(&pHead, 1);
	listPushBack(&pHead, 2);
	listPushBack(&pHead, 3);
	listPushBack(&pHead, 4);
	listPushBack(&pHead, 5);
	printlist(pHead);

	listPopBack(&pHead);
	printlist(pHead);

}

void testpushfrontAndpopfront()
{
	pNode pHead;
	listNodeInit(&pHead);

	listPushBack(&pHead, 1);
	listPushBack(&pHead, 2);
	listPushBack(&pHead, 3);
	listPushBack(&pHead, 4);
	listPushBack(&pHead, 5);
	printlist(pHead);

	ListPushFront(&pHead, 6);
	printlist(pHead);

	ListPopFront(&pHead);
	
	printlist(pHead);
}


void testInsertAndErase()
{
	pNode pHead;
	pNode pos;
	listNodeInit(&pHead);

	listPushBack(&pHead, 1);
	listPushBack(&pHead, 2);
	listPushBack(&pHead, 3);
	listPushBack(&pHead, 4);
	listPushBack(&pHead, 5);
	printlist(pHead);

	pos = ListFind(pHead, 3);

	ListInsert(&pHead, pos, 7);
	printlist(pHead);
	ListErase(&pHead, pos);
	printlist(pHead);
	
	ListEmpty(pHead);
	SListDestroy(&pHead);
}


主函数

#include<stdio.h>
#include<Windows.h>

#include"linkedlist.h"

int main()
{

	//testInitPushbackPopback();
	//testpushfrontAndpopfront();
	testInsertAndErase();
	system("pause");
	return;
}


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页