不带头结点的单链表

Don't_Lead_Link_List

#pragma once
#include <crtdbg.h> 
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>

typedef int States;       //状态值

#define TURE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define NULLPTR -3 

typedef int ElemType;

//数据结点
typedef struct LinkNode
{
	ElemType data;
	struct LinkNode *next;
}LinkNode,LinkList;

LinkNode* Buy_Node();
void LinkPrint(LinkNode *phead);
void LinkPushBack(LinkNode **pphead, ElemType newdata);
void LinkPushFront(LinkNode **pphead, ElemType newdata);
void LinkPopBack(LinkNode **pphead);
void LinkPopFront(LinkNode **pphead);
LinkNode* LinkListFind(LinkNode* phead, ElemType value);
void LinkInsert(LinkNode **pphead, LinkNode *pos, ElemType newdata);
void LinkInsertAfter(LinkNode *pos, ElemType newdata);
void LinkErase(LinkNode **pphead, LinkNode *pos);
void LinkDestory(LinkNode **pphead);

.cpp

LinkNode* Buy_Node()
{
	LinkNode *newnode = (LinkNode*)malloc(sizeof(LinkNode));
	assert(newnode != NULL);
	newnode->next = NULL;
	return newnode;
}

//打印链表
void LinkPrint(LinkNode *phead)
{
	LinkNode *cur = phead;
	while (cur != NULL)
	{
		printf("data:  %d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

//尾插
void LinkPushBack(LinkNode **pphead, ElemType newdata)
{
	assert(pphead != NULL);
	LinkNode *newnode = Buy_Node();
	newnode->data = newdata;

	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else
	{
		//找到尾结点
		LinkNode *tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}

//头插
void LinkPushFront(LinkNode **pphead, ElemType newdata)
{
	assert(pphead != NULL);
	LinkNode *newnode = Buy_Node();
	newnode->data = newdata;
	newnode->next = *pphead;
	*pphead = newnode; 
}

//尾删
void LinkPopBack(LinkNode **pphead)
{
	assert(pphead != NULL);
	assert(*pphead != NULL);
	if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		LinkNode *tail = *pphead;
		LinkNode *pur = NULL;
		while (tail->next != NULL)
		{
			pur = tail;
			tail = tail->next;
		}
		free(tail);
		tail = NULL;
		pur->next = NULL;
	}
}

//头删
void LinkPopFront(LinkNode **pphead)
{
	assert(pphead != NULL);
	assert(*pphead != NULL);
	LinkNode *pur = (*pphead)->next;
	free(*pphead);
	*pphead = pur;
}

//查询链表 
LinkNode* LinkListFind(LinkNode* phead, ElemType value)
{
	assert(phead != NULL);
	LinkNode *cur = phead;
	while (cur != NULL)
	{
		if (cur->data == value)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

//在pos位置之前去插入一个结点
void LinkInsert(LinkNode **pphead, LinkNode *pos, ElemType newdata)
{
	assert(pphead != NULL && pos != NULL);
	
	LinkNode *newnode = Buy_Node();
	newnode->data = newdata;
	LinkNode *pre = *pphead;
	if (pos == *pphead)
	{
		newnode->next = *pphead;
		*pphead = newnode;
	}
	else
	{
		while (pre->next != pos)
		{
			pre = pre->next;
		}
		pre->next = newnode;
		newnode->next = pos;
	}
}

//在pos的后面插入
void LinkInsertAfter(LinkNode *pos, ElemType newdata)
{
	assert(pos != NULL);
	LinkNode *newnode = (LinkNode*)Buy_Node();
	newnode->data = newdata;
	newnode->next = pos->next;
	pos->next = newnode;
}

//删除pos位置结点
void LinkErase(LinkNode **pphead, LinkNode *pos)
{
	assert(pphead != NULL && pos != NULL);
	LinkNode *pre = *pphead;
	//头删
	if (pos == *pphead)
	{
		*pphead = pos->next;
		free(pos);
	}
	else
	{
		while (pre->next != pos)
		{
			pre = pre->next;
		}
		pre->next = pos->next;
		free(pos);
	}
}

//删除pos后的结点
void LinkEraseAfter(LinkNode *pos)
{
	assert(pos != NULL);
	LinkNode *next = pos->next;
	pos->next = next->next;
	free(next);
}

//释放链表
void LinkDestory(LinkNode **pphead)
{ 
	LinkNode *tmp = NULL;
	while (*pphead != NULL)
	{
		tmp = (*pphead)->next;
		free(*pphead);
		*pphead = tmp;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值