0% found this document useful (0 votes)
95 views103 pages

Linked List

Linked lists are linear data structures that store data in nodes that are connected through pointers. Each node contains a data field for storing the data and a link field containing the address of the next node. The last node contains a null pointer. Linked lists allow efficient insertion and removal of nodes and do not require contiguous memory locations. Common operations on linked lists include traversing, searching, inserting, and deleting nodes. Circular linked lists connect the last node back to the first node, allowing easy accessibility of all nodes. Doubly linked lists contain links to both the next and previous nodes.

Uploaded by

luna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views103 pages

Linked List

Linked lists are linear data structures that store data in nodes that are connected through pointers. Each node contains a data field for storing the data and a link field containing the address of the next node. The last node contains a null pointer. Linked lists allow efficient insertion and removal of nodes and do not require contiguous memory locations. Common operations on linked lists include traversing, searching, inserting, and deleting nodes. Circular linked lists connect the last node back to the first node, allowing easy accessibility of all nodes. Doubly linked lists contain links to both the next and previous nodes.

Uploaded by

luna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 103

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 cl_node *FIRST = NULL;


struct cl_node *LAST = NULL ;
Deletion from a Linked List
• Deletion is the process of removing a node
from the linked list. The deletion of a node is
made just by a pointer change.
• Suppose a node has to be deleted between
node A and B, the figure below explains it.
• The next pointer field of node A now points to
node B, where node N previously pointed.
• The next pointer field of N now points to the
original first node in the free storage list,
where AVAIL previously pointed.
• AVAIL now points to the deleted node N.
Deletion Algorithm
• All of the algorithms will include the following
pair of assignments, where LOC is the location
of the deleted node N:
• LINK [LOC] := AVAIL and AVAIL := LOC
Deleting the node following a given node
• Let N be the node to be deleted. Suppose we
are given the location LOC of node N in the
list.
• Furthermore, suppose we are given the
location LOC1 of the node preceding N or
when N is the first node, we are given LOC1 =
NULL.
• When LOC1 = NULL then the N will be the first
node and it is deleted just by assigning the
START variable to the link of START variable,
START := LINK [START].
• When LOC1 is not NULL then the deletion is
done by assigning the link of LOC1 to link of
LOC, LINK [LOC1] := LINK [LOC].
Deleting the node with a given ITEM of
Information
• In this deletion method the ITEM information
will be given and we have to delete the first
node N which contains the ITEM.
• So, to delete the node N from the list we need
to know the location of the node preceding N.
• The traversing of the list continues until we
get the ITEM, DATA [P] = ITEM or ITEM ≠ DATA
[P]. Then the pointer variables P contains the
location LOC of the node N and P1 contains
the location LOC1 of the node preceding N.
Circular linked list
• Linear linked list has several shortcomings.
Given a pointer p to a node in a linear list, we
cannot reach any of the nodes that precede
node p.
• If a list is traversed the external dummy
pointer to the list must be preserved to be
able to reference the list again.
• Suppose that a small change is made to the
structure of a linear list, so that the next field
in the last node contains a pointer back to the
first node rather than the null pointer. Such a
list is called a Circular Linked List.
• The advantage of this circular linked list is easy
accessibility of node i.e. every node is
accessible from a given node.
• The circular linked list can be implemented
both in one- way list and two- way lists.
Write the code for declaring the node of a singly
list that stores students related data.
Solution
struct student
{
char name [30] ;
int rollno ;
float percentage ;
};

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

You might also like