Lab 08
Lab 08
Note:
Lab manual cover following below Stack and Queue topics
{Stack with Array and Linked list ,Queue with Array and Linked}
Maintain discipline during the lab.
Just raise hand if you have any problem.
Completing all tasks of each lab is compulsory.
Get your lab checked at the end of the session.
class Stack {
int top;
public:
int a[MAX]; // Maximum size of Stack
bool Stack::push(int x)
{
if (top >= (MAX - 1)) {
cout << "Stack Overflow";
return false;
}
else {
a[++top] = x;
cout << x << " pushed into stack\n";
return true;
}
}
int Stack::pop()
{
if (top < 0) {
cout << "Stack Underflow";
return 0;
}
else {
int x = a[top--];
return x;
}
}
int Stack::peek()
{
if (top < 0) {
cout << "Stack is Empty";
return 0;
}
else {
int x = a[top];
return x;
}
}
bool Stack::isEmpty()
{
return (top < 0);
}
Task-1:
A. Design a Main class of upper code which perform the below task
1. Insert 10 Integers values in the stack
2. If the Insert input reach the Highest index of Array display the message Stack overflow
3. Remove the Inserted values till the Last value and print the message that stack is empty
struct Node
{
int data;
struct Node* link;
};
Task-2:
A. Design a Main class of upper code which perform the below task
1. Insert 10 Integers values in the stack
2. Write a utility function for upper code to display all the inserted integer values in the
linked list in forward and reverse direction both
3. Write utility function to pop top element from the stack
Sample Pseudocode
Begin
initially push some special character say # into the stack
for each character ch from infix expression, do
if ch is alphanumeric character, then
add ch to postfix expression
else if ch = opening parenthesis (, then
push ( into stack
else if ch = ^, then //exponential operator of higher precedence
push ^ into the stack
else if ch = closing parenthesis ), then
while stack is not empty and stack top ≠ (,
do pop and add item from stack to postfix expression
done
Code Snippet
#include<bits/stdc++.h>
using namespace std;
//Function to return precedence of operators
int prec(char c)
{
if(c == '^')
return 3;
else if(c == '*' || c == '/')
return 2;
else if(c == '+' || c == '-')
return 1;
else
return -1;
}
int main()
{
string exp = "a+b*(c^d-e)^(f+g*h)-i";
infixToPostfix(exp);
return 0;
}
Task-3:
A. Use the Upper code snippet implement the utility function with the help of array based
stack infixToPostfix by using sample pseudocode
Task-4:
A. Use the Upper code snippet implement the following utility function in the Array based
Queue
1. Write a function QueueCapacity when the Queue is Full
2. Write a function ADDMember when a new integer value is added in the array
3. Write a function RemoveMember when any data member is remove from the
queue
Queue with Linked list
Sample Code
#include <iostream>
using namespace std;
struct node {
int data;
struct node *next;};
struct node* front = NULL;
struct node* rear = NULL;
struct node* temp;
void Insert() {
int val;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
if (rear == NULL) {
rear = (struct node *)malloc(sizeof(struct node));
rear->next = NULL;
rear->data = val;
front = rear;
} else {
temp=(edlist struct node *)malloc(sizeof(struct node));
rear->next = temp;
temp->data = val;
temp->next = NULL;
rear = temp;
}}
Task-5:
B. Use the Upper code snippet implement the following utility function in the Link based
Queue
4. Write a function QueueCapacity when the Queue is Full
5. Write a function ADDMember when a new integer value is added in the
linkedlist
6. Write a function RemoveMember when any data member is removing from the
queue
Infix Notation
Table-1
A Queue is a linear structure which follows a particular order in which the operations are
performed. The order is First In First Out (FIFO). A good example of a queue is any
queue of consumers for a resource where the consumer that came first is served first. The
difference between stacks and queues is in removing. In a stack we remove the item the
most recently added; in a queue, we remove the item the least recently added.
Applications of Queue
A priority queue in c++ is a type of container adapter, which processes only the
highest priority element, i.e. the first element will be the maximum of all elements in
the queue, and elements are in decreasing order.
Priority Queue container processes the element with the highest priority, whereas
no priority exists in a queue.
Queue follows First-in-First-out (FIFO) rule, but in the priority queue highest
priority element will be deleted first.
If more than one element exists with the same priority, then, in this case, the order
of queue will be taken.
2. size() – This method gives the number of elements in the priority queue container.
It returns the size in an integer. It does not take any parameter.
3. push() – This method inserts the element into the queue. Firstly, the element is
added to the end of the queue, and simultaneously elements reorder themselves
with priority. It takes value in the parameter.
4. pop() – This method delete the top element (highest priority) from the
priority_queue. It does not take any parameter.
5. top() – This method gives the top element from the priority queue container. It
does not take any parameter.
6. swap() – This method swaps the elements of a priority_queue with another
priority_queue of the same size and type. It takes the priority queue in a parameter
whose values need to be swapped.
7. emplace() – This method adds a new element in a container at the top of the
priority queue. It takes value in a parameter.