-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.cpp
157 lines (119 loc) · 2.83 KB
/
LinkedList.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
/*
A linked list implementation using smart pointers
Program handles 5 types of queries (for linked list of size N) :
1. Insert at beginning of list - O(1) complexity
2. Insert at end of list - O(N) complexity
3. Delete at beginning of list - O(1) complexity
4. Delete at end of list - O(N) complexity
5. Print linked list - O(N) complexity
*/
#include <memory>
#include <iostream>
#include <algorithm>
struct node{
int data;
std::shared_ptr<node> next;
node():next(nullptr),data(0){}
node(int val):next(nullptr),data(val){}
node(int val, std::shared_ptr<node> Next):next(Next),data(val){}
};
void insertAtBeg(std::shared_ptr<node>& head){
int val;
std::cout<<"\nEnter value:";
std::cin>>val;
std::shared_ptr<node> newHead = std::make_shared<node>(val,head);
head = newHead;
}
void insertAtEnd(std::shared_ptr<node>& head){
int val;
std::cout<<"\nEnter value:";
std::cin>>val;
std::shared_ptr<node> newRear = std::make_shared<node>(val);
std::shared_ptr<node> headCopy = head;
std::weak_ptr<node> tracker = headCopy;
if(tracker.expired()){
head = newRear;
}
else{
headCopy = head;
tracker = (headCopy->next);
while(!tracker.expired()){
headCopy = headCopy->next;
tracker = headCopy->next;
}
headCopy->next = newRear;
}
}
void deleteAtBeg(std::shared_ptr<node>& head){
std::weak_ptr<node> tracker = head;
if(!tracker.expired()){
head = head->next;
}
}
void deleteAtEnd(std::shared_ptr<node>& head){
std::weak_ptr<node> tracker = head;
std::shared_ptr<node> headCopy;
if(!tracker.expired()){
tracker = head->next;
if(!tracker.expired()){
headCopy = head;
tracker = (headCopy->next->next);
while(!tracker.expired()){
headCopy = headCopy->next;
tracker = headCopy->next->next;
}
headCopy->next = nullptr;
}
else{
head = nullptr;
}
}
}
void print(std::shared_ptr<node> head){
std::weak_ptr<node> tracker = head;
std::cout<<"\nLinked list contents are: ";
while(!tracker.expired()){
std::cout<<(head->data)<<" -> ";
head = head->next;
tracker = head;
}
std::cout<<"NULL\n";
return ;
}
void processQuery(int queryType){
static std::shared_ptr<node> head;
if(queryType == 1){
insertAtBeg(head);
}
else if(queryType == 2){
insertAtEnd(head);
}
else if(queryType == 3){
deleteAtBeg(head);
}
else if(queryType == 4){
deleteAtEnd(head);
}
else if(queryType == 5){
print(head);
}
}
void input(){
int queryType,queries;
bool interactive = false;
std::cout<<"\nEnter number of queries(0 for interactive session): ";
std::cin>>queries;
queries = std::max(queries,0);
interactive = (queries==0);
if(interactive)std::cout<<"\nInteractive mode. Press Ctrl + C to quit \n\n";
while(interactive || queries>0){
std::cout<<"\nEnter query type: ";
std::cin>>queryType;
processQuery(queryType);
queries--;
}
}
int main(){
input();
return 0;
}