0% found this document useful (0 votes)
3 views4 pages

3

The document explains linked lists, including their definition, types, and an algorithm for inserting an item into a singly linked list. It also covers linear arrays, calculating the number of elements in arrays A, B, and C, and finding specific addresses based on given parameters. Additionally, the document defines searching, presents the binary search algorithm with an example, and compares linear search and binary search in terms of their characteristics and performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

3

The document explains linked lists, including their definition, types, and an algorithm for inserting an item into a singly linked list. It also covers linear arrays, calculating the number of elements in arrays A, B, and C, and finding specific addresses based on given parameters. Additionally, the document defines searching, presents the binary search algorithm with an example, and compares linear search and binary search in terms of their characteristics and performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Detailed Answers: Linked List, Arrays, and Searching

3 (a) What is a linked list? Write an algorithm that inserts an ITEM into a linked list. (1 + 4 =
5)

Definition of Linked List:

A linked list is a linear data structure in which elements are stored in nodes.
Each node consists of two parts:
1. Data part – stores the actual value.
2. Link (pointer) part – stores the address of the next node.

Unlike arrays, linked lists do not store elements in contiguous memory locations.
Instead, each node points to the next node in the sequence.
The first node is called the head node, and the last node contains NULL in its link part.

Types of linked lists include:


• Singly Linked List
• Doubly Linked List
• Circular Linked List

Algorithm to Insert an ITEM into a Singly Linked List (at the end):

Algorithm INSERT_END(START, ITEM)

Input: START (pointer to first node), ITEM (value to insert)


Output: Updated linked list with ITEM inserted at the end

Step 1: Create a new node NEW.


Step 2: Assign NEW → DATA = ITEM.
Step 3: Assign NEW → LINK = NULL.
Step 4: If START = NULL then
START = NEW
Go to Step 8
Step 5: Else
Set PTR = START
Step 6: While PTR → LINK ≠ NULL do
PTR = PTR → LINK
Step 7: PTR → LINK = NEW
Step 8: Stop

Explanation:
If the list is empty, the new node becomes the first node.
Otherwise, traverse the list until the last node and attach the new node there.

---------------------------------------------------------------------

3 (b) Consider the linear array A (10:40), B (20) and C (-15:30). (2 + 3 = 5)


(i) Find the number of elements in each array.

Formula:
Number of elements = Upper bound − Lower bound + 1

Array A (10:40)
Number of elements = 40 − 10 + 1 = 31

Array B (20)
This represents a single element.
Number of elements = 1

Array C (-15:30)
Number of elements = 30 − (-15) + 1
= 30 + 15 + 1
= 46

So,
A has 31 elements
B has 1 element
C has 46 elements

(ii) Suppose Base(A) = 1000 and W = 2 words per memory cell for A.
Find the address of A[15], A[25], and A[40].

Formula for address calculation:

Address(A[i]) = Base(A) + (i − LB) × W

Where:
Base(A) = 1000
LB (Lower bound) = 10
W=2

Address of A[15]:
= 1000 + (15 − 10) × 2
= 1000 + (5 × 2)
= 1000 + 10
= 1010

Address of A[25]:
= 1000 + (25 − 10) × 2
= 1000 + (15 × 2)
= 1000 + 30
= 1030

Address of A[40]:
= 1000 + (40 − 10) × 2
= 1000 + (30 × 2)
= 1000 + 60
= 1060

---------------------------------------------------------------------

3 (c) What is searching? Write an algorithm for binary search with suitable example. (6)

Definition of Searching:

Searching is the process of finding the location of a particular element (key) in a data
structure.
If the element is found, its position is returned; otherwise, the search reports failure.

Binary Search:

Binary search is an efficient searching technique that works only on sorted arrays.
It repeatedly divides the search interval into two halves.

Algorithm BINARY_SEARCH(A, LB, UB, ITEM)

Input: Sorted array A, lower bound LB, upper bound UB, ITEM to search
Output: Position of ITEM or message “Not Found”

Step 1: Set BEG = LB


Step 2: Set END = UB
Step 3: While BEG ≤ END do
Step 4: MID = (BEG + END) / 2
Step 5: If A[MID] = ITEM then
Print “ITEM found at position MID”
Stop
Step 6: If ITEM < A[MID] then
END = MID − 1
Else
BEG = MID + 1
Step 7: End While
Step 8: Print “ITEM Not Found”
Step 9: Stop

Example:

Let A = [10, 20, 30, 40, 50]


Search ITEM = 40

Step 1: BEG = 1, END = 5


Step 2: MID = (1+5)/2 = 3 → A[3] = 30
Since 40 > 30, set BEG = 4

Step 3: MID = (4+5)/2 = 4 → A[4] = 40


ITEM found at position 4.

---------------------------------------------------------------------

3 (d) Difference between Linear Search and Binary Search. (4)

Linear Search:
• Works on both sorted and unsorted data.
• Checks each element sequentially from beginning to end.
• Time Complexity: O(n).
• Suitable for small datasets.
• No requirement for sorted order.

Binary Search:
• Works only on sorted data.
• Repeatedly divides the search interval into two halves.
• Time Complexity: O(log n).
• Suitable for large datasets.
• Requires sorted array.

Conclusion:
Linear search is simple but slower for large data.
Binary search is faster but requires sorted input.

You might also like