Stack
Data Structure
Youtube: SL Coder
Intro to
Stack
 ADT(Abstract Data Structure).
 Based on array.
 Logically stack is linear structure.
 Data stores in top of the previous one.
Pros andCons
Advantages(Pros) are
 Insertion and deletion very quick.
 Provides LIFO(Last In First Out).
Disadvantages are
 We can not access every item in stack.
 Fixed size.
 Memory consuming.
WhereStack
are used?
 Use to Implementing recursion
 Return addresses are placed on a stack.
 For reversing string.
 Text Editors Undo/Redo feature.
 Web browser recent visited.
Complexity
 Push(insert) and pop(remove) takes constant O(1) time.
 Search takes O(n).
Operations of
Stack
 push()
 pop()
 peek()
 getSize()
 isEmpty()
 isFull()
Implementation
headerfile(Stack.h)
UsingVisual C++
 Project name is DataStructure.
 Add a class called Stack.
 In header file(Stack.h)
private:
int maxSize;
int top;
int *stack;
Implementation
Continued
header file(Stack.h)
 public:
Stack(int s);
~Stack();
void push(int value);
int pop();
int peek();
int getSize();
bool isEmpty();
bool isFull();
Implementation
Continued
cppfile(Stack.cpp)
 Stack::Stack(int s){
maxSize = s;
top = -1;
stack = new int[size];
}
 Stack::~Stack(){
delete[] stack;
}
Implementation
Continued
cppfile(Stack.cpp)
 void Stack::push(int value){
if(!isFull()){
stack[++top] = value;
}else{
cout<<“Stack is full!”<<endl;
}
}
 Int Stack::pop(){
if(!isEmpty()){
return stack[top--];
}else{
cout<<“Stack is Empty!”<<endl;
return -1;
}
}
Implementation
Continued
cppfile(Stack.cpp)
 int Stack::peek(){
if(!isEmpty()){
return stack[top];
}else{
cout<<“Stack is Empty!”<<endl;
return -1;
}
}
 int Stack::getSize(){
return maxSize;
}
Implementation
Continued
cppfile(Stack.cpp)
 b00l Stack::isEmptly(){
return top == -1;
}
 bool Stack::isFull(){
return top == maxSize - 1;
}
Implementation
Continued
mainfile(DataStructure.cpp)
Stack *numStack = new Stack(4);
numStack.push(2);
numStack.push(5);
numStack.push(6);
numStack.push(9);
numStack.peek();
numStack.pop();
numStack.peek();
Outro to
Stack • You can use Stack in any programming language.
• You can check out my Stack Implementation in java video
tutorial.
• Also Stack other videos(Stack using in projects, char data
type stack and etc.)
• And next video is Queue Data Structure.
Thanks for
Watching

Stack Data structure