This commit is contained in:
K 2023-10-31 11:11:57 +05:30
commit 43034014a1
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F
2 changed files with 327 additions and 48 deletions

0
.Rhistory Normal file
View File

View File

@ -1,60 +1,339 @@
// This code creates a linked list to store PRN and names of n number of students. #include<iostream>
// We're not sure if there's anything more to it. For now, it only stores and displays.
#include <iostream>
using namespace std; using namespace std;
class node { class student
{
int roll; int roll;
string name; string name;
node *next; student *next;
public: public:
static node* create(); student* create();
void display(node* head); 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);
}; };
node* node::create() { student* student::create()
int total; {
node *head=NULL; int n;
node* point=NULL; student *head,*p;
cout<<"Total number of students are:\t"; head = NULL;
cin>>total; cout<<"Enter number of members: ";
cin>>n;
for (int i=0; i<total; i++) { for(int i = 0; i<n; i++)
if (head==NULL) { {
head = new node; if(head == NULL)
cout<<"Enter PRN number for student "<<i+1<<":\t"; {
head = new student;
cout<<"\nEnter prn of president: ";
cin>>head->roll; cin>>head->roll;
cout<<"Enter name for student "<<i+1<<":\t"; cout<<"Enter name of president: ";
cin>>head->name; cin>>head->name;
head->next=NULL; head->next = NULL;
point=head; p = head;
} }
else { else
point->next=new node; {
point=point->next; p->next = new student;
cout << "Enter PRN number for student "<<i+1<<":\t"; p = p->next;
cin>>point->roll; cout<<"\nEnter prn of member: ";
cout << "Enter name for student "<<i+1<<":\t"; cin>>p->roll;
cin>>point->name; cout<<"Enter name of member: ";
point->next=NULL; cin>>p->name;
p->next = NULL;
} }
} }
return head; 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<<"\nEnter 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<<"\nEnter 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<<"\nEnter 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<<"\nInvalid Data....."<<endl;
}
else if(q->roll == y)
{
p->next = q->next;
q->next = p;
}
q = q->next;
}
return head;
} }
void node::display(node* head) { student* student::beg_del(student *head)
node* point=this; {
cout<<endl<<"PRNs and names are:"<<endl; student *p;
for (point=head; point!=NULL; point=point->next) { p = head;
cout<<point->roll<<" -> "<<point->name<<endl; head = head->next;
} delete p;
cout<<endl<<"## DESIGNED AND ENGINEERED BY KSHITIJ\n## END OF CODE"<<endl; return head;
} }
int main() { student* student::end_del(student *head)
node* head = node::create(); {
head->display(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<<"\nEnter prn of member which is to be deleted: ";
cin>>y;
if(head == NULL)
{
cout<<"\nLinked list is empty!!";
return head;
}
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;
}
student* student::total(student *head)
{
student *p;
int total = 0;
for(p = head; p!=NULL; p = p->next)
{
total++;
}
cout<<"\nTotal Number of students are: "<<total<<endl;
}
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<<"\nYOUR CHOICES ARE: ";
cout<<"\n1. LINKED LIST1: \n2. LINKED LIST2: \n3. CONCATENATE LINKED LIST 1 & 2: \n4. EXIT";
cout<<"\nEnter choice: ";
cin>>char0;
switch(char0)
{
case 1:
while(flag1)
{
cout<<"\nWhat operations on list1 would you like to perform?";
cout<<"\n1. Enter prn, name of members: \n2. Add new president: \n3. Add secretary: \n4. Add new members: \n5. Remove president: \n6. Remove secretary: \n7. Remove members: \n8. Total members present: \n9. Exit ";
cout<<"\nEnter 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<<"\nRemoved president: ";
head = obj.beg_del(head);
obj.display(head);
break;
case 6:
cout<<"\nRemoved secretary: ";
obj.end_del(head);
obj.display(head);
break;
case 7:
head = obj.btwn_del(head);
cout<<"\nMember removed: ";
obj.display(head);
break;
case 8:
obj.total(head);
break;
case 9:
flag1 = 0;
break;
default:
cout<<"\nEnter valid choice!!!";
break;
}
}
break;
case 2:
while(flag2)
{
cout<<"\nWhat operations on list1 would you like to perform?";
cout<<"\n1. Enter prn, name of members: \n2. Add new president: \n3. Add secretary: \n4. Add new members: \n5. Remove president: \n6. Remove secretary: \n7. Remove members: \n8. Total members present: \n9. Exit ";
cout<<"\nEnter 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<<"\nRemoved president: ";
head1 = obj.beg_del(head1);
obj.display(head1);
break;
case 6:
cout<<"\nRemoved secretary: ";
obj.end_del(head1);
obj.display(head1);
break;
case 7:
head1 = obj.btwn_del(head1);
cout<<"\nMember removed: ";
obj.display(head1);
break;
case 8:
obj.total(head1);
break;
case 9:
flag2 = 0;
break;
default:
cout<<"\nEnter valid choice!!!";
break;
}
}
break;
case 3:
head = obj.con(head,head1);
obj.display(head);
break;
case 4:
flag = 0;
break;
default:
cout<<"\nEnter valid choice: ";
break;
}
}
return 0; return 0;
} }