simple linked list

Linked Lists : Simple Linked List Example

Linked lists are second most commonly used data structure after arrays. In this tutorial we will see how to create a simple linked list.

Simple Linked List Operations

  • insertFirst(): Inserting item at the beginning of the list
  • deleteFirst(): Deleting item at the begining of the list
  • displayList(): Iterating through linkedlist and display items.

Linked Lists Introduction:

Linked lists are among simplest and most common data structures. They are suitable for use in many kinds of general-purpose databases.

It can be used to implement common abstract data types such as lists, stacks, queues etc.,

In linked list each data item is embedded in a link. A link is an object of a class called Link. Each Link object contains reference (next) to the next link in the list.

Because there are many similar links in the list, it makes sense to use a separate class for them and differentiate from linked list itself. A field in the linked list or list itself contains a reference to the first link.

In this tutorial you will see how to implement a Simple Linked List. Other concepts that you might need to know are: Double-Ended Lists, Sorted Lists, Doubly Linked Lists and Lists with Iterators (an approach to access list elements randomly)

Application of Linked Lists:

You can use linked list in many cases where you are using arrays, if you don’t need frequent random access to individual items using an index.

Simple Linked List Example

Note: 

  • In the constructor of Link class we had initialized only the data and not the next field since it’s automatically set to null when it is created.
  • If you want, you can set null explicitly if you want for clarity. The null value means it does not refer to anything and the link is not connected to any other links.
  • Reference manipulations are the heart of linked-list algorithms.
  • deleteFirst() is the reverse of insertFirst() method.

Output

Recommended Posts

References

Leave a Reply

avatar
  Subscribe  
Notify of