Tuesday, October 14, 2008

6.1 Stack using Arrays

Program Definition
Insert a set of integers into the stack and delete these integers by stack using arrays
Algorithm:
Algorithm for Push operation
1. Algorithm add (item)
2. //push an element on to the stack .return
3. //True if successful; else return false
4. // item is used as an input
5. {
6. If (top>=n-1) then
7. {
8. Write (“stack is full!”); return false;
9. }
10. Else
11. {
12. Top: =top+1;
13. Stack [top]:=item;
14. Return true;
15. }

Algorithm for Pop operation
1. Algorithm delete (item)
2. //pop the top element from the stack .return
3. //true if successful else returns false .item
4. //is used as an output
5. {
6. If (top<0) then
7. {
8. Write (“stack is empty”); return false;
9. }
10. Else
11. {
12. Item: = stack [top]; top=top-1; return true;
13. }
14. }

Stacks using arrays out put:
enter the size of stack: 4
1.insert 2.delete 3.display 4.quit
enter choice: 1
enter element to be inserted 2
1.insert 2.delete 3.display 4.quit
enter choice: 1
enter element to be inserted 3
1.insert 2.delete 3.display 4.quit
enter choice: 1
enter element to be inserted 5
1.insert 2.delete 3.display 4.quit
enter choice: 3
element in stack are 2 3 5
1.insert 2.delete 3.display 4.quit
enter choice: 2
the element popped is5
1.insert 2.delete 3.display 4.quit
enter choice: 2
the element popped is3
1.insert 2.delete 3.display 4.quit
enter choice: 2
the element popped is2
1.insert 2.delete 3.display 4.quit
enter choice: 2
stack is under flow
1.insert 2.delete 3.display 4.quit
enter choice: 4


CODE
#include
#include
#include
class stack
{
private:
int stack[10],max,top,i,element;
public:
void read();
void insert();
void del();
void display();
stack()
{
top=-1;
}
}s;
void stack::read()
{
cout<<"enter the size of stack: ";
cin>>max;
}
void stack::insert()
{
if(top==max-1)
cout<<"stack is over flow";
else
{
top=top+1;
cout<<"enter element to be inserted";
cin>>element;
stack[top]=element;
}
}
void stack::del()
{
if(top==-1)
cout<<"stack is under flow";
else
{
element=stack[top];
cout<<"the element popped is"< top=top-1;
}
}
void stack::display()
{
if(top==-1)
cout<<"stack is empty";
else
{
cout<<"element in stack are";
for(i=0;i<=top;i++)
cout<<" "< }
}
void main()
{
int choice;
clrscr();
s.read();
while(1)
{
cout<<"\n1.insert 2.delete 3.display 4.quit";
cout<<"\nenter choice:";
cin>>choice;
switch(choice)
{
case 1:s.insert();
break;
case 2:s.del();
break;
case 3:s.display();
break;
case 4:exit(0);
}
}
}
RESULT:A set of integers has been inserted and the list of integers also displayed, If delete an integer from the stack that is also be done.
Remarks
This program is only for set of integer when assigned at compile time or this is work only size of the stack by the user.
6.2 Queue Using Arrays
Program Definition
Insert a set of integers into the QUEUE and delete these integers by QUEUE using arrays
Algorithm:
Algorithm for Insert operation
1. Algorithm addq(item)
2. Check overflow condition
If rear>=size-1
Output “Overflow” and return.
3. Increment rear pointer
Rear=rear+1
4. Insert an element
A[rear]=value
5. Set the front pointer
If front ==-1
Front=0
6. Return
Algorithm for Delete operation

1. Algorithm delete(item)
2. Check underflow condition
If front ==-1
Output “Underflow” and return
3. Remove an element
Value=q[front]
4. Check for empty queue
If front==rear
Front=-1
Rear=-1
Else
Front =front+1
5. Return value
Queue using arrays output:

enter n value:5
1.insert 2.delete 3.display 4.quit
enter your choice:1
insert value into queue:2

1.insert 2.delete 3.display 4.quit
enter your choice:1
insert value into queue:3

1.insert 2.delete 3.display 4.quit
enter your choice:1
insert value into queue:5

1.insert 2.delete 3.display 4.quit
enter your choice:1
insert value into queue:6

1.insert 2.delete 3.display 4.quit
enter your choice:3
elements in queue are 2 3 5 6
1.insert 2.delete 3.display 4.quit
enter your choice:2
element deleted is 2
1.insert 2.delete 3.display 4.quit
enter your choice :2
element deleted is 3
1.insert 2.delete 3.display 4.quit
enter your choice :2
element deleted is 5
1.insert 2.delete 3.display 4.quit
enter your choice :2
element deleted is 6
1.insert 2.delete 3.display 4.quit
enter your choice :2
queue is empty
1.insert 2.delete 3.display 4.quit
enter your choice :4





Code
#include
#include
#include
class queue
{
private:
int front,rear,max,a[10],element;
public:
void getn();
void insert();
void del();
void display();
queue()
{
front=0;
rear=-1;
}
}q;
void queue::getn()
{
cout<<"\n enter max value:";
cin>>max;
}
void queue::insert()
{
if(rear==max-1)
cout<<"queue is overflow";
else
{
cout<<"insert value into queue:";
cin>>element;
rear=rear+1;
a[rear]=element;
}
}
void queue::del()
{
int temp;
if(rear {
cout<<"queue is empty";
front=0;
rear=-1;
}
else
{
temp=a[front];
front=front+1;
cout<<"element deleted is "< }
}
void queue::display()
{
int i;
if(rear cout<<"queue is empty";
else
{
cout<<"elements in queue are";
for(i=front;i<=rear;i++)
cout<<" "< }
}
void main()
{
int choice;
clrscr();
q.getn();
while(1)
{
cout<<"\n 1.insert 2.delete 3.display 4.quit";
cout<<"\n enter your choice:";
cin>>choice;
switch(choice)
{
case 1:q.insert();
break;
case 2:q.del();
break;
case 3:q.display();
break;
case 4:exit(0);
}
}
}

No comments: