Generic Stack Linked List help please

Help with some code dealing with template class of linked list

Page 1 of 1

2 Replies - 2083 Views - Last Post: 09 October 2009 - 04:45 PM Rate Topic: -----

#1 jbord39  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 13
  • Joined: 09-October 09

Generic Stack Linked List help please

Posted 09 October 2009 - 03:45 PM

Hey guys. I have an assignment due to create a generic linked list stack and do a bunch of stuff with it. I have a pretty short amount of code that will compile but crashes at the prompt.. If anyone could look at it I would really appreciate it.

code crashes in Stack.cpp; in the push function (I will mark it for you).

in main.cpp:
#include <iostream>
#include "Stack.cpp"

using namespace std;

int main()
{
	cout << "Top of main\n";
	Stack<int> intStack;
	Stack<char> charStack;
	Stack<double> doubleStack;

	cout << "Stacks declared in main\n";

	intStack.push(1);

	cout << "After first push in main\n";

	intStack.push(13);
	int printInt;
	cout << "Popping... " << intStack.pop(printInt) << "\n";

	return 0;
}



in Stack.h:
#ifndef STACK_H
#define STACK_H

template <class T>
class Stack 
{
	private:
	class StackNode
	{
	   friend class Stack;
	   T value;
	 	   StackNode *next;
	   StackNode(T value1, StackNode *next1 = NULL)
	   {
		value = value1;
	 	next = next1;
	   }
		
	}*head;

	public:
	Stack()
	  { head = NULL; }
	void push(T);
	void pop(T&);
	bool isEmpty();

};


#endif



and in Stack.cpp:
#include <iostream>
#include "Stack.h"

using namespace std;


template <class T>
void Stack<T>::push(T value)
{

	cout << "Push at top\n";
	StackNode *newNode;
	cout << "newNode declared as a StackNode\n";	  //this is last output to prompt!!
	newNode->value = value;
	cout << "Push function before if\n";
	if (isEmpty())
	{
		head = newNode;
		newNode->next = NULL;
	}
	
	else
	{
		newNode->next = head;
		head = newNode;
	}
}


template <class T>
void Stack<T>::pop(T &value)
{

	StackNode *tempPtr;
	if(isEmpty())
	{
		cout << "The stack is empty.\n";
		exit(1);
	}
	else
	{
		value = head->value;
		tempPtr = head;
		head = head->next;
		delete tempPtr;
	}
}


template <class T>
bool Stack<T>::isEmpty()
{
	bool status;
	if (!head)
		status = true;
	else
		status = false;
	return status;

}



Is This A Good Question/Topic? 0
  • +

Replies To: Generic Stack Linked List help please

#2 Guest_Neumann*


Reputation:

Re: Generic Stack Linked List help please

Posted 09 October 2009 - 04:23 PM

StackNode *newNode;
newNode->value = value;



What does the newNode point to?
Was This Post Helpful? 0

#3 jbord39  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 13
  • Joined: 09-October 09

Re: Generic Stack Linked List help please

Posted 09 October 2009 - 04:45 PM

View PostNeumann, on 9 Oct, 2009 - 03:23 PM, said:

StackNode *newNode;
newNode->value = value;



What does the newNode point to?


I tried changing the code to this, which does work if my StackNode is a struct, but not if it's a class.

StackNode *newNode;
newNode = new StackNode;
newNode->value = value;



Sadly my StackNode has to be a class, but this command won't compile if StackNode is a class.

Thanks for any help (future or past)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1