DS Question Bank - With Answers - 3
DS Question Bank - With Answers - 3
CSE, CSM,
St. Peter’s Engineering College (Autonomous) Dept. :
CSD, CSC
Dullapally (P), Medchal, Hyderabad – 500100. Academic Year
QUESTION BANK 2023-24
Subject Code : AS22-05ES07 Subject : Data Structures
Class/Section : B.Tech. Year : I Semester : II
BLOOMS LEVEL
Remember L1 Understand L2 Apply L3
Analyze L4 Evaluate L5 Create L6
*****
A linked list is a linear data structure which can store a collection of "nodes" connected
together via links i.e. pointers. Linked lists nodes are not stored at a contiguous location, rather
they are linked using pointers to the different memory locations.
A linked list is a chain of nodes, and each node has the following parts:
Data – Stores the information
Next – Stores the address to next node
SR 22
b) Define Dynamic memory allocation and provide the syntax for its 3M
L2 C123.2
usage?
The mechanism by which memory can be allocated to variables during the run time is called
Dynamic Memory Allocation.
Syntax:
Struct node *temp;
temp=(struct node *)malloc(sizeof(struct node)):
Syntax:
struct node
{
int data;
struct node *next;
};
d) Write the syntax for creating a node in a Doubly Linked List 3M L3 C123.2
Syntax:
struct node
{
int data;
struct node *next;
struct node *previous;
};
3 a) What are the advantages and disadvantages of using a Linked List? 5M L4 C123.2
Advantages of Linked List
• Dynamic data structure - The size of the linked list may vary according to the
requirements. Linked list does not have a fixed size.
• Insertion and deletion - Insertion and deletion operations are quite easier in the linked
list. There is no need to shift elements after the insertion or deletion of an element only
the address present in the next pointer needs to be updated.
Efficient for large data- When working with large datasets linked lists play a crucial
role as it can grow and shrink dynamically.
• Flexible: This is because the elements in Linked List are not stored in contiguous
memory locations unlike the array.
• Implementation - We can implement both stacks and queues using linked list.
A linked list is a linear data structure which can store a collection of "nodes" connected
together via links i.e. pointers. Linked lists nodes are not stored at a contiguous location, rather
they are linked using pointers to the different memory locations.
SR 22
A linked list is a chain of nodes, and each node has the following parts:
Data – Stores the information
Next – Stores the address to next node
A linked list is a dynamic linear data structure whose memory size can be allocated or de-
allocated at run time based on the operation insertion or deletion, this helps in using system
memory efficiently. Linked lists can be used to implement various data structures like a stack,
queue, graph, hash maps, etc.
tail
A linked list starts with a head node which points to the first node. Every node consists of
data which holds the actual data (value) associated with the node and a next pointer which holds
the memory address of the next node in the linked list. The last node is called the tail node in the
list which points to null indicating the end of the list.
Generally, a Linked List means a singly linked list. Singly linked lists contain two fields in
one node; one field holds the data and the other field holds the address of the next node of the list.
Traversals can be done in one direction only as there is only a single link between two nodes of
the same list.
Doubly Linked Lists contain three fields in one node; one field holds the data and the other
fields hold the addresses of the previous and next nodes in the list. The list is traversed twice as
the nodes in the list are connected to each other from both sides.
The basic operations in the linked lists are insertion, deletion, searching, display, and deleting
an element at a given key. These operations are performed on Singly Linked Lists as given below
• Insertion
• Deletion
• Traversal
• Search
SN Operation Description
1 Deletion at It involves deletion of a node from the beginning of the list. This is the
beginning simplest operation among all. It just need a few adjustments in the node
pointers.
2 Deletion at It involves deleting the last node of the list. The list can either be empty
the end of the or full. Different logic is implemented for the different scenarios.
list
3 Deletion after It involves deleting the node after the specified node in the list. we need
specified to skip the desired number of nodes to reach the node after which the
node node will be deleted. This requires traversing through the list.
SR 22
1) Deletion at Beginning
To delete a node from the start/beginning/front of a Linked List, we need to:
• Remove the head from the original first node of Linked List
• Make the second node as the Head of the Linked List.
• Free memory for the node to be deleted
2) Deletion at Ending
To delete a node from the end of a Linked List, we need to:
• Go to the last node of the Linked List
• Change the previous pointer of last node to NULL.
• Free memory for the node to be deleted
In this deletion operation of the linked, we are deleting an element from the ending of the list.
To print the node data from first node to the last node of a Linked List, we need to:
• A pointer is required to travel from first node to the last node.
• While traveling the pointer it prints the node data and move to next node until it reaches to
last node.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}*head=NULL,*ptr;
void traversal()
{
if (head== NULL)
printf("List if Empty");
else
{
ptr = head;
//start from the beginning
while(ptr != NULL)
{
printf(" ->%d ",ptr->data);
ptr = ptr->next;
}
printf("\n");
}
}
ptr=ptr->next;
}
p->next=ptr->next;
free(ptr);
}
}
void main()
{
int i,n,k,ele;
printf("Enter the Number of Nodes\t");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the Data:\t");
scanf("%d",&k);
insertatend(k);
}
printf("Linked List: ");
traversal();
printf("Enter the Node:\t");
scanf("%d",&ele);
deleteaftergivennode(ele);
printf("After deleting Linked List:\t");
traversal();
}
Output:
Enter the Number of Nodes 4
Enter the Data: 10
Enter the Data: 20
Enter the Data: 30
Enter the Data: 40
Linked List: ->10 ->20 ->30 ->40
Enter the Node: 20
After deleting Linked List: ->10 ->30 ->40