Data Structure and Algorithms based on the Object-oriented programming (OOP)
where some are the base of OOP for instant inheritance, polymorphism,
Encapsulation and Abstraction etc. Now we will start learning data structures
in Programming languages such as Linked list, arrays, Structs, Classes, Queues,
Stacks etc.
Today’s Concept:
Stack Data Structure
Prerequisite of
this Lab is all those students who have basic concepts on C++ Programming such
as Syntax of Language, concept of functions, pointers, datatypes etc. Visual
Studio 2012 and onward version will be used for implementation and programming
tasks assigned in the Lab.
Stack Data Structure
works on LIFO (Last in first out) rule for example if we insert values in
stack then the value which added in the last position will delete/gets out
first and then the second one. Stack have following functions for
implementation such as POP(), PUSH(), IsEmpty(),IsFull() etc. all these function
used for implementation of this concept worldwide because this is very well
known concept of data structures and algorithms.
- POP() this function will returns the value at the TOP of the stack
- PUSH() this function will enters/insert the value in the stack
- IsEmpty() & IsFull() use for checking the status of stack for example whenever we want to insert value into the stack we have to check that stack isFull or not same we will use isEmpty function whenever we want to return the value from the stack.
Stack Vs Queue:
- Stacks works on LIFO rule on the other hands Queue works on FIFO rule.
- Stack have two operations such as POP & Push. POP use for return a value from the Top of the Stack and Push use for insert the value into the stack.
- Queue have two operations such as Enqueue and Dequeue. All values insert into the Queue using Enqueue function and all values return from the Queue using Dequeue function.
Implementation of Stack
in C++:
#include<iostream>
using namespace std;
class stack
{
int *stackarr,top,maxsize;
public:
stack(int
size)
{
this->stackarr=new int(size);
this->maxsize=size;
this->top=-1;
}
bool
isEmpty()
{
if(this->top==-1)
{
return true;
}
else
{
return false;
}
}
bool
isFull()
{
if(this->top==this->maxsize-1)
{
return true;
}
else
{
return false;
}
}
void
push(int value)
{
if(isFull())
{
cout<<"Stack is Full no more
operations"<<endl;
}
else
{
this->top++;
this->stackarr[this->top]=value;
}
}
void
pop()
{
if(isEmpty())
{
cout<<"Stack is Empty no
Vlaue "<<endl;
}
else
{
cout<<"Value at the Stack
Top "<<this->stackarr[this->top--]<<endl;
}
}
};
#include"stack.h"
int main()
{
stack obj(5);
obj.push(10);
obj.push(20);
obj.push(30);
obj.push(40);
obj.push(50);
cout<<"poping from the
Stack"<<endl;
obj.pop();
obj.pop();
obj.pop();
obj.pop();
obj.pop();
obj.pop();
system("pause");
return 0;
}
- Write your understanding on the word document about linked list using C++ please read the manual and just write what you understand don't try to copy and past from other sources.
- Create C++ program to run same code given as Example and take screenshot for submission.
- You have implemented stacks data structure using C++ now you have to create a search function which search the value in the stack for example if our stack populated with values 1,2,3,4,5 etc. then if i search 2 then you program should print 2 found in stack otherwise not found in stack.
Tasks Submission:
Post a Comment