Professional Documents
Culture Documents
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”.
1. Push()
2. Pop ()
1. Push() function is used to add or insert new elements into 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.
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;
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;
}
#include<iostream>
using namespace std;
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";
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;
}
{
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)
{
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();
}