单链表的基本操作

#pragma once


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

typedef int Datatype;

typedef struct SListNode
{
	Datatype data;
	SListNode *next;
}SListNode;


SListNode* BuyNode(Datatype x);//申请空间
void PrintNode(SListNode*& pHead);//打印链表
void CreatNode(SListNode*& pHead);//创建表
void DestoryNode(SListNode*& pHead);//销毁
void PushBack(SListNode*& pHead, Datatype x);//尾插
void PopBack(SListNode*& pHead);//尾删
void PushFront(SListNode*& pHead, Datatype x);//头插
void PopFront(SListNode*& pHead);//头删
void Insert(SListNode*& pHead, SListNode*& pos, Datatype x);//指定位置插入
void Delete(SListNode*& pHead, SListNode*& pos);//删除指定位置
void Remove(SListNode*& pHead, Datatype x);//删除x
void Search(SListNode*& pHead, Datatype x);//查找X

#include"SNode.h"


SListNode* BuyNode(Datatype x)
{
	SListNode* Node = (SListNode*)malloc(sizeof(SListNode));
	Node->data = x;
	Node->next = NULL;
	return Node;

}
void CreatNode(SListNode*& pHead)
{
	pHead = NULL;
}
void PrintNode(SListNode*& pHead)//打印链表
{
	SListNode* cur = pHead;
	while (cur)
	{
		printf("%d", cur->data);
		cur = cur->next;
	}
	printf("\n");
}
void DestoryNode(SListNode*& pHead)//销毁
{
	SListNode* cur = pHead;
	while (cur)
	{
		SListNode* temp = cur;
		cur = cur->next;
		free(temp);
		temp = NULL;

	}
}
void PushBack(SListNode*& pHead, Datatype x)
{
	if (pHead == NULL)
	{
		pHead=BuyNode(x);
	}
	else
	{
		SListNode* cur = pHead;
		while (cur->next)
		{
			cur = cur->next;
		}
		cur->next = BuyNode(x);	
	}
}
void PopBack(SListNode*& pHead)
{
	if (pHead == NULL)
	{
		printf("The List is Empty\n");
		return;
	}
	else if (pHead->next == NULL)
	{
		free(pHead);
		pHead = NULL;

	}
	else
	{
		SListNode* pre=NULL;
		SListNode* cur=pHead;
		while (cur->next)
		{
			pre = cur;
			cur = cur->next;
		}
		free(cur);
		pre->next = NULL;
	}
}
void PushFront(SListNode*& pHead, Datatype x)
{
	if (pHead == NULL)
	pHead = BuyNode(x);
	else
	{
		SListNode* temp;
		temp = BuyNode(x);
		temp->next = pHead;
		pHead = temp;

	}
}
void PopFront(SListNode*& pHead)
{
	if (pHead == NULL)
	{
		printf("The List is Empty\n");
		return;
	}
	else if (pHead->next == NULL)
	{
		free(pHead);
		pHead = NULL;
	}
	else
	{
		SListNode* temp = pHead->next;
		free(pHead);
		pHead = temp;

	}
}
void Insert(SListNode*& pHead, SListNode*& pos, Datatype x)//指定位置插入
{
	assert(pos);
	assert(pHead);
	if (pos == pHead)
	{
		PushFront(pHead, x);
	}
	else
	{
		SListNode* pre = NULL;
		SListNode* cur = NULL;
		cur = pHead;
		pre = pHead;
		while (cur != pos)
		{
			pre = cur;
			cur = cur->next;
		}
		SListNode* temp = NULL;
		temp = BuyNode(x);
		temp->next = cur;
		pre->next = temp;
	}
}
void Delete(SListNode*& pHead, SListNode*& pos)//删除指定位置
{
	assert(pos);
	assert(pHead);
	if (pos == pHead)
	{
		free(pHead);
		pHead = NULL;
	}
	else
	{
		SListNode* pre, *cur;
		cur = pHead;
		while (cur!=pos)
		{
			pre = cur;
			cur = cur->next;
		}
		pre->next = cur->next;
		free(cur);
	}
}
void Remove(SListNode*& pHead, Datatype x)//删除x
{
	if (pHead == NULL)
	{
		printf("The List is Empty\n");
	}
	else if (pHead->data == x)
	{
		SListNode* next = pHead->next;
		free(pHead);
		pHead = next;
	}
	else
	{
		SListNode* pre, *cur;
		cur = pHead;
		while (cur->data != x)
		{
			pre = cur;
			cur = cur->next;
		}
		pre->next = cur->next;
		free(cur);

	}
}
SListNode* Search(SListNode*& pHead, Datatype x)//查找X
{
	SListNode* cur = pHead;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值