顺序表(C语言)

SList.h

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

typedef int SLTDateType;
typedef struct SListNode
{
	SLTDateType date;
	struct SListNode* next;
}SLTNode;

void SLTPrint(SLTNode* phead);

SLTNode* BuySLTNode(SLTDateType n);

void SLTPushFront(SLTNode** phead, SLTDateType n);
void SLTPopFront(SLTNode** phead);

void SLTPushBack(SLTNode** phead,SLTDateType n);
void SLTPopBack(SLTNode** phead);

SLTNode* SLTFind(SLTNode* phead, SLTDateType n);
void SLTInsert(SLTNode** phead, SLTNode* pos, SLTDateType n);
void SLTErase(SLTNode** phead, SLTNode* pos);

void SLTInsertAfter(SLTNode* pos, SLTDateType n);
void SLTEraseAfter(SLTNode* pos);

SList.c

#include"SList.h"

void SLTPrint(SLTNode* phead)
{
	assert(phead);
	while (phead)
	{
		printf("%d->", phead->date);
		phead = phead->next;
	}
	printf("NULL");
	printf("\n");
}

SLTNode* BuySLTNode(SLTDateType n)
{
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	newnode->date = n;
	newnode->next = NULL;
	return newnode;
}
void SLTPushFront(SLTNode** phead, SLTDateType n)
{
	assert(phead);
	SLTNode* newnode = BuySLTNode(n);
	newnode->date = n;
	newnode->next = *phead;
	*phead = newnode;
}

void SLTPopFront(SLTNode** phead)
{
	assert(phead);
	assert(*phead);
	SLTNode* Front = (*phead)->next;
	free(*phead);
	*phead = Front;
}

void SLTPushBack(SLTNode** phead, SLTDateType n)
{
	if (*phead == NULL)
	{
		*phead = BuySLTNode(n);
		return;
	}
	SLTNode* newnode = BuySLTNode(n);
	SLTNode* tail = *phead;
	while (tail->next)
	{
		tail = tail->next;
	}
	tail->next = newnode;
}

void SLTPopBack(SLTNode** phead)
{
	assert(phead);
	assert(*phead);
	if ((*phead)->next == NULL)
	{
		free(*phead);
		*phead = NULL;
	}
	else
	{
		SLTNode* tail = *phead;
		while (tail->next->next)
		{
			tail = tail->next;
		}
		free(tail->next);
		tail->next = NULL;
	}
}

SLTNode* SLTFind(SLTNode* phead, SLTDateType n)
{
	while (phead)
	{
		if (phead->date == n)
		{
			return phead;
		}
		phead = phead->next;
	}
	return NULL;
}

void SLTInsert(SLTNode** phead, SLTNode* pos, SLTDateType n)
{
	if (pos == *phead)
	{
		SLTPushFront(phead,n);
	}
	else
	{
		SLTNode* cur = *phead;
		while (cur->next != pos)
		{
			cur = cur->next;
		}
		SLTNode* newnode = BuySLTNode(n);
		cur->next = newnode;
		newnode->next = pos;
	}
}

void SLTErase(SLTNode** phead, SLTNode* pos)
{
	if (*phead == pos)
	{
		SLTPopFront(phead);
	}
	SLTNode* cur = *phead;
	while (cur->next != pos)
	{
		cur = cur->next;
	}
	cur->next = cur->next->next;
	free(pos);
}

void SLTInsertAfter(SLTNode* pos, SLTDateType n)
{
	
	SLTNode* newnode = BuySLTNode(n);
	SLTNode* tmp = pos->next;
	pos->next = newnode;
	newnode->next = tmp;
}

void SLTEraseAfter(SLTNode* pos)
{
	SLTNode* tmp = pos->next->next;
	free(pos->next);
	pos->next = tmp;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值