2023-10-30 11:23:47 +05:30
# include<iostream>
2023-10-17 18:21:09 +05:30
using namespace std ;
2023-10-30 11:23:47 +05:30
class student
{
int roll ;
string name ;
student * next ;
public :
student * create ( ) ;
void display ( student * head ) ;
student * beg_add ( student * head ) ;
student * end_add ( student * head ) ;
student * btwn_add ( student * head ) ;
student * beg_del ( student * head ) ;
student * end_del ( student * head ) ;
student * btwn_del ( student * head ) ;
student * total ( student * head ) ;
student * con ( student * head , student * head1 ) ;
2023-10-17 18:21:09 +05:30
} ;
2023-10-30 11:23:47 +05:30
student * student : : create ( )
{
int n ;
student * head , * p ;
head = NULL ;
cout < < " Enter number of members: " ;
cin > > n ;
for ( int i = 0 ; i < n ; i + + )
{
if ( head = = NULL )
{
head = new student ;
cout < < " \n Enter prn of president: " ;
cin > > head - > roll ;
cout < < " Enter name of president: " ;
cin > > head - > name ;
head - > next = NULL ;
p = head ;
}
else
{
p - > next = new student ;
p = p - > next ;
cout < < " \n Enter prn of member: " ;
cin > > p - > roll ;
cout < < " Enter name of member: " ;
cin > > p - > name ;
p - > next = NULL ;
}
}
return head ;
}
void student : : display ( student * head )
{
student * p ;
for ( p = head ; p - > next ! = NULL ; p = p - > next )
{
cout < < p - > roll < < " " < < p - > name < < " -> " ;
}
cout < < p - > roll < < " " < < p - > name < < endl ;
}
student * student : : beg_add ( student * head )
{
student * p ;
p = new student ;
cout < < " \n Enter prn of new president: " ;
cin > > p - > roll ;
cout < < " Enter name of new president: " ;
cin > > p - > name ;
p - > next = NULL ;
p - > next = head ;
head = p ;
return head ;
}
student * student : : end_add ( student * head )
{
student * p , * q ;
p = new student ;
cout < < " \n Enter prn of secretary: " ;
cin > > p - > roll ;
cout < < " Enter name of secretary: " ;
cin > > p - > name ;
p - > next = NULL ;
for ( q = head ; q - > next ! = NULL ; q = q - > next ) ;
q - > next = p ;
return head ;
}
student * student : : btwn_add ( student * head )
{
int y ;
student * p , * q ;
p = new student ;
cout < < " \n Enter prn of new member: " ;
cin > > p - > roll ;
cout < < " Enter name of new member: " ;
cin > > p - > name ;
cout < < " Enter prn after which new member is to be added: " ;
cin > > y ;
p - > next = NULL ;
q = head ;
while ( q ! = NULL )
{
if ( q = = NULL )
{
cout < < " \n Invalid Data..... " < < endl ;
}
else if ( q - > roll = = y )
{
p - > next = q - > next ;
q - > next = p ;
}
q = q - > next ;
2023-10-17 18:21:09 +05:30
}
2023-10-30 11:23:47 +05:30
return head ;
}
student * student : : beg_del ( student * head )
{
student * p ;
p = head ;
head = head - > next ;
delete p ;
return head ;
}
student * student : : end_del ( student * head )
{
student * p , * q ;
for ( q = head ; q - > next - > next ! = NULL ; q = q - > next ) ;
p = q - > next ;
delete p ;
q - > next = NULL ;
}
student * student : : btwn_del ( student * head )
{
int y ;
student * p , * q ;
cout < < " \n Enter prn of member which is to be deleted: " ;
cin > > y ;
if ( head = = NULL )
{
cout < < " \n Linked list is empty!! " ;
return head ;
2023-10-17 18:21:09 +05:30
}
2023-10-30 11:23:47 +05:30
p = head ;
while ( p - > next ! = NULL )
{
if ( p - > next - > roll = = y )
{
q = p - > next ;
p - > next = p - > next - > next ;
delete q ;
}
p = p - > next ;
}
return head ;
2023-10-17 18:21:09 +05:30
}
2023-10-30 11:23:47 +05:30
student * student : : total ( student * head )
{
student * p ;
int total = 0 ;
for ( p = head ; p ! = NULL ; p = p - > next )
{
total + + ;
}
cout < < " \n Total Number of students are: " < < total < < endl ;
2023-10-17 18:21:09 +05:30
}
2023-10-30 11:23:47 +05:30
student * student : : con ( student * head , student * head1 )
{
student * p ;
for ( p = head ; p - > next ! = NULL ; p = p - > next ) ;
p - > next = head1 ;
return head ;
}
int main ( )
{
int flag = 1 ;
int flag1 = 1 ;
int flag2 = 1 ;
int char0 , char1 , char2 ;
student * head , * head1 ;
student obj ;
while ( flag )
{
cout < < " \n YOUR CHOICES ARE: " ;
cout < < " \n 1. LINKED LIST1: \n 2. LINKED LIST2: \n 3. CONCATENATE LINKED LIST 1 & 2: \n 4. EXIT " ;
cout < < " \n Enter choice: " ;
cin > > char0 ;
switch ( char0 )
{
case 1 :
while ( flag1 )
{
cout < < " \n What operations on list1 would you like to perform? " ;
cout < < " \n 1. Enter prn, name of members: \n 2. Add new president: \n 3. Add secretary: \n 4. Add new members: \n 5. Remove president: \n 6. Remove secretary: \n 7. Remove members: \n 8. Total members present: \n 9. Exit " ;
cout < < " \n Enter choice: " ;
cin > > char1 ;
switch ( char1 )
{
case 1 :
head = obj . create ( ) ;
obj . display ( head ) ;
break ;
case 2 :
head = obj . beg_add ( head ) ;
obj . display ( head ) ;
break ;
case 3 :
head = obj . end_add ( head ) ;
obj . display ( head ) ;
break ;
case 4 :
head = obj . btwn_add ( head ) ;
obj . display ( head ) ;
break ;
case 5 :
cout < < " \n Removed president: " ;
head = obj . beg_del ( head ) ;
obj . display ( head ) ;
break ;
case 6 :
cout < < " \n Removed secretary: " ;
obj . end_del ( head ) ;
obj . display ( head ) ;
break ;
case 7 :
head = obj . btwn_del ( head ) ;
cout < < " \n Member removed: " ;
obj . display ( head ) ;
break ;
case 8 :
obj . total ( head ) ;
break ;
case 9 :
flag1 = 0 ;
break ;
default :
cout < < " \n Enter valid choice!!! " ;
break ;
}
}
break ;
case 2 :
while ( flag2 )
{
cout < < " \n What operations on list1 would you like to perform? " ;
cout < < " \n 1. Enter prn, name of members: \n 2. Add new president: \n 3. Add secretary: \n 4. Add new members: \n 5. Remove president: \n 6. Remove secretary: \n 7. Remove members: \n 8. Total members present: \n 9. Exit " ;
cout < < " \n Enter choice: " ;
cin > > char2 ;
switch ( char2 )
{
case 1 :
head1 = obj . create ( ) ;
obj . display ( head1 ) ;
break ;
case 2 :
head1 = obj . beg_add ( head1 ) ;
obj . display ( head1 ) ;
break ;
case 3 :
head1 = obj . end_add ( head1 ) ;
obj . display ( head1 ) ;
break ;
case 4 :
head1 = obj . btwn_add ( head1 ) ;
obj . display ( head1 ) ;
break ;
case 5 :
cout < < " \n Removed president: " ;
head1 = obj . beg_del ( head1 ) ;
obj . display ( head1 ) ;
break ;
case 6 :
cout < < " \n Removed secretary: " ;
obj . end_del ( head1 ) ;
obj . display ( head1 ) ;
break ;
case 7 :
head1 = obj . btwn_del ( head1 ) ;
cout < < " \n Member removed: " ;
obj . display ( head1 ) ;
break ;
case 8 :
obj . total ( head1 ) ;
break ;
case 9 :
flag2 = 0 ;
break ;
default :
cout < < " \n Enter valid choice!!! " ;
break ;
}
}
break ;
case 3 :
head = obj . con ( head , head1 ) ;
obj . display ( head ) ;
break ;
case 4 :
flag = 0 ;
break ;
default :
cout < < " \n Enter valid choice: " ;
break ;
}
}
return 0 ;
2023-10-17 18:21:09 +05:30
}