#include <iostream>
using namespace std;
struct Node {
int d;
struct Node* nt;
struct Node* prv;
};
void insertFront(struct Node** h, int d) {
struct Node* newNode = new Node;
newNode->d = d;
newNode->nt = (*h);
newNode->prv = NULL;
if ((*h) != NULL)
(*h)->prv = newNode;
(*h) = newNode;
}
void insertAfter(struct Node* prev_node, int d) {
if (prev_node == NULL) {
cout << "Cannot be null";
return;
}
struct Node* newNode = new Node;
newNode->d = d;
newNode->nt = prev_node->nt;
prev_node->nt = newNode;
newNode->prv = prev_node;
if (newNode->nt != NULL)
newNode->nt->prv = newNode;
}
void insertEnd(struct Node** h, int d) {
struct Node* newNode = new Node;
newNode->d = d;
newNode->nt = NULL;
struct Node* temp = *h;
if (*h == NULL) {
newNode->prv = NULL;
*h = newNode;
return;
}
while (temp->nt != NULL)
temp = temp->nt;
temp->nt = newNode;
newNode->prv = temp;
}
void deleteNode(struct Node** h, struct Node* del_node) {
if (*h == NULL || del_node == NULL)
return;
if (*h == del_node)
*h = del_node->nt;
if (del_node->nt != NULL)
del_node->nt->prv = del_node->prv;
if (del_node->prv != NULL)
del_node->prv->nt = del_node->nt;
free(del_node);
}
void displayList(struct Node* node) {
struct Node* last;
while (node != NULL) {
cout << node->d << "->";
last = node;
node = node->nt;
}
if (node == NULL)
cout << "NULL\n";
}
int main() {
struct Node* h = NULL;
insertEnd(&h, 1);
insertFront(&h, 2);
insertFront(&h, 3);
insertEnd(&h, 4);
insertAfter(h, 5);
insertAfter(h->nt, 6);
displayList(h);
deleteNode(&h, h->nt->nt->nt->nt->nt);
displayList(h);
}
Comments
Leave a comment