单链表的实现

#include<stdio.h>  // pirntf; scanf;
#include<stdlib.h>	// malloc free exit 
#include<assert.h>  // assert;
#include<string.h>
#if 0
typedef int ElemType;
typedef struct ListNode
{
	int data;
	struct ListNode* next;
}ListNode;
typedef struct
{
	struct ListNode* head;
	int cursize;
}LinkList;

static ListNode* Buynode()       //此函数是静态函数
{
	ListNode* s = (ListNode*)malloc(sizeof(ListNode));
	if (s == nullptr) exit(EXIT_FAILURE);
	memset(s, 0, sizeof(ListNode));
	return s;
}
static void Freenode(ListNode* p)
{
	free(p);
}
  
void Init_List(LinkList* plist)
{
	assert(plist != nullptr);
	plist->head = Buynode();
	plist->cursize = 0;
}
void Print_List(LinkList* plist)
{
	assert(plist != nullptr);
	ListNode* p = plist->head->next;
	while (p != nullptr)
	{
		printf("%5d ", p->data);
		p = p->next;
	}
	printf("\n");
}
int GetSize(LinkList* plist)
{
	assert(plist != nullptr);
	return plist->cursize;
}
bool Is_Empty(LinkList* plist)
{
	assert(plist != nullptr);
	return plist->cursize == 0;
}
ListNode* FindValue(LinkList* plist, ElemType  val)
{
	assert(plist != nullptr);
	ListNode* p = plist->head->next;
	while (p != nullptr && p->data!= val)
	{
		p = p->next;
	}
	return p;
}
ListNode* FindValue_Pre(LinkList* plist, ElemType  val)
{
	assert(plist != nullptr);
	ListNode* pre = plist->head;
	while (pre != nullptr && pre->next->data != val)
	{
		pre = pre->next;
	}
	if (pre->next == nullptr)
	{
		pre == nullptr;
	}
		return pre;
}
ListNode* FindPos(LinkList* plist, int pos)
{
	assert(plist != nullptr);
	if (pos<1 || pos>plist->cursize) return nullptr;
	ListNode* p = plist->head->next;
	for (int i = 1; i < pos; ++i)
	{
		p = p->next;
	}
	return p;
}
ListNode* FindPos_Pre(LinkList* plist, int pos)
{
	assert(plist != nullptr);
	if (pos<1 || pos>plist->cursize+1) return nullptr;
	ListNode* p = plist->head;
	for (int i = 1; i < pos; ++i)
	{
		p = p->next;
	}
	return p;
}
void Insert(LinkList* plist, ListNode* pre, ElemType val)
{
	assert(plist != nullptr && pre != nullptr);
	ListNode* p = Buynode();
	p->next = pre->next;
	pre->next = p;
	p->data = val;
	plist->cursize += 1;
}
bool Insert_Item(LinkList* plist, int pos, ElemType val)
{
	assert(plist != nullptr);
	ListNode*pre= FindPos_Pre(plist, pos);
	if (pre == nullptr) return false;
	Insert(plist, pre, val);
	return true;
}
void push_back(LinkList* plist, ElemType val)
{
	assert(plist != nullptr);
	Insert_Item(plist, plist->cursize+1, val);
}
void push_front(LinkList* plist, ElemType val)
{
	assert(plist != nullptr);
	Insert(plist, plist->head, val);
}
void Insert_Ar(LinkList* plist, int pos, ElemType* ar, int n)
{
	assert(plist != nullptr && ar != nullptr);
	for (int i = 0; i < n; ++i)
	{
		Insert_Item(plist, pos, ar[i]);
	}
}
void Insert_Val(LinkList* plist, int pos, ElemType val, int n)
{
	assert(plist != nullptr);
	for (int i = 0; i < n; ++i)
	{
		Insert_Item(plist, pos, val);
	}
}
void Erase_Next(LinkList* plist, ListNode* pre)
{
	assert(plist != nullptr);
	ListNode* p = pre;
	if (p == nullptr || p->next == nullptr)  exit(0);
	ListNode* q = p->next;
	p->next = q->next;
	Freenode(q);
	q = nullptr;
	plist->cursize -= 1;
}
bool Erase_Pos(LinkList* plist, int pos)
{
	assert(plist != nullptr);
	bool res = false;
	ListNode* pre = FindPos_Pre(plist, pos);
	if (pre != nullptr)
	{
		Erase_Next(plist, pre);
		res = true;
	}
	return res;
}
void pop_back(LinkList* plist)
{
	assert(plist != nullptr);
	Erase_Pos(plist, plist->cursize);
}
void pop_front(LinkList* plist)
{
	assert(plist != nullptr);
	Erase_Pos(plist, 1);
}
void remove(LinkList* plist, ElemType val)
{
	assert(plist != nullptr);
	ListNode* p = FindValue_Pre(plist, val);
	Erase_Next(plist, p);
}
void Clear_List(LinkList* plist)
{
	assert(plist != nullptr);
	while (plist->head->next != nullptr)
	{
		Erase_Next(plist, plist->head);
	}
}
void Destroy_List(LinkList* plist)
{
	assert(plist != nullptr);
	Clear_List(plist);
	Freenode(plist->head);
	plist->head = nullptr;
}
void remvoe_all(LinkList* plist, ElemType val)
{
	assert(plist != nullptr);
	ListNode* p = plist->head;
	while (p ->next!= nullptr)
	{
		if (p->next->data == val)
		{
			Erase_Next(plist, p);
		}
		else
		{ 
			p = p->next;
		}
	}
}

int main()
{
	LinkList mylist;
	Init_List(&mylist);
	Print_List(&mylist);
	for (int i = 0; i < 10; ++i)
	{
		push_back(&mylist, i + 10);
	}
	//Insert_Val(&mylist, 1, 23, 10);
	Print_List(&mylist);
	return 0;
}
#endif

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值