Understanding the Pakistan Budgeting Process: Basics and Key Insights
VCE Unit 02 (1).pptx
1. CCC Digital India Pvt Ltd. 2nd Floor, Quantum Hub, Siripuram Jn, Visakapatnam-530003
UNIT-2
Linked lists: Introduction, linked lists and types, Representation of linked lists, Operations on linked list, Comparison of liked
lists with arrays and Dynamic arrays .
DATA STRUCTURES
•Data structure is a process of storing and organizing the data in different ways
•Data structures can be used to solve many real time problems
•Each data structure is useful in different situations
TYPES OF DATA STRUCTURES
4. PRE REQUISITE FOR DATA STRUCTURES:
To do operations on data structures, everyone should have good skills over the following concepts
•Pointers
•Structures
•Dynamic memory allocation
•Recursion
LINKED LIST
•It is a linear data structure with collection of similar elements
•Each element in a linked list is known as node
•In this it consist set of nodes
•Each node contains a relation with next node using node’s address
•Compared to arrays, linked list are used when number of elements are unknown
•Compared to arrays memory is efficiently used in linked list because of dynamic nature
5. TYPES OF LINKED LIST
Types of linked list are
•Single linked list
•Double linked list
•Circular single linked list
•Circular double linked list
APPLICATIONS OF LINKED LIST
•To implement other data structures like stack, queue, trees and graphs
•To perform arithmetic operations on long integers
•To perform arithmetic operations on polynomials
•To maintain directory of names
•To represent sparse matrices etc.
6. SINGLE LINKED LIST
•In this each node consist two fields. They are data field and address field. They are
i) Data field: Consist value to be stored
ii) Address field: Consist address of the next node
•In this we can traverse only in forward direction
•A head/start pointer consist address of the first node
•Initially head==NULL
•Every node address field consist address of its next node
•Last node address is NULL to indicate end of the list
•NULL is a pre-defined macro. It’s value is 0
•printf(“null value=%d”,NULL); //null value=0
8. SINGLE LINKED LIST DECLARATION:
struct node
{
int data;
struct node *next;
};
struct node *newnode, *head=NULL;
newnode=(struct node*)malloc(sizeof(struct node));
OPERATIONS ON LINKED LIST:
•Inserting at the beginning of the list
•Inserting at the end of the list
•Inserting at the particular position of the list
•Deleting from the beginning
•Deleting from the end
•Deleting from the specified location
•Displaying list of values
•Counting number of nodes
•Printing list in reverse order
•Finding cycle in the list
•Finding merge point of the two lists etc.
9. INSERT AT BEGINNING OF SINGLE LINKED LIST ALGORITHM:
INSERT AT BEGINNING OF SINGLE LINKED LIST ALGORITHM
10. STEP-1
Allocate memory to newnode, store x in data field, NULL in address field
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=x;
newnode->next=NULL;
STEP-2
If there are no nodes in the linked list ,then assign newnode address to head
if(head==NULL) //case 1: there are no nodes in LL
head=newnode;
STEP-3
Else, first store address of newnode in the next field then make newnode as first node
by storing its address in head
else // case 2: there are more nodes in LL
{
newnode->next=head;
head=newnode;
}
12. STEP-1
Allocate memory to newnode, store x in data field, NULL in address field
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=x;
newnode->next=NULL;
STEP-2
If there are no nodes in the linked list ,then assign newnode address to head
if(head==NULL) //case 1: there are no nodes in LL
head=newnode;
STEP-3
Else, create a pointer P, store head address in it.
struct node *p=head;
STEP-4
Now move P to the end of the linked list
while(p->next!=NULL)//while(p->next)
p=p->next;
STEP-5
Now store newnode address in P next field in place of NULL
p->next=newnode;
14. STEP-1
Allocate memory to newnode, store x in data field, NULL in address field
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=x;
newnode->next=NULL;
STEP-2
If there are no nodes in the linked list ,then assign newnode address to head
if(head==NULL) //case 1: there are no nodes in LL
head=newnode;
STEP-3
If position==1 insert node at the beginning
newnode->next=head;
head=newnode;
STEP-4 else,
STEP-5
Now store newnode address in P next field in place of NULL
p->next=newnode;
15. DISPLAYING SINGLE LINKED LIST ALGORITHM
STEP-1
Check whether list is EMPTY or not with condition
(head==NULL)
STEP-2
If list is EMPTY, then display "List is empty, cannot display element”, and terminate
the function
STEP-3
If list is NOT EMPTY, then define a pointer called temp, and initialize it with head i.e.,
temp=head
STEP-4 Display the node data with print(tempdata)
STEP-5
Now move temp to the next node using address
temp=tempnext, until it reaches end of list i.e., while(temp!=NULL)
18. We can use the following steps to delete a node from beginning of the single linked list...
•Step 1 - Check whether list is Empty (head == NULL)
•Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
•Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
•Step 4 - Check whether list is having only one node (temp → next == NULL)
•Step 5 - If it is TRUE then set head = NULL and delete temp (Setting Empty list conditions)
•Step 6 - If it is FALSE then set head = temp → next, and delete temp.
Deleting from End of the list
We can use the following steps to delete a node from end of the single linked list...
19. •Step 1 - Check whether list is Empty (head == NULL)
•Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
•Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize 'temp1' with head.
•Step 4 - Check whether list has only one Node (temp1 → next == NULL)
•Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate the function.
•(Setting Empty list condition)
•Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node.
•Repeat the same until it reaches to the last node in the list. (until temp1 → next == NULL)
•Step 7 - Finally, Set temp2 → next = NULL and delete temp1.
Delete a Particular Node
20. The steps below can be used to remove a specific node from the single linked list:
Step 1: Check to see if the list is empty (head == NULL).
Step 2: If the list is empty, display the message ‘List is Empty!!!’ Deletion is not possible’, and the function is terminated.
Step 3: If it’s not empty, create two Node pointers, ‘temp1’ and ‘temp2,’ and set ‘temp1’ to head.
Step 4: Continue dragging the temp1 until it approaches the last node or the particular node to be deleted.
And before moving ‘temp1’ to the next node, always set ‘temp2 = temp1’.
Step 5: Once you’re at the last node, the message “Given node not found in the list! Deletion Not Possible” appears.
Finally, the function is terminated.
Step 6: If you’ve reached the particular node which you want to delete, check to see if the list has only one node.
Step 7: Set head = NULL and remove temp1 (free(temp1)) if list contains just one node and that is the node to be deleted.
Step 8: Check if temp1 is the first node in the list (temp1 == head) if the list has several nodes.
Step 9: If temp1 is the initial node, remove temp1 and shift the head to the next node (head = head→next).
Step 10: If temp1 isn’t the first node in the list (temp1→next == NULL), see if it’s the final one.
Step 11: Set temp2→next = NULL and remove temp1 (free(temp1)) if temp1 is the final node.
Step 12: Set temp2→next = temp1 →next and remove temp1 (free(temp1)) if temp1 is not the first or last node.
22. DYNAMIC ARRAY Vs LIKED LIST
Compared to linked lists, dynamic arrays have faster indexing (constant time versus linear time) and typically
faster iteration due to improved locality of reference; however, dynamic arrays require linear time to insert or delete
at an arbitrary location, since all following elements must be moved, while linked lists can do this in constant time.
Linked lists do not require the whole data structure to be stored contiguously whereas it may be expensive or
impossible to find contiguous space for a large dynamic array
Dynamic arrays are really contiguous memory slots allocated dynamically to a variable.
With a reference/pointer to the first memory address in the allocated contiguous memory blocks, you have direct
access to each slot - Array indexes are "added" to the address and the values stored at the resulting
address is returned, making it constant-time access. Nodes within a linked list is accessed sequentially.
To get to a node, you need to run through every node prior to it within the list.
CIRCULAR LINKED LIST
•In CLL the last node does not consist NULL pointer
•Last node consist address of the first node
23. CLL ADVANTAGES
•Any node can be a starting point
•We don’t need to maintain two pointers for front and rear if we use circular linked list.
We can maintain a pointer to the last inserted node and front can always be obtained as next of last
•Circular lists are useful in applications to repeatedly go around the list.
CLL REPRESENTATION
24. •In CLL take a pointer tail instead of head for easy access
struct Node
{
int data;
struct Node *next;
}*tail=NULL, *temp;
INSERT AT BEGINNING OF CIRCULAR LL ALGORITHM
25. STEP-1
Create a newnode, store value add NULL address
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data=value;
newNode->next=NULL;
STEP-2
Check whether list is EMPTY or not with condition
(tail==NULL)
STEP-3
If list is EMPTY, then assign newnode address to tail and tail->next
tail=newnode
tail->next=newnode
STEP-4
If list is NOT EMPTY, then in the newnode->next store tail->next address and make
newnode as first node with tail
newNode->next=tail->next;
tail->next=newNode;
STEP-5 Display value is inserted in Circular LL
27. STEP-1
Create a newnode, store value add NULL address
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data=value;
newNode->next=NULL;
STEP-2
Check whether list is EMPTY or not with condition
(tail==NULL)
STEP-3
If list is EMPTY, then assign newnode address to tail and tail->next
tail=newnode
tail->next=newnode
STEP-4
If list is NOT EMPTY, then in the newnode->next store tail->next address and make
newnode as last node with tail
newNode->next=tail->next;
tail->next=newNode;
tail=newNode;
STEP-5 Display value is inserted in Circular LL
28. INSERT AT SPECIFIC LOCATION OF CIRCULAR LL ALGORITHM
These methods are used to put data into a circular linked list at a specified point.
•Terminate the function if the circular linked list is empty.
•If not, create two node pointers.
•Create a new node to store the data. Now, navigate to the desired location and place this new node there.
•Connect the pointer of this new node to the node that was previously existing at this location.
•To observe the outcome, print the new circular linked list.
29. Deletion of the First Node
The instructions below can be used to
remove a node from the circular linked
list’s beginning:
Step 1: Check for Overflow
if start = Null then
print list is empty
Exit
End if
Step 2: set ptr = start
Step 3: set start = start -> next
Step 4: print Element deleted is ,ptr ->
info
Step 5: set last -> next = start
Step 6: free ptr
Step 7: EXIT
31. The instructions below can be used to remove a node from the circular linked list’s end:
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Steps 4 and 5 while PTR NEXT != START
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR NEXT [END OF LOOP]
Step 6: SET PREPTR NEXT = START
Step 7: FREE PTR
Step 8: EXIT
START is used to initialize the pointer variable PTR, i.e., PTR now points to the linked list’s first node.
We use another pointer variable PREPTR in the while loop so that PREPTR always refers to one node before PTR.
We set the next pointer of the second last node to START after we reach the last node and the second last node,
making it the (new) final node of the linked list.
32. Deletion from Specific Point
Deletion from the nth place in a circular linked list is one of the deletion operations that we commonly execute on this
type of list. We utilize pointers to traverse the circular linked list and conduct deletion from a specified position.
These instructions are used to delete a specified place in a circular linked list:
•Initialize the p and q node pointers.
•Create a variable called k that will serve as a counter variable.
•Set the value of del to pos-1.
•Repeat step 5 until k does not equal del.
•Make q=p and p=p->next in this loop.
•After each successful repetition, increase the value of k by one.
•Make the next of q equal to the next of p.
•Delete the node referred to by the node pointer p.
33. DOUBLE LINKED LIST
•In SLL it is possible to move only in the forward direction
•You cannot move back in SLL because every node consist only address of next node
•But in DLL we can move in the forward and backward directions
•In DLL every node consist 3 three fields. They are
i) Data field: stores value
ii) Previous pointer field: stores address of previous node
iii) Next pointer field: stores address of next node
•First node previous field consist NULL value
•Last node next field consist NULL value
36. STEP-1
Create a newnode, store value in data field and NULL in previous field of newnode
newnode -> data = value;
newnode -> previous = NULL;
STEP-2
If head==NULL, make the newnode as first node with assigning NULL in next field, and
make head as newnode
newnode -> next = NULL;
head = newnode;
STEP-3
Else store head address in next field of newnode
And make it as first node
newnode -> next = head;
head->prev=newnode;
head = newnode;
STEP-4 Display node is inserted
38. STEP-1
Create a newnode, store value in data field and NULL in next field of newnode
newNode -> data = value;
newNode ->next = NULL;
STEP-2
If head==NULL, make the newnode as last node with assigning NULL in previous field, and make head as newnode
newNode -> previous = NULL;
head = newNode;
STEP-3
Else create a temp node, and assign head address in it. Move temp node to the last node. Then store temp address in
previous and next fields
struct Node *temp = head;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = newNode;
newNode -> previous = temp;
STEP-4 Display node is inserted
39. Deletion
In a double linked list, the deletion operation can be performed in three
ways as follows...
•Deleting from Beginning of the list
•Deleting from End of the list
•Deleting a Specific Node
Deleting from Beginning of the list
40. We can use the following steps to delete a node from beginning of the double linked list...
•Step 1 - Check whether list is Empty (head == NULL)
•Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function
.
•Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head.
•Step 4 - Check whether list is having only one node (temp → previous is equal to temp → next)
•Step 5 - If it is TRUE, then set head to NULL and delete temp (Setting Empty list conditions)
•Step 6 - If it is FALSE, then assign temp → next to head, NULL to head → previous and delete temp.
42. We can use the following steps to delete a node from end of the double linked list...
•Step 1 - Check whether list is Empty (head == NULL)
•Step 2 - If it is Empty, then display 'List is Empty!!! Deletion is not possible' and terminate
the function.
•Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head.
•Step 4 - Check whether list has only one Node (temp → previous and temp → next both
are NULL)
•Step 5 - If it is TRUE, then assign NULL to head and delete temp. And terminate from the
function. (Setting Empty list condition)
•Step 6 - If it is FALSE, then keep moving temp until it reaches to the last node in the list.
(until temp → next is equal to NULL)
•Step 7 - Assign NULL to temp → previous → next and delete temp.
44. We can use the following steps to delete a specific node from the double linked list...
•Step 1 - Check whether list is Empty (head == NULL)
•Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
•Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize with head.
•Step 4 - Keep moving the temp until it reaches to the exact node to be deleted or to the last node.
•Step 5 - If it is reached to the last node, then display 'Given node not found in the list! Deletion not
possible!!!' and terminate the function.
•Step 6 - If it is reached to the exact node which we want to delete, then check whether list is having only one node
or not
•Step 7 - If list has only one node and that is the node which is to be deleted then set head to NULL and
delete temp (free(temp)).
•Step 8 - If list contains multiple nodes, then check whether temp is the first node in the list (temp == head).
•Step 9 - If temp is the first node, then move the head to the next node (head = head → next),
set head of previous to NULL (head → previous = NULL) and delete temp.
45. •Step 10 - If temp is not the first node, then check whether it is the last node in the list
(temp → next == NULL).
•Step 11 - If temp is the last node then set temp of previous of next to NULL (temp →
previous → next = NULL) and delete temp (free(temp)).
•Step 12 - If temp is not the first node and not the last node, then
set temp of previous of next to temp of next (temp → previous → next = temp →
next), temp of next of previous to temp of previous (temp → next → previous = temp
→ previous) and delete temp (free(temp)).
46. We can use the following steps to display the elements of a double linked list...
•Step 1 - Check whether list is Empty (head == NULL)
•Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the
function.
•Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize
with head.
•Step 4 - Display 'NULL <--- '.
•Step 5 - Keep displaying temp → data with an arrow (<===>)
until temp reaches to the last node
•Step 6 - Finally, display temp → data with arrow pointing to NULL (temp →
data ---> NULL).
Displaying a Double Linked List