Module 3 DS
Module 3 DS
Module 3
Iliyaz Pasha M
Assistant Professor
De
1 partment of Computer Science, RLJIT
Outline
Definition.
Representation of linked lists in Memory.
Memory allocation
Garbage Collection.
Linked list operations: Traversing, Searching, Insertion, and
Deletion.
Doubly Linked lists.
Circular linked lists.
Header linked lists.
Linked Stacks and Queues.
Applications of Linked lists –
Polynomials.
Sparse matrix representation.
Programming Examples.
Department of Computer Science, RLJIT 2
Linked List
A linked list is a data structure which is collection of zero or more
nodes where each node is connected to one or more nodes. Each
node has two fields namely
info field – This field is used to store the data or information
to be manipulated.
link or next field – This field contains address of the next
node.
The pictorial representation of a singly linked list where each
node is connected to the next node is shown below.
Observe from the above structure definition that a node has two fields:
info – it contains the information.
link – it is a pointer field and hence it should contain the address.
The link field contains the address of the next node in the list whose type
is same as the link field.
To create the variable of a node, we follow the declaration.
Along with structure declaration of node, create a typedef name for node.
typedef struct node *NODE;
A pointer variable for NODE can be declared as shown below
NODE start;
If the linked list is empty, we store a special value NULL in the start i.e.,
start = NULL //Department
emptyof Computer
linkedScience,
list RLJIT 8
Linked List Operations (One – Way List):
Singly Linked List (SLL):
It is a collection of zero or more nodes, where each node has 2 or more fields,
but only one link field which contains address of the next node. Each node in
the list can be accessed using the link field which contains address of the next
node.
Example: Singly Linked List (SLL) consisting of items 50, 20,5,10 and 80 is
shown below.
Note: The variable first contains address of the first node. The link field of
the last node contains \0 (NULL) indicating it is the last node.
Observe from above figure that:
This list contains 5 nodes and each node contains of two fields, info and link.
The info field of each node contains the data. In this list, the items 50, 20, 45, 10
and 80 represent the data.
The numbers on top of each node i.e, 2004, 1020, 5012, 1008 and 6016 represent
the address of each node in theDepartment
memory. of Computer Science, RLJIT 9
Singly Linked List (SLL):
It is called singly because this list consists of only one link, to point to next
node.
Example:
Note: The last field of the last node is \0 (NULL), means here is no further
nodes.
Initially, when no nodes are created, the node first will contain \0 (NULL).
This indicate the linked list does not exist. Hence called an empty linked list.
Create a Node:
void create ( )
{
temp = (NODE) malloc (sizeof(struct node));
if ( temp == NULL )
{
printf(“Insufficient Memory or out of memory\n”);
return ;
}
printf (“Enter the information to be stored\n”);
scanf (“%d”, &temp -> info);
temp ->link = NULL; Department of Computer Science, RLJIT 12
}
Insertion at the beginning (front) :
void insertBeg ( )
{
if ( first == NULL ) // List is empty
{
create ( );
first = temp;
last = first;
}
else
{
create ( );
temp ->link = first;
first = temp;
}
}
Implementation:
struct node
{
int info;
struct node *next;
};
NODE first;
first = NULL;
Implementation:
struct node
{
int info;
struct node *prev, *next;
}; Department of Computer Science, RLJIT 46
Insertion node at the Beginning:
NODE insertBeg ( NODE head, int item)
{
NODE temp, first;
temp = (NODE) malloc(sizeof(NODE));
temp->info = item;
first = head->next;
temp->prev = head;
head->next = temp;
temp->next = first;
first->prev = temp;
return head;
}
Or
insertEnd()
deleteEnd()
display()
h1=head->next;
Example:
The above matrix can be represented using linked list as shown below.
cur = first;
while ( cur->next != NULL)
{
cur=cur->next;
}
cur -> next = sec; // attach 1st node of 2nd list to end of 1st list
return first;
}