DS (Module 2 Note) - 1
DS (Module 2 Note) - 1
List
List is a linear data structure with a sequence of zero or more elements.
List is an ordered collection of elements. So it is also known as ordered list or linear list.
Examples
Days of the week:
(SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY)
Months of year:
(JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER,
OCTOBER, NOVEMBER, DECEMBER)
Boolean list:
(TRUE, FALSE)
Letters:
(A, B, C, D……Z, a, b, c, d…..z)
Digits:
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
➢ Successor is the next element in sequence. The last node has no successor.
Successor of A1 Is A2
Successor of A2 Is A3
Successor of Ai is Ai+1
i.e. Ai precedes Ai+1
SMPC 2 DATA STRUCTURES Module 2
➢ Predecessor is the previous element in sequence. The first node has no predecessor.
Predecessor of A2 is A1
Predecessor of A3 is A2
Predecessor of Ai is Ai-1
i.e. Ai follows Ai-1
➢ A list’s length is the number of elements in it. A list may be empty (contain no elements)
➢ Two standard implementations for the list ADT
✓ Array-based
✓ Linked list
List ADT
AbstractDataType LinearList
{
Instances
Ordered finite collection of zero or more elements
}
{
Operations
isempty() : return true if the list is empty, otherwise false.
boolean datatype
size() : return the list size.
(i.e. number of elements in the list)
find, get(index) : return the index of element in list.
(i.e. locate the position of an object in a list)
indexof(x) : return the index of x in the list.
SMPC 3 DATA STRUCTURES Module 2
Return -1 if x not in list
remove(index), erase(index) : remove the element at given index.
Elements with higher index of given index reduce by 1
insert(index, x) : insert element x at given index.
Element with higher index of given index increased by 1
printList() : print the list
output he list elements from left to right
}
Consider an Example:-
• First element of list is element[0], Second element of list is element[1]
• Last element of list is element[n-1], List (5, 2, 4, 8, 1)
5 2 4 8 1
insert(2, 12)
5 2 12 4 8 1
remove(2)
5 2 4 8 1
Link
➢ Linked list can be used to implement other data structures such as stacks, queues and their types.
➢ Each node contains two parts:-
1. Data (any type)
2. Pointer to the next node in the list
START
1 2 3 X
NULL, X
If start=1, the first data stored at address=1 which is H. The corresponding the NEXT stores the
address of the next node, which is 4. The data element at address 4 is E. If the NEXT pointer
contains -1 or NULL, this means that the end of the list.
AVAIL=4
AVAIL=6
Figure 6.5 (a) Students linked list (b) linked list after the insertion of new student mark.
SMPC 6 DATA STRUCTURES Module 2
o If we want to insert a node to an already existing linked list in memory, first find free
space in the memory and then use it to store the information.
o The computer maintains a list of all the free memory cells. This list is of available space is
called the free pool.
o Every linked list has a pointer variable START which stores the address of the first node
of the list.
o Another pointer variable AVAIL which stores the address of the first free space.
o Garbage collection
The operating system scans through all the memory cells and marks those cells that are
being used by some other programs.
Then, it collects all the cells which are not being used and adds their address to the free
pool. So that it can be reused by other programs. This process is called garbage
collection. The whole process of collecting unused memory cells is transparent to the
programmer.
Linked lists provide an efficient way of storing related data and perform basic operations such as
traverse, search, insert, delete of data. But extra space required for storing address of the next
node.
That is in detail:-
1) PrintList
2) Find
3) FindKth
4) Insert
5) Delete
6) MakeEmpty
SMPC 7 DATA STRUCTURES Module 2
7) Successor
8) Predecessor etc.
I. Traversing
• Traversing means accessing (visiting) the nodes of the list in order to perform some
process on them.
• The pointers used in the algorithms are START, PTR (which points to the node that is
currently being accessed)
II. Searching
• Searching means to find or search a particular element in the linked list.
• This algorithm returns the address of the node that contains the value.
• Two algorithms are:-
1. Search an unsorted list
2. Search a sorted list
SMPC 8 DATA STRUCTURES Module 2
Else
Go to step 4
Step 4: SET POS=NULL
Step 5: EXIT
III. Insertion
✓ To do insertion, we will take 5 cases
1. The new node is inserted at the beginning
2. The new node is inserted at the end
3. The new node is inserted after a given node
4. The new node is inserted before a given node
5. The new node is inserted in a sorted linked list
1. Insert at beginning
Algorithm to insert a new node at the beginning of linked list (Algorithm case 1)
Step 1: if AVAIL=NULL, then print “overflow”, go to step 7
Step 2: SET New_Node=AVAIL
Step 3: SET AVAIL=AVAIL->NEXT
Step 4: SET New_Node->DATA=VAL
Step 5: SET New_Node->Next=START
SMPC 10 DATA STRUCTURES Module 2
2. Insert at end
Algorithm to insert a new node at the end of linked list (Algorithm case 2)
Step 1: if AVAIL=NULL, then write overflow go to step 10
Step 2: SET New_Node=AVAIL
Step 3: SET AVAIL=AVAIL->NEXT
Step 4: SET New_Node->DATA=VAL
Step 5: SET New_Node->Next=NULL
Step 6: SET PTR=START
Step 7: Repeat step 8 while PTR->NEXT! =NULL
Step 8: SET PTR=PTR->NEXT
Step 9: SET PTR->NEXT=New_Node
Step 10: Exit
✓ PREPTR is a pointer variable which stores the address of node preceding PTR.
i.e. previous of PTR.
✓ In the while loop, traverse through the linked list to reach the node that has value equal to
NUM.
SMPC 11 DATA STRUCTURES Module 2
For example: Insert a new node with value 9 after the node containing data 3.
VAL = 9, NUM = 3
Algorithm to insert a new node after a node that has value NUM (Algorithm case 3)
Step 1: if AVAIL=NULL, then print “overflow” go to step 12
Step 2: SET New_Node=AVAIL
Step 3: SET AVAIL=AVAIL->NEXT
Step 4: SET New_Node->DATA=VAL
Step 5: SET PTR=START
Step 6: SET PREPTR = PTR
Step 7: Repeat step 8 and 9 while PREPTR->DATA!=NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR=PTR->NEXT
Step 10: SET PREPTR->NEXT=New_Node
Step 11: SET New_Node->NEXT=PTR
Step 12: Exit
SMPC 12 DATA STRUCTURES Module 2
✓ PREPTR is a pointer variable which stores the address of node preceding PTR.
i.e. previous of PTR.
✓ In the while loop, traverse through the linked list to reach the node that has value equal to
NUM.
For eg: Insert a new node with value 9 before the node containing data 3. VAL = 9, NUM = 3
Algorithm to insert a new node before a node that has value NUM (Algorithm case 4)
Step 1: if AVAIL=NULL, then print “overflow” go to step 12
Step 2: SET New_Node=AVAIL
Step 3: SET AVAIL=AVAIL->NEXT
Step 4: SET New_Node->DATA=VAL
Step 5: SET PTR=START
Step 6: SET PREPTR = PTR
Step 7: Repeat step 8 and 9 while PTR->DATA!=NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR=PTR->NEXT
Step 10: SET PREPTR->NEXT=New_Node
Step 11: SET New_Node->NEXT=PTR
Step 12: exit
SMPC 13 DATA STRUCTURES Module 2
✓ UNDERFLOW is a condition that occurs when we try to delete a node from an empty
linked list. This happens when START = NULL. i.e. no node to delete.
✓ After deletion, free the deleted space and return to free pool.
PTR
START is made to point to the next node and finally the memory occupied by the node pointed
by PTR (the first node of the list) is freed and returned to the free pool.
✓ PREPTR is a pointer variable which stores the address of node before PTR.
i.e. previous of PTR.
✓ SET PREPTR->NEXT=NULL.
So that PREPTR now becomes the (new) last node of linked list. The memory of previous last
node is freed and returned to the free pool.
SMPC 15 DATA STRUCTURES Module 2
✓ PREPTR is a pointer variable which stores the address of node preceding PTR.
i.e. previous of PTR.
✓ The memory of the node after (succeeding) given node is freed and returned to the free
pool.
➢ PREPTR is a pointer variable which stores the address of node preceding PTR.
i.e. previous of PTR.
➢ The memory of the node pointed by PTR is freed and returned to the free pool.
SMPC 17 DATA STRUCTURES Module 2
Algorithm to delete the node from a sorted linked list (Algorithm case 4)
LinkedList ADT
AbstractDataType LinkedList
{
Instances
Linear list of elements and node;
A pointer to the next node in the list called NEXT;
A pointer to the first node in the list called START, HEAD;
A pointer to the address of the first free space is called AVAIL.
}
{
Operations
➢ IsEmpty : Return true if linked list is empty (underflow), return false otherwise.
If the list is initially empty, START is set to NULL.
boolean data type
➢ Find(x) : Search for a node with the value equal to x in the list.
If such a node is found, return its position.
Otherwise (If x not in linked list), return 0.
SMPC 18 DATA STRUCTURES Module 2
➢ FindKth() : Search for a node with the position equal to K in the list.
If such a node is found, return its value.
If K=1, return First node value
➢ PrintList(), DisplayList() : print all the nodes in the list.
Print the number of the nodes in the list
➢ InsertNode : Insert a new node at a particular position.
Insert does not require moving the other elements
➢ InsertAfter() : Insert after a given node
➢ InsertBefore() : Insert before a given node
➢ DeleteNode( ) : Delete a node with the value equal to x from the list.
Delete does not require moving the other elements
➢ DeleteAfter() : Delete after a given node
➢ MakeEmpty(): Delete all nodes in linked list.
After this operation, linked list will be empty
➢ Successor(x) : Return the next node of x
The last node has no successor
➢ Predecessor(x) : Return the previous node of x
The first node has no predecessor
• In a linked stack, every node has two parts-one that stores data and another that stores the
address of the next node.
• The START pointer of the linked list is used as TOP.
• All insertions and deletions are done at the node pointed by the TOP.
• If TOP=NULL, then it indicates that the stack is empty.
1. Push operation
✓ The push operation is used to insert an element. The new element is added at the topmost
position.
✓ Allocate memory for a new node; store the value in its data part.
✓To insert an element, first check TOP=NULL. If so, allocate memory for a
new node, store the value in its data part and NULL in its next part
SMPC 19 DATA STRUCTURES Module 2
Algorithm
Step 1: Allocate memory for the new node and name it as New_Node
Step 2: SET New_Node->DATA=VAL
Step 3: If TOP=NULL, then
SET New_Node->NEXT=NULL
Set TOP= New_Node
Else
SET New_Node->NEXT =TOP
SET TOP= New_Node
Step 4: end
2. Pop operation
PTR
Algorithm
Step 1: If TOP=NULL, then print “UNDERFLOW” go to step 5
Step 2: SET PTR=TOP
Step 3: SET TOP=TOP->NEXT
Step 4: FREE PTR
Step 5: Exit
LinkedStack ADT
AbstractDataType LinkedStack
SMPC 20 DATA STRUCTURES Module 2
{
Instances
Linear list of elements;
Insertion, deletion take place at one end called top;
}
{
Operations
IsEmpty : Return true if linked stack is empty (underflow), return false otherwise.
If the linked stack is initially empty, START is set to NULL.
boolean data type
• In a linked queue, every node has two parts-one that stores data and another that stores
the address of the next node.
• The START pointer of the linked list is used as FRONT.
• REAR store the address of last element in queue
• All insertions will be done at REAR end and all deletions will be done at FRONT
end.
• If FRONT=REAR=NULL, then the queue is empty.
1. Insert operation
PTR
Algorithm
Step 1: Allocate memory for the new node and name it as NEWNODE
Step 2: SET NEWNODE ->DATA=VAL
Step 3: if FRONT=NULL, then
Set FRONT=REAR= NEWNODE
Set FRONT->NEXT=REAR->NEXT=NULL
Else
Set REAR->NEXT= NEWNODE
Set REAR= NEWNODE
Set REAR->NEXT=NULL
Step 4: end
2. Delete operation
PTR
Algorithm
Step 1: if FRONT=NULL, then print “underflow” go to step 5
Step 2: SET PTR=FRONT
Step 3: FRONT=FRONT->NEXT
Step 4: FREE PTR
Step 5: Exit
SMPC 22 DATA STRUCTURES Module 2
LinkedQueue ADT
AbstractDataType LinkedQueue
{
Instances
Linear list of elements;
Insertion take place at one end called rear;
Deletion take place at other end called front;
}
{
Operations
IsEmpty : Return true if linked queue is empty (underflow), return false otherwise.
boolean data type
✓ The last node contains a pointer to the first node of the list.
✓ we can traverse the list in any direction, forward or backward
✓ A circular linked list has no beginning and no ending.
✓ There are Circular singly linked list and Circular doubly linked list.
✓ Circular list is shown below:-
✓ Application
1) widely used in the operating systems for task maintenance
2) Used to maintain the sequence of the web pages visited:
Traversing this circular linked list either in forward or backward direction helps to revisit
the pages again using back and forward buttons.
From the figure, the START pointer will point the first node in the list. Here the data is H and
NEXT pointing location is 4. Finally, P element point NEXT field to 1.
• The PREV field is used to store the address of the previous (preceding) node. This helps to
traverse backward direction.
• The PREV field of the first node and the NEXT field of the last node will contain NULL or X
or -1.
• Advantage
1) It makes searching twice as efficient
2) Easy to manipulate the elements of the list, because it maintains pointers to nodes
in both direction
• Disadvantage
Requires more space per node and more expensive basic operations
SMPC 24 DATA STRUCTURES Module 2
Memory representation of Doubly linked list
The figure shows memory representation of a circular linked list
Here START=1, so first data is stored at address 1, which is H. This is the first node and its
previous node PREV is -1. We will traverse the list until we reach a position where NEXT
contains -1 or NULL or X.
N
f (x) = a ix i
i=0
SMPC 25 DATA STRUCTURES Module 2