Skip to content

Commit dfe5ba7

Browse files
committed
Linked List Questions
1 parent c3ac112 commit dfe5ba7

File tree

5 files changed

+635
-0
lines changed

5 files changed

+635
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include<iostream>
2+
using namespace std;
3+
class Node{
4+
public:
5+
int data;
6+
Node * next;
7+
Node(int data){
8+
this->data = data;
9+
this-> next = NULL;
10+
}
11+
};
12+
int main(){
13+
Node* node1 = new Node(10);
14+
cout << node1 -> data << endl;
15+
cout << node1 -> next << endl;
16+
return 0;
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include<iostream>
2+
using namespace std;
3+
class Node{
4+
public:
5+
int data;
6+
Node * next;
7+
Node(int data){
8+
this->data = data;
9+
this->next = NULL;
10+
}
11+
};
12+
void InsertionAtHead(Node* &head, int data){
13+
Node * node1 = new Node(data);
14+
node1->next = head;
15+
head = node1;
16+
}
17+
void printLinkedList(Node* head){
18+
while(head != NULL){
19+
cout << head->data << " ";
20+
head = head->next;
21+
}
22+
cout << endl;
23+
}
24+
int main(){
25+
Node * node1 = new Node(10);
26+
cout << "Initial Linked List: ";
27+
printLinkedList(node1);
28+
InsertionAtHead(node1, 20);
29+
cout << "Linked List after insertion at head: ";
30+
printLinkedList(node1);
31+
return 0;
32+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#include<iostream>
2+
#include<map>
3+
using namespace std;
4+
class Node {
5+
public:
6+
int data;
7+
Node* next;
8+
//constrcutor
9+
Node(int d) {
10+
this->data = d;
11+
this->next = NULL;
12+
}
13+
~Node() {
14+
int value = this->data;
15+
if(this->next != NULL) {
16+
delete next;
17+
next = NULL;
18+
}
19+
cout << " memory is free for node with data " << value << endl;
20+
}
21+
};
22+
void insertNode(Node* &tail, int element, int d) {
23+
//empty list
24+
if(tail == NULL) {
25+
Node* newNode = new Node(d);
26+
tail = newNode;
27+
newNode -> next = newNode;
28+
}
29+
else{
30+
//non-empty list
31+
//assuming that the element is present in the list
32+
Node* curr = tail;
33+
while(curr->data != element) {
34+
curr = curr -> next;
35+
}
36+
//element found -> curr is representing element wala node
37+
Node* temp = new Node(d);
38+
temp -> next = curr -> next;
39+
curr -> next = temp;
40+
}
41+
}
42+
void print(Node* tail) {
43+
Node* temp = tail;
44+
//empty list
45+
if(tail == NULL) {
46+
cout << "List is Empty "<< endl;
47+
return ;
48+
}
49+
do {
50+
cout << tail -> data << " ";
51+
tail = tail -> next;
52+
} while(tail != temp);
53+
cout << endl;
54+
}
55+
void deleteNode(Node* &tail, int value) {
56+
//empty list
57+
if(tail == NULL) {
58+
cout << " List is empty, please check again" << endl;
59+
return;
60+
}
61+
else{
62+
//non-empty
63+
//assuming that "value" is present in the Linked List
64+
Node* prev = tail;
65+
Node* curr = prev -> next;
66+
while(curr -> data != value) {
67+
prev = curr;
68+
curr = curr -> next;
69+
}
70+
prev -> next = curr -> next;
71+
//1 Node Linked List
72+
if(curr == prev) {
73+
tail = NULL;
74+
}
75+
//>=2 Node linked list
76+
else if(tail == curr ) {
77+
tail = prev;
78+
}
79+
curr -> next = NULL;
80+
delete curr;
81+
}
82+
}
83+
bool isCircularList(Node* head) {
84+
//empty list
85+
if(head == NULL) {
86+
return true;
87+
}
88+
Node* temp = head -> next;
89+
while(temp != NULL && temp != head ) {
90+
temp = temp -> next;
91+
}
92+
if(temp == head ) {
93+
return true;
94+
}
95+
return false;
96+
}
97+
bool detectLoop(Node* head) {
98+
if(head == NULL)
99+
return false;
100+
map<Node*, bool> visited;
101+
Node* temp = head;
102+
while(temp !=NULL) {
103+
//cycle is present
104+
if(visited[temp] == true) {
105+
return true;
106+
}
107+
visited[temp] = true;
108+
temp = temp -> next;
109+
}
110+
return false;
111+
}
112+
int main() {
113+
Node* tail = NULL;
114+
// insertNode(tail, 5, 3);
115+
//print(tail);
116+
// insertNode(tail, 3, 5);
117+
// print(tail);
118+
/*
119+
insertNode(tail, 5, 7);
120+
print(tail);
121+
insertNode(tail, 7, 9);
122+
print(tail);
123+
insertNode(tail, 5, 6);
124+
print(tail);
125+
insertNode(tail, 9, 10);
126+
print(tail);
127+
insertNode(tail, 3, 4);
128+
print(tail);
129+
deleteNode(tail, 5);
130+
print(tail);
131+
*/
132+
if(isCircularList(tail)) {
133+
cout << " Linked List is Circular in nature" << endl;
134+
}
135+
else{
136+
cout << "Linked List is not Circular " << endl;
137+
}
138+
139+
return 0;
140+
}

Linked_List/doublyLinked_List.cpp

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#include<iostream>
2+
using namespace std;
3+
class Node {
4+
public:
5+
int data;
6+
Node* prev;
7+
Node* next;
8+
//constructor
9+
Node(int d ) {
10+
this-> data = d;
11+
this->prev = NULL;
12+
this->next = NULL;
13+
}
14+
~Node() {
15+
int val = this -> data;
16+
if(next != NULL) {
17+
delete next;
18+
next = NULL;
19+
}
20+
cout << "memory free for node with data "<< val << endl;
21+
}
22+
};
23+
//traversing a linked list
24+
void print(Node* head) {
25+
Node* temp = head ;
26+
while(temp != NULL) {
27+
cout << temp -> data << " ";
28+
temp = temp -> next;
29+
}
30+
cout << endl;
31+
}
32+
//gives length of Linked List
33+
int getLength(Node* head) {
34+
int len = 0;
35+
Node* temp = head ;
36+
while(temp != NULL) {
37+
len++;
38+
temp = temp -> next;
39+
}
40+
return len;
41+
}
42+
void insertAtHead(Node* &tail, Node* &head, int d) {
43+
//empty list
44+
if(head == NULL) {
45+
Node* temp = new Node(d);
46+
head = temp;
47+
tail = temp;
48+
}
49+
else{
50+
Node* temp = new Node(d);
51+
temp -> next = head;
52+
head -> prev = temp;
53+
head = temp;
54+
}
55+
56+
}
57+
void insertAtTail(Node* &tail,Node* &head, int d) {
58+
if(tail == NULL) {
59+
Node* temp = new Node(d);
60+
tail = temp;
61+
head = temp;
62+
}
63+
else{
64+
Node* temp = new Node(d);
65+
tail -> next = temp;
66+
temp -> prev = tail;
67+
tail = temp;
68+
}
69+
}
70+
void insertAtPosition(Node* & tail, Node* &head, int position, int d) {
71+
//insert at Start
72+
if(position == 1) {
73+
insertAtHead(tail,head, d);
74+
return;
75+
}
76+
Node* temp = head;
77+
int cnt = 1;
78+
while(cnt < position-1) {
79+
temp = temp->next;
80+
cnt++;
81+
}
82+
//inserting at Last Position
83+
if(temp -> next == NULL) {
84+
insertAtTail(tail,head,d);
85+
return ;
86+
}
87+
//creating a node for d
88+
Node* nodeToInsert = new Node(d);
89+
nodeToInsert ->next = temp -> next;
90+
temp -> next -> prev = nodeToInsert;
91+
temp -> next = nodeToInsert;
92+
nodeToInsert -> prev = temp;
93+
}
94+
void deleteNode(int position, Node* & head) {
95+
//deleting first or start node
96+
if(position == 1) {
97+
Node* temp = head;
98+
temp -> next -> prev = NULL;
99+
head = temp ->next;
100+
temp -> next = NULL;
101+
delete temp;
102+
}
103+
else
104+
{
105+
//deleting any middle node or last node
106+
Node* curr = head;
107+
Node* prev = NULL;
108+
int cnt = 1;
109+
while(cnt < position) {
110+
prev = curr;
111+
curr = curr -> next;
112+
cnt++;
113+
}
114+
curr -> prev = NULL;
115+
prev -> next = curr -> next;
116+
curr -> next = NULL;
117+
delete curr;
118+
}
119+
}
120+
int main() {
121+
Node* head = NULL;
122+
Node* tail = NULL;
123+
print(head);
124+
//cout << getLength(head) << endl;
125+
insertAtHead(tail,head, 11);
126+
print(head);
127+
cout << "head " << head -> data << endl;
128+
cout << "tail " << tail -> data << endl;
129+
insertAtHead(tail,head, 13);
130+
print(head);
131+
cout << "head " << head -> data << endl;
132+
cout << "tail " << tail -> data << endl;
133+
insertAtHead(tail,head, 8);
134+
print(head);
135+
cout << "head " << head -> data << endl;
136+
cout << "tail " << tail -> data << endl;
137+
insertAtTail(tail,head, 25);
138+
print(head);
139+
cout << "head " << head -> data << endl;
140+
cout << "tail " << tail -> data << endl;
141+
insertAtPosition(tail, head, 2, 100);
142+
print(head);
143+
cout << "head " << head -> data << endl;
144+
cout << "tail " << tail -> data << endl;
145+
insertAtPosition(tail, head, 1, 101);
146+
print(head);
147+
cout << "head " << head -> data << endl;
148+
cout << "tail " << tail -> data << endl;
149+
insertAtPosition(tail, head, 7, 102);
150+
print(head);
151+
cout << "head " << head -> data << endl;
152+
cout << "tail " << tail -> data << endl;
153+
deleteNode(7, head);
154+
print(head);
155+
cout << "head " << head -> data << endl;
156+
cout << "tail " << tail -> data << endl;
157+
return 0;
158+
}

0 commit comments

Comments
 (0)