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

Module 2(Linked List)

Uploaded by

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

Module 2(Linked List)

Uploaded by

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

Data Structures & Algorithms

101009/IT200C (Module 2)
S2 CU
Tinku Soman Jacob
Asst. Prof IT
RSET

1
Index
• Self referential structure
• Dynamic memory allocation
• Linked list
• Single linked list
• Doubly linked list
• Circular linked list
• Stack and queue using linked list
• Polynomial representation using linked list

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 2


Drawback of array
1. Size of the array must be defined in advance ie, fixed at compile
time and is not possible to define maximum size in advance. (Static
data structure)
2. Insertion and deletion is difficult as the elements are stored in
consecutive memory locations and the shifting operation is costly.
3. Allocating more memory than the requirement leads to wastage of
memory space and less allocation of memory also leads to a
problem.

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 3


Self referential structure
• A self-referential structure is one in which one or more of its
components is a pointer to itself.
• Self Referential structures are those structures that have one or more
pointers which point to the same type of structure, as their member.
• In other words, structures pointing to the same type of structures are
self-referential in nature.
• It usually require dynamic storage management routine (malloc &
free) to explicitly obtain and release of memory.
• Self referential structures are very useful in creation of other complex
data structures like linked list, stack, queue, tree, graph etc
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 4
Self referential structure
• Syntax
Pointer variable = (cast_type *) malloc(byte_size)
• Eg: ptr = (strcut node *)malloc(sizeof(struct node));
• Achieved through dynamic memory allocation.
• Allocates requested size of bytes and returns a pointer to the first byte
of the allocated space.
• If allocation fails, it will return a NULL.
• free(ptr);

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 5


Pointer
• A pointer is a variable whose value is the address of another variable,
i.e., direct address of the memory location. 1,20,25,32,56,68
• Declaration

• x=10

• ptr=&x

• y=*ptr

• *ptr=25
6
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous)
Memory Allocation
• To execute a program, memory for the required data should be allocated to the
- Neil Armstrong
•program.
• Two methods for memory allocation.
• Static memory allocation
• Memory is allocated at compile time.
• Exact size of memory of each variable must be know.
• Stack is used to implement static memory allocation
• Memory cannot be increased while executing the program.
• Dynamic Memory Allocation
• Memory is allocated at run time.
• Depends on the program need memory is allocated at run time.
• Pointers are used for the implementation.
• Heap is used to implement dynamic memory allocation.
• It is the free memory that can be used at run time.
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous)
Dynamic Memory Allocation Functions
1.malloc()
2.calloc()
3.realloc()
4.free()

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous)


malloc()
 The malloc() function reserves a block of memory of the specified number of
bytes.
 It returns a pointer of void which can be casted into pointers of any form.
 The malloc() function allocates memory and leaves the memory uninitialized.
Syntax
ptr = (castType*) malloc(size);
Example
ptr = (float*) malloc(100 * sizeof(float));
• The above statement allocates 400 bytes of memory.
• It's because the size of float is 4 bytes.
• The pointer ptr holds the address of the first byte in the allocated memory.
• The 101009/IT200C
expression results in a NULLMr.pointer
_S2 CU_Module-2 Tinku Soman if the
Jacob, DIT, memory cannot be allocated.
RSET(Autonomous)
calloc()
• The name "calloc" stands for contiguous allocation.
• Whereas, the calloc() function allocates memory and initializes all bits
to zero.
• Syntax of calloc()
• ptr = (castType*)calloc(n, size);
• Example:
• ptr = (float*) calloc(25, sizeof(float));
• The above statement allocates contiguous space in memory for 25
elements of type float.
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous)
free()
• Dynamically allocated memory created with either calloc() or malloc()
doesn't get freed on their own.
• We must explicitly use free() to release the space.
Syntax of free()
free(ptr);
• This statement frees the space allocated in the memory pointed by ptr.

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous)


realloc()
• If the dynamically allocated memory is insufficient or more than
required, you can change the size of previously allocated memory
using the realloc() function.
• Syntax of realloc()
• ptr = realloc(ptr, x);
• Here, ptr is reallocated with a new size x.

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous)


