Skip to content

Week 8: Java – ListIterator & LinkedList


Q1. MarksUpdater – Using ListIterator with ArrayList

Objective: Maintain a list of student marks and manipulate it using a ListIterator.

import java.util.*;

class MarksUpdater {
    private List<String> studentMarks = new ArrayList<>();
    private ListIterator<String> iterator;
    private boolean canRemoveOrSet = false;

    public void initializeIterator() {
        iterator = studentMarks.listIterator();
        canRemoveOrSet = false;
    }

    public void nextEntry() {
        if (iterator.hasNext()) {
            System.out.println("Cursor at: " + iterator.next());
            canRemoveOrSet = true;
        } else System.out.println("Already at the end of the list.");
    }

    public void previousEntry() {
        if (iterator.hasPrevious()) {
            System.out.println("Cursor at: " + iterator.previous());
            canRemoveOrSet = true;
        } else System.out.println("Already at the beginning of the list.");
    }

    public void setEntry(String entry) {
        if (canRemoveOrSet) {
            iterator.set(entry);
            System.out.println("Updated current to: " + entry);
        } else System.out.println("Cannot set. Move next or previous first.");
    }

    public void addEntry(String entry) {
        iterator.add(entry);
        System.out.println("Added: " + entry);
        canRemoveOrSet = false;
    }

    public void removeEntry() {
        if (canRemoveOrSet) {
            iterator.remove();
            System.out.println("Removed last returned entry.");
            canRemoveOrSet = false;
        } else System.out.println("Cannot remove. Move next or previous first.");
    }

    public void displayList() {
        System.out.print("List: ");
        for (String entry : studentMarks) System.out.print(entry + " → ");
        System.out.println("null");
    }
}

Key Points:

  • ListIterator allows bi-directional traversal (next(), previous()).
  • set() and remove() only work after a next() or previous().
  • Use add() to insert elements at the current cursor position.
  • Perfect for managing lists where elements are frequently updated.

Q2. TaskManager – Using LinkedList

Objective: Maintain a task list with add, remove, update, and display operations.

import java.util.*;

class TaskManager {
    private LinkedList<String> tasks = new LinkedList<>();

    public void addTask(String task) {
        tasks.add(task);
        System.out.println("Task added: " + task);
    }

    public void removeTask(String task) {
        if (tasks.remove(task)) System.out.println("Task removed: " + task);
        else System.out.println("Task not found: " + task);
    }

    public void updateTask(String oldTask, String newTask) {
        int index = tasks.indexOf(oldTask);
        if (index != -1) {
            tasks.set(index, newTask);
            System.out.println("Task updated: " + oldTask + " → " + newTask);
        } else System.out.println("Task not found: " + oldTask);
    }

    public void displayTasks() {
        System.out.print("Tasks: ");
        for (String task : tasks) System.out.print(task + " → ");
        System.out.println("null");
    }
}

Key Points:

  • LinkedList is ideal for frequent insertions and deletions.
  • indexOf() helps find elements for updating.
  • Traversal and display can be done with a for-each loop.
  • LinkedList maintains order and allows efficient head/tail operations.

Week 8 Concepts Summary

  1. ListIterator

  2. Traversal: next(), previous()

  3. Modification: add(), set(), remove()
  4. Track current position with boolean flags like canRemoveOrSet.
  5. LinkedList

  6. Dynamic data structure for tasks or items.

  7. Supports add(), remove(), set(), and indexOf().
  8. Dynamic List Management

  9. Both ArrayList and LinkedList support dynamic size.

  10. ArrayList is better for random access, LinkedList for frequent insertion/deletion.
  11. User-driven menus with Scanner and loops allow interactive manipulation.

Comments