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

Write an efficient java program to implement an integer doubly linked list Dequeue which insertion and...
Write an efficient java program to implement an integer doubly linked list Dequeue which insertion and deletion can be than at either end. You have to write 6 methods: add front, delete front, add rear, delete rear, print forward (left to right) and print backward (right to left). After each addition or deletion dequeue elements are printed forward or backward respectively. Your main method should be as follow: public static void main(String args[]) { xxxxx dq = new xxxxx ();...
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...
In JAVA: Create a circular doubly linked list. It need not be generic. Implement addToStart and...
In JAVA: Create a circular doubly linked list. It need not be generic. Implement addToStart and addToEnd methods, as well as printList method. Implement delete(Node n) method that deletes a node n, if n is in the linked list. Make no assumptions about n. Test your linked list.
Data structure program Implement (your own) the Radix Sort using single linked list java language
Data structure program Implement (your own) the Radix Sort using single linked list java language
Using java: Implement a basic doubly-linked list that implements a priority system sorting the elements that...
Using java: Implement a basic doubly-linked list that implements a priority system sorting the elements that are inserted. Sort based on the speed of the warrior. Driver code: public class LinkedListDriver { public static void main(String[] args) { LinkedList list = new SortedDoublyLinkedList(); System.out.println(list); Warrior krogg = new Warrior("Krogg", 30, 50, 200); list.insert(krogg); System.out.println(list); Warrior gurkh = new Warrior("Gurkh", 40, 45, 180); list.insert(gurkh); System.out.println(list); Warrior brynn = new Warrior("Brynn", 45, 40, 190); list.insert(brynn); System.out.println(list); Warrior dolf = new Warrior("Dolf", 20,...
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...
Linked Lists Problem 1 Implement a program that reads the following ages (type int) from text...
Linked Lists Problem 1 Implement a program that reads the following ages (type int) from text file ages.txt and stores them in a linked list: 16 18 22 24 15 31 27 19 13 Implement the class LinkedList along with any applicable member functions (e.g. void insert(int num)). Perform the following actions on the list (if applicable, use recursion): 1. Display the data in the list 2. Insert 18 at the front of the list 3. Insert 23 at the...
**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...
Java program to implement the merge sort your own and test it to sort a list...
Java program to implement the merge sort your own and test it to sort a list of names based on the frequency.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT