class Node
{
private:
	int integer;
	Node *pointer;
public:
	Node(int x = 0, Node *y = NULL)
	{
            integer = x;
            pointer = y;
	}
	int getInteger()
	{
            return integer;
	}
	Node getPointer()
	{
            return *pointer;
	}
};
private:
	Node *stack;
	Node *top;
	int listSize;
	int pushedValue;
        int pushCounter;
public:
	Stack(int size)
        {
            top = NULL;
            pushCounter = 0;
            listSize = size;
	}

//other code... ////

void push(int value) 
	{
            if(pushCounter == listSize)
            {
                cout<<"\nOverflow"<<endl;
            }	  
            else
            {
                stack = new Node(value, top);
                top = stack;
		cout<<"\nYou just pushed the number "<<value<<"."<<endl;
                pushCounter++;
		printStack();
            }
        }

void printStack()
        {
            if(top = NULL)
            {
                cout<<"\nStack is empty"<<endl;
            }
            else
            {
                cout<<"\nYour stack is now:"<<endl;
                for(Node *i = top; i != NULL; i->getPointer())
                {
                  cout<<i->getInteger()<<endl;
                }
            }
	}

So when I run it I can't seem to get it to print the stack... It tells me
"Your stack is now:"

and then nothing. It just continues the code. I'm thinking a possibility would be that I am not actually saving a value into the node?
Any help?

Recommended Answers

All 7 Replies

Hi Krazy,

Your problem is that it's not actually a linked list yet. I see you have a variable called "pointer" in your Node class so maybe you just forgot to do the linking after you make the new nodes.

If not, just remember that when you "push", although you're successfully creating a new node, you are not yet linking the previous node to it... Whenever you create a bunch of "new Node"s, it's allocating memory blindly for you. There's no guarantee that they will be right next to each other in memory, especially if you're running something else on your computer--or if you suddenly run into a used block, it'll skip ahead and start allocating far away from your previous one.

So you have to link it up manually using pointers. The nodes of a linked list have two basic elements: The data and the pointer to the next node.

struct node {
  int x;
  node *next;
};

And then when you create "Node* nextnode = new Node", you simply have to make sure you have the label of your old node and say "oldNode->next = nextnode". Or in your case your "pointer" variable.

You also had a typo "if (top = NULL)" should be "if (top == NULL)"

There was also a problem with your loop.. i->getPointer() doesn't move the pointer around any:

for(Node *i = top; i != NULL; i->getPointer()){
    cout<<i->getInteger()<<endl;
}

It should be something like:
for (Node *i = top; i != NULL; i=i->next)

I gotta pass out for now let me know how it goes
-Greywolf

Hi Krazy,

Your problem is that it's not actually a linked list yet. I see you have a variable called "pointer" in your Node class so maybe you just forgot to do the linking after you make the new nodes.

Thanks I forgot about that.. but I'm not sure if I fixed it right.

Stack
private:
Node *nextNode;
////
nextNode = NULL;
////
void push(int value) 
	{
            if(pushCounter == listSize)
            {
                cout<<"\nOverflow"<<endl;
            }	  
            else
            {
                stack = nextNode;
                nextNode =  new Node(value, top);
                top = nextNode;
		cout<<"\nYou just pushed the number "<<value<<"."<<endl;
                pushCounter++;
		printStack();
            }
        }

You also had a typo "if (top = NULL)" should be "if (top == NULL)"

Thanks I missed that.

There was also a problem with your loop.. i->getPointer() doesn't move the pointer around any:

for(Node *i = top; i != NULL; i->getPointer()){
    cout<<i->getInteger()<<endl;
}

It should be something like:
for (Node *i = top; i != NULL; i=i->next)

I gotta pass out for now let me know how it goes
-Greywolf

Umm. the -> operator only allows me to put functions I have made in Node since i is a node.
I am I suppose to have something that sets the nodes or something in the node class?
and now that I added that = in top = NULL, it gives me a segmentation error.

How is this a stack?

Do you just want to print a linked list?

I'm making a stack using a linked list. And I can't seem to print out the stack.

I'm not certain that I share your definition of 'stack' (any of them), so I'll ask you again, are you just trying to print your linked list in linear order?

Do you want to copy the data from the linked list into a stack type container? What is the stack implementation if that is the case? Is your stack FILO or FIFO? May I see your stack class or the name of a standard container you are using as a stack?

I have a Stack Class. The user inputs the size of the stack. They push and then enter a number to push. It pushes and then prints the current stack from top to bottom. My problem is that it will not print. I get a segmentation error and the program quits.

A problem that it might be is that I am not making the linked list correctly..

I got it working.

in Node

Node *getPointer()
{
     return pointer;
}

In push

void push(int value) 
{
            if(pushCounter == listSize)
            {
                cout<<"\nOverflow"<<endl;
            }	  
            else
            {
                top =  new Node(value, top);
		cout<<"\nYou just pushed the number "<<value<<"."<<endl;
                pushCounter++;
		printStack();
            }
}

and in printStack

void printStack()
{
            if(top == NULL)
            {
                cout<<"\nStack is empty"<<endl;
            }
            else
            {
		cout<<"\nYour stack is now:"<<endl;
		for(Node *i = top; i != NULL; i=i->getPointer())
		{
			cout<<i->getInteger()<<endl;
		}
	    }
}

Thanks for all the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.