Linked list
• Linked list is a collection of similar elements where each element
points to the next element.
• Each element is called a node, which has two parts.
1. Information/data field – which contain the necessary information
about the item of the list to be stored are processed.
2. Link/Next field – which contain address of the next node (adjacent
node). This field is used to access the next data item maintained in
the other nodes.

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 13


Linked list
• Next field contains a pointer giving the address of the next item in the
list. This field is used to maintain the linking of the data items in the
list.
• Data items of the linked list are not necessarily maintained in the
contiguous memory locations, they may be placed anywhere in the
memory.
• NULL pointer – indicates the end of list.

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 14


Linked list- Advantages, Disadvantages
Advantages
1. Linked list are dynamic data structure – they can grow or shrink
during the execution.
2. Efficient memory utilization – memory is allocated whenever it is
required and deallocated when it is no longer needed.
3. Insertion and deletion are easier & efficient.
4. Easy access to data in nodes
Disadvantages
5. More memory – if number of fields are more, more memory
required.
6. Access to arbitrary data is time consuming.
_S2 CU_Module-2
101009/IT200C Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 15
Linked list- Representation
• 2 ways of representation
1. Static or sequential or array representation
2. Dynamic or pointer or linked representation

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 16


Linked list- Types
1. Singly linked list
2. Doubly linked list
3. Circular linked list

17
Linked list- Structure definition
Array Linked List Linked List
struct node struct node struct student
{ { {
int info; char name[10];
int info;
int next; int rollno;
struct node *next;
int mark[3],total;
}; }; struct student * next;
struct node list[12]; struct node * start; };
struct node *start=NULL;

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 18


Linked list- creation/insertion
struct node
printf(“Enter item”);
{
scanf (“%d”, &second->info)
int info;
second->next=NULL;
struct node *next;
//display
};
temp=start
main()
printf(“Elements are:”);
{
while(temp!=NULL)
struct node *start, *second,*temp;
{
start = NULL;
printf(“%d”, temp->info);
start = (struct node*)malloc(sizeof(struct node));
temp=temp->next;
second = (struct node*)malloc(sizeof(struct node));
}
printf(“Enter item”);
scanf (“%d”, &start->info)
}
start->next=second;

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 19


Linked list- Operations
• Traversing the list
• Inserting a node into the list
• Deleting a node from the list
• Copying the list to make a duplicate of it
• Merging the linked list with another one to make a larger list
• Searching for an element in the list

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 20


Linked list- Insertion
• To insert an element into linked list, 3 things should be done.
1. Allocation of node
2. Assign the data
3. Adjust the pointers
• Inserting a new node into linked list has the following 3 instances
1. Insert a node at the beginning of the linked list
2. Insert a node at the end of the linked list
3. Insert a node at a specified location of the linked list

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 21


Linked list- Insertion beginning
Insert_Beg(list,item,start,ptr)
//status of linked list of nodes
//start pointer NULL or points to the first node of the linked list
//ptr points to the new node
//item is the new data to insert
1. If ptr=NULL
1. Print “overflow error”
2. Exit
2. Set ptr->info=item
3. Set ptr->next=start and start=ptr
4. Exit
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 22
Linked list- Insertion end
Insert_end(list,item,start,ptr)
//status of linked list of nodes
//start pointer NULL or points to the first node of the linked list
//ptr points to the new node
//item is the new data to insert
//temp is a temporary pointer
1. If ptr=NULL
1. Print “overflow error”
2. Exit
2. Set ptr->info=item
3. Set temp=start
4. While temp->next!=NULL //to find the last node
1. temp=temp->next
5. Set ptr->next=NULL or ptr->next=temp->next
6. Set temp->next=ptr
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 23
Linked list- Insertion at Specified position
Insert_position(list,item,start,ptr,pos)
1. If ptr=NULL
1. Print “overflow error”
2. Exit
2. Set temp=start, count=1
3. While count<(pos-1)
1. temp=temp->next
2. Count=count+1
4. Set ptr->info=item
5. ptr->next=temp->next
6. Set temp->next=ptr
7. Exit _S2 CU_Module-2
101009/IT200C Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 24
Linked list- Traversal
traversal(list,start,temp)
//temp is temporary pointer
1. Set temp=start
2. While temp !=NULL
1. Print temp->info
2. temp=temp->next
3. Exit

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 25


