Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Lab Assignment: Stack

Algorithm
1. Start
2. If(top = size – 1)
a. Print “ Stack is Overflow”
3. Otherwise
a. Increase top by 1
b. Read the data and store it in the top position.
4. Stop

</>
#include <iostream>
#define CAPACITY 5
using namespace std;
struct Stack
{
int stack[CAPACITY];
int top=-1;
void Push(int);
void ShowTop();
void Pop();
int isFull();
int isEmpty();
void Show();
int Length();
};

int main()
{
Stack s;
s.Push(4);
s.Push(9);
s.Push(10);
s.Push(23);
s.Push(56);
cout << "Before Poping" << endl;
s.Show();
s.Pop();
s.Pop();
cout << "After Poping" << endl;
s.Show();
cout << "At Top" << endl;
s.ShowTop();
return 0;
}
int Stack::isFull()
{
if(top == CAPACITY - 1)
return 1;
return 0;
}
int Stack::isEmpty()
{
if(top == -1)
return 1;
return 0;
}
void Stack::ShowTop()
{
if(isEmpty())
cout << "Nothing on Top!" << endl;
else
cout << stack[top] << endl;
}
void Stack::Push(int data)
{
if(isFull())
cout << "Stack is Overflow!" << endl;
else
{
top++;
stack[top] = data;
}
}
void Stack::Pop()
{
if(isEmpty())
cout << "Stack is Empty!" << endl;
else
{
cout << stack[top] << endl;
top--;
}
}
void Stack::Show()
{
for(int i=0;i<top;i++)
cout << stack[i] << "<-";
cout << stack[top] << endl;
}
int Stack::Length()
{
return top+1;
}

O/P
Before Poping
4<-9<-10<-23<-56
56
23
After Poping
4<-9<-10
At Top
10

Lab Assignment: Linear Queue without Shifting


Algorithm
Enqueue
1. Start
2. If(rear = size -1)
a. Print “Queue is Full”
3. Otherwise
a. Increase rear by 1
b. Read the data and add it in the rear position
4. Stop

Deque
1. Start
2. If(rear < front)
Print “Queue is Empty”
3. Otherwise
Increase front by 1
4. Stop

</>
#include <iostream>
#define CAPACITY 5
using namespace std;
int queue[CAPACITY];
int rear = -1;
int front = 0;

void Enqueue(int);
void Dequeue();
void Show();
int Length();

int main()
{
Enqueue(5);
Enqueue(3);
Enqueue(2);
Enqueue(6);
Enqueue(9);
cout << "Before Dequeueing" << endl;
Show();
Dequeue();
cout << "After Dequeueing" << endl;
Show();
return 0;
}

void Enqueue(int data){


if( rear == CAPACITY - 1 )
cout << "Queue is Full." << endl;
else{
rear++;
queue[rear] = data;
}
}

void Dequeue(){
if(rear < front)
cout << "Queue is Empty" << endl;
else{
cout<<queue[front]<<endl;
front++;
}
}

void Show(){
if(rear < front)
cout << "Queue is Empty" << endl;
else{
for(int i=front;i<rear;i++)
cout << queue[i] << "-";
cout << queue[rear] << endl;
}
}

int Length(){
int count = 0;
for(int i=front;i<rear;i++)
count++;
return count+1;
}

O/P
Before Dequeueing
5-3-2-6-9
5
After Dequeueing
3-2-6-9
Lab Assignment: Linear Queue with Shifting
Algorithm
Enqueue
1. Start
2. If(rear = size -1)
a. Print “Queue is Full”
3. Otherwise
a. Increase rear by 1
b. Read the data and add it in the rear position
4. Stop

Deque
1. Start
2. If(rear < front)
a. Print “Queue is Empty”
3. Otherwise
a. Shift all items from front + 1 towards from front by 1
b. Decrease rear by 1
4. Stop

</>
#include <iostream>
#define CAPACITY 5
using namespace std;
int queue[CAPACITY];
int rear = -1;
int front = 0;

void Enqueue(int);
void Dequeue();
void Show();
int Length();

int main()
{
Enqueue(1);
Enqueue(2);
Enqueue(3);
Enqueue(4);
Enqueue(5);
Enqueue(10);
Show();
cout << "Before Dequeueing" << endl;
Dequeue();
cout << "After Dequeueing" << endl;
Show();
return 0;
}
void Enqueue(int data){
if( rear == CAPACITY - 1 )
cout << "Queue is Full." << endl;
else{
rear++;
queue[rear] = data;
}
}

void Dequeue(){
if(rear < front)
cout << "Queue is Empty" << endl;
else{
cout<<queue[front]<<endl;
for(int i=0;i<=rear;i++)
queue[i] = queue[i+1];
rear--;
}
}

void Show(){
if(rear < front)
cout << "Queue is Empty" << endl;
else{
for(int i=front;i<rear;i++)
cout << queue[i] << "-";
cout << queue[rear] << endl;
}
}

int Length(){
int count = 0;
for(int i=front;i<rear;i++)
count++;
return count+1;
}

O/P
Queue is Full.
1-2-3-4-5
Before Dequeueing
1
After Dequeueing
2-3-4-5

You might also like