Welcome! Log In Create A New Profile

Advanced

Stack Headaches - Please help

Posted by 36187844 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Stack Headaches - Please help
September 27, 2007 03:28PM
Hi

I'm having a big problem with stacks now whilst preparing for the exam. I'm trying to do question 01 on page 466 of Malik.

I'm using the source code provided for chapter 7 the UNISA 2007 CD.

My problem is with the copyStack and copy constructor: below is my code for the copy constructor:

template<class Type> //copy constructor
linkedStackType<Type>::linkedStackType(const linkedStackType<Type>& otherStack)
{
initializeStack();
copyStack(otherStack);

}//end copy constructor

The initializeStack(); statement causes the program to crash. if i changed it to stackTop = NULL; then this constructor runs fine, until i try to use otherStack thats been passed to copyStack. Then the compiler starts b1tchin' about

instantiated from `linkedStackType<Type>::linkedStackType(const linkedStackType<Type>& ) [with Type = int]' and some error about this
here is what i did in the copyStack method:

template<class Type>
void linkedStackType<Type>::copyStack(const linkedStackType<Type>& otherStack)
{
nodeType<Type> *current; //to traverse the stack

linkedStackType<Type> tempStack; //temp list to store reverse read stack values

current = otherStack.top();

while(current !=NULL) //while otherstack has more elements, fill up tempStack
{
tempStack.push(current->info);
current = current->link;
}

current = tempStack.top(); //now going to copy values from tempStack to the
//new stack in the correct order.

while(current !=NULL) //while my tempstack has values, fill up the stack
{ //(not otherstack)
push(current->info);
current = current->link;
}
}

and here is my test program:

#include <iostream>
#include "linkedStack.h"

using namespace std;

int main()
{
linkedStackType<int> list1;

for(int i = 1; i < 50; i++)
list1.push(i);

list1.print();

linkedStackType<int> list2(list1);

list2.print();

return 0;
}

here is the print() incase you are wondering:

template<class Type>
void linkedStackType<Type>::print()
{
nodeType<Type> *current;

current = stackTop;

while(current != NULL)
{
cout<<current->info<<" ";
current = current->link;
}
cout << endl;
}

this works 100%

I also noted that in the copy constructor and the copyStack methods if i type otherStack(dot) that no code completion box comes up which leads me to believe that the otherStack is not being seen somewhere.

Any help would be greatly appreciated.

Thanx confused smiley
Re: Stack Headaches - Please help
September 28, 2007 12:03AM
Nevermind. I figured it out after 6 friggin' hours.

If anybody has the same problem: here's the code for copyStack:

template<class Type>
void linkedStackType<Type>::copyStack(const linkedStackType<Type>& otherStack)
{
nodeType<Type> *current;

linkedStackType<int> tempStack;

current = otherStack.stackTop;

while(current != NULL)
{
assert(current != NULL);

tempStack.push(current->info);

current = current->link;
}

current = tempStack.stackTop;

while(current != NULL)
{
assert(current != NULL);

push(current->info);

current = current->link;
}
}

I hate this language... drinking smiley
Re: Stack Headaches - Please help
September 30, 2007 09:55AM
Are we not supposed to use the private member *stackTop to implement the copyStack function. Can we do it the way you did it. Will we not lose mark for it?
Re: Stack Headaches - Please help
September 30, 2007 12:45PM
I did it this way so that if you traverse the stack with another pointer you don't break it down. I'm just "looking" at the elements and not changing them. The parameter is a CONST so changing the stackTop pointer won't work also if it was the case that i used stackTop, i would have had to re-create the "otherStack" because stacktop = stacktop->link cause you to loose track of the actual top of the stack... and as far as I'm concerned, it works. This is the most efficient way i could do it, don't know of any other way. Minimum effort, maximum result.
Re: Stack Headaches - Please help
October 05, 2007 04:52PM
I changed mine to use a recursive function to get to the bottom of otherStack and then push the info of each node into the current stack object.
Sorry, only registered users may post in this forum.

Click here to login