无头单向非循环链表实现

  • SList.h
#pragma once
//无头单向非循环链表
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef int SlistDataType;
typedef struct SlistNode//每个节点是一个结构体
{
	SlistDataType _data;//数据域
	struct SlistNode* _next;//指针域
}SlistNode;

typedef struct Slist
{
	SlistNode* _head;
}Slist;

void  Slist_Int(Slist* pcb);

void  SList_Tail_Insert(Slist* pcb, SlistDataType x);//尾插
void  Slist_Tail_Delete(Slist* pcb);//尾删

void  Slist_Head_Insert(Slist* pcb, SlistDataType x);//头插
void  Slist_Head_Delete(Slist* pcb);//头删

SlistNode* Slist_Find(Slist* pcb, SlistDataType x);//查找

void Slist_Insert_After(SlistNode* pos, SlistDataType x);//在POS的后面进行插入
void Slist_Erase_After(SlistNode* pos);//把pos后面的那一个删除

void Slist_Insert_Front(SlistNode* pos, SlistDataType x);//在pos的前面进行插入
void Slist_Erase_Front(SlistNode * pos, SlistDataType x);//把POS的前面那一个删除

void Slist_Remove(Slist* pcb, SlistDataType x);//找出一组数据中重复数据的第一个进行删除
void Slist_All_Remove(Slist* pcb, SlistDataType x);//删除与 X 相同的所有数据
void Slist_Print(Slist* pcb);//打印
  • SList.c
#include"SList.h"

void  Slist_Int(Slist* pcb)
{
	assert(pcb);
	pcb->_head = NULL;
}
void SList_Tail_Insert(Slist* pcb, SlistDataType x)
{
	assert(pcb);
	SlistNode* newnode = (SlistNode*)malloc(sizeof(SlistNode));
	if (newnode == NULL)
	{
		printf("malloc error");
		exit(-1);
	}
	newnode->_data = x;
	newnode->_next = NULL;

	if (pcb->_head == NULL)//为空时
	{
		pcb->_head = newnode;
	}
	else//非空时
	{
		SlistNode* cur = pcb->_head;
		while (cur->_next != NULL)
		{
			cur = cur->_next;
		}
		cur->_next = newnode;
	}
}
void  Slist_Tail_Delete(Slist* pcb)
{
	assert(pcb);
	SlistNode* cur = pcb->_head;
	if (cur == NULL)//没有节点时
	{
		return;
	}
	else if(cur->_next == NULL)
	{
		free(cur);
		pcb->_head = NULL;
	}
	else
	{
		while (cur->_next->_next != NULL)
		{
			cur = cur->_next;
		}
		free(cur->_next);
		cur->_next = NULL;
	}
}
void  Slist_Head_Insert(Slist* pcb, SlistDataType x)
{
	assert(pcb);
	SlistNode* newnode = (SlistNode*)malloc(sizeof(SlistNode));
	{
		if (newnode == NULL)
		{
			printf("newnode error");
			exit(-1);
		}
		newnode->_data = x;
		newnode->_next = NULL;

		newnode->_next = pcb->_head;
		pcb->_head = newnode;
	}
}	
void  Slist_Head_Delete(Slist* pcb)
{
	assert(pcb);
	SlistNode* cur = pcb->_head;
	if (cur == NULL)
	{
		return;
	}
	else 
	{
      pcb->_head = cur->_next;
	  free(cur);
	  cur = NULL;
	}
}
void Slist_Print(Slist* pcb)
{
	assert(pcb);
	SlistNode* cur = pcb->_head;
	while (cur != NULL)
	{
		printf("%d->", cur->_data);
		cur = cur->_next;
	}
	printf("NULL\n");
}
SlistNode* Slist_Find(Slist* pcb, SlistDataType x)
{
	assert(pcb);
	SlistNode* cur = pcb->_head;
	while (cur!= NULL)
	{
		if (cur->_data == x)
		{
			return cur;
		}
		cur = cur->_next;
	}
	return NULL;
}
void  Slist_Insert_After(SlistNode* pos, SlistDataType x)
{
	assert(pos);
	SlistNode* newnode = (SlistNode*)malloc(sizeof(SlistNode));
	newnode->_data = x;
	newnode->_next = NULL;
	newnode->_next = pos->_next;
	pos->_next = newnode;
}
void Slist_Erase_After(SlistNode* pos)
{
	assert(pos);
	if (pos->_next == NULL)
	{
		return;
	}
	else
	{
		SlistNode* next = pos->_next;
		pos->_next = next->_next;
		free(next);
		next = NULL;
	}
}
void Slist_Insert_Front(Slist* pcb,SlistNode* pos, SlistDataType x)
{
	assert(pcb);
	assert(pos);
	SlistNode* newnode = (SlistNode*)malloc(sizeof(SlistNode));
	newnode->_data = x;
	newnode->_next = NULL;
	SlistNode* cur = pcb->_head;
	SlistNode* pcur = NULL;
	while (cur != NULL)
	{
		if (cur->_data == pos->_data)
		{
			if (pcur == NULL)
			{
				pcb->_head = newnode;
				newnode->_next = cur;
			}
			else
			{
			   pcur->_next = newnode;
			   newnode->_next = cur;
			}
			return;
		}
		else
		{
			pcur = cur;
			cur = cur->_next;
		}
	}
}
void Slist_Remove(Slist* pcb, SlistDataType x)
{
	assert(pcb);
	SlistNode* cur = pcb->_head;
	SlistNode* pcur = NULL; 
	while (cur != NULL)
	{
		if (cur->_data == x)
		{
			if (pcur == NULL)
				pcb->_head = cur->_next;
			else
				pcur->_next = cur->_next;
			free(cur);
			cur = NULL;
			return;
		}
		else
		{
			pcur = cur;
			cur = cur->_next;
		}
	}
}
void Slist_All_Remove(Slist* pcb, SlistDataType x)
{
	assert(pcb);
	SlistNode* cur = pcb->_head;
	SlistNode* prev = NULL;
	SlistNode* next = NULL;
	while (cur != NULL)
	{
		if (cur->_data == x)
		{
			next = cur->_next;
			free(cur);
			cur = NULL;
			cur = next;
			if (prev == NULL)
			{
				pcb->_head = next;
			}
			else
			{
				prev->_next = cur;
			}
		}
		else
		{
			prev = cur;
			cur = cur->_next;
		}
	}
	return pcb->_head;
}
  • test.c
#include"SList.h"
void Slist_Test()
{
	Slist  cb;
	Slist_Int(&cb);
	SList_Tail_Insert(&cb, 1);
	SList_Tail_Insert(&cb, 2);
	SList_Tail_Insert(&cb, 2);
	SList_Tail_Insert(&cb, 3);
	SList_Tail_Insert(&cb, 4);
	SList_Tail_Insert(&cb, 5);
	
	Slist_Tail_Delete(&cb);
	Slist_Head_Insert(&cb, 2);
	Slist_Head_Delete(&cb);
	Slist_Print(&cb);

	SlistNode* pos = Slist_Find(&cb,1);
	Slist_Insert_After(pos,25);

	SlistNode* pos1 = Slist_Find(&cb, 3);
	Slist_Insert_After(pos1, 25);

	Slist_Erase_After(pos);

	Slist_Remove(&cb, 2);
	SlistNode* pos3 = Slist_Find(&cb, 3);
	Slist_Insert_Front(&cb,pos3,55);

	Slist_All_Remove(&cb, 2);

	Slist_Print(&cb);
}
int main()
{
	Slist_Test();
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值