#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
单链表的实现
最新推荐文章于 2024-09-01 23:02:17 发布