typedef int ElemType;
typedef struct ListNode //结点
{
struct ListNode* next; //指针域next
ElemType data; //数据域
}ListNode;
typedef struct
{
struct ListNode* head;
int cursize;
}LinkList;
ListNode* Buynode()
{
ListNode* s = (ListNode*)malloc(sizeof(ListNode));
if (nullptr == s) exit(1);
memset(s, 0, sizeof(ListNode));
return s;
}
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");
}
ListNode* FindValue(LinkList* plist, ElemType val)
{
assert(plist != nullptr);
ListNode* p = plist->head->next; // first;
while (p != nullptr && p->data != val)
{
p = p->next;
}
return p;
}
ListNode* FindValue_Pre(LinkList* plist, ElemType val)//法二(一个指针)
{
assert(plist != nullptr);
ListNode* p = plist->head; //first data;
while (p->next != nullptr && p->next->data != val)
{
p = p->next;
}
if (p->next == nullptr)
{
p = nullptr;
}
return p;
}
void Print_List(LinkList list)
{
ListNode* p = list.head->next;
while (p != nullptr)
{
printf("%5d", p->data);
p = p->next;
}
printf("\n");
}
ListNode* FindPos(LinkList* plist, int pos)
{
assert(plist != nullptr);
if (pos < 1 || pos > plist->cursize) return nullptr;
ListNode* p = plist->head->next; //first;
int i = 1;
while (i < pos)
{
p = p->next;
++i;
}
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; //first;
int i = 1;
while (i < pos)
{
p = p->next;
++i;
}
return p;
}
void Insert(LinkList* plist, ListNode* pre, ElemType val)
{
assert(plist != nullptr&& pre !=nullptr);
ListNode* s = Buynode();
s->data = val;
s->next = pre->next;
pre->next = s;
plist->cursize += 1;
}
bool Insert_Item(LinkList* plist, int pos, ElemType val)
{
assert(plist != nullptr);
ListNode* p = FindPos_Pre(plist, pos);
if (p == nullptr) return false;
ListNode* s = Buynode();
s->data = val;
s->next = p->next;
p->next = s;
plist->cursize += 1;
return true;
}
void push_back(LinkList* plist, ElemType val)
{
Insert_Item(plist, plist->cursize + 1, val);
}
void push_front(LinkList* plist, ElemType val)
{
Insert_Item(plist, 1, val);
}
void Insert_Ar(LinkList* plist, int pos, ElemType* ar, int n)
{
for (int i = 0; i < n; ++i)
{
Insert_Item(plist, pos, ar[i]);
}
}
void Insert_Val(LinkList* plist, int pos, ElemType val, int n)
{
for (int i = 0; i < n; ++i)
{
Insert_Item(plist, pos, val);
}
}
void Erase_Next(LinkList* plist, ListNode* pre) //删除节点
{
assert(plist != nullptr && pre != nullptr && pre->next!=nullptr);
ListNode* p = pre->next;
pre->next = p->next;
free(p);
p = 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 true;
}
void pop_back(LinkList* plist)
{
Erase_Pos(plist, plist->cursize);
}
void pop_front(LinkList* plist)
{
Erase_Pos(plist, 1);
}
void remove(LinkList* plist, ElemType val)
{
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);
free(plist->head);
plist->head = nullptr;
}
void remove_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);
Insert_Val(&mylist, 1, 23, 10);
Erase_Pos(&mylist, 3);
Print_List(&mylist);
return 0;
}
void Print_List(LinkList* plist)
//单链表打印(1) 传递地址 更节省空间
{
assert(plist != nullptr);
ListNode* p = plist->head->next;
while (p != nullptr)
{
printf("%5d", p->data);
p = p->next;
}
printf("\n");
}
Print_List(&mylist);
void Print_List(LinkList list)
//单链表打印(2) 传递值
{
ListNode* p = list.head->next;
while (p != nullptr)
{
printf("%5d", p->data);
p = p->next;
}
printf("\n");
}
Print_List(mylist);
(*p).data 等价于 p->data