Mohammed Sikander
Team Lead
CranesVarsity
Mohammed.sikander@cranessoftware.com
STACK
 void push(int )
 void pop( )
 bool empty( )
 int & top( )
QUEUE
 void push(int)
 void pop( )
 bool empty( )
 int &front( )
Sikander 2
STACK
 Push: Increment top Index and
add the element at top
 Pop : Delete the element at top
and decrement top Index
 Empty: If top Index is -1; Stack is
empty
 int & top( )
QUEUE
 Push: Increment rear Index and
add the element at rear
 Pop : Delete the element at front
and increment front Index
 Empty : If front Index is greater
than rear Index; Queue is empty
 int &front( )
Sikander 3
int main( )
{
int dschoice ,ch , ele;
cout << "1. Stack 2. Queue n";
cin >> dschoice ;
if(dschoice == 1)
{
Stack s1;
for( ; ; ) {
cout <<"StackOperations n";
cout <<"1. Push n 2. Pop 3. Display n";
cin >> ch;
switch(ch) {
case 1 : cout <<"Enter the element to push : ";
cin >> ele;
s1.push(ele);
break;
case 2 : if(s1.empty() == true)
cout <<"Stack is empty n";
else
s1.pop( );
break;
case 3 : s1.display( );
}
}
}
Sikander 4
else
{
Queue q1;
for( ; ; )
{
cout <<"QueueOperations n";
cout <<"1. Push n 2. Pop 3. Display n";
cin >> ch;
switch(ch)
{
case 1 : cout <<"Enter the element to push : ";
cin >> ele;
q1.push(ele);
break;
case 2 : if(q1.empty() == true)
cout <<"Queue is empty n";
else
q1.pop( );
break;
case 3 : q1.display( );
} //End of Switch
} //End of For
} //End of else
} //End of Main
Sikander 5
LDS
Stack Queue
• Stack is a Linear Data Structure
•Queue is a Linear Data Strucuture
class LDS
{
};
Sikander 6
class Stack : public LDS
{
public : void push(int ele);
};
classQueue : public LDS
{
public : void push(int ele);
};
int main( )
{
LDS *ptr = new Stack; //Valid
ptr->push( 5 ); // Invalid; as we don’t have push in LDS
}
class LDS
{
public : virtual void push(int ele);
};
Sikander 7
class Stack : public LDS
{
public : void push(int ele);
};
classQueue : public LDS
{
public : void push(int ele);
};
int main( )
{
LDS *ptr = new Stack; //Valid
ptr->push( 5 ); // No Compilation Error
// Linker Error
}
class LDS
{
public : virtual void push(int ele) = 0;
};
Sikander 8
class Stack : public LDS
{
public : void push(int ele);
};
classQueue : public LDS
{
public : void push(int ele);
};
int main( )
{
LDS *ptr = new Stack; //Valid
ptr->push( 5 ); // No Compilation Error, No Linker Error
//Invokes push of Stack.
ptr = new Queue;
ptr->push(6); //Invokes push of Queue
}
class LDS
{
public: virtual void push(int ) = 0;
virtual void pop( ) = 0;
virtual void display( ) = 0;
virtual bool empty( ) = 0;
};
class Stack : publicLDS
{
public: void push(int ) { cout << “Stack Push n”;}
void pop( ) ;
void display( ) ;
bool empty( ) ;
};
classQueue : public LDS
{
public: void push(int ) { cout << “Queue Push n”;}
void pop( ) ;
void display( ) ;
bool empty( ) ;
};
Sikander 9
int main( )
{
int dschoice ,ch , ele;
LDS *ptr;
cout << "1. Stack 2. Queue n";
cin >> dschoice ;
if(dschoice == 1)
ptr = new Stack;
else
ptr = new Queue;
for( ; ; ) {
cout <<"1. Push n 2. Pop 3. Display n";
cin >> ch;
switch(ch) {
case 1 : cout <<"Enter the element to push : ";
cin >> ele;
ptr->push(ele);
break;
case 2 : if(ptr->empty() == true)
cout <<“Empty n";
else
ptr->pop( );
break;
case 3 : ptr->display( );
}
}
}
void dsoperations(LDS &ref);
int main( )
{
int dschoice ,ch , ele;
LDS *ptr;
Stack s1;
Queue q1;
cout << "1. Stack 2.Queue n";
cin >> dschoice ;
if(dschoice == 1)
dsoperations(s1);
else
dsoperations(q1);
}
Sikander 10
void dsoperations(LDS &ref)
{
for( ; ; ) {
cout <<"1. Push n 2. Pop 3. Display n";
cin >> ch;
switch(ch) {
case 1 : cout <<"Enter the element to push : ";
cin >> ele;
ref.push(ele);
break;
case 2 : if(ref.empty() == true)
cout <<“Empty n";
else
ref.pop( );
break;
case 3 : ref.display( );
}
}
}
class Shape
{ public : virtual int area( ) = 0;
};
class Rectangle : public Shape
{ int m_l , m_b;
public :
Rectangle(int l , int b ) : m_l(l) , m_b(b)
{ }
int area( ){
return m_l * m_b;
}
};
class Circle : public Shape
{ int m_radius;
Public : Circle(int r) : m_radius( r )
{}
int area( )
{
return 3.14 * m_radius * m_radius;
}
}
Sikander 11
int main( )
{
Shape *ps = new Rectangle(4 , 5);
cout << ps->area( );
ps = new Circle(6);
cout << ps->area( );
}
class Shape
{
public : virtual int area( ) = 0;
};
class Rectangle : public Shape
{
int m_l , m_b;
public : Rectangle(int l , int b )
{
m_l = l , m_b = b;
}
int area( ){
return m_l * m_b;
}
};
Sikander 12
int main( )
{
Rectangle r(4 , 5);
Shape &s = r;
s.area( );
}
classA
{
public : virtual void display( ) = 0;
};
voidA::display( )
{
cout <<“A display n”;
}
int main( )
{
}
Sikander 13

