Beruflich Dokumente
Kultur Dokumente
Subject:
Data Structure
Submitted to:
Assignment 2
Deadline: Sunday 21 April 2019 10PM (Soft + Hard form)
Q1# What is Stack. Implement Stack with functionality of Push, Pop, Display, Search, Size
Used, is Empty.
Q#2: What is Queue. Implement queue with complete functionality like Enqueue,
Deque, Display, Delete, Search, is Empty, size Used.
What is Stack?
• Stack is an ordered list of the same type of elements.
• It is a linear list where all insertions and deletions are permitted only at one end of the list.
• Stack is a LIFO (Last In First Out) structure
• In a stack, when an element is added, it goes to the top of the stack.
Definition “Stack is a collection of similar data items in which both insertion and deletion operations are
performed based on LIFO principle”. There are two basic operations performed in a Stack: 1. Push() 2.
Pop () 1. Push() function is used to add or insert new elements into the stack. 2. Pop() function is used
to delete or remove an element from the stack.
Stack Programme
#include <iostream>
using namespace std;
int stack[100], n = 100, top = -1;
void isempty()
{
if (top < 0)
cout << "Stack is empty " << endl;
else
cout << "Stack is not empty " << endl;
}
void check_size()
{
cout << "stack use size = " << top << endl;
cout << "Total Stack size = " << n << endl;
}
void push(int val) {
if (top >= n - 1)
cout << "Stack Overflow" << endl;
else {
top++;
stack[top] = val;
}
}
void pop() {
if (top <= -1)
cout << "Stack Underflow" << endl;
else {
cout << "The popped element is " << stack[top] << endl;
Queue Programme
#include <iostream>
using namespace std;
int queue[100], n = 100, front = -1, rear = -1;
void isempty()
{
if (front == -1 || front > rear)
cout << "Queue is Empty \n";
else
cout << "Queue is not Empty";
}
void Insert()
{
int val;
if (rear == n - 1)
cout << "Queue Overflow" << endl;
else {
if (front == -1)
void checksize()
{
cout << "total size of queue = " << n << endl;
cout << "use of size = " << rear << endl;}
void search()
{
int n, k = 0;
cout << "Enter the value for search :";
cin >> n;
if (front == -1)
cout << "Queue is empty" << endl;
else {
for (int i = front; i <= rear; i++)
if (queue[i] == n)
{
cout << "Find value on index#=" << i << endl;
k = 1;
}
The End