#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
typedef struct Node
{
int value;
struct Node* prev;
struct Node* next;
}Node;
Node* CreateNode(int data)
{
Node* newNode = (Node*)calloc(1, sizeof(Node));
assert(newNode);
newNode->value = data;
return newNode;
}
typedef struct List
{
struct Node* frontNode;
struct Node* tailNode;
int size;
}List;
List* CreateList()
{
List* list = (List*)calloc(1, sizeof(List));
if (list == NULL)exit(EXIT_FAILURE);
return list;
}
void PushFront(List* list, int val)
{
if (!list)return;
Node* newnode = CreateNode(val);
if (list->size == 0)
{
list->tailNode = newnode;
}
else
{
newnode->next = list->frontNode;
list->frontNode->prev = newnode;
}
list->frontNode = newnode;
list->size++;
}
void PushBack(List* list, int val)
{
if (!list)return;
Node* node = CreateNode(val);
if (list->size == 0)
{
list->tailNode = node;
}
else
{
node->prev = list->tailNode;
list->tailNode->next = node;
}
list->tailNode = node;
list->size++;
}
void InsertIndex(List* list, int pos, int val)
{
if (list->size == 0 || (!list))
return;
Node* cur = list->frontNode;
Node* prenode = NULL;
Node* node = CreateNode(val);
pos--;
while (pos != 0)
{
prenode = cur;
cur = cur->next;
pos--;
}
if (cur == NULL)return;
else if (cur == list->frontNode)
PushFront(list, val);
else
{
/*prenode->next = node;
node->next = cur;
cur->prev = node;
node->prev = prenode
*/
cur->prev = node;
node->next = cur;
prenode->next = node;
node->prev = prenode;
}
}
void PopFront(List* list)
{
if (!list || list->size == 0)return;
Node* nextnode = list->frontNode->next;
free(list->frontNode);
list->frontNode = nextnode;
nextnode ? (nextnode->prev = NULL) : (list->tailNode = NULL);
list->size--;
}
void PopBack(List* list)
{
if (!list || list->size == 0)return;
Node* prenode = list->tailNode->prev;
if (!prenode)
{
free(list->tailNode);
list->tailNode = NULL;
list->frontNode = NULL;
list->size--;
}
free(list->tailNode);
list->tailNode = prenode;
list->size--;
list->tailNode->next = NULL;
}
void EraseValue(List* list, int val)
{
if (!list)return;
Node* cur = list->frontNode;
Node* prenode = NULL;
while (cur)
{
prenode = cur->prev;
if ((cur->value == val) && (cur == list->frontNode))
{
list->frontNode = list->frontNode->next;
Node* temp = cur;
free(temp);
temp = NULL;
cur->prev = NULL;
cur = cur->next;
list->size--;
}
else if (cur->value == val && cur == list->tailNode)
{
list->tailNode = list->tailNode->prev;
free(cur);
cur = NULL;
list->tailNode->next = NULL;
list->size--;
break;
}
else if (cur->value == val)
{
Node* temp = cur;
prenode->next = cur->next;
cur->next->prev = prenode;
cur = cur->next;
free(temp);
temp = NULL;
list->size--;
}
cur = cur->next;
}
}
void PrintList(List* list)
{
if (!list)return;
Node* cur = list->frontNode;
while (cur)
{
printf("%d\t", cur->value);
cur = cur->next;
}
printf("\n");
}
void test()
{
List* list = CreateList();
PushFront(list, 3);
PushFront(list, 2);
PushFront(list, 1);
PushBack(list, 4);
PushBack(list, 5);
PrintList(list);
//PopFront(list);
//PopBack(list);
//InsertIndex(list, 5, 88);
// EraseValue(list,1);
// EraseValue(list,5);
EraseValue(list, 3);
PrintList(list);
}
int main()
{
test();
return 0;
}
双向链表基本操作
最新推荐文章于 2024-08-21 17:54:47 发布