Jump to content

Little problem with List Implementation of Stack (Template)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
I am having a problem while make a template of a stack using linked list , the problem is that how can i get to know my Node class what type of data is to be stored in it?

here's the code


class Node{ 


public: 

      Node(){ next=NULL; value=0; } 


      void setNext(Node *next){ this->next=next; }

	  void setValue(int v){ value=v; } 


      Node *getNext(){ return next; }

      int getValue(){ return value; } 


private: 

      int value;

      Node *next; 

}; 


template<class T>

class Stack{


public:


	Stack(){

		count=0;

		newNode=NULL;

		head=NULL;

	}


	T getTop(){	return head->getValue();	}

	int getCount(){	return count;	}


	void push(T &v){

		newNode=new Node();

		newNode->setValue(v);

		newNode->setNext(NULL);


		if(head==NULL){	

			head=newNode;

		}


		else{

			newNode->setNext(head);

			head=newNode;			

		}

		count++;

	}



	int pop(){

		T temp;

	if(!isEmpty()){

		Node *t1=head;

		temp=head->getValue();

		head=head->getNext();

		count--;

		delete t1;

		}

	else

		cout<<"Stack Empty";

	return temp;


	}



	bool isEmpty(){

		if(head==NULL)

			return true;

		else

			return false;

	}



	void displayStack(){


		Node *t=head;

		if(!isEmpty() ){

		while(t!=NULL){

			cout<<" "<<t->getValue()<<"  ";

			t=t->getNext();

			}

		}

		else

			cout<<" Stack Empty \n";


	}


private:

	

	int count;

	Node *newNode;

	Node *head;


};




#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Node will need to be a template as well.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thanks I got the problem solved made some other changes to it also :)