Data Structures Assignments (Java)


Linked Lists

  1. Write a Java program to create and display a singly linked list.
    • Expected Output:
    Linked List: 1 -> 2 -> 3 -> null
  2. Write a Java program to insert an element at the beginning of a singly linked list.
    • Input: Insert 5
    • Expected Output:
    Linked List after inserting 5: 5 -> 1 -> 2 -> 3 -> null
  3. Write a Java program to insert an element at the end of a singly linked list.
    • Input: Insert 4
    • Expected Output:
    Linked List after inserting 4: 1 -> 2 -> 3 -> 4 -> null
  4. Write a Java program to delete an element from a singly linked list by key.
    • Input: Delete 2
    • Expected Output:
    Linked List after deleting 2: 1 -> 3 -> 4 -> null
  5. Write a Java program to find the length of a singly linked list.
    • Expected Output:
      Length of Linked List: 4

Stacks

  1. Write a Java program to implement a stack using an array. Include operations for push, pop, and display.
    • Input: Push 5, 10, 15
    • Expected Output:
    Stack: [5, 10, 15]
  2. Write a Java program to implement a stack using linked list. Include operations for push, pop, and display.
    • Input: Push 5, 10, 15
    • Expected Output:
    Stack: 15 -> 10 -> 5
  3. Write a Java program to reverse a string using a stack.
    • Input: Hello
    • Expected Output:
      Reversed String: olleH

Queues

  1. Write a Java program to implement a queue using an array. Include operations for enqueue, dequeue, and display.
    • Input: Enqueue 5, 10, 15
    • Expected Output:
    Queue: [5, 10, 15]
  2. Write a Java program to implement a queue using linked list. Include operations for enqueue, dequeue, and display.
    • Input: Enqueue 5, 10, 15
    • Expected Output:
    Queue: 5 -> 10 -> 15
  3. Write a Java program to reverse the first K elements of a queue.
    • Input: Queue 1, 2, 3, 4, 5 and K 3
    • Expected Output:
      Queue after reversing first 3 elements: 3 -> 2 -> 1 -> 4 -> 5

Trees

  1. Write a Java program to implement a binary search tree (BST). Include operations for insertion, deletion, and traversal (inorder, preorder, and postorder).
    • Input: Insert 5, 3, 7, 2, 4, 6
    • Expected Output: (Inorder traversal)
    BST: 2 -> 3 -> 4 -> 5 -> 6 -> 7
  2. Write a Java program to find the height of a binary tree.
    • Input: BST 5, 3, 7, 2, 4, 6
    • Expected Output:
    Height of BST: 3
  3. Write a Java program to count leaf nodes in a binary tree.
    • Input: BST 5, 3, 7, 2, 4, 6
    • Expected Output:
      Leaf nodes in BST: 3

Mahesh Wabale
Latest posts by Mahesh Wabale (see all)

Leave a Comment