-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment 4.cpp
More file actions
181 lines (177 loc) · 3.05 KB
/
Assignment 4.cpp
File metadata and controls
181 lines (177 loc) · 3.05 KB
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
//============================================================================
// Name : Assignment 4.cpp
// Author : 21258
// Version :
// Copyright : Your copyright notice
// Description : Singly linked list (Pinnacle Club)
//============================================================================
#include <iostream>
using namespace std;
class Node
{
public:
double prn;
string name;
Node *ptr;
Node()
{
prn=0;
ptr=NULL;
}
};
class LinkedList
{
public:
Node *first,*last,*n,*temp;
LinkedList()
{
first=NULL;
last=NULL;
n=NULL;
temp=NULL;
}
void initial()
{
first=new Node();
last=new Node();
n=new Node();
first->ptr=n;
n->ptr=last;
cout<<"Enter President's Name and PRN : "<<endl;
cin>>first->name;
cin>>first->prn;
cout<<"Enter Secretary's Name and PRN : "<<endl;
cin>>last->name;
cin>>last->prn;
cout<<"Enter Node's Name and PRN : "<<endl;
cin>>n->name;
cin>>n->prn;
}
void add()
{
int c;
double o;
cout<<"Enter 1 for President, 2 for Secretary, 3 for Member : ";
cin>>c;
if(c==1)
{
cout<<"Enter President's Name and PRN : "<<endl;
cin>>first->name;
cin>>first->prn;
}
if(c==2)
{
cout<<"Enter Secretary's Name and PRN : "<<endl;
cin>>last->name;
cin>>last->prn;
}
if(c==3)
{
Node *temp,*t;
temp=new Node();
cout<<"Enter name and PRN : "<<endl;
cin>>temp->name;
cin>>temp->prn;
cout<<"Enter the PRN of node after which you would like to add new node : ";
cin>>o;
t=first;
if(o==last->prn)
cout<<"Node cannot be added after secretary"<<endl;
else
while(o!=t->prn)
{
t=t->ptr;
}
temp->ptr=t->ptr;
t->ptr=temp;
}
}
void del()
{
double o;
Node *t,*y;
t=new Node();
y=new Node();
t=first;
cout<<"Enter the PRN of the node to be deleted : ";
cin>>o;
if(o==first->prn)
{
first=first->ptr;
}
else
{
while(t!=NULL&&t->prn!=o)
{
y=t;
t=t->ptr;
}
if(t->prn==last->prn)
{
y->ptr=NULL;
last=y;
}
else
y->ptr=t->ptr;
}
}
void display()
{
int count=0;
Node *t;
t=new Node();
t=first;
cout<<"The members are : "<<endl;
while(t->ptr!=NULL)
{
cout<<" "<<t->name<<" "<<t->prn<<endl;
t=t->ptr;
count++;
}
cout<<" "<<last->name<<" "<<last->prn<<endl;
count++;
cout<<"The number of nodes is : "<<count<<endl;
}
void reverse(Node *t)
{
if(t!=NULL)
{
reverse(t->ptr);
cout<<" "<<t->name<<" "<<t->prn<<endl;
}
}
};
int main() {
LinkedList l1,l2;
l1.initial();
int e;
char a;
do
{
cout<<"Enter your choice : \n1.Add new node\n2.Display members and count of members\n3.Delete node\n4.Print in reverse order"<<endl;
cin>>e;
switch(e)
{
case 1:
l1.add();
break;
case 2:
l1.display();
break;
case 3:
l1.del();
break;
case 4:
l1.reverse(l1.first);
break;
default:
cout<<"Please enter valid option"<<endl;
}
cout<<"Would you like to perform another operation? (Y/N) ";
cin>>a;
}
while(a=='Y');
if(a=='N')
cout<<"Thank you!";
return 0;
}