- Build a linked list of integers from 1 to [email protected] CS1027 public class RemoveLinkedList private static LinearNode<Integer> head - Build a linked list of integers...

This preview shows page 1 - 2 out of 2 pages.

/** * Build a linked list of integers from 1 to 10 * * @author CS1027 */ public class RemoveLinkedList { private static LinearNode<Integer> head; public static void main(String[] args) { // create a linked list that holds 1, 2, ...,5 // by starting at 5 and adding each node at head of list head = null; // create empty linked list LinearNode<Integer> intNode; for (int i = 5; i >= 1; i--) { // create a new node for i intNode = new LinearNode<Integer>(new Integer(i)); // add it at the head of the linked list intNode.setNext(head); head = intNode; System.out.println("head is "+ head.getElement()); } printElements(); // remove the node storing the value 5 from the list remove(3); printElements(); } // This method removes from the linked list pointed by head the node // storing the value specified by the second parameter private static void remove(int value) {
Image of page 1

Subscribe to view the full document.

Image of page 2

Unformatted text preview: LinearNode&lt;Integer&gt; current = head, previous = null; System.out.println(&quot;Removing the element &quot; + value); // Find the node storing value System.out.println(&quot;current is &quot;+ current.getElement()); while (current != null) { if (current.getElement().intValue() == value) { // node storing value was found previous.setNext(current.getNext()); current.setNext(null); // Node deleted break; // Exit loop } else { previous = current; current = current.getNext(); } } } // Prints the values stored in the list pointed by head private static void printElements() { System.out.print(&quot;List of elements: &quot;); LinearNode&lt;Integer&gt; current = head; while (current != null) { System.out.print(current.getElement()); current = current.getNext(); if (current != null) System.out.print(&quot;, &quot;); } System.out.println(&quot;&quot;); } }...
View Full Document

  • Fall '09

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes