#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int Datatype;
typedef struct ListNode
{
Datatype _val;
struct ListNode* _prev;
struct ListNode* _next;
}ListNode;
typedef struct List
{
ListNode* _head;
}List;
void ListInt(List* pcb);
void ListDestroy(List* pcb);
void List_print(List* pcb);
void ListHead_insert(List* pcb, Datatype x);
void ListHead_delete(List* pcb);
void ListTail_insert(List* pcb, Datatype x);
void ListTail_delete(List* pcb);
ListNode* BuyListNode(int val);
ListNode* List_find(List* pcb, Datatype x);
void ListFind_insert(ListNode* pos,Datatype x);
void ListFind_delete(ListNode* pos1);
#include"Dlist.h"
ListNode* BuyListNode(Datatype val)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
newnode->_next = NULL;
newnode->_prev = NULL;
newnode->_val = val;
return newnode;
}
void ListInt(List* pcb)
{
assert(pcb);
ListNode* head = (ListNode*)malloc(sizeof(ListNode));
head->_next = head;
head->_prev = head;
pcb->_head = head;
}
void ListTail_insert(List* pcb, Datatype x)
{
assert(pcb);
ListNode* head = pcb->_head;
ListFind_insert(head, x);
}
void ListTail_delete(List* pcb)
{
assert(pcb);
ListNode* head = pcb->_head;
ListFind_delete(head->_prev);
}
void ListHead_insert(List* pcb, Datatype x)
{
assert(pcb);
ListNode* head = pcb->_head;
ListFind_insert(head->_next, x);
}
void ListHead_delete(List* pcb)
{
assert(pcb);
ListNode* head = pcb->_head;
ListFind_delete(head->_next);
}
ListNode* List_find(List* pcb, Datatype x)
{
assert(pcb);
ListNode* head = pcb->_head;
ListNode* cur = head->_next;
while (cur != head)
{
if (cur->_val == x)
{
return cur;
}
cur = cur->_next;
}
return NULL;
}
void ListFind_insert(ListNode* pos,Datatype x)
{
assert(pos);
ListNode* prev_pos = pos->_prev;
ListNode* newnode = BuyListNode(x);
prev_pos->_next = newnode;
newnode->_prev = prev_pos;
newnode->_next = pos;
pos->_prev = newnode;
}
void ListFind_delete(ListNode* pos1)
{
assert(pos1);
ListNode* pos1_next = pos1->_next;
ListNode* pre_pos1 = pos1->_prev;
pre_pos1->_next = pos1_next;
pos1_next->_prev = pre_pos1;
free(pos1);
}
void List_print(List* pcb)
{
assert(pcb);
ListNode* head = pcb->_head;
ListNode* cur = head->_next;
printf("head<=>");
while (cur != head)
{
printf("%d<=>", cur->_val);
cur = cur->_next;
}
printf("\n");
}
void ListDestroy(List* pcb)
{
assert(pcb);
ListNode* head = pcb->_head;
ListNode* cur = head->_next;
ListNode* next = NULL;
while (cur != head)
{
next = cur->_next;
free(cur);
cur = next;
}
}
#include"Dlist.h"
void List_test1()
{
List cb;
ListInt(&cb);
ListHead_insert(&cb, 1);
ListHead_insert(&cb, 2);
ListHead_insert(&cb, 3);
ListHead_insert(&cb, 4);
ListHead_insert(&cb, 5);
ListHead_delete(&cb);
ListHead_delete(&cb);
ListHead_delete(&cb);
ListHead_delete(&cb);
List_print(&cb);
}
void List_test2()
{
List cb;
ListInt(&cb);
ListTail_insert(&cb, 1);
ListTail_insert(&cb, 2);
ListTail_insert(&cb, 3);
ListTail_insert(&cb, 4);
ListTail_insert(&cb, 5);
ListTail_delete(&cb);
ListTail_delete(&cb);
ListTail_delete(&cb);
ListTail_delete(&cb);
List_print(&cb);
ListNode* pos = List_find(&cb,1);
ListFind_insert( pos, 5);
List_print(&cb);
ListNode* pos1 = List_find(&cb, 3);
ListFind_delete(pos1);
List_print(&cb);
ListDestroy(&cb);
}
int main()
{
List_test2();
system("pause");
return 0;
}