DSA - Linked List PDF
DSA - Linked List PDF
DSA - Linked List PDF
Strictly for internal circulation (within KIIT) and reference only. Not for outside circulation without permission
3 Linked List 8
Singly Linked Lists and Chains, Representing
Chains in C, Polynomials, Sparse Matrix,
Doubly Linked Lists, Circular & Header
Linked Lists
Linked List is the linear data structure which are connected together via links and is a
sequence of nodes which contains items and connection to another node. It is the
second most used data structure after array.
Important Concepts:
Linked list can be visualized as a chain of nodes, where every node points to the next
node.
Advantages
They are a dynamic in nature which allocates memory when required.
Insertion and deletion operations can be easily implemented.
Stacks and queues can be easily executed.
Linked List reduces the access time.
Disadvantages
The memory is wasted as pointers require extra memory for storage.
No element can be accessed randomly; it has to access each node sequentially.
Reverse traversing is difficult in linked list.
Applications
Need where initial size is unknown
Need to insert elements at the beginning or/and end of the list
Used to implement stacks, queues, graphs, etc
In memory the linked list is stored in scattered locations. The memory for each node is
allocated dynamically i.e. as and when required. So the Linked List can increase as per
the user wish and the size is not fixed, it can vary.
Suppose first node of linked list is allocated with an address 1008. Its graphical
representation looks like :
Suppose next node is allocated with an address 10 and the list becomes
Address Comments Next
Declaration of struct that should be used to create a list where each node
holds info.
struct node
{
int info;
struct node *next;
};
The next step is to declare a pointer to serve as the list head, i.e.
struct node *head;
Once node data structure is declared and have created a NULL head pointer,
an empty linked list is created.
The next step is to implement operations with the list.
1 22 9
2 74 4
Boy CR 2 3 77 8
4 82 3
Girl CR 7 5
6 51 1
7 10 6
8 11 0
9 27 0
School of Computer Engineering
Class Work
10
Suppose the personnel file of a small company contains the following data of its
employees : Name, Employee No, Sex, Salary. Design a linked list using arrays.
Solution
So four parallel arrays say Name,
Name EmpNo Sex Salary LINK
EmpNo, Sex, Salary are required
to store the data. Additionally,
parallel array LINK is required
for the next pointer field of the
list and the variable HEAD is to
point to the 1st record in the list.
0 can be used to represent the
null pointer.
Head
School of Computer Engineering
Class Work
11
Suppose the personnel file of a small company contains the following data of its
employees : Name, Employee No, Sex, Salary. Design a linked list using structure.
Solution
struct personnel
{
8080
char *name;
8080 A 1 M 10 7080
Single Linked List− Item navigation is forward only. Also called as one-way list
Double Linked List − Items can be navigated forward and backward way
Circular Linked List − Last item contains link of the first element as next and first
element has link to last element as prev. Circular, singly linked list
Chains – A chain is a linked list in which each node represents one element
Two-Way Lists – Double and Circular Linked List
School of Computer Engineering
Double Linked List
13
Doubly Linked List is a variation of Linked list in which navigation is possible in both
ways i.e. either forward and backward.
Important Concepts:
Node − Each node of a linked list store the data
Next − Each node of a linked list contain a link to next link
Prev − Each node of a linked list contain a link to previous link
Linked List − A Linked List contains the connection link to the first Link called First
and to the last link called Last. First is also called as Head and Last is also called as
Tail.
Traversing linked list means visiting each and every node of the singly linked list. Following steps are
involved while traversing the singly linked list –
1. Firstly move to the first node
2. Fetch the data from the node and if required, perform the operations such as arithmetic operation
or any operation depending on data type.
3. After performing operation, advance pointer to next node and perform all above steps on visited
node.
Pseudocode: Class Work
void traverse() Design search algorithm for SLL (Single
{ Linked List)
struct node *temp = start; //Move to First Node
do
{
printf(“%d”, temp->data);
temp = temp->next; //Move Pointer to Next Node
}while(temp!=NULL);
}
School of Computer Engineering
Insertion at Start
18
Pseudocode for Delete Last Node from Singly Linked List: Class Work
struct node *temp = head, t = NULL;
Design an algorithm to delete a node at a
if(head->next==NULL)
specified position
{
free(head); Design a recursive algorithm to count
head=NULL; number of nodes in SLL
}
else
{
for(;temp->next != NULL; temp=temp->next, t = temp);
free(t->next);
t->next=NULL;
}
Singly Linked List Double Linked List Singly Linked List as Circular
Header linked list is a linked list which always contains a special node
called the Header Node, at the beginning of the list. It does not contain
actual data item included in the list but usually contains some useful
information about the entire linked list. It has two types:
1. Grounded Header Link List− Last Node Contains the NULL Pointer
2. Circular Header Link List − Last Node Points Back to the Header
Node.
Note: Start/Head always points to Header Node.
Class Work
Write creation and display method of
Circular Header Link List
Examples –
Polynomial with single variable P(X) = 4X3 + 6X2+7X+9 (4,6,7 are coefficient & 3,2
are exponent)
Polynomial with 2 variables P(X, Y) = X3 - 2XY +1 (2 is coefficient & 3 is exponent)
Definition–
Polynomial with single variable P(X) =
Node of a polynomial:
Coefficient Exponent Next
In each node the exponent field will store the corresponding exponent and the
coefficient field will store the corresponding coefficient. Link field points to the next item
in the polynomial.
Points to keep in mind
1. The sign of each coefficient and exponent is stored within the coefficient and the
exponent itself
2. The storage allocation for each term in the polynomial must be done in descending
order of their exponent
Sparse matrix can be represented using a list of such nodes, one per
non–zero element of the matrix. For example, consider the sparse matrix
The linked list can be represented using linked list as shown below
Class Work
Design non-array based data structure to represent lower triangular, upper
triangular and tri-diagonal matrix.
http://www.geeksforgeeks.org/data-structures/linked-list/
https://www.tutorialspoint.com/data_structures_algorithms/linked_list_alg
orithms.htm
http://www.studytonight.com/data-structures/introduction-to-linked-list
http://nptel.ac.in/courses/106103069/8
https://www.tutorialspoint.com/learn_c_by_examples/linked_list_programs
_in_c.htm