Linked list- Deletion
• Deletion of a node from linked list has the following 3 instances

1. Delete a node from the beginning of the linked list


2. Delete a node from the end of the linked list
• If the node to be deleted is the last node then traverse the entire list to find the address of
the second last node so that we can adjust the pointer
3. Delete a node at a specified location of the linked list
• If we want to delete a node containing some specific information or a node at a particular
position, then we have to traverse the list to find that node and adjust the pointers

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 26


Linked list- Deletion beginning
Delete_beg(list,start,temp)
//give suitable naming & descriptions
1. If start = NULL
1. Print “Underflow error”
2. exit
2. Set temp=start
3. Assign item=temp->info//item a variable
4. Set start =start->next//second node become the starting node
5. Delete temp node or release to memory heap
6. exit

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 27


Linked list- Deletion end
Delete_end(list,start,ptr,prev) //start,ptr,prev are pointers
1. If start = NULL
1. Print “Underflow error”
2. exit
2. Set ptr=start
3. If ptr->next=NULL//list has only one node
1. Assign item=ptr>info//item is a variable
2. Start=NULL
3. Delete ptr node or release to memory heap
4. exit
4. Otherwise while ptr->next!=NULL
1. prev=ptr
2. ptr=ptr->next
5. Assign item=ptr>info and print item
6. prev->next=NULL
7. Delete ptr node or release to memory heap
8. exit
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 28
Linked list- Deletion from a position
Delete_pos(list,start,ptr,prev,item) //start,ptr,prev are pointers & item hold the data which we want to delete
//prev is a pointer used to hold the address of the previous node of the node which we want to delete from
list
1. If start = NULL
1. Print “Underflow error”
2. exit
2. Set ptr=start
3. If ptr->info=item//list has only one node
1. Assign value=ptr>info//value is a variable
2. Start=NULL
3. Delete ptr node or release to memory heap
4. exit
4. Otherwise while ptr->next!=NULL && ptr->info!=item
1. prev=ptr
2. ptr=ptr->next
5. Assign value=ptr>info and print item
6. prev->next=ptr->next
7. Delete ptr node or release to memory heap
8. exit
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 29
Task
Define a node with the data fields
student name
Roll number (unique)
Age
Gender- M/F
Write the pseudocode for the operations like insertion, deletion,
traversal and search a student based on roll number.

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 30


Stack using Linked list
• Dynamic representation of stack data structure is termed as linked
stack.
• In linked stack, each node contain the basic information/data field and
address field.
• Top pointer variable points to the top of the stack
• If top is NULL, then the stack is empty
• The next field of the last node will be NULL which shows the bottom
of the stack.

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 31


Linked stack - push
push(item) //insertion of new data in the beginning of Linked list
//give suitable naming & descriptions
1. Create a new node ptr
2. If ptr = NULL
1. Print “Overflow error”
2. exit
3. Assign ptr->info=item//item is a variable
4. If top=NULL
1. Set top=ptr
2. ptr->next=NULL
5. else set ptr->next=top
1. Set top=ptr
2. exit

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 32


Linked stack - pop
Pop()//deletion of data from the beginning of Linked list
//give suitable naming & descriptions
1. If top = NULL
1. Print “Underflow error”
2. exit
2. otherwise
1. Assign item=top->info//item is a variable
2. Set ptr=top
3. Set top=top->next
4. Release the memory space of ptr
5. exit

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 33


Linked stack - traversal
traversal()
//give suitable naming & descriptions
1. If top = NULL
1. Print “stack is empty”
2. exit
2. otherwise
1. Set ptr=top
2. While ptr!=NULL
1. Assign item=ptr->info and print item
2. ptr=ptr->next
3. exit

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 34


