forked from AyanSharma1001/Hacktoberfest22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoublyLinkedList.cpp
157 lines (155 loc) · 3.25 KB
/
DoublyLinkedList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//DOUBLY LINKED LIST
#include<bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
Node* prev;
//Default Constructor
Node()
{
data=0;
next=NULL;
prev=NULL;
}
//Parametrised constructor
Node(int data){
this->data = data;
this->next = NULL;
this->prev = NULL;
}
};
class LinkedList {
Node* head;
public:
//Default constructor
LinkedList()
{
head=NULL;
}
void traversal();
void insertAtbeginning(int);
void insertAtlast(int);
void insertAtindex(int , int);
void deleteAtbeginning();
void deleteAtlast();
void deleteAtindex(int);
};
void LinkedList::traversal(){
Node* temp= head;
if(temp != NULL){
cout<<"The list contains: ";
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
else {
cout<<"The list is empty"<<endl;
}
}
void LinkedList::insertAtbeginning(int new_data){
Node* newNode = new Node(new_data);
if(head==NULL){
newNode->prev=NULL;
head = newNode;
return;
}
newNode->next = head;
newNode->prev= NULL;
head->prev=newNode;
head=newNode;
}
void LinkedList::insertAtlast(int new_data){
Node* newNode = new Node(new_data);
if(head==NULL){
newNode->prev=NULL;
head = newNode;
return;
}
Node* temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=newNode;
newNode->prev=temp;
}
void LinkedList::insertAtindex(int new_data, int index){
Node* newNode = new Node(new_data);
if(head==NULL){
newNode->prev=NULL;
head = newNode;
return;
}
int i=0;
Node* p=head;
Node* q=head->next;
while(i!=index-1){
i++;
p=p->next;
q=q->next;
}
p->next=newNode;
newNode->prev=p;
newNode->next=q;
q->prev=newNode;
}
void LinkedList::deleteAtbeginning(){
if(head==NULL){
return;
}
Node* temp=head;
head=temp->next;
head->prev=temp;
free(temp);
}
void LinkedList::deleteAtlast(){
if(head==NULL){
return;
}
Node* p=head;
Node* q=head->next;
while(q->next!=NULL){
p=p->next;
q=q->next;
}
p->next=NULL;
free(q);
}
void LinkedList::deleteAtindex(int index){
if(head==NULL){
return;
}
Node* p=head;
Node* q=head->next;
for(int i=0;i<index-1;i++){
p=p->next;
q=q->next;
}
p->next=q->next;
p->next->prev=p;
free(q);
}
int main(){
LinkedList list;
//inserting nodes
list.insertAtbeginning(1);
list.insertAtbeginning(2);
list.insertAtbeginning(3);
list.insertAtbeginning(4);
list.insertAtlast(5);
list.insertAtlast(6);
list.insertAtindex(7,2);
list.insertAtindex(8,4);
list.deleteAtbeginning();
list.deleteAtlast();
list.deleteAtindex(3);
list.deleteAtindex(2);
cout<<"Element of the list are: ";
//traversing the list
list.traversal();
cout<<endl;
return 0;
}