Introduction:
Linked list is a linear data structure. It is used for dynamic memory allocation, All the elements in linked list are stored in linear manner. The concept of linked list came into use due to some problem in array. The disadvantages of array are
- Fixed size – Limitation of array is that it is of fixed or static size.
- Contiguous and fixed position: When we want to insert an element at a specific position which previously covered by some other element then we got to shift right by one position all the elements to the right of that position. This will vacate the position for you to insert the new element at the desired position.
Information in linked list is stored in the form of nodes. Each node consists of two parts: INFO (DATA) and LINK (Next pointer field). INFO part contains the information and links those points to the next item. The last item does not point to anything. We set its link member to NULL.
Examples of Linked Lists –
A waiting line of customers: John, Mary, Dan, Sue (from the head to the tail of the line) is an example of a linked list.
A stack of numbers (from top to bottom): 10, 8, 6, 8, 2. A linked list of ints can represent this stack.
Advantages of Using Linked Lists:
- Flexibility – insert at (or delete from) any position in constant time
- No single allocation of memory needed – fragmented memory can be put to a better use.
- Dynamic allocation – the size is not required to be known in advance.
Operations on Linked list:
- Traversing a linked list
- Searching a liked list
- Inserting an element
- Deleting an Element
An Algorithm to Create a Singly Linked List-
Algorithm has three parts:
(a) Declaration
(b) Initial Condition
(c) Steps for Algorithm
(a) Declaration
struct node{
int info;
struct node * next;
} *head,*current,*temp
(b) Initial Condition
head=null temp=null Current=null
(c) Steps for Algorithms
Begin
Step 1 Read the element into x
Step 2 Create a new temporary(temp) node in the computer memory
temp =(struct node )sizeof (node)
Step 3 Assign or copy the values in temporary(temp) node as
temp -> info =x
temp ->next=nul
Step 4 check whether head is null or not
if (head=null)
{
head=temp
current=temp
}
else
{
current ->next =temp
current ->current ->next
}
Step 5 follow steps 1 to 4 to insert remaining element in the list.
Frequently Asked Questions (FAQs)
Q.1 What is linked list?
Ans: Linked list is a linear data structure for dynamic memory allocation.
Q.2 Node of linked list has how many parts?
Ans: A node is consist of two parts: INFO and LINK
Q.3 Write two advantages of using Linked Lists.
Ans : Flexibility and Dynamic memory allocation
Q.4 Write Operations for Linked list
Ans: Traversing a linked list, searching a liked list, inserting an element, deleting an Element
Author:
Mr.Rahul Agarwal
Associate Professor,Department Of CS & IT
Biyani Group of College, Jaipur
