954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

stack of linked lists

Hi,
I am completely lost on a program assignment. We are to use the stl stack adapter class. The elements of the stack are linked lists which represent polynomials. So in essense it is a stack of linked lists/polynomials. Note I'm NOT trying to make the stack itself a linked list, rather I'm trying to make the stack hold linked lists. I can get the code to work in int main(){} if I do:

#include
#include "poly.h"
#include

using namespace std;

int main()
{

stack st; // the stack of polynomial pointers

poly p1, p3; // create polynomial objects from the poly class

p1.getPoly(); // get the terms and create the 1st polynomial
cout << endl; // go to next line
cout << "The polynomial is: " << p1 << "\n";
p3 = p1.derivative(); // take the derivative
cout << "The derivative is: " << p3 << endl;

poly* p2;
p2 = &p1;
st.push(p2); // push the polynomial onto the stack
p2 = &p3;
st.push(p2);
p2 = st.top();
cout << *p2;
st.pop();
p2 = st.top();
cout << *p2;
st.pop();
cin.get(); // pause screen so can read the output
}

The code does not work for some reason when I declare poly* p2 before calling the derivative function. I don't know why.

My problem lies in that I need to read a command sequence in from the screen and perform the commands. Simple Example: rdw, means ask for a polynomial, push onto the stack, take the derivative, pop the polynomial read in and push the derivative on, and then finally pop the derivative off the stack and write it to the screen. I tried making a function that processes the commands, i.e if it is 'r' then call a read function to create the polynomial and push it onto the stack, if it is 'w' call a write function that pops the stack and writes the polynomial. But garbage comes out when I call the write function. I attached all the files I'm using. Only the read and write functions are implemented so far for the interface. If anyone can help or tell me of someone who can, I would very much appreciate it. Thanks for reading,

Patrick

Attachments poly.cpp (11.79KB) poly.h (2.41KB) polyStack.cpp (0.72KB) polyStack.h (0.63KB)
pobrien11
Newbie Poster
1 post since Sep 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You