First let me describe the program
All it does is pull in some chars and stores them in a linked list of stacks.
I have the code written to pull them in but when I need to print them I dont know how to go backwards without deleting the node.

This is how I created the nodes

void BigStack::Push(char ch)
{
    MiniStack* stackPtr;
    ListNode* nextPtr;
    
    if (num == 0 || headPtr->stackPtr->IsFull())
	{
		ListNode* tempPtr = new ListNode;
		tempPtr->nextPtr = headPtr;
		headPtr = tempPtr;	

		tempPtr->stackPtr = new MiniStack;
		tempPtr->stackPtr->Push(ch);
		delete tempPtr;
 	}
 	
	else
		headPtr->stackPtr->Push(ch);

	num++;
}

And this is the function that is supposed to print the stack from top to bottom so that they print out in the LIFO fashon. The other program has a Print() function in it that has to be used.
This is what I have so far but I know it is not correct.
How would I do this?

void BigStack::Print() const    // Prints stack contents, top to bottom
{
	int n;
	ListNode* printPtr;
	ListNode* printPtr2;
	for (n = num; n != -1; n--)
	{   
		if (headPtr->stackPtr->IsEmpty() && nextPtr == NULL)
		{
		    printPtr = headPtr;
		    printPtr->nextPtr = printPtr2;
		}
	}
}

Recommended Answers

All 8 Replies

First let me describe the program
All it does is pull in some chars and stores them in a linked list of stacks.
I have the code written to pull them in but when I need to print them I dont know how to go backwards without deleting the node.

This is how I created the nodes

void BigStack::Push(char ch)
{
    MiniStack* stackPtr;
    ListNode* nextPtr;
    
    if (num == 0 || headPtr->stackPtr->IsFull())
	{
		ListNode* tempPtr = new ListNode;
		tempPtr->nextPtr = headPtr;
		headPtr = tempPtr;	

		tempPtr->stackPtr = new MiniStack;
		tempPtr->stackPtr->Push(ch);
		delete tempPtr;
 	}
 	
	else
		headPtr->stackPtr->Push(ch);

	num++;
}

And this is the function that is supposed to print the stack from top to bottom so that they print out in the LIFO fashon. The other program has a Print() function in it that has to be used.
This is what I have so far but I know it is not correct.
How would I do this?

void BigStack::Print() const    // Prints stack contents, top to bottom
{
	int n;
	ListNode* printPtr;
	ListNode* printPtr2;
	for (n = num; n != -1; n--)
	{   
		if (headPtr->stackPtr->IsEmpty() && nextPtr == NULL)
		{
		    printPtr = headPtr;
		    printPtr->nextPtr = printPtr2;
		}
	}
}

Well, I can't see your whole code, so I have to guess what num represents in the bottom. Is that the number of ListNodes? You should probably change that loop to a while loop and have it go till some pointer to a ListNode is null.

line 14 on top looks wrong to me:

delete tempPtr;

Why are you doing this? You're creating a ListNode, then deleting it?

As far as traversing through the list, if you want to be able to go forwards and backwards, you probably want to add this data member to your ListNode class:

ListNode* lastPtr

You already have this:

LastNode* nextPtr

I can't really get my hands around the code much more because I can't see a lot of the stuff it's referring to. At minimum, post the MiniStack and ListNode data members and describe what each represents.

Well, I can't see your whole code, so I have to guess what num represents in the bottom. Is that the number of ListNodes? You should probably change that loop to a while loop and have it go till some pointer to a ListNode is null.

line 14 on top looks wrong to me:

delete tempPtr;

Why are you doing this? You're creating a ListNode, then deleting it?

As far as traversing through the list, if you want to be able to go forwards and backwards, you probably want to add this data member to your ListNode class:

ListNode* lastPtr

You already have this:

LastNode* nextPtr

I can't really get my hands around the code much more because I can't see a lot of the stuff it's referring to. At minimum, post the MiniStack and ListNode data members and describe what each represents.

Ok here is the whole thing except I took out the delete tempPtr

This is the ministack.h file

//
// ministack.h
//

#ifndef MINISTACK_H

#define MINISTACK_H

const int MINI_STACK_SIZE = 5;  // Fixed number of element in stack

class MiniStack
{
  private:
    int num;               // Number of values stored in MiniStack
    char* stackPtr;        // Pointer to array representing stack

