单链表

Slist.h

#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
typedef int DATATYPE;

typedef struct SList
{
	DATATYPE _data;
	struct SListNode *_next;
}SList;

typedef struct SListHead
{
	SList *_head;
}SListHead;

void SListInit(SListHead *plist);
void SListDestory(SListHead *plist);
void SListPushFront(SListHead *plist,DATATYPE x); 
SList* SListCreate(DATATYPE x);
void SListPushBack(SListHead *plist, DATATYPE x);
void SListPrint(SListHead *plist);
void SListPopBack(SListHead *plist);
void SListPopFront(SListHead *plist);
void SListRemoveAll(SListHead *plist, DATATYPE x);
void SListEraseAfter(SList *pos);

Slist.c

#include"SList.h"
void SListInit(SListHead *plist)
{
	assert(plist);
	plist->_head = NULL;
}

void SListDestory(SListHead *plist)
{
	assert(plist);
	SList* node = plist->_head;
	SList* next = node;
	while (node)
	{
		next = node->_next;
		free(node);
		node = next;
	}
	plist->_head = NULL;
}

void SListPrint(SListHead *plist)
{
	assert(plist);
	SList *node = plist->_head;
	while (node)
	{
		printf("%d->", node->_data);
		node = node->_next;
	}
	printf("\n");
}

SList* SListCreate(DATATYPE x)
{
	SList *node = (SList*)malloc(sizeof(SList));
	node->_data = x;
	node->_next = NULL;
	return node;
}

void SListPushBack(SListHead *plist, DATATYPE x)
{
	assert(plist);
	if (plist->_head == NULL)
	{
		plist->_head = SListCreate(x);
	}
	else
	{
		SList *cur = plist->_head;
		while (cur->_next)
		{
			cur = cur->_next;
		}
		cur->_next = SListCreate(x);
	}
	
}

void SListEraseAfter(SList *pos)
{
	assert(pos);
	if (pos->_next == NULL)
	{
		return;
	}
	SList* node = pos->_next;
	SList* next = node->_next;
	pos->_next = next;
	free(node);
	node = NULL;
	
}


void SListPopBack(SListHead *plist)
{
	assert(plist);
	if (plist->_head == NULL)
	{
		return;
	}
	else
	{
		SList* node = plist->_head;
		SList* prev = node;
		while (node->_next)
		{
			prev = node;
			node = node->_next;
		}
		if (node == plist->_head)
		{
			plist->_head = NULL;
		}
		else
		{
			free(node);
			node = NULL;
			prev->_next = NULL;
		}
		
	}
}

void SListPushFront(SListHead *plist, DATATYPE x)
{
	assert(plist);
	SList* new = SListCreate(x);
	SList* node = plist->_head;
	new->_next = node;
	plist->_head = new;
}

void SListPopFront(SListHead *plist)
{
	assert(plist);
	if (plist->_head == NULL)
	{
		return;
	}
	SList* node = plist->_head;
	plist->_head = plist->_head->_next;
	free(node);
	node = NULL;
}

void SListRemoveAll(SListHead *plist, DATATYPE x)
{
	assert(plist);                                        //待改进
	SList* node = plist->_head;
	SList* prev = NULL;
	while (node)
	{
		if (node->_data == x)
		{
			if (prev == NULL)
			{
				plist->_head = node->_next;
			}
			else
			{
				prev->_next = node->_next;
			}
			free(node);
			node = prev->_next;
		}
		else
		{
			prev = node;
			node = node->_next;
		}
	}
}

test.c

#include"SList.h"
int main()
{
	SListHead _head;
	SListInit(&_head);
	SListPushBack(&_head, 1);
	SListPushBack(&_head, 3);
	SListPushBack(&_head, 3);
	SListPushBack(&_head, 4);
	SListPushBack(&_head, 3);
	SListPrint(&_head);
	SListEraseAfter(_head._head);
	SListPrint(&_head);
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值