Double Linked List
Double Linked List
h>
#define ll long long
using namespace std;
struct node{
int value;
node *next, *prev;
};
int listSize = 0;
node* newNode(int x) {
node* newNode = (node*)malloc(sizeof(struct node));
newNode->value = x;
newNode->next = NULL;
newNode->prev = NULL;
listSize++;
return newNode;
}
void pushHead(int x) {
if(!head) {
head = tail = newNode(x);
return;
}
node *temp = newNode(x);
head->prev = temp;
temp->next = head;
head = temp;
return;
}
void pushTail(int x) {
if(!head) {
head = tail = newNode(x);
return;
}
node *temp = newNode(x);
temp->prev = tail;
tail->next = temp;
tail = temp;
return;
}
int popHead() {
if(!head) {
return -1;
}
int ret = head->value;
curr = head;
head = head->next;
head->prev = NULL;
free(curr);
listSize--;
return ret;
}
int popTail() {
if(!head) {
return -1;
}
if(head == tail) {
popHead();
}
int ret;
ret = tail->value;
curr = tail;
tail->prev->next = NULL;
tail = tail->prev;
free(curr);
listSize--;
return ret;
}
void showLL() {
curr = head;
while(curr) {
cout << curr->value << " - ";
curr = curr->next;
}
cout << endl << listSize;
}
void showLLreverse() {
curr = tail;
while(curr) {
cout << curr->value << " - ";
curr = curr->prev;
}
}
int main() {
pushHead(1);
pushHead(6);
pushTail(2);
pushTail(5);
showLL();
cout << endl;
pushMid(4,3);
showLL();
cout << endl;
popMid(3);
showLL();
cout << endl;
showLLreverse();
}