Question

This is a Practice problem for a Exam. PLEASE WRITE IN JAVA ONLY. Be able to...

This is a Practice problem for a Exam.

PLEASE WRITE IN JAVA ONLY.

Be able to write the code for inserting a node into a Doubly Linked List.

Please answer the following scenarios

1.At the Start
2.At the End
3.In the Middle
0 0
Add a comment Improve this question Transcribed image text
Answer #1

package HomeworkLib;
// Java Program to Implement Doubly Linked List
import java.util.Scanner;
/* creating Class Node for holding data and links to other nodes(previousious and next)*/
class Node
{
protected int data;
protected Node next, previous;

/* default Constructor */
public Node()
{
next = null;
previous = null;
data = 0;
}
/* parameterized Constructor */
public Node(int d, Node n, Node p)
{
data = d;
next = n;
previous = p;
}
/* Function to set data to node */
public void setData(int d)
{
data = d;
}
/* Function to get data of node */
public int getData()
{
return data;
}
}
/* creating Class linkedList */
class linkedList
{
protected Node start;
protected Node end ;
public int size;

/*default Constructor */
public linkedList()
{
start = null;
end = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to get size of list */
public int getSize()
{
return size;
}
/* Function to insert element at begining */
public void insertAtStart(int val)
{
Node newPointer = new Node(val, null, null);
if(start == null)
{
start = newPointer;
end = start;
}
else
{
start.previous=newPointer;
newPointer.next=start;
start = newPointer;
}
//incrementing size of linked list
size++;
}
/* Function to insert element at end */
public void insertAtEnd(int val)
{
Node newPointer = new Node(val, null, null);
if(start == null)
{
start = newPointer;
end = start;
}
else
{
newPointer.previous=end;
end.next=newPointer;
end = newPointer;
}
//incrementing size of linked list
size++;
}
public void insertAtMiddle(int val) {
   Node newPointer = new Node(val, null, null);
   Node myptr=start;
   System.out.print("size: "+size);
   int cnt=(size%2==0)?(size/2 - 1):(((size+1)/2 )-1);
   for(int i=0;i<cnt;i++) {
       myptr=myptr.next;
       System.out.print("data : "+myptr.data);
   }
   newPointer.previous=myptr;
   newPointer.next=myptr.next;
   myptr.next.previous=newPointer;
   myptr.next=newPointer;
   //incrementing size of linked list
   size++;
}
public void display()
{
System.out.print("Doubly Linked List = ");
if (size == 0)
{
System.out.print("empty");
return;
}
if (start.next== null)
{
System.out.println(start.getData() );
return;
}
Node ptr = start;
System.out.print(start.getData()+ " <-> ");
ptr = start.next;
while (ptr.next!= null)
{
System.out.print(ptr.getData()+ " <-> ");
ptr = ptr.next;
}
System.out.print(ptr.getData()+ "\n");
}
}

/*creating Class DoublyLinkedList which involves driver for whole code*/
public class DoublyLinkedList
{
  
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of linkedList */
linkedList list = new linkedList();
System.out.println("Doubly Linked List Test");
char ch;
/* Perform list operations */
do
{
System.out.println("Doubly Linked List Operations");
System.out.println("1. insert at begining");
System.out.println("2. insert at end");
System.out.println("3. insert at middle");
  
System.out.print("Enter your choice: ");
  
//scanning input as string and then converting it to int or we can simply use int choice=scan.nextInt();
String input=scan.next();
int choice =Integer.parseInt(input);
switch (choice)
{
case 1 :
System.out.print("Enter integer element to insert: ");
list.insertAtStart( scan.nextInt() );   
break;
case 2 :
System.out.print("Enter integer element to insert: ");
list.insertAtEnd( scan.nextInt() );   
break;   
case 3 :
System.out.print("Enter integer element to insert: ");
list.insertAtMiddle( scan.nextInt() );   
break;
  
default :
System.out.println("Wrong Entry \n ");
break;   
}
/* Display List */
list.display();
System.out.print("Do you want to continue (Type y or n) : ");
ch = scan.next().charAt(0);

} while (ch == 'Y'|| ch == 'y');
scan.close();
}
}

