stack data structre introduction and source code

15
Stack Data Structure Youtube: SL Coder

Upload: sl-coder

Post on 12-Apr-2017

59 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Stack Data Structre Introduction and source code

StackData Structure

Youtube: SL Coder

Page 2: Stack Data Structre Introduction and source code

Intro toStack

ADT(Abstract Data Structure). Based on array. Logically stack is linear structure. Data stores in top of the previous one.

Page 3: Stack Data Structre Introduction and source code

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.

Page 4: Stack Data Structre Introduction and source code

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.

Page 5: Stack Data Structre Introduction and source code

Complexity Push(insert) and pop(remove) takes constant O(1)

time. Search takes O(n).

Page 6: Stack Data Structre Introduction and source code

Operations of Stack

push() pop() peek() getSize() isEmpty() isFull()

Page 7: Stack Data Structre Introduction and source code

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;

Page 8: Stack Data Structre Introduction and source code

Implementation Continuedheader file(Stack.h)

public:Stack(int s);~Stack();void push(int value);int pop();int peek();int getSize();bool isEmpty();bool isFull();

Page 9: Stack Data Structre Introduction and source code

Implementation Continuedcpp file(Stack.cpp)

Stack::Stack(int s){maxSize = s;top = -1;stack = new int[size];

} Stack::~Stack(){

delete[] stack;}

Page 10: Stack Data Structre Introduction and source code

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;}

}

Page 11: Stack Data Structre Introduction and source code

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;

}

Page 12: Stack Data Structre Introduction and source code

Implementati

on Continuedcpp file(Stack.cpp)

b00l Stack::isEmptly(){return top == -1;

}

bool Stack::isFull(){return top == maxSize - 1;

}

Page 13: Stack Data Structre Introduction and source code

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();

Page 14: Stack Data Structre Introduction and source code

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.

Page 15: Stack Data Structre Introduction and source code

Thanks for Watching