UNIT 3 C Programming
UNIT 3 C Programming
3.2 LIST ADT it will get appended after the float value object having value '8.56'. And we can
add repeating values to these list-objects.
➢ The list can be defined as an abstract data type in which the elements are stored
3.2.1 Operations on the List Data Structure
in an ordered manner for easier and efficient retrieval of the elements. List Data
Structure allows repetition that means a single piece of data can occur more than Add or Insert Operation:
once in a list. In the Add or Insert operation, a new item (of any data type) is added in the List
➢ In the case of multiple entries of the same data, each entry of that repeating data Data Structure or Sequence object.
is considered as a distinct item or entry. It is very much similar to the array but Replace or reassign Operation:
the major difference between the array and the list data structure is that array In the Replace or reassign operation, the already existing value in the List object
stores only homogenous data in them whereas the list (in some programming is changed or modified. In other words, a new value is added at that particular index of
languages) can store heterogeneous data items in its object. List Data Structure is the already existing value.
also known as a sequence.
Delete or remove Operation:
➢ The list can be called Dynamic size arrays, which means their size increased as
we go on adding data in them and we need not to pre-define a static size for the In the Delete or remove operation, the already present element is deleted or
list removed from the Dictionary or associative array object.
➢ The various operations like student's details, employee's details, or product details
can be implemented using the linked list as the linked list uses the structure data 3.5 DOUBLY LINKED LIST
type that can hold different data types. ➢ Doubly linked list is a complex type of linked list
➢ Using linked list, we can implement stack, queue, tree, and other various data ➢ Here, a node contains a pointer to the previous as well as the next node in the
structures. sequence.
➢ If we want to represent the graph as an adjacency list, then it can be implemented ➢ Therefore, in a doubly-linked list, a node consists of three parts:
as a linked list.
Node data,
➢ A linked list can be used to implement dynamic memory allocation. The dynamic
Pointer to the next node in sequence (next pointer), and
memory allocation is the memory allocation done at the run-time.
Pointer to the previous node (previous pointer).
3.4.8 Operations performed on Linked list
A sample node in a doubly linked list is shown in Fig. 3.5
➢ Insertion - This operation is performed to add an element into the list.
➢ Deletion - It is performed to delete an operation from the list.
➢ Display - It is performed to display the elements of the list.
➢ Search - It is performed to search an element from the list using the given key.
3.5.1 Memory Representation of a doubly linked list Sl. No. Operation Description
➢ Memory Representation of a doubly linked list is shown in Fig. 3.7. Generally, 1. Insertion at beginning Adding the node into the linked list at beginning.
doubly linked list consumes more space for every node and therefore, causes more 2. Insertion at end Adding the node into the linked list to the end.
expansive basic operations such as insertion and deletion. However, we can easily
3. Insertion after Adding the node into the linked list after the
manipulate the elements of the list since the list maintains pointers in both the
specified node specified node.
directions (forward and backward).
➢ In Fig 3.7, the first element of the list that is i.e. 13 stored at address 1. The head 4. Deletion at beginning Removing the node from beginning of the list
pointer points to the starting address 1. Since this is the first element being added 5. Deletion at the end Removing the node from end of the list.
C Programming and Data Structures 3.15 3.16 Linear Data Structures
6. Deletion of the node Removing the node which is present just after the Step 2: SET NEW_NODE = ptr
having given data node containing the given data. Step 3: SET ptr = ptr -> NEXT
7. Searching Comparing each node data with the item to be Step 4: SET NEW_NODE -> DATA = VAL
searched and return the location of the item in the
Step 5: SET NEW_NODE -> PREV = NULL
list if the item found else return null
Step 6: SET NEW_NODE -> NEXT = START
8. Traversing Visiting each node of the list at least once in order
to perform some specific operation like searching, Step 7: SET head -> PREV = NEW_NODE
sorting, display, etc. Step 8: SET head = NEW_NODE
Step 9: EXIT
3.5.2.1 Insertion at beginning
➢ There are two scenarios of inserting any element into doubly linked list. Either the
list is empty or it contains at least one element.
The following steps to be performed for insert a node in doubly linked list at beginning.
➢ Allocate the space for the new node in the memory.
➢ Check whether the list is empty or not. The list is empty if the condition head ==
NULL holds.
➢ In that case, the node will be inserted as the only node of the list and therefore the
prev and the next pointer of the node will point to NULL and the head pointer will
point to this node. Fig. 3.8: Insertion into Doubly Linked List at the beginning
➢ In the second scenario, the condition head == NULL become false and the node
will be inserted in beginning. 3.5.2.2 Insertion at end
➢ The next pointer of the node will point to the existing head pointer of the node. ➢ To insert a node in doubly linked list at the end, make sure whether the list is
empty or it contains any element. Use the following steps in order to insert the
➢ The prev pointer of the existing head will point to the new node being inserted.
node in doubly linked list at the end.
➢ Since, the node being inserted is the first node of the list and therefore it must
➢ Allocate the memory for the new node. Make the pointer ptr point to the new node
contain NULL in its prev pointer.
being inserted.
➢ Hence assign null to its previous part and make the head point to this node.
➢ Check whether the list is empty or not. The list is empty if the condition head ==
Algorithm 3.1 NULL holds.
Step 1: IF ptr = NULL ➢ In that case, the node will be inserted as the only node of the list and therefore the
Write OVERFLOW prev and the next pointer of the node will point to NULL and the head pointer will
point to this node.
Go to Step 9
➢ In the second scenario, the condition head == NULL become false. The new node
[END OF IF] will be inserted as the last node of the list.
C Programming and Data Structures 3.17 3.18 Linear Data Structures
➢ For this reason, we have to traverse the whole list in order to reach the last node 3.5.2.3 Insertion after specified node
of the list. Initialize the pointer temp to head and traverse the list by using this
To insert a node after the specified node in the list, we need to skip the required
pointer.
number of nodes in order to reach the mentioned node and then make the pointer
➢ Make the next pointer of temp point to the new node being inserted i.e. ptr. adjustments as required.
➢ Make the previous pointer of the node ptr point to the existing last node of the list
The following steps are used for this purpose.
i.e. temp.
➢ Allocate the memory for the new node.
➢ Make the next pointer of the node ptr point to the null as it will be the new last
➢ Traverse the list by using the pointer temp to skip the required number of nodes
node of the list.
in order to reach the specified node.
Algorithm 3.2 ➢ The temp would point to the specified node at the end of the for loop. The new
Step 1: IF PTR = NULL node needs to be inserted after this node. Make the next pointer of ptr point to the
Write OVERFLOW next node of temp.
Go to Step 11 ➢ Make the prev of the new node ptr point to temp.
[END OF IF] ➢ Make the next pointer of temp point to the new node ptr.
Step 2: SET NEW_NODE = PTR ➢ Make the previous pointer of the next node of temp point to the new node.
Step 3: SET PTR = PTR -> NEXT Algorithm 3.3
Step 4: SET NEW_NODE -> DATA = VAL Step 1: IF PTR = NULL
Step 5: SET NEW_NODE -> NEXT = NULL Write OVERFLOW
Step 6: SET TEMP = START Go to Step 15
Step 7: Repeat Step 8 while TEMP -> NEXT != NULL [END OF IF]
Step 8: SET TEMP = TEMP -> NEXT Step 2: SET NEW_NODE = PTR
[END OF LOOP] Step 3: SET PTR = PTR -> NEXT
Step 9: SET TEMP -> NEXT = NEW_NODE Step 4: SET NEW_NODE -> DATA = VAL
Step 10C: SET NEW_NODE -> PREV = TEMP
Step 5: SET TEMP = START
Step 11: EXIT
Step 6: SET I = 0
Step 7: REPEAT 8 to 10 until I
Step 8: SET TEMP = TEMP -> NEXT
STEP 9: IF TEMP = NULL
STEP 10: WRITE "LESS THAN DESIRED NO. OF ELEMENTS"
GOTO STEP 15
Fig. 3.9: Insertion into Doubly Linked List at the end
C Programming and Data Structures 3.19 3.20 Linear Data Structures
[END OF IF]
[END OF LOOP]
3.5.2.6 Deletion of the node having given data Fig. 3.13: Deletion of a specified node in Doubly Linked List at the end
➢ Copy the head pointer into a temporary pointer temp.
3.5.2.7 Searching for a specific node
➢ Traverse the list until we find the desired data value.
To search for a specific element in the list, traverse the list in order. The
➢ Check if this is the last node of the list. If it is so then we can't perform deletion.
following operations to be performed in order to search a specific operation.
➢ Check if the node which is to be deleted, is the last node of the list, if it so then
➢ Copy head pointer into a temporary pointer variable ptr
we have to make the next pointer of this node point to null so that it can be the
➢ Traverse the list until the pointer ptr becomes null. Keep shifting pointer to its
new last node of the list.
next and increasing i by +1.
➢ Otherwise, make the pointer ptr point to the node which is to be deleted.
➢ Compare each element of the list with the item which is to be searched.
➢ Make the next of temp point to the next of ptr.
➢ If the item matched with any node value then the location of that value I will be
➢ Make the previous of next node of ptr point to temp. free the ptr.
returned from the function else NULL is returned.
Algorithm 3.6
Algorithm 3.7
Step 1: IF HEAD = NULL
Step 1: IF HEAD == NULL
Write UNDERFLOW
WRITE "UNDERFLOW"
Go to Step 9
GOTO STEP 8
[END OF IF]
[END OF IF]
Step 2: SET TEMP = HEAD
C Programming and Data Structures 3.23 3.24 Linear Data Structures
Step 2: Set PTR = HEAD ➢ As with a singly linked list, it is the easiest data structure to implement.
Step 3: Set i = 0 ➢ The traversal of this doubly linked list is bidirectional which is not possible in a
singly linked list.
Step 4: Repeat step 5 to 7 while PTR != NULL ➢ Deletion of nodes is easy as compared to a Singly Linked List.
Step 5: IF PTR → data = item 3.5.4 Disadvantages of Doubly Linked Lists
return i ➢ It uses extra memory when compared to the array and singly linked list.
[END OF IF] ➢ Since elements in memory are stored randomly, therefore the elements are
accessed sequentially no direct access is allowed.
Step 6: i = i + 1
Step 7: PTR = PTR → next 3.6 CIRCULAR LINKED LIST
Step 8: Exit ➢ In a circular singly linked list, the last node of the list contains a pointer to the first
node of the list.
3.5.2.8 Traversing in doubly linked list
➢ We can have circular singly linked list as well as circular doubly linked list.
Traversing is the most common operation in case of each data structure. For this purpose,
➢ We traverse a circular singly linked list until we reach the same node where we
➢ Copy the head pointer in any of the temporary pointer ptr. started.
➢ Traverse through the list by using while loop. ➢ The circular singly liked list has no beginning and no ending.
➢ Keep shifting value of pointer variable ptr until we find the last node. ➢ There is no null value present in the next part of any of the nodes.
➢ The last node contains null in its next part. ➢ Circular linked list are mostly used in task maintenance in operating systems. Fig.
Algorithm 3.8 3.14 shows a circular singly linked list.
Step 1: IF HEAD == NULL
WRITE "UNDERFLOW"
GOTO STEP 6
[END OF IF]
Step 2: Set PTR = HEAD
Step 3: Repeat step 4 and 5 while PTR != NULL
Step 4: Write PTR → data Fig. 3.14: Circular Singly Linked List.
Step 5: PTR = PTR → next 3.6.1 Memory Representation of circular linked list
Step 6: Exit ➢ Fig 3.15 shows the memory representation of a circular linked list containing
marks of a student in 4 subjects. However, the image shows a glimpse of how the
3.5.3 Advantages of Doubly Linked Lists circular list is being stored in the memory. The start or head of the list is pointing
➢ Reversing the doubly linked list is very easy. to the element with the index 1 and containing 13 marks in the data part and 4 in
➢ It can allocate or reallocate memory easily during its execution.
C Programming and Data Structures 3.25 3.26 Linear Data Structures
the next part. Which means that it is linked with the node that is being stored at Traversing Visiting each element of the list at least once in
5.
4th index of the list. order to perform some specific operation.
➢ However, due to the fact that we are considering circular linked list in the memory
therefore the last node of the list contains the address of the first node of the list. 3.6.2.1 Insertion at the beginning
➢ There are two scenario in which a node can be inserted in circular singly linked
list at beginning. Either the node will be inserted in an empty list or the node is to
be inserted in an already filled list.
➢ Firstly, allocate the memory space for the new node by using the malloc method
of C language.
➢ In the first scenario, the condition head == NULL will be true. Since, the list in
which, we are inserting the node is a circular singly linked list, therefore the only
node of the list (which is just inserted into the list) will point to itself only.
➢ Also need to make the head pointer point to this node.
➢ In the second scenario, the condition head == NULL will become false which
means that the list contains at least one node.
➢ In this case, traverse the list in order to reach the last node of the list.
➢ At the end of the loop, the pointer temp would point to the last node of the list.
➢ Make the next pointer of the last node point to the head node of the list and the
Fig. 3.15: Memory Representation of Circular Linked List new node which is being inserted into the list will be the new head node of the list
➢ Therefore the next pointer of temp will point to the new node ptr.
3.6.2 Operations on Circular Singly linked list
Algorithm 3.9
Table 3.2 describes all the operations performed on a Doubly Linked List
Step 1: IF PTR = NULL
Table 3.2: Operations on Doubly Linked List
Write OVERFLOW
Sl. No. Operation Description
Go to Step 11
Insertion at beginning Adding a node into circular singly linked list at
1. [END OF IF]
the beginning.
Insertion at end Adding a node into circular singly linked list at Step 2: SET NEW_NODE = PTR
2.
the end. Step 3: SET PTR = PTR -> NEXT
Deletion at beginning Removing the node from circular singly linked Step 4: SET NEW_NODE -> DATA = VAL
3.
list at the beginning. Step 5: SET TEMP = HEAD
Searching Compare each element of the node with the Step 6: Repeat Step 8 while TEMP -> NEXT != HEAD
4. given item and return the location at which the
Step 7: SET TEMP = TEMP -> NEXT
item is present in the list otherwise return null.
C Programming and Data Structures 3.27 3.28 Linear Data Structures
[END OF LOOP] Go to Step 1
[END OF IF]
Step 8: SET NEW_NODE -> NEXT = HEAD
Step 2: SET NEW_NODE = PTR
Step 9: SET TEMP → NEXT = NEW_NODE
Step 3: SET PTR = PTR -> NEXT
Step 10: SET HEAD = NEW_NODE
Step 11: EXIT Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET NEW_NODE -> NEXT = HEAD
Step 6: SET TEMP = HEAD
Step 7: Repeat Step 8 while TEMP -> NEXT != HEAD
Step 8: SET TEMP = TEMP -> NEXT
[END OF LOOP]
Step 9: SET TEMP -> NEXT = NEW_NODE
Step 10: EXIT
3.6.2.5 Searching
➢ Traversing in circular singly linked list can be done through a loop.
➢ Initialize the temporary pointer variable temp to head pointer and run the while
loop until the next pointer of temp becomes head.
Fig. 3.19: Deletion in a Circular Linked List at the end
Algorithm 3.14
3.6.2.5 Searching
STEP 1: SET PTR = HEAD
➢ Searching in circular singly linked list needs traversing across the list.
STEP 2: IF PTR = NULL
➢ The item which is to be searched in the list is matched with each node data of the
list once. WRITE "EMPTY LIST"
➢ If the match found then the location of that item is returned otherwise -1 is GOTO STEP 8
returned. END OF IF
Algorithm 3.13 STEP 4: REPEAT STEP 5 AND 6 UNTIL PTR → NEXT != HEAD
STEP 1: SET PTR = HEAD STEP 5: PRINT PTR → DATA
STEP 2: Set I = 0 STEP 6: PTR = PTR → NEXT
STEP 3: IF PTR = NULL [END OF LOOP]
WRITE "EMPTY LIST" STEP 7: PRINT PTR→ DATA
GOTO STEP 8 STEP 8: EXIT
END OF IF 3.6.3 Advantages of Circular Linked Lists
STEP 4: IF HEAD → DATA = ITEM It is possible to traverse from the last node back to the first i.e. the head node.
WRITE i+1 RETURN [END OF IF] ➢ The starting node does not matter as we can traverse each and every node despite
STEP 5: REPEAT STEP 5 TO 7 UNTIL PTR->next != head whatever node we keep as the starting node.
STEP 6: if ptr → data = item ➢ The previous node can be easily identified.
write i+1 ➢ There is no need for a NULL function to code. The circular list never identifies a
NULL identifier unless it is fully assigned.
RETURN
➢ Circular linked lists are beneficial for end operations as start and finish coincide
C Programming and Data Structures 3.33 3.34 Linear Data Structures
3.6.4 Disadvantages of Circular Linked Lists the value entered first will be removed last. In the above case, the value 5 is
➢ If the circular linked list is not handled properly then it can lead to an infinite loop entered first, so it will be removed only after the deletion of all the other elements.
as it is circular in nature.
➢ In comparison with singly-linked lists, doubly linked lists are more complex in
nature
➢ Direct accessing of elements is not possible.
➢ It is generally a complex task to reverse a circular linked list
➢ Stack has one end, whereas the Queue has two ends (front and rear). 3.7.2 Operations on Stack
➢ It contains only one pointer top pointer pointing to the topmost element of the The following are some common operations implemented on the stack:
stack. ➢ push(): When we insert an element in a stack then the operation is known as a
➢ Whenever an element is added in the stack, it is added on the top of the stack, and push. If the stack is full then the overflow condition occurs.
the element can be deleted only from the stack. ➢ pop(): When we delete an element from the stack, the operation is known as a pop.
➢ In other words, a stack can be defined as a container in which insertion and If the stack is empty means that no element exists in the stack, this state is known
deletion can be done from the one end known as the top of the stack. as an underflow state.
➢ isEmpty(): It determines whether the stack is empty or not.
3.7.1 Working of Stack
➢ isFull(): It determines whether the stack is full or not.'
➢ Stack works on the LIFO pattern. As we can observe in figure 3.20, there are five
memory blocks in the stack; therefore, the size of the stack is 5. ➢ peek(): It returns the element at the given position.
➢ Suppose we want to store the elements in a stack and let's assume that stack is ➢ count(): It returns the total number of elements available in a stack.
empty. We have taken the stack of size 5 as shown below in which we are pushing ➢ change(): It changes the element at the given position.
the elements one by one until the stack becomes full. ➢ display(): It prints all the elements available in the stack
➢ Since our stack is full as the size of the stack is 5. In the above cases, we can
3.7.2.1 PUSH operation
observe that it goes from the top to the bottom when we were entering the new
element in the stack. The stack gets filled up from the bottom to the top. ➢ Before inserting an element in a stack, we check whether the stack is full.
➢ If we try to insert the element in a stack, and the stack is full, then the overflow
➢ When we perform the delete operation on the stack, there is only one way for entry
condition occurs.
and exit as the other end is closed. It follows the LIFO pattern, which means that
➢ When we initialize a stack, we set the value of top as -1 to check that the stack is
empty.
C Programming and Data Structures 3.35 3.36 Linear Data Structures
➢ When the new element is pushed in a stack, first, the value of the top gets
incremented, i.e., top=top+1, and the element will be placed at the new position
of the top.
➢ The elements will be inserted until we reach the max size of the stack.
}
}
C Programming and Data Structures 3.41 3.42 Linear Data Structures
If there are some nodes in the list already, then add the new element in the if(head==NULL)
beginning of the list (to not violate the property of the stack). For this purpose, {
assign the address of the starting element to the address field of the new node
and make the new node, the starting node of the list. ptr->val = val;
} Adjust the head pointer accordingly: In stack, the elements are popped only
from one end, therefore, the value stored in the head pointer must be deleted
else
and the node must be freed. The next node of the head node now becomes the
{ head node.
printf("Enter the value"); Time Complexity: O (n)
scanf("%d",&val);
C Programming and Data Structures 3.43 3.44 Linear Data Structures
} {
else printf("Printing Stack elements \n");
{ while(ptr!=NULL)
item = head->val; {
ptr = head;
printf("%d\n",ptr->val);
head = head->next;
ptr = ptr->next;
free(ptr);
}
printf("Item popped");
} }
} }
Evaluation of Arithmetic Expression requires two steps: Infix Notation Prefix Notation Postfix Notation
➢ First, convert the given expression into special notation. A*B *AB AB*
➢ Evaluate the expression in this new notation. (A+B)/C /+ ABC AB+C/
Notations for Arithmetic Expression (A*B) + (D-C) +*AB - DC AB*DC-+
There are three notations to represent an arithmetic expression:
➢ Infix Notation Let's take the example of Converting an infix expression into a postfix expression
➢ Prefix Notation
➢ Postfix Notation
For tracing the algorithm let us assume that the input is () (() [()])
Fig. 3.24: Invoking Function Calls
Input Symbol Operation Stack Output ➢ When we invoke function A, which contains a call to function B, then its
( Push ( ( processing will not be completed until function B has completed its execution and
) Pop ( returned. Similarly for function B and C. So we observe that function A will only
be completed after function B is completed and function B will only be completed
Test if ( and A[i] match? YES
after function C is completed.
( Push ( (
C Programming and Data Structures 3.51 3.52 Linear Data Structures
➢ Therefore, function A is first to be started and last to be completed. To conclude, If one can successfully place a queen in the last row, then a solution is found.
the above function activity matches the last in first out behavior and can easily be Now backtrack to find the next solution
handled using Stack.
➢ We can use a stack to indicate the positions of the queens. Importantly, notice that
➢ Consider addrA, addrB, addrC be the addresses of the statements to which control we only have to put the column positions of the queens on the stack. We can
is returned after completing the function A, B, and C, respectively. determine each queen's coordinates given only the stack. We simply combine the
position of an element in the stack (the row) with the value of that element (the
column) for each queen.
➢ Two examples of this are shown below:
If there is no valid position, then one backtracks to the previous row and try ➢ Putting all this into pseudo-code form, we have the following algorithm...
the next position
C Programming and Data Structures 3.53 3.54 Linear Data Structures
Create empty stack and set current position to 0 character of the String is on the Top of the Stack and after performing the pop
Repeat { operation in the Stack, the Stack returns the String in Reverse order.
loop from current position to the last position until valid position found //current
row
if there is a valid position {
push the position to stack, set current position to 0 // move to next row
}
if there is no valid position {
if stack is empty, break // stop search
else pop stack, set current position to next position // backtrack to previous Fig. 3.26: Reverse a String using Stack
row
3.9.5.2 Converting Decimal to Binary
}
➢ Although decimal numbers are used in most business applications, some scientific
if stack has size N { // a solution is found and technical applications require numbers in either binary, octal, or hexadecimal.
pop stack, set current position to next position // backtrack to find next solution A stack can be used to convert a number from decimal to
} binary/octal/hexadecimal form.
} ➢ For converting any decimal number to a binary number, we repeatedly divide the
decimal number by two and push the remainder of each division onto the Stack
3.9.5 Reverse a Data until the number is reduced to 0. Then we pop the whole Stack and the result
➢ To reverse a given set of data, we need to reorder the data so that the first and last obtained is the binary equivalent of the given decimal number.
elements are exchanged, the second and second last element are exchanged, and
Example: 3.9:
so on for all other elements. For example, suppose we have a string “welcome”,
then on reversing it would be “emoclew”. Converting 14 number Decimal to Binary:
➢ There are different reversing applications:
Reversing a string
Converting Decimal to Binary
3.11.4 Implementation of Priority Queue Fig. 3.34 Max heap Fig. 3.35 Min heap
➢ The priority queue can be implemented in four ways that include arrays, linked Max heap: The max heap is a heap in which the value of the parent node is
list, heap data structure and binary search tree. The heap data structure is the most greater than the value of the child nodes.
efficient way of implementing the priority queue, so we will implement the
C Programming and Data Structures 3.63 3.64 Linear Data Structures
Min heap: The min heap is a heap in which the value of the parent node is
less than the value of the child nodes.
3.11.7.1 Inserting the element in a priority queue (max heap) Now, let’s try to insert a new element, 6. Since there are nodes present in the heap, we
If we insert an element in a priority queue, it will move to the empty slot by insert this node at the end of heap so it looks like this:
looking from top to bottom and left to right.
If the element is not in a correct place then it is compared with the parent node; if
it is found out of order, elements are swapped. This process continues until the element
is placed in a correct position.
Algorithm 3.18
START
If(no node):
Create node
Else:
Then, heapify operation is implemented. After which, the heap will look like this:
Insert node at end of heap
Heapify
END
Let us now see with an example how this works:
Example 3.10
Let’s say the elements are 1,4,2,7,8,5. The max-heap of these elements would look like:
C Programming and Data Structures 3.65 3.66 Linear Data Structures
3.11.8 Applications of Priority Queue figure, the queue will look something like following. The value of rear will become 5
while the value of front remains same.
The following are the applications of a Priority Queue:
➢ It is used in Djikstra’s Algorithm – To find the shortest path between nodes in a
graph.
➢ It is used in Prim’s Algorithm – To find the Minimum Spanning Tree in a
weighted undirected graph.
➢ It is used in Heap Sort – To sort the Heap Data Structure
➢ It is used in Huffman Coding – A Data Compression Algorithm
➢ It is used in Operating Systems for:
Fig. 3.36: Array representation of Queue
o Priority Scheduling – Where processes must be scheduled according to their
priority.
o Load Balancing – Where network or application traffic must be balanced
across multiple servers.
o Interrupt Handling – When a current process is interrupted, a handler is
assigned to the same to rectify the situation immediately.
➢ A* Search Algorithm – A graph traversal and a path search algorithm
} }
C Programming and Data Structures 3.75 3.76 Linear Data Structures
REVIEW QUESTIONS 7. What are the pitfall encountered in singly linked list?
➢ The singly linked list has only forward pointer and no backward link is provided.
PART-A
Hence the traversing of the list is possible only in one direction. Backward
1. What do you mean by non-linear data structure? Give example. traversing is not possible.
➢ The non-linear data structure is the kind of data structure in which the data may ➢ Insertion and deletion operations are less efficient because for inserting the
be arranged in hierarchical fashion. For example- Trees and graphs. element at desired position the list needs to be traversed. Similarly, traversing of
the list is required for locating the element which needs to be deleted.
2. List the various operations that can be performed on data structure.
➢ Various operations that can be performed on the data structure are 8. What is Singly Linked List?
➢ A singly linked list is a type of linked list that is unidirectional, that is, it can be
Create
traversed in only one direction from head to the last node (tail).
Insertion of element
9. Define doubly linked list.
Deletion of element ➢ Doubly linked list is a kind of linked list in which each node has two link fields.
Searching for the desired element One link field stores the address of previous node and the other link field stores
Sorting the elements in the data structure the address of the next node.
Reversing the list of elements. 10. Write down the steps to modify a node in linked lists.
3. What is abstract data type o Enter the position of the node which is to be modified.
➢ The abstract datatype is special kind of datatype, whose behavior is defined by o Enter the new value for the node to be modified.
a set of values and set of operations. o Search the corresponding node in the linked list.
4. What is list ADT in data structure? o Replace the original value of that node by a new value.
➢ The list ADT is a collection of elements that have a linear relationship with each o Display the messages as “The node is modified”.
other. A linear relationship means that each element of the list has a unique 11. Difference between arrays and lists.
successor. ➢ In arrays any element can be accessed randomly with the help of index of array,
5. What Are Arrays in Data Structures? whereas in lists any element can be accessed by sequential access only.
➢ An array is a linear data structure that collects elements of the same data type ➢ Insertion and deletion of data is difficult in arrays on the other hand insertion
and stores them in contiguous and adjacent memory locations. Arrays work on and deletion of data is easy in lists.
an index system starting from 0 to (n-1), where n is the size of the array. 12. State the properties of LIST abstract data type with suitable example.
6. What is a linked list? ➢ It is linear data structure in which the elements are arranged adjacent to each
➢ A linked list is a set of nodes where each node has two fields ‘data’ and ‘link’. other.
The data field is used to store actual piece of information and link field is used ➢ It allows to store single variable polynomial.
to store address of next node. ➢ If the LIST is implemented using dynamic memory, then it is called linked list.
Example of LIST are- stacks, queues, linked list.
C Programming and Data Structures 3.79 3.80 Linear Data Structures
13. State the advantages of circular lists over doubly linked list. ➢ Understandability: ADT specifies what is to be done and does not specify the
➢ In circular list the next pointer of last node points to head node, whereas in implementation details. Hence code becomes easy to understand due to ADT.
doubly linked list each node has two pointers: one previous pointer and another ➢ Reusability: the ADT can be reused by some program in future.
is next pointer. 20. What is queue ADT?
➢ The main advantage of circular list over doubly linked list is that with the help ➢ Queue is an abstract data structure, somewhat like Stacks. Unlike stacks, a queue
of single pointer field we can access head node quickly. Hence some amount of is open at both its ends. One end is always used to insert data (enqueue) and the
memory get saved because in circular list only one pointer is reserved. other is used to remove data (dequeue). Queue follows First-In-First-Out
14. What are the advantages of doubly linked list over singly linked list? methodology, i.e., the data item stored first will be accessed first.
➢ The doubly linked list has two pointer fields. One field is previous link field, and 21. What is priority queue
another is next link field. Because of these two pointer fields we can access any ➢ A priority queue is a special type of queue in which each element is associated
node efficiently whereas in singly linked list only one pointer field is there which with a priority value. Elements are served on the basis of their priority. That is,
stores forward pointer. higher priority elements are served first. However, if elements with the same
15. Why is the linked list used for polynomial arithmetic? priority occur, they are served according to their order in the queue.
➢ We can have separate coefficient and exponent fields for representing each term 22. What is stack?
of polynomial. Hence there is no limit for exponent. We can have any number ➢ Stack is an abstract data type that serves as a collection of elements, with two
as an exponent. main operations: Push, which adds an element to the collection, and Pop, which
16. What is the advantage of linked list over arrays? removes the most recently added element that was not yet removed.
➢ The linked list makes use of the dynamic memory allocation. Hence the user can 23. How is Stack represented in Data Structure?
allocate or de allocate the memory as per his requirements. On the other hand, ➢ A stack may be represented in the memory in various ways. There are two main
the array makes use of the static memory location. Hence there are chances of ways: using a one-dimensional array and a single linked list.
wastage of the memory or shortage of memory for allocation. 24. List some applications of queue data structure.
17. What is the circular linked list? ➢ Managing requests on a single shared resource such as CPU scheduling and disk
➢ The circular linked list is a kind of linked list in which the last node is connected scheduling.
to the first node or head node of the linked list. ➢ Handling hardware or real-time systems interrupts.
18. What is the basic purpose of header of the linked list? ➢ Handling website traffic.
➢ The header node is the very first node of the linked list. Sometimes a dummy ➢ Routers and switches in networking.
value such - 999 is stored in the data field of header node. ➢ Maintaining the playlist in media players.
➢ This node is useful for getting the starting address of the linked list. 25. List some applications of stack data structure.
19. What is the advantage of an ADT? ➢ A Stack can be used for evaluating expressions consisting of operands and
operators.
➢ Change: the implementation of the ADT can be changed without making
changes in the client program that uses the ADT. ➢ Stacks can be used for Backtracking, i.e., to check parenthesis matching in an
expression.
C Programming and Data Structures 3.81
➢ It can also be used to convert one form of expression to another form. It can be
used for systematic Memory Management.
PART B