Sie sind auf Seite 1von 6

Assignment#:2

Subject:
Data Structure
Submitted to:

Sir Mr ShafiQ Sahib


Submitted by:
Asjad Ali
Reg#:
FA18-MCS-055
Date:
20-04-2019
Page# 1

Instructor: Sir Mr. Shafiq Sahib Assignment: Data Structur

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;

Prepared: Asjad Ali Reg#: FA18-MCS-055


Page# 1

Instructor: Sir Mr. Shafiq Sahib Assignment: Data Structur


top--;
}
}
void display() {
if (top >= 0) {
cout << "Stack elements are:";
for (int i = top; i >= 0; i--)
cout << stack[i] << " ";
cout << endl;
}
else
cout << "Stack is empty";
}
void search()
{
int n, k = 0;
cout << "Enter the value for Search :";
cin >> n;
if (top >= 0) {
for (int i = top; i >= 0; i--)
if (stack[i] == n)
{
cout << "Value find stack index =" << i << endl;
k = 1;
}
if (k == 0)
cout << "value not in stack." << endl;
}
else
cout << "Stack is empty";
}
int main() {
int ch, val;
cout << "1) Check stack is empty or not" << endl;
cout << "2) Push in stack" << endl;
cout << "3) Pop from stack" << endl;
cout << "4) Display stack" << endl;
cout << "5) Check the SIZE use stack" << endl;
cout << "6) Search the value" << endl;
cout << "7) Exit" << endl;
do {
cout << "Enter choice: " << endl;
cin >> ch;
switch (ch) {
case 1: {isempty();
break; }
case 2: { cout << "Enter value to be pushed:" << endl;
cin >> val;
push(val);
break; }

Prepared: Asjad Ali Reg#: FA18-MCS-055


Page# 1

Instructor: Sir Mr. Shafiq Sahib Assignment: Data Structur


case 3: {pop();
break; }
case 4: {display();
break; }
case 5: {check_size();
break; }
case 6: {search();
break; }
case 7: {cout << "Exit" << endl;
break; }
default: {
cout << "Invalid Choice" << endl; }
}
} while (ch != 7);
return 0;
}
What is Queue?
• Queue is a linear data structure where the first element is inserted from one end called REAR and
deleted from the other end called as FRONT.
• Front points to the beginning of the queue and Rear points to the end of the queue.
• Queue follows the FIFO (First - In - First Out) structure.
• According to its FIFO structure, element inserted first will also be removed first.
• In a queue, one end is always used to insert data (enqueue) and the other is used to delete data
(dequeue), because queue is open at both its ends.
• The enqueue() and dequeue() are two important functions used in a queue.

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)

Prepared: Asjad Ali Reg#: FA18-MCS-055


Page# 1

Instructor: Sir Mr. Shafiq Sahib Assignment: Data Structur


front = 0;
cout << "Insert the element in queue : " << endl;
cin >> val;
rear++;
queue[rear] = val;
}
}
void Delete()
{
if (front == -1 || front > rear) {
cout << "Queue Underflow ";
return;
}
else {
cout << "Element deleted from queue is : " <<
queue[front] << endl;
front++;;
}
}
void Display()
{
if (front == -1)
cout << "Queue is empty" << endl;
else {
cout << "Queue elements are : ";
for (int i = front; i <= rear; i++)
cout << queue[i] << " ";
cout << endl;}}

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;
}

Prepared: Asjad Ali Reg#: FA18-MCS-055


Page# 1

Instructor: Sir Mr. Shafiq Sahib Assignment: Data Structur


if (k == 0)
cout << "value not found in Queue " << endl;}
}
int main() {
int ch;
cout << "1) Check queue is empty or not" << endl;
cout << "2) Insert element to queue" << endl;
cout << "3) Delete element from queue" << endl;
cout << "4) Display all the elements of queue" << endl;
cout << "5) Search the value" << endl;
cout << "6) Check the SIZE use stack" << endl;
cout << "7) Exit" << endl;
do {
cout << "Enter your choice : " << endl;
cin >> ch;
switch (ch) {
case 1: isempty();
break;
case 2: Insert();
break;
case 3: Delete();
break;
case 4: Display();
break;
case 5: search();
break;
case 6: checksize();
break;
case 7: cout << "Exit" << endl;
break;
default: cout << "Invalid choice" << endl;
}
} while (ch != 7);
return 0;}

The End

Prepared: Asjad Ali Reg#: FA18-MCS-055

Das könnte Ihnen auch gefallen