Queue using Linked list
• A linked queue is a queue implemented as a linked list with two
pointer variables front and rear pointing to the nodes which is in the
front and rear, respectively.
• In linked queue, each node contain the basic information/data field and
address field.
• If front is NULL, then the queue is empty

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 35


Linked Queue - Enqueue
Enqueue(front,rear,item,ptr) //insert a new node at the end of linked list
//give suitable naming & descriptions
//initially front and rear are NULL indicate queue empty
1. Create a new node ptr //ptr is a pointer
2. If ptr = NULL
1. Print “Overflow error”
2. exit
3. Assign ptr->info=item//item is a variable
4. ptr->next=NULL
5. If front=NULL
1. Set front=rear=ptr
6. else set rear->next=ptr
1. Set rear=ptr
2. exit
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 36
Linked Queue - Dequeue
Dequeue() //delete a new node from the beginning of linked list
//give suitable naming & descriptions
1. If front = NULL
1. Print “Underflow error”
2. exit
2. Set ptr = front
3. Set item = ptr->info
4. If front=rear
1. Set front=rear=NULL
2. Release ptr back to memory
3. Exit
5. else set front = front->next
1. Release ptr back to memory
2. exit
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 37
Linked Queue – Traversal/Display
Display()
1. If front = NULL
1. Print “Queue empty”
2. exit
2. Set ptr = front
3. While ptr!=NULL
1. Assign item=ptr->info and print
2. Set ptr=ptr->next
4. Exit

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 38


Doubly linked list
• Backward traversal is not possible in single linked list.

• next pointer points to the next node of current node.


• prev pointer points to the previous node of the current node.
• By using these two pointers, we can access the successor &
predecessor node of any arbitrary node within the list.
• Bidirectional traversal is possible.

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 39


Doubly linked list
struct node
{
struct node * prev;
int info;
struct node * next;
};

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 40


Task

Identify the differences between


single linked list and double linked list

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 41


Operations on Doubly linked list
• Insertion at the beginning of the doubly linked list
• Insertion at the end of the doubly linked list
• Insertion at any position in the doubly linked list
• Deletion from the beginning of the doubly linked list
• Deletion from the end of the doubly linked list
• Deletion at any position in the doubly linked list

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 42


Doubly linked list-Insertion beginning
Insert_beg(start,item)
//give suitable naming & descriptions
1. Allocate memory for the new node ptr
2. If ptr=NULL
1. Print overflow error
2. exit
3. Set ptr->prev=NULL
4. Set ptr->info=item//item is a variable
5. Set ptr->next=start
6. Set start->prev=ptr
7. Set start=ptr
8. exit
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 43
Doubly linked list-Insertion end
Insert_end(start,item)
//give suitable naming & descriptions
1. Allocate memory for the new node ptr
2. If ptr=NULL
1. Print overflow error
2. exit
3. Set temp=start//temp is a temporary pointer
4. Repeat while temp->next!=NULL
1. Set temp=temp->next
5. Set ptr->info=item
6. Set ptr->prev=temp
7. Set temp->next=ptr
8. Set ptr->next=NULL
9. Exit
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 44
Doubly linked list-Insertion at any position
Insert_pos(start,item)
//give suitable naming & descriptions
1. Allocate memory for the new node ptr
2. If ptr=NULL
1. Print overflow error
2. exit
3. Set temp=start//temp is a temporary pointer
4. Repeat while temp->info!=value// value after which new data wants to insert
1. temp1=temp
2. Set temp=temp->next
3. temp2=temp->next
5. Set ptr->info=item
6. Set ptr->next=temp->next
7. Set temp2->prev=ptr
8. Set temp->next=ptr
9. Set ptr->prev=temp
10. Exit
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 45
Doubly linked list-Deletion beginning
delete_beg(list,start)
//give suitable naming & descriptions
1. If start=NULL
1. Print underflow error
2. exit
2. if start->next=NULL
1. set temp=start
2. Assign item=start->info and print item
3. Set start=NULL and release temp to memory
3. Assign item=start->info and print item
4. Set start=start->next
5. Set start->prev=NULL
6. exit
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 46
Doubly linked list-Deletion end
delete_end(list,start)
//give suitable naming & descriptions
1. If start=NULL
1. Print underflow error
2. exit
2. Set temp=start
3. if start->next=NULL
1. Assign item=start->info and print item
2. Set start=NULL and release temp to memory
4. Repeat while temp->next!=NULL
1. Set ptr=temp
2. Set temp=temp->next
5. Assign item=temp->info
6. Set ptr->next=NULL//temp->prev->next=NULL
7. Release temp node to memory
8. Exit
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 47
Doubly linked list-Deletion from any position
delete_pos(list,start,pos)
//give suitable naming & descriptions
1. i=1, ptr=start
2. If start=NULL
1. Print underflow error
2. exit
3. if start->next=NULL and i=pos
1. Assign item=start->info and print item
2. Set start=NULL and release ptr to memory
4. Set ptr=start
5. Repeat while i<pos
1. Set temp=ptr
2. Set ptr=ptr->next
3. Increment i
6. Assign item=ptr>info
7. Set ptr 1= ptr->next
8. Set temp->next=ptr 1
9. Set ptr 1->prev=temp
10. Release ptr node to memory
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 48
11. Exit
Polynomial Representation using linked list
struct poly
{
int coef;
int exp;
struct poly*next;
};
Eg: 10x14-4x9+6x5+x

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 49


