View
59
Download
0
Category
Preview:
Citation preview
StackData Structure
Youtube: SL Coder
Intro toStack
ADT(Abstract Data Structure). Based on array. Logically stack is linear structure. Data stores in top of the previous one.
Pros and Cons
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.
Where Stack 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()
Implementationheader file(Stack.h)
Using Visual C++ Project name is DataStructure. Add a class called Stack. In header file(Stack.h)
private:int maxSize;int top;int *stack;
Implementation Continuedheader file(Stack.h)
public:Stack(int s);~Stack();void push(int value);int pop();int peek();int getSize();bool isEmpty();bool isFull();
Implementation Continuedcpp file(Stack.cpp)
Stack::Stack(int s){maxSize = s;top = -1;stack = new int[size];
} Stack::~Stack(){
delete[] stack;}
Implementation Continuedcpp file(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 Continuedcpp file(Stack.cpp)
int Stack::peek(){if(!isEmpty()){
return stack[top];}else{
cout<<“Stack is Empty!”<<endl;return -1;
}}
int Stack::getSize(){return maxSize;
}
Implementati
on Continuedcpp file(Stack.cpp)
b00l Stack::isEmptly(){return top == -1;
}
bool Stack::isFull(){return top == maxSize - 1;
}
Implementati
on Continuedmain file(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 toStack
Click icon to add picture
• 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
Recommended