0% found this document useful (0 votes)
11 views

Linked List

c programming

Uploaded by

Haf hafeefa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Linked List

c programming

Uploaded by

Haf hafeefa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

LINKED

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

◦ Circular linked list

◦ Double linked list


⚫ Two ways to represent a linked list in memory:
◦ Static representation using arrays

◦ Dynamic representation using free pool of storage


⚫ For static representation, two parallel arrays of
equal size are used
◦ One for data

◦ Other for link

⚫ Used in languages which doesn’t support


dynamic memory allocation
⚫ For dynamic representation we use a free pool
of storage or memory bank
⚫ It is a collection of free memory spaces
⚫ The program which controls memory allocation
is called the memory manager
⚫ Whenever a node is required, a request is placed
to the memory manager
⚫ Memory manager searches the free pool for a
block of required size and if available, it is
allocated.
⚫ Another program called garbage collector
returns the unused node to memory
block whenever a node is deleted.
⚫ This is called dynamic memory
management.
Example
⚫ A pool of free memory is there whose pointer
to first block is stored in AVAIL.
⚫ When a request for node comes, this pool is
searched.
⚫ If AVAIL is NULL or a block of right size is
not found, accordingly a message is returned.
⚫ If a block XY is found, memory manager
returns its starting address in a temporary buffer
NEW.
⚫ This node can be inserted at any position in the
linked list by changing pointers accordingly.
⚫ Allocation of a node from memory bank to a linked list
⚫ Returning a node from a linked list to memory bank
Self Referential
Structures
⚫ A structure in which one or more of the

components is a pointer to itself


⚫ Require dynamic storage management to acquire
and release memory
Eg: typedef struct{
char data;
struct list *link;
}list;
⚫ Each instance of the structure list will have two
components , data and link.
⚫ Data is a single character, whereas link is a
pointer to a list structure.
⚫ The value of link is either the address in
memory of an instance of list or a null pointer.
list item1, item2, item3;

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

◦ Inserting at the front (as first element)

◦ Inserting at the end (as last element)

◦ Inserting at any other position in the list

⚫ Before we perform insertion we need to get a


memory block of correct size from memory bank
⚫ A procedure GetNode() is used to get a pointer
to a memory block from the free pool which
suits our need
⚫ The GetNode() procedure takes as input the type
of data for which a memory block is to be
allocated
⚫ Returns an error message if the allocation fails,
otherwise returns a pointer to the allotted memory
block
Inserting at Front
⚫ Inserts a node at the front of a singly linked list
⚫ Input: HEADER is the pointer to header node
and X is the data of node to be inserted
⚫ Output: A singly linked list with the new node
at front
Inserting at the
end
Inserting at any
position
Singly Linked List -
Deletion
⚫ There are three cases in deletion also:

◦ Deleting from front of list


◦ Deleting from end of list
◦ Deleting from any position in the list
⚫ When a node is deleted, the memory block is
returned to memory bank.
⚫ We assume a procedure ReturnNode(ptr) which
returns a node having pointer ptr to free pool.
⚫ This procedure inserts the freed node at the end of the pool of
free storage. The header address of free storage is denoted by
AVAIL.
Deleting From
front
Deleting At The
End
Deleting From Any
Position
Copying a Singly
Linked List
Merging Two Linked
Lists
Searching for an
Element

You might also like