  public:
    MiniStack();           // Default constructor
    void Push(char ch);    // Adds element to top of stack assuming stack not full
    void Pop();            // Removes element from top of stack
    void MakeEmpty();      // Empties ministack
    char Top();            // Returns copy of value stored at top of stack
    bool IsFull() const;   // Returns true if ministack is full; false otherwise
    bool IsEmpty() const;  // Returns true if ministack empty; false otherwise
    void Print() const;    // Prints stack contents, top to bottom
    ~MiniStack();          // Destructor
};

#endif

ministack.cpp file

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include <new>
#include <cstddef>
#include "ministack.h"
#include "bigstack.h"

using namespace std;

MiniStack::MiniStack()					// Default constructor
{
    stackPtr = new char[MINI_STACK_SIZE];
    num = 0;
}

void MiniStack::Push(char ch)			// Adds element to top of stack assuming stack not full
{
 	 
	 if (num >=0 && num <=4)
	 {
	  	 stackPtr[num] = ch;
 	 	 num++;
	 }
	 else if(num < 0)
	 {
		 num = 0;
	 	 stackPtr[num] = ch;
	 	 num++;
	 }
	 else
	    num = MINI_STACK_SIZE;
}

void MiniStack::Pop()					// Removes element from top of stack
{
 	num--;
}

void MiniStack::MakeEmpty()				// Empties ministack
{
 	num = -1;
}

char MiniStack::Top()					// Returns copy of value stored at top of stack
{
 	return stackPtr[num-1];
}

bool MiniStack::IsFull() const			// Returns true if ministack is full; false otherwise
{
	if (num == 5 )
	return true;
	else
	return false;
	
}

bool MiniStack::IsEmpty() const			// Returns true if ministack empty; false otherwise
{
	return (num == -1);
}

void MiniStack::Print() const			// Prints stack contents, top to bottom
{
	int n;
	n = num - 1;
	
	do
	{		
		if (n <= -1)
		break;
		else
		{
		cout << stackPtr[n] << " ";
		n--;
		}
		
	}while (n != -1);
   cout << endl;	
}

MiniStack::~MiniStack()					// Destructor
{
    delete [] stackPtr;
}

bigstack.h file

#ifndef BIGSTACK_H

#define BIGSTACK_H

#include "ministack.h"

struct ListNode            // Description of a ListNode struct
{
    MiniStack* stackPtr;   // Pointer to a MiniStack object
    ListNode*  nextPtr;    // Pointer to next ListNode
};


class BigStack             // Description of BigStack class
{
  private:
    int num;               // Total number of values stored in MiniStack
    ListNode* headPtr;     // Pointer to head of list of nodes representing bigstack

  public:
    BigStack();            // Default constructor
    void Push(char ch);    // Adds element to top of stack assuming stack not full
    void Pop();            // Removes element from top of stack
    char Top();            // Returns copy of top value assuming stack not empty
    bool IsFull() const;   // Returns true if ministack is full; false otherwise
    bool IsEmpty() const;  // Returns true if ministack empty; false otherwise
    void Print() const;    // Prints stack contents, top to bottom
    ~BigStack();           // Destructor
};

#endif

bigstack.cpp file

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include <new>
#include <cstddef>
#include "ministack.h"
#include "bigstack.h"

using namespace std;

BigStack::BigStack()             // Default constructor
{
    headPtr = NULL;
    num = 0;
}

void BigStack::Push(char ch)    // Adds element to top of stack assuming stack not full
{
    MiniStack* stackPtr;
    ListNode* nextPtr;
    
    if (num == 0 || headPtr->stackPtr->IsFull())
	{
		ListNode* tempPtr = new ListNode;
		tempPtr->nextPtr = headPtr;
		headPtr = tempPtr;	

		tempPtr->stackPtr = new MiniStack;
		tempPtr->stackPtr->Push(ch);
 	}
 	
	else
		headPtr->stackPtr->Push(ch);

	num++;
}

void BigStack::Pop()            // Removes element from top of stack
{
	headPtr->stackPtr->Pop();
}

char BigStack::Top()            // Returns copy of top value assuming stack not empty
{
	headPtr->stackPtr->Top();
}

bool BigStack::IsFull() const   // Returns true if ministack is full; false otherwise
{
	headPtr->stackPtr->IsFull();
}

