欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

数据结构(C语言):单链表的创建、插入、修改、删除、查找、修改等操作

程序员文章站 2022-03-22 19:21:51
...

.h头文件

#ifndef _LINK_H_
#define _LINK_H_

#include <stdio.h>
#include <stdlib.h>


/*数据域数据类型*/
typedef int DateType;

/*结点数据类型*/
typedef struct node
{
	DateType data;//数据域
	struct node *pNext;//指针域
}LinkNode;

//标签数据类型
typedef struct list
{
	LinkNode *pHead;//头结点指针
	int cLen;//当前链表结点个数
}LinkList;

#endif

.c文件

#include "link.h"

/*创建空链表*/
LinkList* createLinkList()
{
	LinkList *pList = malloc(sizeof(LinkList));
	if(NULL == pList)
	{
		perror("fail to malloc");
		return NULL;
	}
	pList->pHead = NULL;
	pList->cLen = 0;

	return pList;	
}

/*遍历打印链表*/
void showForEachList(LinkList *pList)
{
	LinkNode *pTmpNode = pList->pHead;

	while(pTmpNode != NULL)
	{
		printf("%d ",pTmpNode->data);
		pTmpNode = pTmpNode->pNext;
	}
	printf("\n");
}

/*判断链表是否为空*/
int isEmptyLinkList(LinkList *pList)
{
	return pList->cLen == 0;
}

/*头插*/
int insertHeadLinkList(LinkList *pList,DateType data)
{
	LinkNode *pInsertNode = malloc(sizeof(LinkNode));
	if(NULL == pInsertNode)
	{
		perror("insertHeadLinkList malloc error!\n");
		return 1;
	}
	pInsertNode->data = data;

	pInsertNode->pNext = pList->pHead;
	pList->pHead = pInsertNode;
	pList->cLen++;
	
	return 0;
}

/*尾插*/
int insertTailLinkList(LinkList *pList,DateType data)
{
	LinkNode *pInsertNode = malloc(sizeof(LinkNode));
	if(NULL == pInsertNode)
	{
		perror("insertTailLinkList malloc error!\n");
		return 1;
	}
	pInsertNode->data = data;
	pInsertNode->pNext = NULL;

	
	if(NULL == pList->pHead)
	{
		pList->pHead = pInsertNode;
	}
	else
	{
		LinkNode *pTmpNode = pList->pHead;
		while(pTmpNode->pNext != NULL)
		{
			pTmpNode = pTmpNode->pNext;
		}
		pTmpNode->pNext = pInsertNode;
	}
	pList->cLen++;

	return 0;
}

/*头删*/
int deleteHeadLinkList(LinkList *pList)
{
	if(isEmptyLinkList(pList))
	{
		return 0;
	}
	LinkNode *pTmpNode = pList->pHead;
	pList->pHead = pTmpNode->pNext;
	free(pTmpNode);
	pList->cLen--;

	return 0;
}

/*尾删*/
int deleteTailLinkList(LinkList *pList)
{
	if(isEmptyLinkList(pList))
	{
		return 0;
	}
	else if(1 == pList->cLen)
	{
		deleteHeadLinkList(pList);
	}
	else
	{
		LinkNode *pTmpNode = pList->pHead;
		while(pTmpNode->pNext->pNext != NULL)
		{
			pTmpNode = pTmpNode->pNext;
		}
		free(pTmpNode->pNext);
		pTmpNode->pNext = NULL;
		pList->cLen--;
	}

	return 0;
}

/*查找*/
LinkNode *findLinkList(LinkList *pList,DateType data)
{
	
	LinkNode *pTmpNode = pList->pHead;
	while(pTmpNode != NULL)
	{
		if(data == pTmpNode->data)
		{
			return pTmpNode;
		}
		pTmpNode = pTmpNode->pNext;
	}

	return NULL;
}

/*修改*/
int changeLinkList(LinkList *pList,DateType oldData,DateType newData)
{
	LinkNode *pTmpNode = NULL;
	while((pTmpNode = findLinkList(pList,oldData)) != NULL)
	{
		pTmpNode->data = newData;
	}

	return 0;
}

/*链表销毁*/
void destroyLinkList(LinkList **ppList)
{
	while((*ppList)->pHead !=NULL)
	{
		deleteHeadLinkList(*ppList);
	}
	free(*ppList);
	*ppList = NULL;
}

/*查找中间结点,不知道结点数*/
LinkNode *findMidNode(LinkList *pList)
{
	LinkNode *pFast = pList->pHead;
	LinkNode *pSlow = pList->pHead;

	while(pFast != NULL)
	{
		pFast = pFast->pNext;
		if(NULL == pFast)
		{
			break;
		}
		pFast = pFast->pNext;
		pSlow = pSlow->pNext;
	}
	return pSlow;
}

