18
Prepared by Mohammed Sikander Technical Lead Cranes Software International Limited

Implementing stack

Embed Size (px)

Citation preview

Page 1: Implementing stack

Prepared by Mohammed Sikander Technical Lead Cranes Software International Limited

Page 2: Implementing stack

This document will help you to understand the concepts of C++ through simple programs. The topics covered in this document are Size of Class Constructor and Destructor Lifetime of Objects Memory Leakage

[email protected] 2

Page 3: Implementing stack

C

int buffer[20]; int topIndex = -1; void push(int ele); int pop( ); void display( );

DRAWBACK OF THIS APPROACH

This approach works only if you have One stack in the program.

Sikander 3

Page 4: Implementing stack

WORK WITH LOCAL VARIABLES AND PARAMETERS

void push(int buffer[],int *ti,int ele); int pop(int buffer[],int *ti); void display(const int buffer[],int ti); int main( ) { int buffer1[20] , buffer2[20]; int ti1 =-1 , ti2 = -1; //Insert in first Stack. push(buffer1 , &ti1 , 45); //Insert in Second Stack push(buffer2 , &ti2 , 75); }

DRAWBACK OF THIS APPROACH

Every time you want to add a new stack, we need to create two separate variables (Array and TopIndex)

Possibility of passing buffer1 and ti2.

Sikander 4

Page 5: Implementing stack

GROUP BUFFER AND TOP INDEX INTO ONE UNIT

struct MyStack { int ti; int buffer[20]; }; void push(MyStack *ps,int ele) { buffer[++ti] = ele; ps->buffer[++ps->ti] = ele; } int pop(MyStack *ps); void display(const MyStack *ps);

int main( ) { struct MyStack s1 = {-1} , s2 = {-1};

//Insert in first Stack. push( &s1 , 45); //Insert in Second Stack push(&2 , 75); }

Sikander 5

Page 6: Implementing stack

GROUPING DATA AND FUNCTIONS INTO ONE UNIT

struct MyStack { int ti; int buffer[20]; void push(int ele) { buffer[++ti] = ele; } };

int main( ) { MyStack s1 = {-1} , s2 = {-1}; s1.push(45); s2.push( 75); }

Sikander 6

Page 7: Implementing stack

• All members are private by default in class. • Cannot access private members from outside the class. • Cannot initialize the members of class / struct with traditional

method if the members are private

class MyStack { int ti; int buffer[20]; void push(int ele) { buffer[++ti] = ele; } };

int main( ) { MyStack s1 = {-1} , s2 = {-1}; s1.push(45); s2.push( 75); }

Sikander 7

Page 8: Implementing stack

• All members are private by default in class. • Cannot access private members from outside the class. • Cannot initialize the members of class / struct with traditional

method if the members are private

class MyStack { int ti; int buffer[20]; public: void push(int ele) { buffer[++ti] = ele; } };

int main( ) { MyStack s1, s2; s1.push(45); s2.push( 75); }

Sikander 8

Page 9: Implementing stack

• What happens if we forget to invoke initialize function? • Can we write a function such that it invokes automatically on Object

Creation?

class MyStack { int ti; int buffer[20]; public: void initialize( ) { ti = -1; } void push(int ele); };

int main( ) { MyStack s1, s2; s1.initialize( ); s2.initialize( ); s1.push(45); s2.push( 75); }

Sikander 9

Page 10: Implementing stack

class MyStack { int ti; int buffer[20]; public: MyStack( ) { ti = -1; } void push(int ele) { if(top == 20-1) cout <<“Stack Full”; else buffer[++topIndex] = ele; } };

int main( ) { MyStack s1, s2; s1.push(45); s2.push( 75); }

Sikander 10

Page 11: Implementing stack

mystack.cpp #include “mystack.h” MyStack::MyStack( ) { ti = -1; } void MyStack::push(int ele) { buffer[++ti] = ele; } int MyStack::pop() { return buffer[ti--]; }

Testmystack.cpp #include “mystack.h” int main( ) { MyStack s1; s1.push(45); s1.push( 75); cout << s1.pop( ) << endl; cout << s1.pop( ) << endl; }

Sikander 11

mystack.h class MyStack { int ti; int buffer[20]; public: MyStack( ); void push(int ele); int pop(); };

Page 12: Implementing stack

class MyStack { int ti; int buffer[20]; public: MyStack( ); void push(int ele); int pop(); void display(); };

int main( ) { MyStack s1, s2; }

Sikander 12

S1 can store 20 elements. S2 can store 20 elements. Requirement : I would like to have a say on the size of buffer when creating object. Different Objects might have different sizes.

Page 13: Implementing stack

class MyStack { int topIndex; int *buffer; int size; public: MyStack(int sz) { topIndex = -1; size = sz; buffer = new int[size]; } void push(int ele) { if(top == size -1) cout <<“Stack Full”; else buffer[++topIndex] = ele; } };

int main( ) { //Create a stack object which can hold 5 elements.

MyStack s1 = MyStack(5); //Create a stack object which can hold 10 elements.

MyStack s2 = MyStack(10); cout <<“Inserting in first Stack \n”; for(int index = 0 ; index < 7 ; index++) s1.push(3); cout <<“ Inserting in Second Stack \n”; for(int index = 0 ; index < 7 ; index++) s2.push(5); }

Sikander 13

Page 14: Implementing stack

Creating object with below statement throw error. MyStack s3;

[email protected] 14

If the user does not specify the size, it should take a fixed size of 6

MyStack(int sz = 6) { topIndex = -1; size = sz; buffer = new int[size]; }

Page 15: Implementing stack

class MyStack { int topIndex; int *buffer; int size; public: MyStack(int sz = 6) { topIndex = -1; size = sz; buffer = new int[size]; } void push(int ele) { buffer[++topIndex] = ele; } int pop( ) { return buffer[topIndex --]; } };

int main( ) { //Create a stack object which can hold 5 elements.

MyStack s1 = MyStack(5); cout <<“Inserting in Stack \n”; for(int index = 0 ; index < 5 ; index++) s1.push(3); cout <<“Deleting from Stack \n”; for(int index = 0 ; index < 5 ; index++) s1.pop( ); }

Sikander 15

Page 16: Implementing stack

class MyStack { int topIndex; int *buffer; int size; public: MyStack(int sz = 6) ; void push(int ele); int pop( ); ~MyStack( ) { delete [ ] buffer; } };

int main( ) { //Create a stack object which can hold 5 elements.

MyStack s1 = MyStack(5); cout <<“Inserting in Stack \n”; for(int index = 0 ; index < 5 ; index++) s1.push(3); cout <<“Deleting from Stack \n”; for(int index = 0 ; index < 5 ; index++) s1.pop( ); }

Sikander 16

Page 17: Implementing stack

class MyStack { int topIndex; int *buffer; int size; public: MyStack(int sz = 6) ; void push(int ele); int pop( ); ~MyStack( ) { delete [ ] buffer; } };

int main( ) { //Create a stack object which can hold 5 elements.

MyStack *ptr; ptr = new MyStack(5); cout <<“Inserting in Stack \n”; for(int index = 0 ; index < 5 ; index++) ptr->push(3); cout <<“Deleting from Stack \n”; for(int index = 0 ; index < 5 ; index++) ptr->pop( ); }

Sikander 17

Page 18: Implementing stack

class MyStack { int topIndex; int *buffer; int size; public: MyStack(int sz = 6) ; void push(int ele); int pop( ); ~MyStack( ) { delete [ ] buffer; } };

int main( ) { //Create a stack object which can hold 5 elements.

MyStack *ptr; ptr = new MyStack(5); cout <<“Inserting in Stack \n”; for(int index = 0 ; index < 5 ; index++) ptr->push(3); cout <<“Deleting from Stack \n”; for(int index = 0 ; index < 5 ; index++) ptr->pop( ); delete ptr; }

Sikander 18