bool BigStack::IsEmpty() const  // Returns true if ministack empty; false otherwise
{
	if(num == 0)
	return true;
	else
	return false;
}

void BigStack::Print() const    // Prints stack contents, top to bottom
{
	int n;
	ListNode* nextPtr;
	ListNode* printPtr;
	ListNode* printPtr2;
	for (n = num; n != -1; n--)
	{   
		if (headPtr->stackPtr->IsEmpty() && nextPtr == NULL)
		{
		    printPtr = nextPtr;
		    printPtr->nextPtr = printPtr2;
		}
	}
}

BigStack::~BigStack()           // Destructor
{
    do
    {
	 num--;
	 }while (num >= 0);
}

main.cpp

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include <new>
#include <cstddef>
#include "ministack.h"
#include "bigstack.h"

using namespace std;

int main(int argc, char* const argv[])
{
	ifstream inputs;
	
	if (argc !=2)
	{
		cout << "Usage:\n program05 <inputfile>\n";
		return 1;
	}
	
	inputs.open(argv[1]);			//attempt to open input file
	
	if (!inputs)
    {
		cout << "Unable to open file" << endl;
		return 1;
    }
    
	 BigStack BigStackInput;
	char CharInputFromFile;
	
	inputs >> CharInputFromFile;
	//cout << CharInputFromFile << endl;
	while (!inputs.eof())
	{  
		                                                 //c + - p t d
		switch (CharInputFromFile)
		{
		    case 'c':
               
			   cout << "Constructor -- status = Successful" << endl;
                break;
                
			case '+':
             
				//BigStackInput.IsFull();
				
				inputs >> CharInputFromFile;
				cout << "Push ('" << CharInputFromFile << "') -- Status = ";
				//cout << CharInputFromFile << endl;
				BigStackInput.Push(CharInputFromFile);
				break;

			case '-':
				cout << "Pop() -- Status = ";  
				BigStackInput.IsEmpty();
				BigStackInput.Pop();
				
				break;

			case 'p':
				cout << "Print --"; BigStackInput.Print(); 
				break;
				
			case 't':
				cout << "Top() -- Status =";
				//try
				//{
				BigStackInput.Top();
				//}
				//catch 
				//{
				 		
				//}
				break;

			case 'd':
				cout << "Destructor -- status =";
				
				BigStackInput.~BigStack();
				
				break;

			default:
				cout << "unknown" << endl;
			
			}
		
	     inputs >> CharInputFromFile;
	
	}
	system("pause");
}	return 0;

Keep in mind that I am not done with it yet. I also have some things commented out that will be deleted or used later.

void BigStack::Push(char ch)    // Adds element to top of stack assuming stack not full
{
    MiniStack* stackPtr;
    ListNode* nextPtr;
    
    if (num == 0 || headPtr->stackPtr->IsFull())
	{
		ListNode* tempPtr = new ListNode;
		tempPtr->nextPtr = headPtr;
		headPtr = tempPtr;	

		tempPtr->stackPtr = new MiniStack;
		tempPtr->stackPtr->Push(ch);
 	}
 	
	else
		headPtr->stackPtr->Push(ch);

	num++;
}

You aren't treating headPtr as a pointer to the first MiniStack/ListNode. You need at least these two data members in BigStack:

ListNode* headPtr;  // points to first ListNode
ListNode* currentPtr; // points to current ListNode.

Since ListNode itself contains a pointer to a MiniStack, headPtr and currentPtr are effectively pointing to the first and curent MiniStacks, though indirectly. headPtr only needs to be set once. It won't change unless you delete all the data in all the stacks. Its purpose is to be able to quickly return you to the front of the list without having to iterate through from your current position. You want currentPtr to point to the ListNode that points to the current MiniStack.

If you want to be able to traverse both backwards and forwards, you may want to consider adding this:

ListNode* tailPtr;  // points to last ListNode in the List

to BigStack, and add this to ListNode:

ListNode* previousPtr; // points to previous ListNode in the list.
void BigStack::Push(char ch)    // Adds element to top of stack assuming stack not full
{
    MiniStack* stackPtr;
    ListNode* nextPtr;
    
    if (num == 0 || headPtr->stackPtr->IsFull())
	{
		ListNode* tempPtr = new ListNode;
		tempPtr->nextPtr = headPtr;
		headPtr = tempPtr;	

		tempPtr->stackPtr = new MiniStack;
		tempPtr->stackPtr->Push(ch);
 	}
 	
	else
		headPtr->stackPtr->Push(ch);

	num++;
}