Polynomial addition using linked list
Eg:
3x14+2x7+1 ---------------a
10x14-4x9+6x5+x -------------b

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 50


Polynomial addition using linked list
Poly_add(struct poly a, struct poly b) case ‘<‘ //a->exp < b->exp
//a is a pointer to polynomial a {
//b is a pointer to polynomial b
result(b->coef,b->exp)
//a=starta & b=startb
b=b->next
while a!=NULL & b!=NULL
{ break;
switch(compare a->exp, b->exp) }
{ case ‘>‘ //a->exp > b->exp
case ‘=‘
{
{
result(a->coef,a->exp)
sum=a->coef + b->coef
if (sum!=0) a=a->next
{ break;
result(sum,a->exp)
}
a=a->next
b=b->next }//end of switch
} }//end of while
break;
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 51
}
Polynomial addition using linked list
while a !=NULL
{
result(a->coef,a->exp)
a=a->next

}
while b!=NULL
{
result(b->coef,b->exp)
b=b->next

}
}//end of poly add

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 52


Polynomial addition using linked list
Result (int c, int e) else
{ {
struct poly * ptr, *start=NULL,*temp; temp = start
Allocate new node ptr while temp->next!=NULL
if ptr =NULL temp=temp->next
print “Overflow” and exit temp->next=ptr
else ptr->next=NULL
{ }
ptr->coef=c }//end of else
ptr->exp=e }//end of result
if start=NULL
{
start=ptr
ptr->next=NULL
}
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 53
Applications of linked list
• Implement stack, queue, tree, graph etc
• Implement hash table (open chain hashing)
• Undo functionality in word, photoshop etc
• Polynomial representation
• Web browser history section for webpage selection
• Heap memory management

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 54


Advantages of linked list
• Dynamic data structure
• Easy to perform insertion and deletion
• No need to define an initial size for linked list
• Data can add or remove at anywhere in linked list
• Backtracking is possible

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 55


Disadvantages of linked list
• More memory is required than array because of pointer
• Access data in sequential order
• Nodes store in-contiguously – increase time for accessing individual
elements in list especially CPU cache

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 56


References to text
1. Header node – Samantha Text page no. 37
2. Memory allocation - Samantha Text page no. 38
3. Dynamic Storage management- Samantha Text page no. 73
4. Storage allocation strategies - Samantha Text page no. 80

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 57


Task
• Write the pseudocode to arrange the elements in a linked list in
ascending order.
• Let L be a double linked list in memory. Write an algorithm that
reverses the list.
• Consider a doubly linked list having n nodes. The data items d1,
d2, ...., dn are stored in the n nodes. Design an algorithm for deleting a
value di in the list. Also, outline the algorithm to insert di to the
beginning of the list.
• Check if the given string is palindrome in an efficient method.

58

You might also like