-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcllgsll.cpp
More file actions
196 lines (196 loc) · 3.44 KB
/
Copy pathcllgsll.cpp
File metadata and controls
196 lines (196 loc) · 3.44 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;
struct node
{
int urn,semester,phone;
string branch;
string name;
struct node *next;
};
class linked_list
{
node *first,*last;
int count=0;
public:
linked_list()
{
first=NULL;
last=NULL;
}
void create();
void insert();
void dlt();
void display();
int inscheck(int);
int delcheck(int);
};
void linked_list::create()
{
node *temp=new node;
if(first==NULL)
{
cout<<"Enter University Roll Number of student"<<endl;
cin>>temp->urn;
cin.ignore();
cout<<"Enter student's name"<<endl;
getline(cin,temp->name);
cout<<"Enter student's branch"<<endl;
getline(cin,temp->branch);
cout<<"Enter semester"<<endl;
cin>>temp->semester;
cout<<"Enter student's phone number"<<endl;
cin>>temp->phone;
temp->next=NULL;
count++;
first=temp;
last=temp;
}
else
insert();
}
void linked_list::insert()
{
count++;
node *temp=new node;
node *trav=first;
node *save=first;
cout<<"Enter University Roll Number of student"<<endl;
cin>>temp->urn;
cin.ignore();
cout<<"Enter student's name"<<endl;
getline(cin,temp->name);
cout<<"Enter student's branch"<<endl;
getline(cin,temp->branch);
cout<<"Enter semester"<<endl;
cin>>temp->semester;
cout<<"Enter student's phone number"<<endl;
cin>>temp->phone;
int number=inscheck(temp->urn);
if(number==count-1)
{
last->next=temp;
temp->next=NULL;
last=temp;
}
else
{
for(int i=0;i<number;i++)
{
save=trav;
trav=save->next;
}
temp->next=save->next;
save->next=temp;
}
}
int linked_list::inscheck(int item)
{
node *temp=first;
int num=0;
if(item<=temp->urn)
num++;
else
{
while(temp!=NULL)
{
if(item<=temp->urn)
break;
else
num++;
temp=temp->next;
}
}
return(num);
}
void linked_list::dlt()
{
node *temp=first;
node *save=first;
int item;
cout<<"enter roll number of student whose data is to be deleted"<<endl;
cin>>item;
int number=delcheck(item);
if(number==-1)
cout<<"item not present in list"<<endl;
else
{
if(number==0)
first=first->next;
else
{
for(int i=0;i<number;i++)
{
save=temp;
temp=temp->next;
}
save->next=temp->next;
}
count--;
}
}
int linked_list::delcheck(int item)
{
node*temp=first;
int num=-1;
int count=0;
if(item<first->urn)
num=-1;
else if(item==first->urn)
num=0;
else
{
while(temp!=NULL)
{
if(item==temp->urn)
{
num=count;
break;
}
else
count++;
temp=temp->next;
}
}
return(num);
}
void linked_list::display()
{
if(first==NULL)
cout<<"empty linked list"<<endl;
else
{
cout<<setw(15)<<"ROLL NO."<<setw(15)<<"NAME"<<setw(15)<<"BRANCH"<<setw(15)<<"SEMESTER"<<setw(15)<<"PHONE NO"<<endl;
node *temp=first;
while(temp!=NULL)
{
cout<<setw(15)<<temp->urn<<setw(15)<<temp->name<<setw(15)<<temp->branch<<setw(15)<<temp->semester<<setw(15)<<temp->phone<<endl;
temp=temp->next;
}
cout<<"number of nodes = "<<count<<endl;
}
}
int main()
{
linked_list list;
int choice;
option: cout<<"enter the number against operation to be performed on singly linked list\n1. create a node\t2. insert a node\t3. delete a node\t4. display list\t5. exit"<<endl;
cin>>choice;
switch(choice)
{
case 1: list.create();
goto option;
case 2: list.insert();
goto option;
case 3: list.dlt();
goto option;
case 4: list.display();
goto option;
case 5: cout<<"exiting.."<<endl;
break;
default: cout<<"invalid option"<<endl;
goto option;
}
return 0;
}