Fds Practical No.7

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Practical No.

#include<stdio.h> void create();

#include <iostream> void display();

#include<string> void insertAtBeginning();

using namespace std; void insertAtEnd();

class list; void insertAfter();

class node void deleteAtFirst();

{ void deleteByValue();

int prn; void deleteAtEnd();

string name; int computeTotal();

node *next; void sortList();

public: void concatList(list &q1);

node(int x,string nm) void displayRev(node *t);

{ bool reverseDisplay()

prn=x; {

next=NULL; if(start==NULL)

name=nm; return false;

} node *temp=start;

friend class list; displayRev(temp);

}; return true;

class list }};

{ void list::displayRev(node *t)

node *start; {

public: if(t==NULL)

list(){ return;

start=NULL; else

} {
displayRev(t->next); else

cout<<"\nPRN NO:"<<t->prn<<" Name: "<<t- { cout<<"\n====== List: ======\n";


>name;
while(t!=NULL){
}}
cout<<t->prn<<" "<<t->name<<" \n";
void list::create()
t=t->next;
{
}}}
int no;
void list::insertAtBeginning()
string nam;
{
if(start==NULL)
int no;
{
string nam;
cout<<"Enter PRN number: ";
node *temp;
cin>>no;
if(start==NULL)
cout<<"Enter name: ";
{
cin>>nam;
create();
cout<<nam;
}
start=new node(no,nam);
else
cout<<"\n====== List Created =====";
{
}
cout<<"\nEnter PRN number: ";
else
cin>>no;
{
cout<<"Enter name: ";
cout<<"\nList is already created.";
cin>>nam;
}}
temp=new node(no,nam);
void list::display()
temp->next=start;
{
start=temp;;
node *t;
cout<<"Inserted "<<temp->name<<" at the
t=start; beginning.";

if(start==NULL) }}

cout<<"\nList is Empty"; void list::insertAtEnd()


{ {

int no; if(t->prn==prev_no)

string nam; {

node *t; flag=1;break;

if(start==NULL) }

create(); t=t->next;

else }

{ if(flag==1)

cout<<"\nEnter PRN number: "; {

cin>>no; node *p;

cout<<"Enter name: "; cout<<"\nEnter PRN number: ";

cin>>nam; cin>>no;

t=start; cout<<"Enter name: ";

while(t->next!=NULL) cin>>nam;

t=t->next; p=new node(no,nam);

node*p=new node(no,nam); p->next=t->next;

t->next=p; t->next=p;

}} }

void list::insertAfter() else

{ {

int prev_no; cout<<"\n"<<prev_no<<" is not in list.";

cout<<"\nENter PRN No. after do you want insert:"; }}

cin>>prev_no; void list::deleteAtFirst()

node *t; {

t=start; node *t;

string nam; if(start==NULL)

int flag=0,no; cout<<"\nClub is Empty..";

while(t!=NULL) else
{ prev->next=t->next;

t=start; t->next=NULL;

start=start->next; delete t;

t->next=NULL; //Not necessary cout<<"\nMember with prn no: "<<no<<" is


deleted.";
delete t;
}
cout<<"\nPresident deleted..";
else
}}
cout<<"\nMember not found in List./president or
void list::deleteByValue() secretary cannot be deleted.";

{ }}

int no,flag=0; void list::deleteAtEnd()

node *t,*prev; {

if(start==NULL) node *t,*prev;

cout<<"\nList/Club is empty;"; t=start;

else if(start==NULL)

{ cout<<"\nClub is Empty..";

cout<<"\nEnter PRN no. of member to be deleted: "; else

cin>>no; {

t=start->next; while(t->next!=NULL)

while(t->next!=NULL) {

}if(t->prn==no){ prev=t;

flag=1; t=t->next;
break; }

} prev->next=NULL;

prev=t; delete t;

t=t->next; cout<<"\nSecretary Deleted.";


} }}

if(flag==1) int list::computeTotal()


{ {
node *t; {

int count=0; if((j->prn)>(j->next->prn))

t=start; {

if(start==NULL) tprn=j->prn;

{ tname=j->name;

cout<<"\nList is empty."; j->prn=j->next->prn;

return 0; j->name=j->next->name;

} j->next->prn=tprn;

while(t!=NULL) j->next->name=tname;

{ }}}

count++; cout<<"\n List is sorted.";

t=t->next; display();

} }

return count; void list::concatList(list &q1)

} {

void list::sortList() node *t,*p;

{ t=q1.start;

node *i,*j,*last=NULL; if(t==NULL)

int tprn; {

string tname; cout<<"\nList 2 is empty";

if(start==NULL) return;

{ }

cout<<"\nList is empty."; p=start;

return ; while(p->next!=NULL)

} {

for(i=start;i->next!=NULL;i=i->next) p=p->next;

{ }

for(j=start;j->next!=last;j=j->next) p->next=t;
q1.start=NULL; cout<<"\n1. create\n2.Insert President\n3.Insert
secretary\n4.insert after position(member)\n5.Display
cout<<"\nAfter concatenationlist"; list"

display(); <<"\n6.Delete President\n7.Delete


Secretary\n8.Delete Member\n9.Find total No. of
}
members\n10.Sort list\n11. Reselect List ++--##"
int main() {
<<"\n12.Combine lists\n13.Reverse Display\n0.
list *l; Exit\nENter your choice:\t";

int choice,selectList; cin>>choice;

list l1,l2; switch(choice)

l=&l1; {

X:cout<<"\nSelect List\n1.List 1\n2.List 2\nEnter case 1: l->create();


choice: ";
break;
cin>>selectList;
case 2: l->insertAtBeginning();
if(selectList==1)
break;
{
case 3: l->insertAtEnd();
l=&l1;
break;
}
case 4: l->insertAfter();
else if(selectList==2)
break;
{
case 5: l->display();
l=&l2;
break;
}
case 6: l->deleteAtFirst();
else
break;
{
case 7: l->deleteAtEnd();
cout<<"\nWrong list Number.";
break;
goto X;
case 8: l->deleteByValue();
}
break;
do
case 9: cout<<"\nTotal members(including
{ President & Secretary): "<<l->computeTotal();

break;
case 10: l->sortList();

break;

case 11:

goto X;

break;

case 12:

l1.concatList(l2);

break;

case 13:

l->reverseDisplay();

break;

default:

cout<<"Wrong choice";

}while(choice!=0);

cout<<"\n========== GOOD BYE


====================\n";

return 0;

You might also like