/*查找链表中第K个结点(不知道结点数)*/
LinkNode *findLastKNode(LinkList *pList ,int K)
{
	LinkNode *pFast = pList->pHead;
	LinkNode *pSLow = pList->pHead;
	int i = 0;
	for (i = 0; i < K; i++)
	{
		pFast = pFast->pNext;
	}
	while (pFast != NULL)
	{
		pFast = pFast->pNext;
		pSLow = pSLow->pNext;
	}
	return pSLow;
}

/*链表逆序*/
void reverseLinkList(LinkList *pList)
{
	LinkNode *pTmpNode = pList->pHead;
	LinkNode *pInsertNode = NULL;

	pList->pHead = NULL;

	while (pTmpNode != NULL)
	{
		pInsertNode = pTmpNode;
		pTmpNode = pTmpNode->pNext;

		pInsertNode->pNext = pList->pHead;
		pList->pHead = pInsertNode;
	}
}

/*删除链表中指定结点*/
int deletePointNode(LinkList *pList,DateType data)
{
	LinkNode *pTmpNode = pList->pHead;
	LinkNode *pPreNode = pList->pHead;

	while (pTmpNode != NULL)
	{
		if (pTmpNode->data == data)
		{
			if (pTmpNode == pPreNode)
			{
				pList->pHead = pTmpNode->pNext;
				free(pTmpNode);
				pTmpNode = pList->pHead;
				pPreNode = pList->pHead;
			}
			else
			{
				pPreNode->pNext = pTmpNode->pNext;
				free(pTmpNode);
				pTmpNode = pPreNode->pNext;
			}
			pList->cLen--;
		}
		else
		{
			pPreNode = pTmpNode;
			pTmpNode = pTmpNode->pNext;
		}
	}
}

/*创建环型链表*/
LinkList *loopLinkList(LinkList *pList)
{
	LinkNode *pTmpNode = pList->pHead;
	while(pTmpNode->pNext != NULL)
	{
		pTmpNode = pTmpNode->pNext;
	}
	pTmpNode->pNext = pList->pHead;//首尾环

	return pList;
}

/*判断链表是否有环*/
int isloopLinkList(LinkList *pList)
{
	LinkNode *pFast = pList->pHead;
	LinkNode *pSlow = pList->pHead;
	while(1)
	{
		pFast = pFast->pNext;
		if(NULL == pFast)
		{
			return 0;//无环
		}
		if(pFast->data == pSlow->data)
		{
			return 1;//有环
		}

		pFast = pFast->pNext;
		if(NULL == pFast)
		{
			return 0;
		}

		pSlow = pSlow->pNext;

		if(pFast->data == pSlow->data)
		{
			return 1;
		}
	}
}

/*约瑟夫环*/
LinkNode *deleteJosephNode(LinkList *pList)
{
	LinkNode *pPreNode = pList->pHead;
	LinkNode *pFreeNode = pList->pHead;
	while(pList->cLen > 1)
	{
		pPreNode = pPreNode->pNext;
		pFreeNode = pFreeNode->pNext->pNext;
		pPreNode->pNext = pFreeNode->pNext;
		free(pFreeNode);
		pList->cLen--;
		pFreeNode = pPreNode->pNext;
		pPreNode =pFreeNode;	
	}

	return pPreNode;
}

/*链表的插入排序*/
void insertSortLinkList(LinkList *pList)
{
	if(isEmptyLinkList(pList) || 1 == pList->cLen)
	{
		return ;
	}
	LinkNode *pInsertNode = NULL;
	LinkNode *pTmpNode = pList->pHead->pNext;
	pList->pHead->pNext = NULL;//从第一个结点后断开链表

	while(pTmpNode != NULL)
	{
		/*拿到要插入的结点*/
		pInsertNode = pTmpNode;
		pTmpNode = pTmpNode->pNext;
		pInsertNode->pNext = NULL;

		/*当要插入的结点为头结点时*/
		if(pInsertNode->data < pList->pHead->data)
		{
			pInsertNode->pNext = pList->pHead;
			pList->pHead = pInsertNode;
		}
		else
		{
			/*寻找非头结点插入的位置*/
			LinkNode *pTmp = pList->pHead;
			while(pTmp != NULL && pTmp->pNext != NULL && pInsertNode->data > pTmp->data)
			{
				pTmp = pTmp->pNext;
			}
			/*结点插入*/
			pInsertNode->pNext = pTmp->pNext;
			pTmp->pNext = pInsertNode;
		}		
	}
}

int main(void)
{
	LinkList *pList = NULL;
	LinkNode *pTmpNode = NULL;
	pList = createLinkList();
	
	int i = 0;
	for(i = 1;i < 10;i++)
	{
		insertHeadLinkList(pList,i);
	}
	showForEachList(pList);
/*
	loopLinkList(pList);
	printf("%d\n",isloopLinkList(pList));

	printf("%d\n",deleteJosephNode(pList)->data);	
*/
	insertSortLinkList(pList);
	showForEachList(pList);

	return 0;
}