Add a comment
Know the answer?
Add Answer to:
This is a Practice problem for a Exam. PLEASE WRITE IN JAVA ONLY. Be able to...
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
  • This is a c programming problem. Would you please help me to write this problem??? I...

    This is a c programming problem. Would you please help me to write this problem??? I really appreciate it if you add comments for explanation step by step. Thank you. Reverse a Doubly linked list using recursion: Given a doubly linked list. Reverse it using recursion. Original Doubly linked list: next pointer - DDHIHI Null prev painter Reversed Doubly linked list: next pointer Start Pointer Null prev pointer Include: a) A struct for a node of the doubly linked list....

  • Problem 3 will have the following class Node. static class Node { public char data; public...

    Problem 3 will have the following class Node. static class Node { public char data; public Node next; public Node previous; }; 3. Answer all the sub-questions but for the following circular doubly-linked list with the external reference P2 given in the below figure, write the statements of each following question. You can only use pointer P2 to access the doubly-linked list, write program statements to do the following, each question is independent based on the original status. 1) Display...

  • JAVA please Java problem that has to be in doubly linked list. It is game problem...

    JAVA please Java problem that has to be in doubly linked list. It is game problem that I have to keep of the players' scores. this should have a Java blueprint class named player   It should have two fields, the name and score and include the constructors, toString() method, and getters and setters. Scores must be kept in ascending order (smallest at the front ) in a doubly linked list. It has menu as well: Load original data Print the...

  • This exam is closed book. Write your name on every page. Write your solutions in the...

    This exam is closed book. Write your name on every page. Write your solutions in the space provided. If you need more space, attach a sheet and staple to your exam. Do not spend too much time on any problem. Show your work, as partial credit will be given. You will be graded not only on he correctness and efficiency of your answers, but also on your clarity. Be neat. public class Node public int icem,: publie Node next: Node...

  • Exercise-2: 15 points Develop a node class and a doubly list class. The node class should have tw...

    Exercise-2: 15 points Develop a node class and a doubly list class. The node class should have two state variables namely data and nextNode. The doubly list class should contain the following methods: Middlelnsert- insert a node somewhere in the middle of the list Startinsert-insert a node at start of the Linked list Endinsert- insert a node at the end of the Linked list Delete-delete a node Traverse-prints all the node's data Reverse-reverses the linked list . . Note: Choose...

  • Would you please help me to do this problem? this is c programming. 1. write figures of inserting...

    Would you please help me to do this problem? this is c programming. 1. write figures of inserting 3 elements into a stack linked list and a queue linked list with code fragments and then delete 2 elements from each with associated code fragments. thank you.

  • b) Write the function as Java code, including comments as you see fit. This problem concerns...

    b) Write the function as Java code, including comments as you see fit. This problem concerns creating a function String secondInOrder (Node start) which takes a linked list of Node elements (shown below), and returns the String in the list which comes second in alphabetic order. Recall that the String class has the method public int compareTo(String anotherString) which returns O if the strings are equal, a negative integer if this is earlier than anotherString and a positive integer otherwise....

  • part (C, F ) only. With the java code (write the sequence of method calls as...

    part (C, F ) only. With the java code (write the sequence of method calls as well) Trees and java code please provide me your email, ill send the code there Show how we visualize the binary search tree with root referenced by rootA (page 490) after each of the following changes. Also list the sequence of Binary- SearchTree method calls, both public and private, that would be made when executing the change. Use the original tree to answer each...

  • Write a java code : Student ID at University is composed of year of admission and...

    Write a java code : Student ID at University is composed of year of admission and students number. we aim to implement a structure that improves operations of inserting and searching for a student. To enhance these operations, we will build a tree of linked lists (TreeOfLists) to combine advantages of both structures. Each node in this tree contains a linked list of all students who were admitted in that year. Each node in the linked list represents a student(id,...

  • PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data...

    PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data fields: name (String), age(int), company(String) •Methods: •Public String getName(); •Public int getAge(); •Public String getCom(); •Public void setName(String n); •Public void setAge(int a); •Public void setCom(String c); •toString(): \\ can be used to output information on this name card 2, write a class DoublyNameCardList.java, which is a doubly linked list and each node of the list a name card. In the DoublyNameCardList.java: •Data field:...

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