#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
//typedef struct NodeStruct {
// int data;
// struct NodeStruct* next;
//} Node;
typedef struct DoubleEdgeNodeStruct {
int data;
struct DoubleEdgeNodeStruct* prev;
struct DoubleEdgeNodeStruct* next;
} DoubleEdgeNode;
typedef struct {
DoubleEdgeNode* head;
DoubleEdgeNode* tail;
} DoublyLL;
//Node* CreateNode(int data) {
// Node* newNode = (Node*)malloc(sizeof(Node));
// newNode->data = data;
// newNode->next = NULL;
// return newNode;
//}
DoubleEdgeNode* CreateDoubleEdgeNode(int data) {
DoubleEdgeNode* newNode = (DoubleEdgeNode*)malloc(sizeof(DoubleEdgeNode));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
DoublyLL* CreateDoublyLL() {
DoublyLL* list = (DoublyLL*)malloc(sizeof(DoublyLL));
list->head = CreateDoubleEdgeNode(0);
list->tail = CreateDoubleEdgeNode(0);
list->head->next = list->tail;
list->tail->prev = list->head;
return list;
}
//void LinkedListPush(Node* head, int data) {
// Node* tail = head;
// while (tail->next != NULL) {
// tail = tail->next;
// }
// tail->next = CreateNode(data);
//}
//
//void LinkedListPop(Node* head) {
// Node* node = head;
// while (node->next->next != NULL) {
// node = node->next;
// }
// free(node->next);
// node->next = NULL;
//}
//
//void LinkedListDestroy(Node* head) {
// Node* node = head;
// Node* nextNode;
// while (node != NULL) {
// nextNode = node->next;
// free(node);
// node = nextNode;
// }
//}
//
//void LinkedListRemove(Node* head, int data) {
// Node* node = head;
// while (node->next->data != data) {
// node = node->next;
// if (node->next == NULL) {
// return;
// }
// }
// Node* node2 = node->next->next;
// free(node->next);
// node->next = node2;
//}
//
//void LinkedListInsert(Node* head, int prev, int data) {
// Node* node = head->next;
// while (node->data != prev) {
// node = node->next;
// if (node == NULL) {
// return;
// }
// }
// Node* node2 = node->next;
// node->next = CreateNode(data);
// node->next->next = node2;
//}
void DoublyLLInsertBack(DoublyLL* list, int data) {
DoubleEdgeNode* newNode = CreateDoubleEdgeNode(data);
DoubleEdgeNode* dummyNode = list->tail;
DoubleEdgeNode* prevNode = dummyNode->prev;
prevNode->next = newNode;
dummyNode->prev = newNode;
newNode->prev = prevNode;
newNode->next = dummyNode;
}
void DoublyLLInsertAfter(DoublyLL* list, int prev, int data) {
DoubleEdgeNode* prevNode = list->head->next;
while (prevNode->data != prev) {
prevNode = prevNode->next;
if (prevNode == list->tail) {
return;
}
}
DoubleEdgeNode* newNode = CreateDoubleEdgeNode(data);
DoubleEdgeNode* nextNode = prevNode->next;
prevNode->next = newNode;
nextNode->prev = newNode;
newNode->prev = prevNode;
newNode->next = nextNode;
}
void DoublyLLRemoveBack(DoublyLL* list) {
DoubleEdgeNode* dummyNode = list->tail;
DoubleEdgeNode* prevNode = dummyNode->prev->prev;
if (prevNode == NULL) {
return;
}
free(prevNode->next);
prevNode->next = dummyNode;
dummyNode->prev = prevNode;
}
void DoublyLLRemove(DoublyLL* list, int data) {
DoubleEdgeNode* prevNode = list->head;
while (prevNode->next->data != data) {
prevNode = prevNode->next;
if (prevNode == list->tail) {
return;
}
}
DoubleEdgeNode* nextNode = prevNode->next->next;
free(prevNode->next);
prevNode->next = nextNode;
nextNode->prev = prevNode;
}
void DoublyLLDestroy(DoublyLL* list) {
DoubleEdgeNode* node = list->head;
DoubleEdgeNode* nextNode;
while (node != NULL) {
nextNode = node->next;
free(node);
node = nextNode;
}
free(list);
}
int main() {
// Node* listHead = CreateNode(0);
// LinkedListPush(listHead, 41);
// LinkedListPush(listHead, 14);
// LinkedListPop(listHead);
// LinkedListPush(listHead, -5);
// LinkedListInsert(listHead, 11, 8);
// LinkedListPush(listHead, 63);
// LinkedListRemove(listHead, 41);
// for (Node* node = listHead->next; node != NULL; node = node->next) {
// printf("%d ", node->data);
// }
// LinkedListDestroy(listHead);
DoublyLL* list = CreateDoublyLL();
DoublyLLInsertBack(list, 41);
DoublyLLInsertBack(list, 14);
DoublyLLRemoveBack(list);
DoublyLLInsertBack(list, -5);
DoublyLLInsertAfter(list, 41, 8);
DoublyLLInsertBack(list, 63);
DoublyLLRemove(list, 41);
for (DoubleEdgeNode* node = list->head->next; node != list->tail; node = node->next) {
printf("%d ", node->data);
}
DoublyLLDestroy(list);
return 0;
}