IMPORTANT QUESTIONS FOR LISTS
Q1: Deleting Maximum and Minimum Nodes in a Linked List
Write a deletion function for a singly linked list (with integer data fields) that deletes the nodes
with the maximum and minimum values found currently in the linked list. The function should
delete two nodes on every single call to the delete function.
• Requirements:
1. The delete function for a linked list.
2. Node structure and necessary pointers assumed to be NULL initially.
Q2: Checking for Even Numbers at Even Indices and Odd Numbers at Odd
Indices
Assume that a C++ function check_Even_Odd(int *a) is passed an array filled with integers
whenever it is called. Implement this function to check the following:
1. How many even numbers are present at even indices?
2. How many odd numbers are present at odd indices?
3. How many indices are not fulfilling the above two conditions?
Note: Extra credit for pointer-oriented code.
Q3: Resizing an Array-Based List
Suppose an array-based list of integers exists in memory (i.e., the ArrayList class is already
present). Write a function named resizeList that resizes the list to a new size without losing
any elements. The new size should be passed as a parameter.
• If the new size is smaller than the current size, reduce the list to the new size.
• If the new size is larger than the current size, the original list should be copied to the start
of the new list.
• Return true if the operation is successful and false if the new size is invalid (e.g., less
than zero).
Note: Use of array index is not allowed, only pointer-based solutions will receive credit.
MADE WITH ♥ By Muhammad Taha Nasir
Q4: Sorted Insertion in a Linked List
You are given a sorted linked list of integers. Create a function sortedInsert that inserts a
value into the linked list while keeping the list sorted. The list may be sorted either in ascending
or descending order. Your function must work for both orders.
Requirements:
• A separate function to determine whether the list is in ascending or descending order.
• Ensure that sorting is maintained without re-sorting the entire list after insertion.
Q5: Linked List vs. Array-Based List for Sorted Insertion
Consider the sortedInsert operation mentioned in Question 4, but this time, the operation is to
be performed on an array-based list, and the size of the array is not an issue. Comment on
whether a linked list or an array-based list is better suited for this situation.
• Use an example to show which data structure requires fewer operations.
Q6: Pushing and Popping at Index 0 in an Array-Based Stack
In an array-based implementation of a stack, is it efficient to insert (push) and delete (pop) only
at index 0 of the array? Provide arguments and an example to demonstrate, using a diagram to
show whether it is a good or bad idea.
Q7: Detecting Loops in a Circular Linked List
Write a function hasLoop that determines whether a circular linked list contains a loop. If the
circular linked list contains a loop (not counting the loop from the last node to the head), the
function should return true; otherwise, it should return false.
Q8: Palindrome Check Using Pointers
A palindrome is a word that reads the same backward as forward. Examples include words like
"MADAM", "POP", and "DAD". Write a C++ program to check if a given word, stored in an
array, is a palindrome.
Note: Only pointers can be used for processing the array. No credit will be given for solutions
that use array indexing or additional arrays.
Q9: Generate the First N Terms of a Series
Write a program that generates the first N terms of a series, where every term is generated by
adding the last three terms of the series. The first three initial terms are provided as input to the
program, and the rest of the terms should be computed and displayed.
For example, if the input is 9, 6, 3 and N = 6, the series generated should look like: 9, 6, 3,
18, 27, 48.
Q10: Search Optimization in a Doubly Circular Linked List
Assume a doubly circular linked list (DR) filled with unique integers exists in memory. Write a
C++ program that tells whether a given key can be searched faster using the next pointers or
using the previous pointers.
Q11: Checking if a Singly Linked List is Sorted
Write a C++ program that tells whether a given singly linked list of integers is sorted in
ascending order, descending order, or is unsorted.
Q12: Comparing Arrays for Common Values and Same Positions
Given two arrays a[] and b[], each containing N distinct integers, write a program to determine:
1. How many integers are placed at the same positions with the same values in both arrays.
2. How many elements are common between the two arrays (irrespective of their positions).
For example, if the input arrays are:
• a[] = {2, 45, 57, 53}
• b[] = {52, 45, 5, 57}
MADE WITH ♥ By Muhammad Taha Nasir
The answer would be:
1. 2 elements are in the same position and have the same values.
2. 4 elements are common in both arrays.
Q13: Checking Proximity of a Pointer in a Doubly Linked List
Given a doubly linked list (DLL) with some nodes, and a current pointer pointing to one of
these nodes, write a program that tells whether the current pointer is closer to the end, the head,
or equidistant from both ends of the list.
Q14: Sum of Series with Alternating Signs
Write a program to compute the sum of the following series:
The program should take the value of X and the number of terms N as input and compute the sum
of the series for N terms.
Note: No built-in functions should be used for factorial or power computations.
Q15: Counting Duplicates in a Singly Linked List
Given a singly linked list, write a program that counts how many values are repeated.
Q16: Splitting a Singly Linked List into Two
Write a function SplitList() that splits a singly linked list into two sublists: one for the front
half and one for the back half. If the number of elements is odd, the extra element should go into
the front list.
For example, splitting the list (2, 3, 5, 7, 11) should yield the two lists (2, 3, 5) and (7,
11).
Q17: Reversing a Doubly Linked List
Write a function ReverseList() that reverses a doubly linked list by rearranging all the
previous and next pointers, as well as updating the head pointer. The reversal should require
only one pass through the list.
Q18: Removing Duplicates from a Sorted Doubly Linked List
Write a function RemoveDuplicates() which removes any duplicate nodes from a sorted doubly
linked list. The list should be traversed only once, and you should not have to sort the list
yourself (assume it is already sorted).
Q19: Fixing a Deletion Function for Circular Linked Lists
You are given the following function to delete all nodes in a circular linked list:
void deletelist() {
if(head == NULL) return;
node* temp = head;
do {
temp = temp->next;
delete temp;
} while(temp != head);
head = NULL;
}
Identify the problem in the code, mention it, and then fix it. Write the correct code.
Q20: Deleting a Specific Value from an Array Using Only Pointers
Given an array arr[] containing N integers (with the size stored in SIZE), write a function to
delete all instances of a specified number from the array.
Restrictions:
• Only pointer notations can be used.
• No additional array or data structure can be used.
MADE WITH ♥ By Muhammad Taha Nasir