Question

ASSIGNMENT INFO POD 40: Cut! (LinkedList Element Removal) - Week 12.4 / 1. POD: Cut! 5 points possible Reset to Starter Code

//LinkedList

import java.util.Scanner;

public class PoD

{

  

public static void main( String [] args )

{

Scanner in = new Scanner( System.in );

LinkedList teamList = new LinkedList();

final int TEAM_SIZE = Integer.valueOf(in.nextLine());

for (int i=0; i<TEAM_SIZE; i++)

{

String newTeamMember = in.nextLine();

teamList.append(newTeamMember);

}

while (in.hasNext())

{

String removeMember = in.nextLine();

teamList.remove(removeMember);

}

  

System.out.println("FINAL TEAM:");

System.out.println(teamList);

in.close();

System.out.print("END OF OUTPUT");

}

}

===========================================================================================

//PoD

import java.util.NoSuchElementException;

/**

* A listnked list is a sequence of nodes with efficient element

* insertion and removal. This class contains a subset of the methods

* of the standard java.util.LinkedList class.

*

* You will finish off the contains method.

*/

public class LinkedList

{

//attributes

private Node head;

private Node tail;

//Node

class Node

{

public Object data;

public Node previous;

public Node next;

}

/**

* Constructs an empty linked list/

*/

public LinkedList()

{

head = null;

tail = null;

}

/**

* @method removeHead()

* @param Object : object to be removed

* @returns void

*

* Removes node from the linked list

*/

public void remove(Object data)

{

//PLEASE START WORK HERE

//===============================================

//===============================================

//PLEASE END WORK HERE

}

/**

* Appends a new node to the end of the linked list.

*/

public void append(Object element)

{

if (head == null) //Empty linked list

{

Node firstNode = new Node();

firstNode.data = element;

firstNode.previous = null;

firstNode.next = null;

head = firstNode;

tail = firstNode;

}

else //At least one node already exists.

{

Node newNode = new Node();

newNode.data = element;

newNode.previous = tail;

newNode.next = null;

tail.next = newNode;

tail = newNode;

}

}

public String toString()

{

Node position = head;

String output = "";

while (position != null)

{

output += position.data + "\n";

position = position.next;

}

return output;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

LinkedList.java Code:

  package doublyLinkedListRemove; public class LinkedList {      // attributes   private Node head;      private Node tail;      // Node         class Node      {               public Object data;             public Node previous;           public Node next;       }       /**      *       * Constructs an empty linked list/      *       */     public LinkedList()     {               head = null;            tail = null;    }       /**      *       *       *       * @param Object : object to be removed          *       * @returns void         *       *       *       * Removes node from the linked list     *       */     public void remove(Object data)         {               //start traversing from start node              //till find the data            Node current=head;              while(current!=null && !(current.data.equals(data)))            {                       current=current.next;           }               if(current!=null)//if data found at current node                {                       removeNode(current);//remove that particular node from list             }       }       //Utility method to remove a particular node    private void removeNode(Node node)      {                // Change previous only if node to be deleted // is NOT the first node                 if(node.previous!=null)                 {                       node.previous.next=node.next;           }               //if node is head change the head to its next node              else            {                       head=node.next;                 }               // Change next only if node to be deleted // is NOT the last node               if(node.next!=null)             {                       node.next.previous=node.previous;               }               //if node is tail change the tail to its previous node          else            {                       tail=node.previous;             }       }                       /**      *       * Appends a new node to the end of the linked list.     *       */     public void append(Object element)      {               if (head == null) // Empty linked list          {                       Node firstNode = new Node();                    firstNode.data = element;                       firstNode.previous = null;                      firstNode.next = null;                  head = firstNode;                       tail = firstNode;               }               else // At least one node already exists.               {                       Node newNode = new Node();                      newNode.data = element;                         newNode.previous = tail;                        newNode.next = null;                    tail.next = newNode;                    tail = newNode;                 }       }       public String toString()        {               Node position = head;           String output = "";             while (position != null)                {                       output += position.data + "\n";                         position = position.next;               }               return output;  } } 

ToD.java Code:

 package doublyLinkedListRemove; import java.util.Scanner; public class PoD {        public static void main(String[] args)  {                                               Scanner in = new Scanner( System.in );          LinkedList teamList = new LinkedList();                 final int TEAM_SIZE = Integer.valueOf(in.nextLine());           for (int i=0; i<TEAM_SIZE; i++)              {                       String newTeamMember = in.nextLine();                           teamList.append(newTeamMember);                 }               while (in.hasNext())            {                       String removeMember = in.nextLine();                            teamList.remove(removeMember);          }                                       System.out.println("FINAL TEAM:");                              System.out.println(teamList);                           in.close();                             System.out.print("END OF OUTPUT");      } } 

Sample Input:

6
Baryney Stinson
Ted Mosby
Robin Scherbatsky
Lily Aldrin
Marshall Eriksen
Stella Zinman
Stella Zinman

Sample Output:

FINAL TEAM:
Baryney Stinson
Ted Mosby
Robin Scherbatsky
Lily Aldrin
Marshall Eriksen

END OF OUTPUT

Image Of remove method:

540 public void remove(Object data) //start traversing from start node //till find the data Node current=head; while(current

Input/Output Image:

6 Baryney Stinson Ted Mosby Robin Scherbatsky Lily Aldrin Marshall Eriksen Stella Zinman Stella Zinman FINAL TEAM: Baryney St

Add a comment
Know the answer?
Add Answer to:
//LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args )...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Convert into pseudo-code for below code =============================== class Main {    public static void main(String args[])...

    Convert into pseudo-code for below code =============================== class Main {    public static void main(String args[])    {        Scanner s=new Scanner(System.in);        ScoresCircularDoubleLL score=new ScoresCircularDoubleLL();        while(true)        {            System.out.println("1--->Enter a number\n-1--->exit");            System.out.print("Enter your choice:");            int choice=s.nextInt();            if(choice!=-1)            {                System.out.print("Enter the score:");                int number=s.nextInt();                GameEntry entry=new GameEntry(number);   ...

  • P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList()...

    P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList() { header = null; } public final Node Search(int key) { Node current = header; while (current != null && current.item != key) { current = current.link; } return current; } public final void Append(int newItem) { Node newNode = new Node(newItem); newNode.link = header; header = newNode; } public final Node Remove() { Node x = header; if (header != null) { header...

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • import java.util.LinkedList; public class testprintOut { private static LinkedList[] array; public static void main(String[] args) {...

    import java.util.LinkedList; public class testprintOut { private static LinkedList[] array; public static void main(String[] args) { int nelems = 5; array = new LinkedList[nelems]; for (int i = 0; i < nelems; i++) { array[i] = new LinkedList<String>(); } array[0]=["ab"]; System.out.println(array[0]); } } //I want to create array of linked lists so how do I create them and print them out efficiently? //Syntax error on token "=", Expression expected after this token Also, is this how I can put them...

  • Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element...

    Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...

  • Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test...

    Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test class: Remember your header with name, date, and assignment. Also include class names that will be tested. Psuedocode (level 0 or mixture of level 0 and algorithm [do not number steps]) is required if main() contains more than simple statements (for example, your program includes constructs for decisions (if/else), loops, and methods. For Secondary class(es): Include a JavaDoc comment that describes the purpose of...

  • LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete...

    LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 ___________________________________________________________________________________________________________________________________________________ SortedList.java (READ ONLY!!!) import java.util.Scanner; public class SortedList { public static void main...

  • When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses...

    When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Any suggestions? public class LinkedList<T> {    Node<T> itsFirstNode;    Node<T> itsLastNode;    private int size;    public LinkedList() {        itsFirstNode = null;        itsLastNode = null;          size = 0;    }    public Iterator<T> getIterator() {        return new Iterator(this);    }    // THIS WILL NEED...

  • CSCI-2467 Lab 11 – Refactor LinkedList Application to use Generics Background The code consists of three...

    CSCI-2467 Lab 11 – Refactor LinkedList Application to use Generics Background The code consists of three files that implement and use a simple linked list. The code was written in early Java-style using the Object class in order to allow the linked list to be a list of objects of any type. While the code works, it is not type-safe. Refactor the code to use Java Generics. You will need to change the Main class to create a linked list...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT