You are on page 1of 8

Double Link List:

1 #include <iostream>
2 using namespace std;
3
4 // A doubly linked list node
5 struct Node {
6 int data;
7 struct Node* next;
8 struct Node* prev;
9 };
10
11 //inserts node at the front of the list
12 void insert_front(struct Node** head, int new_data)
13 {
14 //allocate memory for New node
15 struct Node* newNode = new Node;
16
17 //assign data to new node
18 newNode->data = new_data;
19
//new node is head and previous is null, since we are adding at the
20
front
21 newNode->next = (*head);
22 newNode->prev = NULL;
23
24 //previous of head is new node
25 if ((*head) != NULL)
26 (*head)->prev = newNode;
27
28 //head points to new node
29 (*head) = newNode;
30 }
/* Given a node as prev_node, insert a new node after the given node
31
*/
32 void insert_After(struct Node* prev_node, int new_data)
33 {
34 //check if prev node is null
35 if (prev_node == NULL) {
36 cout<<"Previous node is required , it cannot be NULL";
37 return;
38 }
39 //allocate memory for new node
40 struct Node* newNode = new Node;
41
42 //assign data to new node
43 newNode->data = new_data;
44
45 //set next of newnode to next of prev node
46 newNode->next = prev_node->next;
47
48 //set next of prev node to newnode
49 prev_node->next = newNode;
50
51 //now set prev of newnode to prev node
52 newNode->prev = prev_node;
53
54 //set prev of new node's next to newnode
55 if (newNode->next != NULL)
56 newNode->next->prev = newNode;
57 }
58
59 //insert a new node at the end of the list
60 void insert_end(struct Node** head, int new_data)
61 {
62 //allocate memory for node
63 struct Node* newNode = new Node;
64
65 struct Node* last = *head; //set last node value to head
66
67 //set data for new node
68 newNode->data = new_data;
69
70 //new node is the last node , so set next of new node to null
71 newNode->next = NULL;
72
73 //check if list is empty, if yes make new node the head of list
74 if (*head == NULL) {
75 newNode->prev = NULL;
76 *head = newNode;
77 return;
78 }
79
80 //otherwise traverse the list to go to last node
81 while (last->next != NULL)
82 last = last->next;
83
84 //set next of last to new node
85 last->next = newNode;
86
87 //set last to prev of new node
88 newNode->prev = last;
89 return;
90 }
91
// This function prints contents of linked list starting from the given
92
node
93 void displayList(struct Node* node) {
94 struct Node* last;
95
96 while (node != NULL) {
97 cout<<node->data<<"<==>";
98 last = node;
99 node = node->next;
100 }
101 if(node == NULL)
102 cout<<"NULL";
103 }
104
105 //main program
106 int main() {
107 /* Start with the empty list */
108 struct Node* head = NULL;
109
110 // Insert 40 as last node
111 insert_end(&head, 40);
112
113 // insert 20 at the head
114 insert_front(&head, 20);
115
116 // Insert 10 at the beginning.
117 insert_front(&head, 10);
118
119 // Insert 50 at the end.
120 insert_end(&head, 50);
121
122 // Insert 30, after 20.
123 insert_After(head->next, 30);
124
125 cout<<"Doubly linked list is as follows: "<<endl;
126 displayList(head);
127 return 0;
128 }

Deleting At Beginning:
#include <bits/stdc++.h>
using namespace std;

/* a node of the doubly linked list */


class Node
{
public:
int data;
Node* next;
Node* prev;
};

/* Function to delete a node in a Doubly Linked List.


head_ref --> pointer to head node pointer.
del --> pointer to node to be deleted. */
void deleteNode(Node** head_ref, Node* del)
{
/* base case */
if (*head_ref == NULL || del == NULL)
return;

/* If node to be deleted is head node */


if (*head_ref == del)
*head_ref = del->next;

/* Change next only if node to be


deleted is NOT the last node */
if (del->next != NULL)
del->next->prev = del->prev;

/* Change prev only if node to be


deleted is NOT the first node */
if (del->prev != NULL)
del->prev->next = del->next;

/* Finally, free the memory occupied by del*/


free(del);
return;
}

/* Function to print nodes in a given doubly linked list


This function is same as printList() of singly linked list */
void printList(Node* node)
{
while (node != NULL)
{
cout << node->data << " ";
node = node->next;
}
}

/* Driver code*/
int main()
{
/* Start with the empty list */
Node* head = NULL;

/* Let us create the doubly linked list 10<->8<->4<->2 */


push(&head, 2);
push(&head, 4);
push(&head, 8);
push(&head, 10);

cout << "Original Linked list ";


printList(head);

/* delete nodes from the doubly linked list */


deleteNode(&head, head); /*delete first node*/
deleteNode(&head, head->next); /*delete middle node*/
deleteNode(&head, head->next); /*delete last node*/
/* Modified linked list will be NULL<-8->NULL */
cout << "\nModified Linked list ";
printList(head);

return 0;
}

You might also like