Data Structure and Algorithm
Data Structure and Algorithm
UNIT 1
4Marks
1.The List Abstract Data Type (ADT) is a fundamental data structure that organizes
a collection of elements in a linear order. It allows for efficient insertion, deletion,
and retrieval of elements based on their position within the list. The concept of a
list ADT is abstract in the sense that it defines the behavior and operations of a list
without specifying the internal implementation details.
Dynamic Size: Lists can grow or shrink dynamically as elements are added or
removed.
Support for Duplicates: Lists can contain duplicate elements, depending on the
implementation
#include <stdio.h>
int i, j;
*comparisons = 0;
*swaps = 0;
(*comparisons)++;
(*swaps)++;
arr[j] = arr[j+1];
arr[j+1] = temp;
}
int main() {
printf("\n");
return 0;
Bubble Sort:
Explanation: Bubble Sort compares adjacent elements and swaps them if they are
in the wrong order, iterating through the entire array multiple times. As the input
size increases, the number of comparisons and swaps grows quadratically, leading
to O(n^2) time complexity.
4. Linked lists are a fundamental data structure in computer science used to store
and manage collections of data. They consist of nodes, where each node contains
a data element and a pointer /reference to the next node in the sequence.
Linked lists can be categorized into several types based on their structure and
functionality:
In a singly linked list, each node points to the next node in the sequence, forming
a unidirectional chain.
Illustration:
In a doubly linked list, each node has pointers to both the next and previous
nodes, enabling bidirectional traversal.
Illustration:
In a circular linked list, the last node points back to the first node, forming a
circular structure.
Illustration:
Each type of linked list offers different advantages and is suitable for various
applications based on requirements such as traversal direction, insertion/deletion
efficiency, and memory utilization.
5.import numpy as np
In this program:
We import the numpy library as np.
We declare three arrays of different types: integers, floats, and strings using the
np.array() function.
#include <stdio.h>
int main() {
// Initialize variables
int list[MAX_SIZE];
do {
// Display menu
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
// Insert Element
break;
scanf("%d", &position);
printf("Invalid position.\n");
break;
scanf("%d", &element);
list[position] = element;
size++;
printf("Element inserted successfully.\n");
break;
case 2:
// Delete Element
if (size == 0) {
break;
scanf("%d", &position);
printf("Invalid position.\n");
break;
size--;
break;
case 3:
// Display List
printf("List elements: ");
printf("\n");
break;
case 4:
// Exit
printf("Exiting program.\n");
break;
default:
return 0;
In this program:
We define a list of integers using an array list with a maximum size of MAX_SIZE.
The program presents a menu to the user with options to insert an element,
delete an element, display the list, or exit the program.
Memory Allocation:
Array: Uses contiguous memory allocation, where elements are stored in adjacent
memory locations.
Linked List: Uses non-contiguous memory allocation, where each node can be
located anywhere in memory.
Size Flexibility:
Array: Has a fixed size determined at compile time, making it less flexible for
dynamically changing data.
Linked List: Can easily accommodate dynamic size changes as nodes can be
dynamically allocated and deallocated.
Linked List: Does not support efficient random access; accessing an element in a
linked list may require traversing the list from the beginning, leading to linear
time complexity.
Array: Can perform binary search efficiently if the array is sorted, but linear search
is generally slower compared to hash tables or binary search trees for unsorted
arrays.
Linked List: Linear search is the only option as there is no direct access to
elements; each node must be traversed sequentially.
Memory Overhead:
Array: Typically has lower memory overhead as it only stores the data elements
themselves.
8.To incorporate a new element at the start of a singly linked list, you would
follow these steps:
Set the next pointer of the new node to point to the current head of the linked
list.
Update the head pointer to point to the new node, making it the new first
element of the list.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
if (newNode == NULL) {
printf("Memory allocation failed.\n");
return;
newNode->data = value;
newNode->next = head;
head = newNode;
void displayList() {
temp = temp->next;
printf("\n");
int main() {
insertAtStart(30);
insertAtStart(20);
insertAtStart(10);
return 0;
9.to incorporate a new element at the start of a doubly linked list, you would
follow these steps:
Set the next pointer of the new node to point to the current head of the linked
list.
Set the previous pointer of the new node to NULL since it will be the new head.
If the list is not empty, set the previous pointer of the current head to point to the
new node.
Update the head pointer to point to the new node, making it the new first
element of the list
10. The statement "The objective behind considering a circular linked list is to
simplify the insertion and deletion operations performed on a doubly linked list"
can be justified by comparing the complexities of insertion and deletion
operations in both types of linked lists. Let's consider an example to demonstrate
this:
Suppose we have a doubly linked list and a circular linked list, both containing
integer elements. We will analyze the insertion and deletion operations in both
lists to justify the statement.
Insertion Operation:
Doubly Linked List (DLL): To insert a new node at the start or end of a doubly
linked list, we need to update the pointers of neighboring nodes. For example,
inserting at the start involves updating the previous pointer of the current head (if
applicable) and the next pointer of the new node and thecurrent head.
Circular Linked List (CLL): In a circular linked list, inserting a new node at the start
or end is simplified because there is no need to update pointers of neighboring
nodes. For example, inserting at the start involves updating the next pointer of
the new node and the head pointer only.
Deletion Operation:
Doubly Linked List (DLL): Deleting a node from a doubly linked list requires
updating the pointers of neighboring nodes. For example, deleting a node
involves updating the next pointer of the previous node and the previous pointer
of the next node to bypass the node being deleted.
Circular Linked List (CLL): In a circular linked list, deleting a node is simplified
because there is no need to update pointers of neighboring nodes. For example,
deleting a node involves updating the next pointer of the previous node and the
next pointer of the next
Multilist Data Structure: In social networks, multilist data structures are used to
represent connections between users. Each user can have multiple types of
connections, such as friends, followers, groups, etc.
Reason for Use: Multilist structures are chosen because they efficiently capture
the complex relationships in social networks. They allow for easy navigation
between different types of connections, such as finding mutual friends, group
members, or followers of a user.
File Systems:
Multilist Data Structure: In file systems, multilist data structures are used to
manage directory hierarchies and file attributes. Each directory can contain
multiple files and subdirectories, each with its own properties.
Reason for Use: Multilist structures are preferred for file systems because they
provide a hierarchical organization of data while allowing efficient access to files
and directories. They support operations like navigating directories, listing file
attributes, and managing file permissions.
Reason for Use: Multilist structures are suitable for employee management
systems because they accommodate the diverse relationships within an
organization. They allow for easy retrieval of information such as reporting chains,
team members, and roles assigned to employees.
Library Catalogs:
Multilist Data Structure: In library catalogs, multilist data structures are used to
organize books, authors, genres, and other metadata. Each book can belong to
multiple categories and have various attributes.