单链表的实现

单链表的实现:

头文件list.h

#ifndef __LIST_H
#define __LIST_H
#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      

typedef int DataType;
typedef struct Node
{
	DataType data;
	struct Node* next;
}Node, *pNode, *pList;

void InitList(pList* pplist);
void PushBack(pList* pplist, DataType d);
void PushFront(pList* pplist, DataType d);
void PopBack(pList* pplist);
void PopFront(pList* pplist);
int GetListLength(pList plist);
void PrintfList(plist);
pNode Find(pList plist, DataType d);
void Remove(pList* pplist, DataType d);
void RemoveAll(pList* pplist, DataType d);
void Insert(pList* pplist, pNode pos, DataType d);
void Erase(pList* pplist, pNode pos);
void DestroyList(pList* pplist);

#endif

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "list.h"
void menu()
{
	printf("*************     单链表        **************\n");
	printf("***   1.InitList         2.PushBack       ****\n");
	printf("***   3.PushFron         4.PopBack        ****\n");
	printf("***   5.PopFront         6.GetListLength  ****\n");
	printf("***   7.PrintfList       8.Find           ****\n");
	printf("***   9.Remove           10.RemoveAll     ****\n");
	printf("***   11.Insert          12.Erase         ****\n");
	printf("***   13.DestroyList     0.Exit           ****\n");
	printf("**********************************************\n");
}

int main()
{
	int input = 0;
	pList plist = NULL;
	pList ret = 0;
	do{
		menu();
		DataType data = 0;
		printf("请输入你的选择:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			InitList(&plist);
			break;
		case 2:
			printf("请输入要插入的元素:");
			scanf("%d", &data);
			PushBack(&plist, data);
			break;
		case 3:
			printf("请输入要插入的元素:");
			scanf("%d", &data);
			PushFront(&plist, data);
			break;
		case 4:
			PopBack(&plist);
			break;
		case 5:
			PopFront(&plist);
			break;
		case 6:
			GetListLength(plist);
			break;
		case 7:
			PrintfList(plist);
			break;
		case 8:
		{
			printf("请输入要查找的元素:");
			scanf("%d", &data);
			ret = Find(plist, data);
		    if (ret == NULL)
			{
				printf("查找的数据不存在\n");
			}
			else
			{
				printf("找到了该元素为:%d\n", ret->data);
			 }
		}
			break;
		case 9:
			printf("请输入要删除的元素:");
			scanf("%d", &data);
		    Remove(&plist, data);
			break;
		case 10:
			printf("请输入要删除的元素:");
			scanf("%d", &data);
			RemoveAll(&plist, data);
			break;
		case 11:
			printf("请输入要插入的位置:");
			scanf("%d", &data);
		    ret = Find(plist, data);
			if (ret == NULL)
		    {
			   printf("查找的数据不存在\n");
			 }
			else
			{
			  printf("请输入要插入的数:");
			  scanf("%d", &data);
			  Insert(&plist, ret, data);
			 }
			break;
		case 12:
			printf("请输入要删除的位置:");
			scanf("%d", &data);
			ret = Find(plist, data);
			if (ret == NULL)
			{
				printf("需要删除的数据不存在\n");
			}
			else
			{
				Erase(&plist, ret);
			}
			break;
		case 13:
			DestroyList(&plist);
			break;
		case 0:
			exit;
			break;
		default:
			printf("选择错误,请重新输入\n");
			break;
		}
	} while (input);
	return 0;
}


list.c


#include "list.h"


void InitList(pList* pplist)
{
	assert(pplist != NULL);
	*pplist = NULL;
	printf("初始化成功\n");
}

pNode BuyNode(DataType d)
{
	pNode pnode = (pNode)malloc(sizeof(Node));
	if (pnode == NULL)
	{
		perror("malloc");
		return NULL;
	}
	pnode->data = d;
	pnode->next = NULL;
	return pnode;
}

void PushBack(pList* pplist, DataType d)
{
	assert(pplist != NULL);
	pNode pNewNode = BuyNode(d);
	if (*pplist == NULL)     //没有结点
	{
		*pplist = pNewNode;
		printf("插入元素成功\n");
	}
	else
	{
		pNode cur = *pplist;
		while (cur->next != NULL)
		{
			cur = cur->next;
		}
		cur->next = pNewNode;
		printf("插入元素成功\n");
	}
}

void PushFront(pList* pplist, DataType d)
{
	assert(pplist != NULL);
	pNode pNewNode = BuyNode(d);
	if (*pplist == NULL)     //没有结点
	{
		*pplist = pNewNode;
		printf("插入元素成功\n");
	}
	else
	{
		pNewNode->next = *pplist;
		*pplist = pNewNode;
		printf("插入元素成功\n");
	}

}
void PopBack(pList* pplist)
{
	pNode cur = *pplist;
	assert(pplist != NULL);
	if (cur == NULL)
	{
		printf("该链表为空\n");
		return;
	}
	if (cur->next==NULL)
	{
		free(cur);
		*pplist = NULL;
		return;
	}
	while (cur->next->next != NULL)
	{
		cur = cur->next;
	}
	free(cur->next);
	cur->next = NULL;
	printf("删除成功\n");
}

void PopFront(pList* pplist)
{
	pNode cur = *pplist;
	pNode del = NULL;
	assert(pplist != NULL);
	if (cur == NULL)
	{
		printf("该链表为空\n");
		return;
	}
	if (cur->next == NULL)
	{
		free(cur);
		*pplist = NULL;
		return;
	}
	del = *pplist;
	*pplist = cur->next;
	free(del);
	printf("删除成功\n");
}

int GetListLength(pList plist)
{
	pNode cur = plist;
	int count = 0;
	while (cur)
	{
		count++;
		cur = cur->next;
	}
	printf("该链表的元素个数为:%d\n", count);
}

pNode Find(pList plist, DataType d)
{
	pNode cur = plist;
	while (cur)
	{
		if (cur->data == d)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

void Remove(pList* pplist, DataType d)
{
	pNode cur = *pplist;
	pNode prev = NULL;
	while (cur)
	{
		pNode del = NULL;
		if (cur->data == d)
		{
			if (cur == *pplist)    //第一个
			{
				del = *pplist;
				*pplist = cur->next;
				free(del);
				printf("该数据删除成功\n");
			}
			else                  //非第一个
			{
				prev->next = cur->next;
				free(cur);
				printf("该数据删除成功\n");

			}
			return;
		}
		prev = cur;
		cur = cur->next;
	}
	if (cur== NULL)

	{
		printf("该数据不存在\n");
	}
}

void RemoveAll(pList* pplist, DataType d)
{

	pNode cur = *pplist;
	pNode prev = NULL;
    pNode del = cur;
	if (cur == NULL)

	{
		printf("该数据不存在\n");
	}
	while (cur)
	{
		pNode del = NULL;
		if (cur->data == d)
		{
			if (cur == *pplist)    //第一个
			{
				del = *pplist;
				*pplist = cur->next;
			}
			else                  //非第一个
			{
				prev->next = cur->next;
				free(del);
			}
			prev = cur;
			cur = cur->next;
		}
		else
		{
			prev = cur;
			cur = cur->next;
		}
	}
	printf("该数据删除成功\n");
}

void Insert(pList* pplist, pNode pos, DataType d)
{
	pNode newNode = BuyNode(d);
	assert(pplist);
	if (pos == NULL)
	{
		PushBack(pplist, d);
	}
	else
	{
		newNode->next = pos->next;
		pos->next = newNode;
		printf("插入成功\n");
	}
}

void Erase(pList* pplist, pNode pos)
{
	assert(pplist);
	if (*pplist == NULL)
	{
		return;
	}
	else
	{
		if (pos->next == NULL)
		{
			PopBack(pplist);
		}
		else
		{
			pNode del = pos->next;
			pos->data = pos->next->data;
			pos->next = pos->next->next;
			free(del);
		}
	}

}

void DestroyList(pList* pplist)
{
	pNode cur = *pplist;
	assert(pplist);
	while (cur)
	{
		pNode del = cur;
		cur = cur->next;
		free(del);
	}
	*pplist = NULL;
	printf("销毁成功\n");
}

void PrintfList(pList plist)
{
	pNode cur = plist;
	if (cur == NULL)
	{
		printf("该链表为空\n");
		return;
	}
	else
	{
		while (cur != NULL)
		{
			printf(" %d", cur->data);
			cur = cur->next;
		}
		printf("\n");
	}
}
     
     
    
    
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值