stack of linked lists

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 1
Reputation: pobrien11 is an unknown quantity at this point 
Solved Threads: 0
pobrien11 pobrien11 is offline Offline
Newbie Poster

stack of linked lists

 
0
  #1
Sep 25th, 2004
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 <iostream>
#include "poly.h"
#include <stack>

using namespace std;

int main()
{

stack<poly*> 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
Attached Files
File Type: cpp poly.cpp (11.8 KB, 52 views)
File Type: h poly.h (2.4 KB, 39 views)
File Type: cpp polyStack.cpp (742 Bytes, 26 views)
File Type: h polyStack.h (640 Bytes, 20 views)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC