| | |
stack of linked lists
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2004
Posts: 1
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Linked lists (C++)
- Stacks using doubly linked lists (C++)
- Concordance, Linked Lists and classes (C++)
- help with linked lists (C++)
- Singly-Linked Lists: Ultimate (C++)
- Bug when creating linked lists in Dev C++ (C++)
- Linked Lists (C)
- C/ Need Help with Linked Lists please (C)
Other Threads in the C++ Forum
- Previous Thread: infinite loop
- Next Thread: how to use loop in this question
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





