Unit 3
Unit 3
Linked List
Concept of List
⚫ List:
⚫ A list is an ordered data structure with elements
separated by a comma and enclosed within
square brackets.
⚫ Linked List:
⚫ Linked List is a linear data structure.
⚫ All elements are stored at contiguous memory
location
⚫ Link list data structure is dynamic in nature
⚫ Linked List can be defined as collection of objects
called nodes that are randomly stored in the
memory.
⚫ Memory will be allocated at run time i.e. while
running program
Linked List
⚫ Linked List is a linear data structure.
⚫ All elements are stored at contiguous
memory location
⚫ Link list data structure is dynamic in
nature
⚫ Memory will be allocated at run time
i.e.while running program
Linked List
⚫ Linked List can be defined as collection of objects
called nodes that are randomly stored in the
memory.
⚫ A node contains two fields i.e. data stored at that
particular address and the pointer which contains
the address of the next node in the memory.
⚫ The last node of the list contains pointer to the
null.
Why Linked List?
⚫ Arrays can be used to store linear data of
similar types, but arrays have the following
limitations:
⚫ 1. The size of the arrays is fixed.
⚫ 2.Insertion of a new element / Deletion of a
existing element in an array of elements is
expensive: The room has to be created for the
new elements and to create room existing
elements have to be shifted ,
but in Linked list if we have the head node
then we can traverse to any node through it
and insert new node at the required position.
Difference in Array and Link List
Advantages of Linked Lists over arrays:
⚫ Dynamic Array.
⚫ Ease of Insertion/Deletion.
⚫ Types of Linked Lists:
⚫ Simple Linked List
⚫ Doubly Linked List
⚫ Circular Linked List
C malloc()
⚫ A node is represented as
A three-member doubly linked list can be created as
Insertion
A node can be added in four ways
1) At the front of the DLL
2) After a given node.
Operations on doubly linked list
Doubly Linked List
⚫ A doubly linked list is a type of linked list in
which each node consists of 3 components:
⚫ *prev - address of the previous node
⚫ data - data item
⚫ *next - address of next node
Single node in doubly link list is represented as
⚫ struct node
⚫ {
⚫ int data;
⚫ struct node *next;
⚫ struct node *prev;
⚫ }
⚫ Each struct node has a data item, a pointer to
the previous struct node, and a pointer to the
next struct node.
Representation of Doubly Linked List
⚫ Finally, free the memory of del_node. And, the linked will look like
this
Code for Deletion of the First Node(doubly link list)
⚫ Code for Deletion of the First Node
Deletion of the Inner Node
⚫ 2. Deletion of the Inner Node
⚫ If del_node is an inner node (second node), we
must have to reset the value
of next and prev of the nodes before and after
the del_node.
⚫ For the node before the del_node (i.e. first
node)
⚫ Assign the value of next of del_node to
the next of the first node.
⚫ For the node after the del_node (i.e. third
node)
⚫ Assign the value of prev of del_node to
the prev of the third node.
Deletion of the Inner Node
⚫ Finally, we will free the memory of del_node.
And, the final doubly linked list looks like this.
.
Circular linked list
⚫ The circular linked list is a linked list where all
nodes are connected to form a circle.
⚫ In a circular linked list, the first node and the last
node are connected to each other which forms a
circle. There is no NULL at the end.
Circular Linked List
⚫ Circular singly linked list:
⚫ In a circular Singly linked list, the last node of
the list contains a pointer to the first node of
the list.
⚫ We traverse the circular singly linked list until
we reach the same node where we started.
⚫ The circular singly linked list has no
beginning or end. No null value is present in
the next part of any of the nodes.
Circular Linked List
⚫ Circular Doubly linked list: Circular Doubly
Linked List has properties of both doubly
linked list and circular linked list
⚫ In this two consecutive elements are linked or
connected by the previous and next pointer,
and the last node points to the first node by
the next pointer and also the first node points
to the last node by the previous pointer.
⚫ i.e. last node of the list contains the address of
the first node of the list. The first node of the
list also contain address of the last node in its
previous pointer.
Operations on circular doubly linked list
.
ADT
.
Three ADTs are List ADT, Stack ADT, Queue ADT
⚫ List ADT
⚫ The data is generally stored in key sequence in a list
which has a head structure consisting
of count, pointers and address of compare
function needed to compare the data in the list.
⚫ The data node contains the pointer to a data structure
and a self-referential pointer which points to the next
node in the list.
⚫ The List ADT Functions is given below:
⚫ get() – Return an element from the list at any given
position.
⚫ insert() – Insert an element at any position of the list.
⚫ remove() – Remove the first occurrence of any
element from a non-empty list.
⚫ removeAt() – Remove the element at a specified
location from a non-empty list.
⚫ replace() – Replace an element at any position by
another element.
Generalized Linked List
⚫ A Generalized Linked List L, is defined as a
finite sequence of n>=0 elements, l1, l2, l3, l4,
…, ln, such that li are either atom or the list
of atoms. Thus
L = (l1, l2, l3, l4, …, ln)
where n is total number of nodes in the list.