单向链表

LinkList.h

#pragma once
#pragma pack(1)

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

#ifdef  __cpulspuls
extern "C" {
#endif
	typedef struct linkNode
	{
		int Value;
		struct linkNode *Next;
	}LINKNODE;

	//初始化链表
	struct linkNode *Init_List();

	//插入元素(尾插)
	LINKNODE * Insert_List(LINKNODE *header, int val);

	//按位置插(pos)(pos大于等于最大长度则按尾插,pos小于等于0也尾插,1则插在头结点后面)
	LINKNODE * InsertByPos_List(LINKNODE *header, int pos, int val);

	//按值删除节点(成功返回0,失败返回-1)
	int DeleteByVal_List(LINKNODE *header, int val);

	//按位置删除(成功返回0,失败返回-1)
	int DeleteByPos_List(LINKNODE *header, int pos);

	//遍历
	void Foreach_List(LINKNODE *header);

	//销毁
	void Destory_List(LINKNODE *header);

	//清空
	LINKNODE *Empty_List(LINKNODE *header);

#ifdef  __cpulspuls
}
#endif

LinkList.c

#include "LinkList.h"

LINKNODE * Init_List()
{
	LINKNODE *header = malloc(sizeof(LINKNODE));
	header->Next = NULL;
	header->Value = -1;
	return header;
}

LINKNODE *Insert_List(LINKNODE * header, int val)
{
	if (header == NULL)
		return NULL;
	/* 创建新节点 */
	LINKNODE *New = malloc(sizeof(LINKNODE));
	New->Next = NULL;
	New->Value = val;
	/* 插入 */
	LINKNODE *pCurrent = header;
	while (pCurrent->Next != NULL)
	{
		pCurrent = pCurrent->Next;
	}
	pCurrent->Next = New;

	return header;
}

LINKNODE *InsertByPos_List(LINKNODE * header, int pos, int val)
{
	if (header == NULL)
		return NULL;
	int i = 0;
	LINKNODE *New = malloc(sizeof(LINKNODE));
	New->Value = val;
	New->Next = NULL;
	
	LINKNODE *pCurrent = header->Next;
	LINKNODE *pPrev = header;
	while (pCurrent->Next != NULL)
	{			
		++i;	
		if (i == pos)
			break;		
		pPrev = pCurrent;
		pCurrent = pCurrent->Next;
	}
	/* i<pos || pos <= 0表示链表没有pos这个位置 尾插*/
	if (pCurrent->Next == NULL)
	{
		pCurrent->Next = New;
		return header;
	}
	/* pPrev为第pos个节点 pCurrent为下一个节点*/
	pPrev->Next = New;
	New->Next = pCurrent;

	return header;
}

int DeleteByVal_List(LINKNODE * header, int val)
{
	if (header == NULL)
		return -1;

	LINKNODE *pPrev = header;
	LINKNODE *pCurrent = header->Next;
	
	while (pCurrent->Next != NULL)
	{
		if (pCurrent->Value == val)
		{
			break;
		}
		pPrev = pCurrent;		
		pCurrent = pCurrent->Next;
	}
	/* 没找到 (当最后一个数正好是val时)*/
	if (pCurrent->Next == NULL && pCurrent->Value != val)
		return -1;
	/* 找到值为val的节点 */
	pPrev->Next = pCurrent->Next;
	free(pCurrent);
	pCurrent = NULL;
	return 0;
}

int DeleteByPos_List(LINKNODE * header, int pos)
{
	if (pos <= 0 || header == NULL)
		return -1;

	LINKNODE *pPrev = header;
	LINKNODE *pCurrent = header->Next;
	int i = 0;
	while (pCurrent != NULL)
	{
		++i;
		if (pos == i)
			break;
		pPrev = pCurrent;
		pCurrent = pCurrent->Next;
	}
	/* 没找到 */
	if (pos > i)
		return -1;
	/* 找到 1  2  3 ---> (删除2) 1 -> 3 free(2)*/
	pPrev->Next = pCurrent->Next;
	free(pCurrent);
	pCurrent = NULL;
	return 0;
}

void Foreach_List(LINKNODE * header)
{
	if (header == NULL)
		return;
	LINKNODE *pCurrent = header;

	while (pCurrent->Next != NULL)
	{	
		pCurrent = pCurrent->Next;
		printf("%d  ", pCurrent->Value);		
	}
	printf("\n");
}

void Destory_List(LINKNODE * header)
{
	if (header == NULL)
		return;
	LINKNODE *pCurrent = header;
	LINKNODE *pRear = header->Next;

	while (pCurrent->Next != NULL)
	{
		free(pCurrent);
		pCurrent = pRear;
		pRear = pCurrent->Next;
	}
}

LINKNODE *Empty_List(LINKNODE * header)
{
	LINKNODE *pCurrent = header->Next;	
	LINKNODE *pRear = pCurrent->Next;
	header->Next = NULL;
	while (pCurrent->Next != NULL)
	{
		free(pCurrent);
		pCurrent = pRear;
		pRear = pCurrent->Next;
	}
	return header;
}

test.c


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "LinkList.h"
int main()
{
	LINKNODE *header =  Init_List();
	Insert_List(header, 1);
	Insert_List(header, 2);
	Insert_List(header, 3);
	Foreach_List(header);
	printf("************************\n");

	InsertByPos_List(header, 2, 5);
	InsertByPos_List(header, -1, 10);
	InsertByPos_List(header, 20, 10);
	Foreach_List(header);
	printf("************************\n");

	DeleteByVal_List(header, 5);
	DeleteByVal_List(header, 10);
	DeleteByVal_List(header, 10);
	DeleteByPos_List(header, 2);
	Foreach_List(header);
	printf("************************\n");

	Empty_List(header);
	Foreach_List(header);
	printf("************************\n");

	Insert_List(header, 1);
	Insert_List(header, 2);
	Insert_List(header, 3);
	Foreach_List(header);
	printf("************************\n");

	Destory_List(header);
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值