queue using array
The write the program to perform insertion and Deletion operation using Qneue using array.
Algorithm:-
- step 1: Start the Program.
- step 2: If Front =1 and rear =0 Print "Overflow and return".
- step 3: IF front =0,Font =1,Rear =1 a[rear] = value.
- step 4: If rear at End of Quence rear =0 rear =1 a (rear) = value.
- step 5: else rear =rear +1 a(rear)=value.
- step 6: Return.
- step 7: Stop the Program.
For Example queue using array program:
#include<iostream>
#include<stdlib.h>
#include<conio.h>
#define max 5
class qarr[max];
int front,rear;
public()
{
front=-1;
rear=-1;
}
void add(int i);
int delq (void);
void display(void);
};
void queue: :add (int i)
{
if (rear==(max-1))
{
count<<"quene is full\n";
return;
}
else
{
rear++;
qarr[rear]=i;
front=0;
}
}
int queue: :delg(void)
{
int d;
if (front==-1)
{
count<<"\n queue is empty\n";
return 0;
}
else
{
d=qarr [front];
count<<"\n deleted element id \n";
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front++;
return d;
}
}
}
void queue: :display (void)
{
if(front==-1)
{
count<<"\n queue is empty";
}
else
{
count<<"\n queue element are:\n";
}
count<<end1;
}
}
void main()
{
queue q;
int ch,item,value;
count<<"\n 1.add";
count<<"\n 2.delete";
count<<"\n 3.display";
count<<"\n 4.exit";
do
{
count<<"\n Enter your choice:";
cin>>ch;
count<<ch<<"\n"
switch(ch)
{
case 1:
count<<"\n enter the value:";
cin>>value;
count<<value<<end1;
q.add(value);
q.display();
break;
case 2:
item=q.delq();
count<<item;
q.display();
break;
Case 3:
q.display();
break;
case4:
exit (0);
default:count<<"\n wrong choice";
break;
}
}
while(ch !=4)
getch();
}
OUTPUT:
1.add
2.delete
3.display
4.exit
Enter your choice
1
Enter the value
10
The elements are
10
Enter your choice
1
Enter the value
20
The elements are
10 20
Enter your choice
3
The elements are
10 20
Enter your choice
2
Delete element is
10
The elements are
20
Enter your choice
2
Deleted element is
20
Enter your choice
3
Queue is empty
Enter your choice
4
queue using pointer
The write the program to perform insertion and Deletion operation using queue using pointer
Algorithm:-
- step 1: Start the Program.
- step 2: If Front =1 and rear =0 Print "Overflow and return".
- step 3: Else IF front =0,Font =1,Rear =1 M[rear] = value.
- step 4: If the rear at the end if value else if rar =n.
- step 5: rear =1 M(rear)=value.
- step 6: Return.
- step 7: Stop the Program.
For Example queue using Pointer program:
#include<iostream>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
int data;
node *link;
};
class queue
{
node *rear,*front;
node *rear,*front;
public:
quence()
{
front=0;
rear=0;
}
void addq(int item);
int delq (void);
void display(void);
};
void queue: :addq (int item)
{
node *t;
node *t;
t=new node;
t->data=item;
t->link=0;
if(front==0)
{
rear=front=t;
}
else
{
rear->link=t;
}
}
}
int queue: :delq(void)
{
if (front==0)
{
count<<"\n\ queue is empty";
return 0;
}
else
{
node *t;
int item;
item=front->datat;
t=front;
front=front->link;
delete t;
count<<"\n deleted element is:";
count<<item;
return (item);
}}
void queue: :display(void)
{
if (front==0)
if (front==0)
{
count<<"\n queue is element";
}
else
{
count<<"\n the element in queue are:";
node *t;
t=front;
while(t!=0)
{
count<<t->data<<"\n";
t=t->link;
}}}
void main()
{
int ch, item;
queue q:
clrscr ();
count<<"\n queue operation using pointer \n";
count<<"\n 1.insert";
count<<"\n 2.delete";
count<<"\n 3.display";
count<<"\n 4.exit";
do
{
count<<"\n Enter the Element:";
cin>>ch;
count<<ch;
switch(ch)
{
case 1:
count<<"\n Enter the element:";
cin>>item;
q.add(item);
q.display();
break;
case 2:
item=q.delq();
q.display();
break;
case 3:
q.display();
break;
case 4:
exit(0);
default:count<<"\n wrong choice";
break;
}
}
while(ch!=4);
}
while(ch!=4);
getch();
}
OUTPUT:
1.insert
2.delete
3.display
4.exit
Enter your choice
1
Enter the element
10
The element in queue are
10
Enter your choice
1
Enter the element
20
The element in queue are
10
20
Enter your choice
1
Enter the element
30
The element in queue are
10
20
30
Enter your choice
3
The element in queue are
10
20
30
Enter Your choice
2
Deleted the elements is
10
The element in queue are
20
30
Enter your choice
3
The element in queue are
20
30
Enter your choice
4
No comments:
Post a Comment