Question

In: Computer Science

In this Java program you will implement your own doubly linked lists. Implement the following operations...

In this Java program you will implement your own doubly linked lists.
Implement the following operations that Java7 LinkedLists have.

1. public DList()
This creates the empty list

2. public void addFirst(String element)
adds the element to the front of the list

3. public void addLast(String element)
adds the element to the end of the list

4. public String getFirst()

5. public String getLast()

6. public String removeLast()
removes & returns the last element of the list.

7. public String removeFirst()
removes & returns the front element of the list.

8. public String get(int index)
Returns the value at “position” index. IndexOutOfBoundsException should be
thrown if the index is non-existent.

9. public String set(int index, String value)
changes the value at “position” index and returns the old value. IndexOutOfBoundsException should be thrown if the index is non-existent.

10. public int indexOf(Object obj)
Returns the index of obj if it is in the list and -1 otherwise.

11. public int size()

12.public boolean contains(Object obj)
Returns true if obj appears in the list and false otherwise.

13. public Iterator iterator()
Returns an iterator over the list. This should be a non static inner class.

Solutions

Expert Solution

Hi,

Doubly linked list implementation code is given below:

public class DoublyLinkedListImpl {

private Node head ;

private Node tail ;

private int size ;

public DoublyLinkedListImpl () {

size = 0;

}

private class node{

E element;

Node next;

Node prev;

public Node(E element,Node next,Node prev){

this.element=element;

this.next=next;

this.prev=prev;

}

}

/** creates an empty list

public Dlist () {

size=0;

head = new DNode ( null,null,null); //create head

tail = new DNode ( null,head,null) // create tail

// make head and tail point to each other

head.setnext(tail);

}

public void addFirst (E element) {

Node tmp = new Node (element , head ,null);

if (head ! = null)

{

head.prev = tmp;

}

head = tmp;

if(tail == null)

{

tail = tmp;

}

size++;

System.out.println("adding:" +element);

}

public void addLast(E element) {

Node tmp = new Node(element,null,tail);

if(tail != null)

{

tail.next=tmp;

}

tail=tmp;

if(head == null)

{

head=tmp;

}

size++;

System.out.prrintln("adding:" +element);

}

public String getFirst(E element) {

final node<E> f= first;

if(f=null)

throw new noSuchElementException();

return f.item;

}

public String getLast(E element) {

final Node<E> l=last;

if(l==null)

throw new noSuchElementException();

return l.item;

}

public String removeFirst() {

if(size == 0) throw new NoSuchElementException();

Nodetmp = head;

head = head.next;

head.prev=null;

size --;

System.out.println("deleted:"+tmp.element);

return tmp.element;

}

public String removeLast() {

if(size == 0) throw new NoSuchElementException();

Node tmp = tail;

tail=tail.prev;

tail.next=null;

size--;

System.out.println("deleted:" +tmp.element);

return tmp.element;

}

public String get(int index) {
int i=0; cn;

if(index == 0) ;throw new IndexOutOfBoundsException();

for(Node *cn = head; cn != Null; cn-> nextnode) {

if(i == index){

return (cn -> value);

}

else {

return -1;

}

}

public String set(int index,E element){

checkElementIndex(index); throw new IndexOutOfBoundsException();

Node <E> x = node(index);

E oldVal = x.item;

x.item = element;

return oldVal;

}

public int indexof(Object o) {

int index = 0;

if(o== null){

for(Node<E> x = first; x!= null; x=x.next){

if(x.item == null)

return index;

index ++;

}

}else {

for (Node<E> x=first;x!= null;x=x.next) {

if(o.equals(x.item))

return index;

index++;

}

}

return -1;

}

public int size () {

return size;

}

public boolean contains (object o){

return indexOf(o)!=-1;

}

public iterator <E> iterator() {

return new iterator();

}

Hope you help this ...

Thank you..


Related Solutions

Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks the user to enter n marks for n students, read the marks and the names and store them in a double linked list. 2. Write a method to find the largest mark and print the name of the student having that mark 3. Write a method to print the content of the list (name, mark) 4. Write a method to search the list for...
Write a program that uses linked lists in order to support the following operations: 1. PUSH...
Write a program that uses linked lists in order to support the following operations: 1. PUSH (S, x) - pushes a value x into a stack S 2. POP (S, i) - gets a number i (positive integer) and pops i numbers of S. If S contains less than i values, the operation is impossible to execute (program prints ERROR in this case – see below). 3. REVERSE (S) - reverse the order of the elements in S (you might...
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given....
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given. The operations are: Add an “H” to the list Add an “I” to the list Add “100” to the list Print the content of the list and its size Add a “H” to the first place of the list Add a “R” to the last place of the list Get the element of position 3 and print it Get the last element and print...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
java Modify doubly Linked List code to include following index (rank) based access operations int RetrieveAt(int...
java Modify doubly Linked List code to include following index (rank) based access operations int RetrieveAt(int index) void DeleteAt(int index) void Swap(int index, int index) index starts at 0 (just like array's subscript) If the index is out of bound, RetrieveAt returns 0, DeleteAt does nothing, do nothing in Swap. Write your own testing program to test the modified class -----------------------------------------DLinkedlist.java---------------------------- public class DLinkedList { private class Node { String data; Node next; Node prev; public Node(String s) { data...
Java Write a menu driven program that implements the following linked list operations : INSERT (at...
Java Write a menu driven program that implements the following linked list operations : INSERT (at the beginning) INSERT_ALPHA (in alphabetical order) DELETE (Identify by contents, i.e. "John", not #3) COUNT CLEAR
Develop a C++ "doubly" linked list class of your own that can hold a series of...
Develop a C++ "doubly" linked list class of your own that can hold a series of signed shorts Develop the following functionality: Develop a linked list node struct/class You can use it as a subclass like in the book (Class contained inside a class) You can use it as its own separate class Your choice Maintain a private pointer to a node class pointer (head) Constructor Initialize head pointer to null Destructor Make sure to properly delete every node in...
Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT