Thread: stack class

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    85

    stack class

    Ok, I've got my stack class I made, but as you know if I push 1,2,3 in a stack i would expect 3 to get popped out first right?

    Mine doesn't!!! What's wrong?

    Code:
    template <class T>
    class List
    {
    	public:
    		List();
    		~List();
    		void Push(T); // Pushes i onto stack
    		T Pop(); // Pops top element from stack
    		int IsEmpty(); // returns 1 if empty; 0 if not empty;
    		int Contains(T);
    	private:
    		struct Node
    		{
    			T Item;
    			Node *Next;
    		};
    		Node *Head;
    };
    
    
    template <class T>
    List<T>::List()
    {
    	Head = 0;
    }
    
    template <class T>
    List<T>::~List()
    {
    }
    
    template <class T>
    void List<T>::Push(T i) // Pushes i onto stack
    {
    	Node *tmp, *curr;
    	
    	//create new node and store integer
    	tmp = new Node;
    	tmp->Item = i;
    	tmp->Next = 0;
    	
    	//add to top of stack if not empty
    	if(Head != 0)
    	{
    		curr = Head;
    		while(curr->Next)
    			curr = curr->Next;
    		curr->Next = tmp;
    	}
    	else
    		Head = tmp;
    }
    
    template <class T>
    T List<T>::Pop() // Pops top element from stack
    {
    	T tmp;
    	
    	if(Head == 0) //nothing to pop!
    		return 0;
    	
    	tmp = Head->Item;
    	Head = Head->Next;
    	
    	return tmp;
    }
    
    template <class T>
    int List<T>::IsEmpty() // returns 1 if empty; 0 if not empty;
    {
    	if(Head == 0) 
    		return 1;
    	return 0;
    }
    
    template <class T>
    int List<T>::Contains(T i)
    {
    	Node *curr;
    	
    	if(Head->Item == i)
    		return 1;
    	else
    	{
    		curr = Head;
    		while(curr->Next)
    		{
    			if(curr->Item == i)
    				return 1;
    			else
    				curr = curr->Next;
    		}
    	}
    	return 0;
    }

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    void List<T>::Push(T i) // Pushes i onto stack
    {
    	Node *tmp, *curr;
    	
    	//create new node and store integer
    	tmp = new Node;
    	tmp->Item = i;
    	tmp->Next = 0;
    	
    	//add to top of stack if not empty
    	if(Head != 0)
    	{
    		curr = Head;
    		while(curr->Next)
    			curr = curr->Next;
    		curr->Next = tmp;
    	}
    	else
    		Head = tmp;
    }
    
    template <class T>
    T List<T>::Pop() // Pops top element from stack
    {
    	T tmp;
    	
    	if(Head == 0) //nothing to pop!
    		return 0;
    	
    	tmp = Head->Item;
    	Head = Head->Next;
    	
    	return tmp;
    }
    You are adding new items to the list at the end while removing items from the beginning. You need to pick one or the other. Is it wise to have to go through the entire list to add or remove an item?

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    im so lost.

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need Help with Stack
    By trongsi in forum C++ Programming
    Replies: 9
    Last Post: 05-23-2006, 04:14 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Finished Stack
    By the pooper in forum C Programming
    Replies: 11
    Last Post: 02-02-2005, 10:52 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM