Linked List
Linked List
Introduction
• Storing list of data in a memory
• Link contains the address of the next element
in the list.
• Successive elements in the list need not
occupy adjacent space in memory. This type of
data structure is called linked list.
• Operations of List
– IsEmpty: determine whether or not the list is
empty
– InsertNode: insert a new node at a particular
position
– FindNode: find a node with a given value
– DeleteNode: delete a node with a given value
– DisplayList: print all the nodes in the list
Linked List and its representation in
memory
• Linked list: linear collection of data elements
(nodes).
• Data field: data are stored
• Link field or next field: address of the next
node in the list.
• Figure shows the linked list with 6 nodes.
• The arrow drawn represents the link between
two nodes.
• Null pointer indicates the end of the list.
• START contains the address of the first node.
• The list with no node is called null list or
empty list.
• DATA [K]
• LINK [K]
• START
• NULL
• The data field of a node may be a record with
more than one data item.
• In such case, the data must be store in some
type of record structure.
Traversing a Linked List
• Processing each node of list.
• DATA and LINK with START pointing to the first
element and NULL indicating the end of the
list.
Algorithm: Traversing a linked list
1. Set P: = START. [ Initializes pointer PTR]
2. Repeat Step 3 and 4 while P ≠ NULL.
3. Apply PROCESS to DATA [P].
4. Set P := LINK [P] .[P points to the next node]
[End of Step 2 loop]
5. Exit
• The pointer moves to the next node in the list
as shown in the figure
• P := START
• Process the data at the first node of the list
DATA [P].
• Then the P is incremented to next node P :=
LINK [P] and then it process the data of
second node.
• Then the P will again get incremented to next
node and so on. This will be continued until
the P = NULL which indicates the end of the
list.
Searching a Linked List
• Searching a linked list is to find the location of
an ITEM in the list.
• Two searching algorithms: first algorithm does
not assume that the data of list are sorted,
whereas the second algorithm assumes that
the list is sorted.
List is unsorted
• The data in the list are not sorted.
• The search is carried out by traversing through
the list and comparing each data in the list
with the ITEM to be searched.
ITEM = DATA [P],
LOC: = P
P: = LINK [P]
• Then it compares the ITEM with the second
node data if it matches then it set the location
to be second node address LOC := P, else the P
will be incremented to next node P := LINK [P]
and so on. This will be continued until P =
NULL.
• The complexity of this search algorithm for
worst case is ‘n’ and for average case it is ‘n/2’
same as linear search.
List is sorted
• In this the list is sorted and the search is
carried out by traversing through the list and
comparing each data in the list with the ITEM
to be searched.
Insertion into a Linked List
• Insertion is the process of adding a new node
to the linked list.
• It requires a new node and executing two next
pointer operations.
• The figure does not take into account that the
memory space for the new node N will come
from the AVAIL list.
• So, for easier processing, the first node in the
AVAIL will be used as the new node N.
• The next pointer field of node A now points to
the new node N, to which AVAIL previously
pointed.
• AVAIL now points to the second node in the
free- storage list, to which node N previously
pointed.
• The next pointer field of node N now points to
node B, to which node A previously pointed.
Insertion Algorithms
• We have algorithms for various situation of
inserting an ITEM into linked list.
• The first one is inserting a node at the
beginning of a list and the second one is
inserting after the node with a given location.
• All over the algorithm ITEM contains the new
data to be added to the list.
• All insertion algorithms will use new node
from AVAIL list, the following are the steps
which is included in all the algorithms.
• Checking to see if space is available in the
AVAIL list. If not then AVAIL = NULL, then the
algorithm will print the message OVERFLOW
• Removing the first node from the AVAIL list.
Using the variable N to keep track of the
location of the new node, this step can be
implemented by the pair of assignments
N := AVAIL, AVAIL := LINK [AVAIL]
• Copying new data into the new node.
DATA [N] := ITEM
Inserting at the beginning of a list
• First the OVERFLOW condition is checked and
then the first node is removed from AVAIL list
and marked as N.
• The data to be inserted is assigned to the new
node data field, DATA [N] := ITEM.
• Now the pointers are changed by making the
N nodes address field is pointed to first node
of the list and the START is made to point the
N node. Thus a node is inserted at the
beginning of the list.
Inserting after a given node
• In this method they will give the location LOC
of a node or the location will be null, LOC =
NULL.
• So, here if the location of some node is given
we insert the ITEM next to that node and if
location is null then the ITEM will be the first
node.
• If location is not null that means some other
nodes location will be given as LOC, then the
address of that LOC will point to the N node,
LINK [LOC] := N and the address of new node
will point to the address where LOC was
previously pointing, LINK [N] := LINK [LOC].
Working with linked list
Representing list in C
Linked implementation of stack
Linked implementation of queue
Key terms
• Singly linked list
• Circular list
• Doubly linked list
• INFO
• NEXT
• PREVIOUS
• FIRST
• LAST
• Insert
• Delete
• Search
• Print
Linked list node declaration
Struct node
{
int INFO ;
struct node *NEXT ;
};
Typedef struct node NODE ;
Key statement
void insert (int);
int delete (int);
void print (void);
struct node *search (int);
while (1)
switch (choice)
insert (num1);
num2= delete (num1);
location= search (num 1);
print ( );
default:
PTR->INFO = value;
FIRST = LAST = PTR;
LAST->NEXT = PTR;
struct node
{
struct student S ;
struct node *NEXT ;
};
typedef struct node NODE ;
Stack as a Circular List
Push operation
Pop operation
Queue as a Circular List
• It is easier to represent a queue as a circular
list than as a linear list.
• A queue is specified by two pointers, one to
the front of the list and the other to its rear.
However, by using a circular list, a queue may
be specified by a single pointer to that list.
• The operations isEmpty(queue) and
remove(queue) are identical with that of stack
operations.
Insert operation
Doubly Linked List
• Although a circularly linked list has advantages
over a linear list, it still has several drawbacks.
One cannot traverse such a list backward, nor
can a node be deleted from a circularly linked
list, given only a pointer to that node.
• In cases, where these facilities are required,
the appropriate data structure is a doubly
linked list.
Node declaration
struct node
{
int INFO;
strcut node *NEXT;
struct node *PREVIOUS;
};
Typedef struct node NODE;
• Each node in such a list contains two pointers,
one to its predecessor and another to its
successor. It is thus possible to move either
direction through the list while keeping only
one pointer.
• With a doubly linked list, traversals in
direction, insertions and deletions from
arbitrary positions in the list can be
programmed without difficulty.
• The cost of a doubly linked list, is the extra
space required in each node for a second link.
Deleting node
Circular Doubly Linked List
• In a doubly-circularly-linked list, each node
has two links, similar to a doubly-linked list,
except that the previous link of the first node
points to the last node and the next link of the
last node points to the first node.
• As in a doubly-linked list, insertions and
removals can be done at any point with access
to any nearby node.
Write a C function to print the elements of a
doubly linked list in reverse order.
Solution
Linked implementation of stack
Linked implementation of queue
Advantages of linked list
• Dynamic data structure
• Efficient memory utilization
• Insertions and deletions are easier
• Complex operations can be easily carried out
Disadvantages
• More memory
• Time consuming
Operations on linked list
• Creation
• Insertion
• Deletion
• Traversing
• Searching
• Display