Linked List
Linked List
LIST
⚫ Arrays store elements in consecutive memory
locations
⚫ A block of memory is required for this and
it should be allocated in advance.
⚫ Static data structure
Linked
List
⚫ Linked list is a dynamic data structure.
⚫ Amount of memory required can be varied
during use.
⚫ Adjacency between elements is maintained
using links or pointers –containing the address
of subsequent elements
⚫ An element in a linked list is termed as node.
⚫ A node consists of two fields:
◦DATA
◦LINK
⚫ Depending on how the pointers are maintained
linked lists can be classified into three:
◦ Single linked list
item1.data=‘a’;
item2.data=‘b’;
item3.data=‘c’;
item1.link=item
2.link=item3.lin
k=NULL:
⚫ Structures item1,item2 and item3 each contain
⚫ We can chain these structures together by
replacing the null link field in item2 with a
pointer that points to item3 and null link field in
item1 with a pointer that points to item2
⚫ item1.link=&item2
⚫ item2.link=&item3
SINGLY LINKED
LIST
Singly Linked
List
⚫ Each node contains only one link, which points
to the subsequent node in list.
⚫ Header is an empty node having DATA set to
NULL and LINK pointing to first node of
list.
⚫ LINK field of all nodes except last one contains
the address of next node.
⚫ LINK field of last node is set to NULL.
⚫ In a singly linked list we can move from left to
right only.
⚫ Also known as one way list.
⚫ If we know the address of header node, we can
move through the list till the last node.
Operations
⚫ Following are the operations possible in a singly
linked list:
◦ 1.Traversing the list
◦ 2.Inserting a node into the list
◦ 3.Deleting a node from the list
◦ 4.Searching for an element in the list
◦ 5.Copying the list
◦ 6.Merging two lists
Traversing
⚫ Visit every node in the list starting from the first
node to last node.
⚫ Any operation can be done at each node during
the visit.
⚫ We denote this operation as Process() in the
algorithm.
⚫ Let X be the pointer to a node
⚫ The value in DATA field is denoted as
X🡪DATA and that in LINK field as
X🡪LINK
⚫ NULL implies a nil value in DATA and
LINK fields
INSERTION
⚫ There are three different cases in inserting a new
node to a linked list