You are on page 1of 2

1-List Data Structures

Practical exercises

Note:  You can select and do some options according to your ability only. We would like to note
you that the more questions you do the better for you in doing final practical  and writing exams.

Question 1. Write a Java program to implement a singly linked list with the following operations:
1.   void addFirst(E e) - adds a node with value e at the head of  a list.
2.   void addLast(E e) - adds a node with value e  at the tail of  a list.
3. void add(E e) - adds a node with value e  at the tail of  a list.
4.   String toString() - traverses from head to tail and returns a string that contains info of all
nodes in the list.
5.   E removeFirst() - deletes the head and return its info, throws NoSuchElementException
if this list is empty.

6.   E removeLast() - deletes the tail and return its info, throws NoSuchElementException
if this list is empty.

7. E getFirst() - returns the info of the head of a list, throws NoSuchElementException if


this list is empty.

8. E getLast() - returns the info of the tail of a list, throws NoSuchElementException if


this list is empty.

9. E get(int index) - return the info of the node at specified position index in the list, throws
IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()).

10. E set(int index, E e) - replaces the info of the node at the specified position index in this list
with the specified element e, throws IndexOutOfBoundsException if the index is out of
range (index < 0 || index >= size()).

11. void add(int index, E e) - adds a node with value e after the node at the specified position
index in this list, throws IndexOutOfBoundsException if the index is out of range (index
< 0 || index >= size()).
12.   E remove(int index) - deletes the node after the node at the specified position index in this
list and return its info.
13.   void remove(E e) - deleles the first node whose info is equal to the element e.
14.   boolean contains(E x) – returns true if the list contains the first node having info e.
15. int size() – returns the number of nodes in the list.
16. Object [] toArray() - create and return array containing info of all nodes in the list.
Question 2: Create an another class named TestMyLinkedList. This class should have a data
field
list that is of type MyLinkedList <Integer>. Implement following methods:

a. Write a method that returns the sum of all elements in the list.

b. Write a method that counts how many odd elements in the list

c. Write a method that returns the average of all prime numbers in the list.

d. Write a method that returns the difference between the largest and smallest elements in the
list

e. Write a method computing largest sum of two consecutive elements in the list.

f. Write a method computing sum of two largest elements in the list.

g. Write a method that reverses the list.

h. Write a method that deletes all negative elements in the list.

i. Write a method deleting all similar elements in the list.

j. Write a method deleting all values that equals to specified element in the list.

k. Write a method that check and return true if the list is sorted, otherwise return false.

l. Suppose the list is sorted as increasing. Write a method inserting a value into the sorted list
so that the new list is sorted as increasing.

m. Merge two ordered singly linked lists of integers into one ordered list.

n. Attach a singly linked list to the end of another singly linked list.

o. Check whether two singly linked list have the same contents.

Question 3: Write a Java program to implement a doubly linked list with the above operations in
question 1.

Question 4: Write a Java program to implement a circular linked list with the above operations in
the question 1.

You might also like