链表的基本操作

·什么是链表

链表是一种物理存储单元上非连续、非顺序的存储结构数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。 

如下图:·代码及其实现

·Linklist.h

#ifndef __LINKLIST_H__ 
#define __LINKLIST_H__ 

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


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

void InitLinkList(pList* pplist);
pNode BuyNode(DataType d);
void DestroyLinkList(pList* pplist);
void PushBack(pList plist, DataType d);
void PopBack(pList* pplist);
void PushFront(pList* pplist, DataType d);
void PopFront(pList* pplist);
pNode Find(pList* pplist, DataType d);
void Print(pList* pplist);

//在指定位置之前插入一个值 
void Insert(pList* pplist, pNode pos, DataType d);
//指定位置删除 
void Erase(pList* pplist, pNode pos);
void Remove(pList* pplist, DataType d);
void RemoveAll(pList* pplist, DataType d);
void EraseNotTailNode(pNode pos);
void PrintLinkList(pList plist);
int GetListLength(pList plist);
//链表面试题 

//1. 逆序打印单项链表 
void PrintReverse(pList plist);
void Reverse(pList plist, pList *newplist);

#endif //__LINKLIST_H__ 

·Linklist.c

#include "linklist.h"


void test()
{
	pList* pplist = NULL;
	pList* newpplist = NULL;
	InitLinkList(&pplist);
	PushFront(&pplist, 1);
	PushFront(&pplist, 2);
	PushFront(&pplist, 3);
	PushFront(&pplist, 4);
	Erase(&pplist, Find(&pplist, 3));
	Print(&pplist);
}

int main()
{
	test();
	system("pause");
	return 0;
}

int GetListLength(pList plist)
{
	assert(plist);
	pNode cur = plist;
	int count = 0;
	while (cur)
	{
		cur = cur->next;
		count++;
	}
	return count;
}

void Reverse(pList plist,pList *newplist)
{
	pNode cur = plist;
	while (cur)
	{
		PushFront(newplist, cur->data);
		cur = cur->next;
	}
	Print(newplist);
}

void PrintReverse(pList plist)
{
	pNode cur = plist;
	if (cur->next)
	{
		PrintReverse(cur->next);
		printf("%d->", cur->data);
	}
	else if (cur->next == NULL)
	{
		printf("%d->", cur->data);
	}
}

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

void InitLinkList(pList* pplist)
{
	assert(pplist);
	*pplist = NULL;
}

void Print(pList* pplist)
{
	pNode cur = *pplist;
	while (cur)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

void PushBack(pList* pplist, DataType d)
{
	assert(pplist);
	pNode newNode = BuyNode(d);
	pNode cur = *pplist;
	if (NULL == *pplist)
	{
		*pplist = newNode;
		return;
	}
	else
	{
		while (cur->next != NULL)
		{
			cur = cur->next;
		}
		cur->next = newNode;
	}
}
void PopBack(pList* pplist)
{
	assert(pplist);
	pNode cur = *pplist;
	pNode pre = NULL;
	while (cur->next != NULL)
	{
		pre = cur;
		cur = cur->next;
	}
	pre->next = NULL;
	free(cur);
}

void PushFront(pList* pplist, DataType d)
{
	assert(pplist);
	pNode newNode = BuyNode(d);
	if (NULL == *pplist)
	{
		*pplist = newNode;
		return;
	}
	else
	{
		newNode->next = *pplist;
		*pplist = newNode;
	}
}

void PopFront(pList* pplist)
{
	assert(pplist);
	pNode cur = *pplist;
	*pplist=cur->next;
	free(cur);
}

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

void Insert(pList* pplist, pNode pos, DataType d)
{
	assert(pplist);
	pNode newNode = BuyNode(d);
	newNode->next = pos->next;
	pos->next = newNode;
}

void Erase(pList* pplist, pNode pos)
{
	assert(pplist);
	pNode cur = pos->next;
	pos->next = cur->next;
	free(cur);
	cur = NULL;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值