Polymorphism

  • 1.
  • 2.
    STACK  void push(int)  void pop( )  bool empty( )  int & top( ) QUEUE  void push(int)  void pop( )  bool empty( )  int &front( ) Sikander 2
  • 3.
    STACK  Push: Incrementtop Index and add the element at top  Pop : Delete the element at top and decrement top Index  Empty: If top Index is -1; Stack is empty  int & top( ) QUEUE  Push: Increment rear Index and add the element at rear  Pop : Delete the element at front and increment front Index  Empty : If front Index is greater than rear Index; Queue is empty  int &front( ) Sikander 3
  • 4.
    int main( ) { intdschoice ,ch , ele; cout << "1. Stack 2. Queue n"; cin >> dschoice ; if(dschoice == 1) { Stack s1; for( ; ; ) { cout <<"StackOperations n"; cout <<"1. Push n 2. Pop 3. Display n"; cin >> ch; switch(ch) { case 1 : cout <<"Enter the element to push : "; cin >> ele; s1.push(ele); break; case 2 : if(s1.empty() == true) cout <<"Stack is empty n"; else s1.pop( ); break; case 3 : s1.display( ); } } } Sikander 4 else { Queue q1; for( ; ; ) { cout <<"QueueOperations n"; cout <<"1. Push n 2. Pop 3. Display n"; cin >> ch; switch(ch) { case 1 : cout <<"Enter the element to push : "; cin >> ele; q1.push(ele); break; case 2 : if(q1.empty() == true) cout <<"Queue is empty n"; else q1.pop( ); break; case 3 : q1.display( ); } //End of Switch } //End of For } //End of else } //End of Main
  • 5.
    Sikander 5 LDS Stack Queue •Stack is a Linear Data Structure •Queue is a Linear Data Strucuture
  • 6.
    class LDS { }; Sikander 6 classStack : public LDS { public : void push(int ele); }; classQueue : public LDS { public : void push(int ele); }; int main( ) { LDS *ptr = new Stack; //Valid ptr->push( 5 ); // Invalid; as we don’t have push in LDS }
  • 7.
    class LDS { public :virtual void push(int ele); }; Sikander 7 class Stack : public LDS { public : void push(int ele); }; classQueue : public LDS { public : void push(int ele); }; int main( ) { LDS *ptr = new Stack; //Valid ptr->push( 5 ); // No Compilation Error // Linker Error }
  • 8.
    class LDS { public :virtual void push(int ele) = 0; }; Sikander 8 class Stack : public LDS { public : void push(int ele); }; classQueue : public LDS { public : void push(int ele); }; int main( ) { LDS *ptr = new Stack; //Valid ptr->push( 5 ); // No Compilation Error, No Linker Error //Invokes push of Stack. ptr = new Queue; ptr->push(6); //Invokes push of Queue }
  • 9.
    class LDS { public: virtualvoid push(int ) = 0; virtual void pop( ) = 0; virtual void display( ) = 0; virtual bool empty( ) = 0; }; class Stack : publicLDS { public: void push(int ) { cout << “Stack Push n”;} void pop( ) ; void display( ) ; bool empty( ) ; }; classQueue : public LDS { public: void push(int ) { cout << “Queue Push n”;} void pop( ) ; void display( ) ; bool empty( ) ; }; Sikander 9 int main( ) { int dschoice ,ch , ele; LDS *ptr; cout << "1. Stack 2. Queue n"; cin >> dschoice ; if(dschoice == 1) ptr = new Stack; else ptr = new Queue; for( ; ; ) { cout <<"1. Push n 2. Pop 3. Display n"; cin >> ch; switch(ch) { case 1 : cout <<"Enter the element to push : "; cin >> ele; ptr->push(ele); break; case 2 : if(ptr->empty() == true) cout <<“Empty n"; else ptr->pop( ); break; case 3 : ptr->display( ); } } }
  • 10.
    void dsoperations(LDS &ref); intmain( ) { int dschoice ,ch , ele; LDS *ptr; Stack s1; Queue q1; cout << "1. Stack 2.Queue n"; cin >> dschoice ; if(dschoice == 1) dsoperations(s1); else dsoperations(q1); } Sikander 10 void dsoperations(LDS &ref) { for( ; ; ) { cout <<"1. Push n 2. Pop 3. Display n"; cin >> ch; switch(ch) { case 1 : cout <<"Enter the element to push : "; cin >> ele; ref.push(ele); break; case 2 : if(ref.empty() == true) cout <<“Empty n"; else ref.pop( ); break; case 3 : ref.display( ); } } }
  • 11.
    class Shape { public: virtual int area( ) = 0; }; class Rectangle : public Shape { int m_l , m_b; public : Rectangle(int l , int b ) : m_l(l) , m_b(b) { } int area( ){ return m_l * m_b; } }; class Circle : public Shape { int m_radius; Public : Circle(int r) : m_radius( r ) {} int area( ) { return 3.14 * m_radius * m_radius; } } Sikander 11 int main( ) { Shape *ps = new Rectangle(4 , 5); cout << ps->area( ); ps = new Circle(6); cout << ps->area( ); }
  • 12.
    class Shape { public :virtual int area( ) = 0; }; class Rectangle : public Shape { int m_l , m_b; public : Rectangle(int l , int b ) { m_l = l , m_b = b; } int area( ){ return m_l * m_b; } }; Sikander 12 int main( ) { Rectangle r(4 , 5); Shape &s = r; s.area( ); }
  • 13.
    classA { public : virtualvoid display( ) = 0; }; voidA::display( ) { cout <<“A display n”; } int main( ) { } Sikander 13