# Data Structure design to perform required operations

• Difficulty Level : Easy
• Last Updated : 03 Apr, 2018

Design a data structure which can do following operations

2. getMinimum() in O(1)
3. deleteMinimum() in O(1)

Source : MakeMyTrip Interview.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

1. Maintain a linkedlist with elements in increasing order.
2. Move head to next position in case of delete Min operation.
3. Return First element in case of get Min Operation.
 `// Java code for linked list to``// perform required operations``import` `java.util.*;`` ` `// Node class``class` `Node``{``    ``int` `data;``    ``Node next;`` ` `    ``Node(``int` `d)``    ``{``        ``data = d;``        ``next = ``null``;``    ``}``}`` ` `// main class``class` `MinDS``{``    ``Node start;`` ` `    ``public` `MinDS()``    ``{``        ``start = ``null``;``    ``}`` ` `    ``// Function to add element``    ``void` `addElement(``int` `d)``    ``{``        ``Node tmp = ``new` `Node(d);`` ` `        ``// If linked list is empty``        ``if` `(start == ``null``)``        ``{``            ``start = tmp;``            ``return``;``        ``}`` ` `        ``// If head itself is greater``        ``if` `(d < start.data)``        ``{``            ``tmp.next = start;``            ``start = tmp;``            ``return``;``        ``}``         ` `        ``// If need to insert somewhere in middle``        ``Node prev = start;``        ``Node ptr = start.next;``        ``while` `(ptr != ``null``)``        ``{``            ``if` `(d < ptr.data)``            ``{``                ``tmp.next = ptr;``                ``prev.next = tmp;``                ``return``;``            ``}``            ``else``            ``{``                ``prev = ptr;``                ``ptr = ptr.next;``            ``}``        ``}``        ``prev.next = tmp;``    ``}`` ` `    ``// Function to get minimum``    ``int` `getMin()``    ``{``        ``return` `start.data;``    ``}`` ` `    ``// Function to delete minimum``    ``int` `delMin()``    ``{``        ``int` `min = start.data;``        ``start = start.next;``        ``return` `min;``    ``}`` ` `    ``// Function to print elements``    ``void` `print()``    ``{``        ``Node ptr = start;``        ``System.out.print(``"Elements: "``);``        ``while` `(ptr != ``null``)``        ``{``            ``System.out.print(ptr.data + ``", "``);``            ``ptr = ptr.next;``        ``}``        ``System.out.println(``"\n"``);``    ``}`` ` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``MinDS x = ``new` `MinDS();``        ``x.addElement(``10``);``        ``x.addElement(``20``);``        ``x.addElement(``5``);``        ``x.addElement(``15``);``        ``x.print();`` ` `        ``System.out.println(``"Get Min: "` `+ x.getMin());``        ``System.out.println(``"Del Min: "` `+ x.delMin());``        ``x.print();`` ` `        ``System.out.println(``"Min: "` `+ x.getMin());``    ``}``}`
Output:
```Elements: 5, 10, 15, 20,

Get Min: 5
Del Min: 5
Elements: 10, 15, 20,

Min: 10
```

My Personal Notes arrow_drop_up