-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedList(operations).cpp
195 lines (153 loc) · 3.59 KB
/
linkedList(operations).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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <iostream>
using namespace std;
struct node{
int data;
node *next;
}*head, *tail;
void LLappend(int item)
{
node *tmp = new node;
tmp -> data = item;
tmp -> next = NULL;
if(head == NULL)
{
head = tmp;
tail = head;
return;
}else{
node *trvlr = head;
while(trvlr -> next != NULL)
{
trvlr = trvlr -> next;
}
trvlr -> next = tmp;
}
}
void LLprepend(int item)
{
node *tmp = new node;
tmp -> data = item;
tmp -> next = NULL;
tmp -> next = head;
head = tmp;
}
void LLCriteriaInsertion(int item, int key)
{
node *tmp = new node;
tmp -> data = item;
tmp -> next = NULL;
char option;
node *trvlr = head;
cout<<"\n enter your option (Before/After)(B/A) : ";
cin>>option;
if(option == 'B' || option == 'b' && key == head -> data)
{
tmp -> next = head;
head = tmp;
}else
{
if(option == 'B' || option == 'b')
{
while(trvlr -> next -> data != key && trvlr != NULL)
{
trvlr = trvlr -> next;
}
}else if(option == 'A' || option == 'a')
{
while(trvlr -> data != key && trvlr != NULL)
{
trvlr = trvlr -> next;
}
}else
{
cout<<"\n Invalid option ";
}
if(option =='a' || option == 'A' || option == 'B' || option == 'b')
{
tmp -> next = trvlr->next;
trvlr->next = tmp;
}
}
}
void printList()
{
node *t = head;
if(head == NULL)
{
cout<<"\n Linked list is empty..";
}else
{
cout<<"\n Nodes in structure: head";
while(t != NULL)
{
cout<<"("<<& t->data<<")->"<<t->data;
t = t->next;
}
cout<<"->Null \n";
}
}
void reverseDisplay_DLL(struct node *head) // this function displays the nodes of the linked list in a
{ // reverse way using recursion
if(head == NULL)
return;
reverseDisplay_DLL(head->next);
cout <<head->data << "->";
}
/* prints nodes of a Linked List, first
from head to end, and then from end to head using recursion. */
void startEnd_endStart_display(node* head)
{
if(head == NULL)
return;
cout << head->data << " ";
if(head->next != NULL )
startEnd_endStart_display(head->next); // if u want to print alternate nodes(skip one and print
cout << head->data << " "; // other, so you should write "->next" after "head->next"
} // in the caller funtion.
int main()
{
head = NULL;
tail = NULL;
int size, item, key;
cout<<"\n enter your size of the linked list: ";
cin>>size;
cout<<"\n enter your choice of insertion in the linked list: ";
cout<<" \n 1: At start(prepend) \n 2: At last(append) \n";
int option;
cin>>option;
switch(option)
{
case 1:
for(int i=1; i<=size; i++)
{
cout<<"\n enter element "<<i<<" of linked list: ";
cin>>item;
LLprepend(item);
}
break;
case 2:
for(int i=1; i<=size; i++)
{
cout<<"\n enter element "<<i<<" of linked list: ";
cin>>item;
LLappend(item);
}
break;
default:
cout<<"\n Incorrect choice entered!! ";
}
printList();
// checking the criteria based function...
cout<<"\n criteria based funtion: ";
cout<<"\n enter an element for inserting into the linked list: ";
cin>>item;
cout<"\n enter the key : ";
cin>>key;
LLCriteriaInsertion(item, key);
printList();
cout<<"\n Reverse display function: "<<endl;
reverseDisplay_DLL(head);
cout<<"\n start to end display & end to start display function: "<<endl;
startEnd_endStart_display(head);
return 0;
}