#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
struct Node
{
int value;
Node* prev;
Node* next;
void Init()
{
value = 0;
prev = NULL;
next = NULL;
}
};
Node* list1 = NULL;
Node* list1End = NULL;
void pushFrontToList(Node* addNode)
{
printf("push Front : %d\n", addNode->value);
if (list1 != NULL)
{
list1->prev = addNode;
addNode->next = list1;
}
else
{
list1End = addNode;
}
list1 = addNode;
}
void pushBackToList(Node* addNode)
{
printf("push Back : %d\n", addNode->value);
if (list1End != NULL)
{
list1End->next = addNode;
addNode->prev = list1End;
}
else
{
list1 = addNode;
}
list1End = addNode;
}
void RemoveFromList(Node* node)
{
printf("제거 : %d\n", node->value);
if (node == list1End || node == list1)
{
if (list1End == list1)
{
//하나밖에 없을 때
list1End = NULL;
list1 = NULL;
}
else
{
if (node == list1)
{
node->next->prev = NULL;
list1 = node->next;
}
else
{
node->prev->next = NULL;
list1End = node->prev;
}
}
}
else
{
node->prev->next = node->next;
node->next->prev = node->prev;
}
}
int main()
{
const int size = 10;
Node* nodes[size];
for (int i = 0; i < size; ++i)
{
Node* N = new Node;
N->Init();
N->value = i;
printf("%d ", N->value);
pushBackToList(N);
nodes[i] = N;
}
for (int i = size - 1; i >= 0; i -= 2)
{
RemoveFromList(nodes[i]);
}
Node* N2 = new Node;
N2->Init();
N2->value = 20;
pushFrontToList(N2);
RemoveFromList(nodes[8]);
RemoveFromList(nodes[0]);
Node* N3 = new Node;
N3->Init();
N3->value = 123;
pushFrontToList(N3);
RemoveFromList(N2);
Node* curNode = list1;
while (curNode)
{
printf("출력값 : %d\n", curNode->value);
curNode = curNode->next;
}
getchar();
}