You aren't treating headPtr as a pointer to the first MiniStack/ListNode. You need at least these two data members in BigStack:

ListNode* headPtr;  // points to first ListNode
ListNode* currentPtr; // points to current ListNode.

Since ListNode itself contains a pointer to a MiniStack, headPtr and currentPtr are effectively pointing to the first and curent MiniStacks, though indirectly. headPtr only needs to be set once. It won't change unless you delete all the data in all the stacks. Its purpose is to be able to quickly return you to the front of the list without having to iterate through from your current position. You want currentPtr to point to the ListNode that points to the current MiniStack.

If you want to be able to traverse both backwards and forwards, you may want to consider adding this:

ListNode* tailPtr;  // points to last ListNode in the List

to BigStack, and add this to ListNode:

ListNode* previousPtr; // points to previous ListNode in the list.

Actually this is suppose to be a stack of stacks one is implemented as array pointing to a stack and the other is almost a linked list but the only difference is that lhe list is traversed backwards
Is the method you mentioned still valid?

You've implemented a stack, so it's API is fixed at push and pop.
If you want to reverse it, then you pop everything off one stack (one at a time), and push it onto another.

Actually this is suppose to be a stack of stacks one is implemented as array pointing to a stack and the other is almost a linked list but the only difference is that lhe list is traversed backwards
Is the method you mentioned still valid?

Hmm. Good question. I suppose the answer depends on whether you are allowed/need to traverse through the stack or if you can only push onto the stack and pop off of the stack. If you need to be able to traverse the list without popping elements off the stack, it's valid. You have to define the top of the stack as the head or the tail and you have to decide whether the stack below it is the "next" element" or the "previous" element. I don't know that it matters, but stay consistent. For sake of argument, let's call the top of the big stack the "head", the bottom as the "tail", and the mini-stack below a mini-stack the "next" stack and the mini-stack above a mini-stack the "previous" stack. Define them explicitly in comments since others may define them differently. Just stay consistent with the terms.

So, using those terms, if you need to be able to iterate back and forth without popping, you'd do best to have these in BigStack:

ListNode* currentNode;
ListNode* headNode;
ListNode* tailNode;

and these in ListNode:

MiniStack * stackPtr;
ListNode* prevNode;
ListNode* nextNode;

If you can't traverse without popping, using my terminology, you could get rid of currentNode because it'll always be the same as headNode. You could get rid of prevNode since there would never be a MiniStack above your current MiniStack since it's already been popped. You'd need to keep nextNode regardless. You'd need some way of knowing when there were no more MiniStacks, so you could keep tailNode or you could just have nextNode point to NULL when there are no more MiniStacks, in which case you wouldn't need tailNode.

So what you need to keep all depends on whether you can traverse your stack or not without having to pop elements. I guess I don't know the technical definition of "Stack" well enough to know whether traversal without popping is allowed, so I'll defer to anyone else on that definition.

Edit:

I see Salem just posted. I'm interpreting that post as saying that you can't access anything but the top element without popping, so no traversal without popping.

Is there anybody out there who can help me with this I am stll not getting the desired output

Is there anybody out there who can help me with this I am stll not getting the desired output

This is not a helpful post. It gives no indication at all of what you've done since the last time you've posted code. It gives no indication at all of what the problem is. We have no idea what you've tried and what the updated code is.

You need to look at your BigStack class and in particular, this:

class BigStack             // Description of BigStack class
{
  private:
    int num;               // Total number of values stored in MiniStack

Is that what num in BigStack really represents? If so, consider if you want it in BigStack and shouldn't move it into MiniStack. It actually looks like it's already in MiniStack:

class MiniStack
{
  private:
    int num;               // Number of values stored in MiniStack
    char* stackPtr;        // Pointer to array representing stack

so think about what num in BigStack means and whether you want to keep it. At minimum, you need to define it better since there is more than one MiniStack element. Which MiniStack are you referring to? The current one? If so, say so explicitly in your comments. Go through BigStack and make sure that num is always used as the number of elements in some particular MiniStack. But consider dropping it totally if it isn't needed.

It looks like you just did a big copy and paste from MiniStack to BigStack, including all of the comments, in a lot of your bigstack.h file. If you did, start over.

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.