Data Structure [ Linked List ]
What is Linked List?
Linked List is a data structure used for storing collections of data. Linked List has following properties.
- Successive elements are connected by pointers.
- Last element points to NULL.
- Can grow or shrink in size during execution of program.
- It does not waste memory space, but takes some extra space for pointers.
Singly Linked List:
Generally "Linked List" means a Singly Linked List. This list consist of number of nodes in which each node has a next pointer to the following element. The link of the last node is NULL, which indicates the end of list.
Advantages of Linked List:
- We can add/remove elements dynamically.
Disadvantages of Linked List:
- Access time to individual elements , Array has random access which mean it takes O(1) to access any element in the array. Linked List takes O(n) for access to an element from linked list in worst case.
- Wastes memory in terms of extra reference points.
Time Complexity:
- O(1) for insertion or deletion at beginning.
- O(n) for insertion or deletion at middle or end.
Implementation Using Java:
ListNode.java
List Node class will represent the structure of node to hold the data and pointers.
LinkedList.java
Linked List class is responsible for creating the Linked List and to perform the operation on it.
LinkedListClient.java
Linked List Client class will help to test the Linked List Program.
Output
Output of Linked List Java Program.
No comments:
Post a Comment