GeeksforGeeks » C/C++ Programming Questions

inheritance

(4 posts)
  • Started 3 months ago by monmegh
  • Latest reply from vikram.kuruguntla
  1. monisha
    Member
    Posted 3 months ago #

    implementing stacks and queues using inheritance from linked list???

  2. vikram.kuruguntla
    Member
    Posted 3 months ago #

    You can try this.

    1> Implement class List or you can use class list provided by the STL.
    2> Create derived class Stack by inheriting class list PRIVATELY. So that all the public functions in class list will become private in class Stack.
    3> Implement public functions related to stack( like push(), pop() and top()) using functions provided by the class List.
    4> for ex: push() can use push_back() funciton of list, pop() can use pop_back() function of list and top() can use back() function of list.
    5> We have to make sure, using Stack object you can access only push(), pop() and top() which are related to stack, but not the functions provided by the class List.

    #include "stdafx.h"
    #include <list>
    #include <iostream>
    using namespace std;
    
    template< typename T >
    class Stack : private list<T>
    {
    public:
    	void push(T element)
    	{
    		push_back(element);
    	}
    	void pop()
    	{
    		if(size() > 0)
    			pop_back();
    		/*else
    			may be some exception*/
    
    	}
    	T& top()
    	{
    		if(size() > 0)
    			return back();
    		/*else
    			may be some exception*/
    	}
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	Stack<int> s;
    	s.push(10);
    	s.push(20);
    	s.push(30);
    	s.pop();
    	//s.front();  can not use list's functions.
    	//s.push_back(40); can not use list's functions.
    	cout << s.top() << endl; // prints 20
    	return 0;
    }
    
  3. monisha
    Member
    Posted 3 months ago #

    ya!! it works gud!! but its bttr not to use templates. Moreover it s a linked list whch being a dynamic memory alloc.
    so main() can be
    for(i=0;i<n;i++)
    cin>>a[i];
    s.push(a[i]);

    for(i=0;i<n;i++)
    s.pop() ;

  4. vikram.kuruguntla
    Member
    Posted 3 months ago #

    @monisha

    Templates allow you to do generic programming. With that stack implementation it will work for any type. You can create Stack of int's or Stack of float's or Stack of user defined objects. Without the templates, implementation need to be restricted to specific type.

    All STL containers(vector, deque, list, ... etc) are implemented using templates.

    Though list is dynamically allocates memory, we don't need to be worried about the freeing memory explicitly. Destructor of the list takes care of deallocating the memory when the object of list goes out of scope.

    for ex:

    void fun()
    {
       list<int> l;
      l.push_back(10);
      l.push_back(20);
      l.push_back(30);
    }

    In this example, we don't need to deallocate the memory. when object "l" goes out of fun() automatically destructor of class list is called and entire memory will be deallocated.


Reply

You must log in to post.

RSS feed for this topic