You are on page 1of 10

Assignment NO:

Subject:

Submitted to:

Submitted by:

Reg#:
Instructor: Sir Mr ShafiQ Sahib Assignment: Data Structure

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.

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.

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


Instructor: Sir Mr ShafiQ Sahib Assignment: Data Structure

Stack Programme
#include <iostream>
using namespace std;
int stack[100], n = 100, top = -1;
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;
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";
}
int main() {
int ch, val;
cout << "1) Push in stack" << endl;
cout << "2) Pop from stack" << endl;
cout << "3) Display stack" << endl;
cout << "4) Exit" << endl;

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


Instructor: Sir Mr ShafiQ Sahib Assignment: Data Structure

do {
cout << "Enter choice: " << endl;
cin >> ch;
switch (ch) {
case 1: {
cout << "Enter value to be
pushed:" << endl;
cin >> val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout << "Exit" << endl;
break;
}
default: {
cout << "Invalid Choice" <<
endl;
}
}
} while (ch != 4);
return 0;
}

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


Instructor: Sir Mr ShafiQ Sahib Assignment: Data Structure

Programme with Queue


#include <iostream>
using namespace std;
int queue[100], n = 100, front = -1, rear = -1;
void Insert()
{
int val;
if (rear == n - 1)
cout << "Queue Overflow" << endl;
else {
if (front == -1)
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;
}
}
int main() {
int ch;
cout << "1) Insert element to queue" << endl;
cout << "2) Delete element from queue" << endl;
cout << "3) Display all the elements of queue" << endl;

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


Instructor: Sir Mr ShafiQ Sahib Assignment: Data Structure

cout << "4) Exit" << endl;


do {
cout << "Enter your choice : " << endl;
cin << ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout << "Exit" << endl;
break;
default: cout << "Invalid choice" << endl;
}
} while (ch != 4);
return 0;}

2. Write a insertion , bubble , and


selection sort .

#include<iostream>
using namespace std;

void accept(int Arr[], int s);


void display(int Arr[], int s);
void isort(int Arr[], int s);
void ssort(int Arr[], int s);
void bsort(int Arr[], int s);

int main()
{
int Arr[100], n, choice;
cout << "Enter Size of Array ";
cin >> n;
do
{
cout << "\n\nMENU";
cout << "\n1. Accept elements of array";
cout << "\n2. Display elements of array";

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


Instructor: Sir Mr ShafiQ Sahib Assignment: Data Structure

cout << "\n3. Sort the array using insertion sort


method";
cout << "\n4. Sort the array using selection sort
method";
cout << "\n5. Sort the array using bubble sort
method";
cout << "\n6. Exit";
cout << "\n\nEnter your choice 1-6 :";
cin >> choice;

switch (choice)
{
case 1: accept(Arr, n);
break;
case 2: display(Arr, n);
break;
case 3: isort(Arr, n);
break;
case 4: ssort(Arr, n);
break;
case 5: bsort(Arr, n);
break;
case 6: break;
default:cout << "\nInvalid choice";
}
} while (choice != 6);

return 0;
}

void accept(int Arr[], int s)


{
for (int I = 0; I<s; I++)
{
cout << "Enter element " << I + 1 << ":";
cin >> Arr[I];
}
}

void display(int Arr[], int s)


{

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


Instructor: Sir Mr ShafiQ Sahib Assignment: Data Structure

cout << "The elements of the array are:\n";


for (int I = 0; I<s; I++)
cout << Arr[I] << " ";
}

void isort(int Arr[], int s)


{
int I, J, Temp;
for (I = 1; I<s; I++)
{
Temp = Arr[I];
J = I - 1;
while ((Temp<Arr[J]) && (J >= 0))
{
Arr[J + 1] = Arr[J];
J--;
}
Arr[J + 1] = Temp;
}
}

void ssort(int Arr[], int s)


{
int I, J, Temp, Small;
for (I = 0; I<s - 1; I++)
{
Small = I;
for (J = I + 1; J<s; J++) //finding the
smallest element
if (Arr[J]<Arr[Small])
Small = J;
if (Small != I)
{
Temp = Arr[I]; //Swapping
Arr[I] = Arr[Small];
Arr[Small] = Temp;
}
}
}

void bsort(int Arr[], int s)

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


Instructor: Sir Mr ShafiQ Sahib Assignment: Data Structure

{
int I, J, Temp;
for (I = 0; I<s - 1; I++)
{
for (J = 0; J<(s - 1 - I); J++)
if (Arr[J]>Arr[J + 1])
{
Temp = Arr[J]; //swapping
Arr[J] = Arr[J + 1];
Arr[J + 1] = Temp;
}
}
}

Binary search
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, arr[50], search, first, last, middle;
cout << "Enter total number of elements :";
cin >> n;
cout << "Enter " << n << " number :";
for (i = 0; i<n; i++)
{
cin >> arr[i];
}
cout << "Enter a number to find :";
cin >> search;
first = 0;
last = n - 1;
middle = (first + last) / 2;
while (first <= last)
{
if (arr[middle] < search)
{

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


Instructor: Sir Mr ShafiQ Sahib Assignment: Data Structure

first = middle + 1;

}
else if (arr[middle] == search)
{
cout << search << " found at location " <<
middle + 1 << "\n";
break;
}
else
{
last = middle - 1;
}
middle = (first + last) / 2;
}
if (first > last)
{
cout << "Not found! " << search << " is not
present in the list.";
}
getch();
}

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

You might also like