DesertFox

Mar 17, 2007, 02:59 AM

Hello,

I have 2 items in each node of my linked list. My goal is to perform polynomial addition and multiplication (assignment from my college prof). The first item in the node is the coefficient and the second is the exponent. Right now im only able to search and delete according to the first item in the list...

How do I check the second items in the node as well in the list as well?

And, also, does anyone know how addition and multiplication is suppose to work with linked lists?

Any help is much appreciated.

Here is the source code:

class LinkListApp

{

public static void main (String [] args )

{

LinkList firstEq = new LinkList(); // create first equation

LinkList secondEq = new LinkList(); // create second equation

// fill equations with coefficient and exponent

firstEq.insertFirst(4, 1);

firstEq.insertFirst(3, 1);

firstEq.insertFirst(2, 2);

firstEq.insertFirst(4, 2);

firstEq.insertLast(3, 3);

secondEq.insertFirst(2, 1);

secondEq.insertFirst(1, 1);

secondEq.insertFirst(4, 2);

secondEq.insertFirst(3, 2);

firstEq.displayList();

secondEq.displayList();

Link x = firstEq.find(4);

if( x != null)

System.out.println("Found link with key " + x.iData);

else

System.out.println("Can't find link");

Link y = firstEq.delete(3);

if( y != null )

System.out.println("Deleted link with key " + y.iData);

else

System.out.println("Can't delete link");

firstEq.displayList();

/*

while (!theList.isEmpty() ) // delete until list is empty

{

Link aLink = theList.deleteFirst();

System.out.print("Deleted ");

aLink.displayLink();

System.out.println("");

}

theList.displayList();

*/

}

}

NOW THE LINKLIST FILE:

class LinkList

{

private Link first; // ref to first link on list

private Link last; // ref to alst link on list

public LinkList()

{

first = null;

last = null;

}

public boolean isEmpty() // true of list is empty

{

return (first==null);

}

// insert at start of list

public void insertFirst( int id, int dd)

{

Link newLink = new Link(id, dd); // makes new link

if (isEmpty() )

last = newLink;

newLink.next = first; // newLink --> old first

first = newLink; // first --> newLink

}

public void insertLast(int id, int dd) // insert at end of list

{

Link newLink = new Link(id, dd); // make new link

if( isEmpty() ) // if empty list,

first = newLink; // first --> newLink

else

last.next = newLink; // old last --> newLink

last = newLink; // newLink <-- last

}

// find link with given key

public Link find (int key)

{

Link current = first; // start at first

while(current.iData != key ) // while no match

{

if (current.next == null) // if end of list

return null; // didnt find it

else

current = current.next; // go to next link

}

return current; // found it

}

public Link delete(int key)

{

Link current = first;

Link previous = first;

while(current.iData != key )

{

if ( current.next == null )

return null;

else

{

previous = current;

current = current.next;

}

}

if(current == first)

first = first.next;

else

previous.next = current.next;

return current;

}

// delete first item (assumes list is not empty)

public Link deleteFirst()

{

Link temp = first; // save reference to link

first = first.next; // delete it: first --> old

return temp; // return deleted link

}

public void displayList()

{

System.out.print("Equation { coefficient, exponent }: ");

Link current = first; // start at beginning of list

while (current != null )

{

current.displayLink(); // print data

current = current.next;

}

System.out.println();

}

}

AND THE LIST FILE:

class Link

{

public int iData;

public int dData;

public Link next;

public Link (int id, int dd)

{

iData = id;

dData = dd;

}

public void displayLink()

{

System.out.print("{ " + iData + ", " + dData + " } ");

}

}

I have 2 items in each node of my linked list. My goal is to perform polynomial addition and multiplication (assignment from my college prof). The first item in the node is the coefficient and the second is the exponent. Right now im only able to search and delete according to the first item in the list...

How do I check the second items in the node as well in the list as well?

And, also, does anyone know how addition and multiplication is suppose to work with linked lists?

Any help is much appreciated.

Here is the source code:

class LinkListApp

{

public static void main (String [] args )

{

LinkList firstEq = new LinkList(); // create first equation

LinkList secondEq = new LinkList(); // create second equation

// fill equations with coefficient and exponent

firstEq.insertFirst(4, 1);

firstEq.insertFirst(3, 1);

firstEq.insertFirst(2, 2);

firstEq.insertFirst(4, 2);

firstEq.insertLast(3, 3);

secondEq.insertFirst(2, 1);

secondEq.insertFirst(1, 1);

secondEq.insertFirst(4, 2);

secondEq.insertFirst(3, 2);

firstEq.displayList();

secondEq.displayList();

Link x = firstEq.find(4);

if( x != null)

System.out.println("Found link with key " + x.iData);

else

System.out.println("Can't find link");

Link y = firstEq.delete(3);

if( y != null )

System.out.println("Deleted link with key " + y.iData);

else

System.out.println("Can't delete link");

firstEq.displayList();

/*

while (!theList.isEmpty() ) // delete until list is empty

{

Link aLink = theList.deleteFirst();

System.out.print("Deleted ");

aLink.displayLink();

System.out.println("");

}

theList.displayList();

*/

}

}

NOW THE LINKLIST FILE:

class LinkList

{

private Link first; // ref to first link on list

private Link last; // ref to alst link on list

public LinkList()

{

first = null;

last = null;

}

public boolean isEmpty() // true of list is empty

{

return (first==null);

}

// insert at start of list

public void insertFirst( int id, int dd)

{

Link newLink = new Link(id, dd); // makes new link

if (isEmpty() )

last = newLink;

newLink.next = first; // newLink --> old first

first = newLink; // first --> newLink

}

public void insertLast(int id, int dd) // insert at end of list

{

Link newLink = new Link(id, dd); // make new link

if( isEmpty() ) // if empty list,

first = newLink; // first --> newLink

else

last.next = newLink; // old last --> newLink

last = newLink; // newLink <-- last

}

// find link with given key

public Link find (int key)

{

Link current = first; // start at first

while(current.iData != key ) // while no match

{

if (current.next == null) // if end of list

return null; // didnt find it

else

current = current.next; // go to next link

}

return current; // found it

}

public Link delete(int key)

{

Link current = first;

Link previous = first;

while(current.iData != key )

{

if ( current.next == null )

return null;

else

{

previous = current;

current = current.next;

}

}

if(current == first)

first = first.next;

else

previous.next = current.next;

return current;

}

// delete first item (assumes list is not empty)

public Link deleteFirst()

{

Link temp = first; // save reference to link

first = first.next; // delete it: first --> old

return temp; // return deleted link

}

public void displayList()

{

System.out.print("Equation { coefficient, exponent }: ");

Link current = first; // start at beginning of list

while (current != null )

{

current.displayLink(); // print data

current = current.next;

}

System.out.println();

}

}

AND THE LIST FILE:

class Link

{

public int iData;

public int dData;

public Link next;

public Link (int id, int dd)

{

iData = id;

dData = dd;

}

public void displayLink()

{

System.out.print("{ " + iData + ", " + dData + " } ");

}

}