Linked list (polynomials) need help from expert since i want actionPerformed but
ID: 3652727 • Letter: L
Question
Linked list (polynomials) need help from expert since i want actionPerformed buttons to do something the basic i want it to do: is add and remove terms from a polynomial how to use method takes a polynomial and real number and multiply them and return new polynomial. how to use method display polynomials in descending order of degrees. 9x16 + 3.4X5 + 1.89X 2 + 0.45 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.LinkedList; import java.util.List; public class ListDemo extends JFrame { JButton addFrontButton = new JButton("Add Front"); JButton addMiddleButton = new JButton("Addd Middle"); JButton addRearButton = new JButton("Add Rear"); JButton removeFrontButton = new JButton("Remove Front"); JButton removeMiddleButton = new JButton("Remove Middle"); JButton removeRearButton = new JButton("Remove Rear"); JButton displayListButton = new JButton("Display List"); LinkedList myList = new LinkedList(); public static void main() { ListDemo demo = new ListDemo(); demo.setSize(400,200); demo.setDefaultCloseOperation(EXIT_ON_CLOSE); demo.setTitle("List Demo"); demo.createGUI(); demo.setVisible(true); } public void createGUI() { Container window = getContentPane(); window.setLayout(new FlowLayout()); addFrontButton.addActionListener(new AddFrontAction()); window.add(addFrontButton); addMiddleButton.addActionListener(new AddMiddleAction()); window.add(addMiddleButton); addRearButton.addActionListener(new AddRearAction()); add(addRearButton); removeFrontButton.addActionListener(new removeFrontAction()); window.add(removeFrontButton); removeMiddleButton.addActionListener(new removeMiddleAction()); window.add(removeMiddleButton); removeRearButton.addActionListener(new removeRearAction()); window.add(removeRearButton); displayListButton.addActionListener(new DisplayListAction()); window.add(displayListButton); } //make any changes in the object private class AddFrontAction implements ActionListener{ public void actionPerformed(ActionEvent e) { ??? } } private class DisplayListAction implements ActionListener{ public void actionPerformed(ActionEvent e) { ????? } } }
Explanation / Answer
header file: #ifndef LINKED LIST_H #define LINKED LIST_H #include #include struct node { int data; node * next; }; class LinkedList { public: LinkedList(); void add(int A); bool empty(); // return true if the list is empty, otherwise return false void InsertInOrder(ElementType x);//insert a value x in numerical order in the list void Delete(int x); //if value x is in the list, remove x void Display();// Display the data values in the list private: node * first;//pointer to the first node in the list }; #endif .cpp file #include "Linked List.h" #include #include using namespace std; LinkedList::LinkedList() { first=NULL; } void LinkedList::add(int A) { if (first==NULL) { first=new node; first->data=A; first->next=NULL; } else { node *curr=first; node *prev=NULL; while (curr!=NULL) { prev=curr; curr=curr->next; } curr=new node; curr->data=A; curr->next=NULL; prev->next=curr; } } bool LinkedList::empty() { if (first==NULL) return true; else return false; } void LinkedList::Delete(int A) { node *curr=first; node *prev=NULL; bool flag=true; if (first->data==A) { first=first->next; delete first; flag=false; } else { while (flag && curr!=NULL) { if (curr->data==A) { prev->next=curr->next; delete curr; flag=false; } prev=curr; curr=curr->next; } if (flag==true) coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.