Beruflich Dokumente
Kultur Dokumente
University Islamabad
Sahiwal Campus
Assignment#:2
Subject:
Data Structure
Submitted to:
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;
top--;
}
}
void display() {
if (top >= 0) {
cout << "Stack elements are:";
for (int i = top; i >= 0; i--)
cout << stack[i] << " ";
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)
front = 0;
cout << "Insert the element in queue : " << endl;
cin >> val;
rear++;
queue[rear] = val;
}
}
void Delete()
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;
}
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;
The End