IMPLEMENTATION OF QUEUE WITH POINTER OBJECTS OF CLASS.


#include<iostream.h>
#include<conio.h>
#include<stdio.h>

struct Student
{
int age;
char name[20];
Student *link;
};

class Queue
{
Student *front, *rear;
public:
Queue()
{
front=rear=NULL;
}
void insert();
void display();
void search();
void del();
};

void Queue::insert()
{
Student *ptr = new Student;
cout<<"Enter age : ";
cin>>ptr->age;
cout<<"Enter name : ";
gets(ptr->name);
ptr->link=NULL;
if(front==NULL)
{
front=ptr;
rear=ptr;
cout<<"\nStudent Added Successfully....\n";
}
else
{
rear->link=ptr;
rear=ptr;
cout<<"\nStudent Added Successfully....\n";
}
}

void Queue::display()
{
Student *ptr=front;
while(ptr!=NULL)
{
cout<<"AGE  : "<<ptr->age<<endl;
cout<<"NAME : "<<ptr->name<<endl<<endl;
ptr=ptr->link;
}
}

void Queue::search()
{
Student *ptr=front;
int s,pos=0,flag=0;
cout<<"Enter the age to search : ";
cin>>s;
while(ptr!=NULL)
{
if(s==ptr->age)
{
pos++;
cout<<"Age found at pos..."<<pos<<endl<<endl;
ptr=front;
flag++;
break;
}
else
{
pos++;
ptr=ptr->link;
}
}
if(flag==0)
cout<<"\nNo Such Data Found....\n";
}

void Queue::del()
{
Student *ptr;
ptr=front;
front=front->link;
delete ptr;
cout<<"Student Deleted....\n";
}

void main()
{
Queue q;
int n;
do
{
clrscr();
cout<<"=====================\n";
cout<<"  QUEUE OPERATIONS\n";
cout<<"=====================\n";
cout<<"0. Exit................"<<endl;
cout<<"1. Add................."<<endl;
cout<<"2. Display............."<<endl;
cout<<"3. Search.............."<<endl;
cout<<"4. Delete.............."<<endl;
cout<<"Enter your choice: ";
cin>>n;
switch(n)
{
case 1: q.insert(); break;
case 2: q.display(); break;
case 3: q.search();     break;
case 4: q.del(); break;
}
getch();
}while(n);
}


No comments

Post your comments

Powered by Blogger.