DS IAT - 1 Answerkey
DS IAT - 1 Answerkey
Course Outcome
CO 1 To understand the concepts of CO 2 To Learn linear data
ADTs structures – lists, stacks, and
queues
CO 3 To understand non-linear data C04 To understand sorting, searching and
structures – trees and graphs hashing algorithms
CO5 To apply Tree and Graph structures
Part A 7 x 2 =14
Q.NO Quest Marks CO BT PO Marks
ions Obtained
1 Define Data structures and its types 2 1 R 1
2 Define ADT and list out its various operations 2 2 R 1
3 Difference between Array and Linked List 2 1 U 1
4 Define Doubly and Circular Linked List. 2 1 R 2
5 What are the advantages of doubly linked list over singly Linked 2 2 U 4
List.
6 Convert the following infix expression to postfix expression 2 1 A 2
a+b*c+(d+e+f)/g
7 Define stack ADT 2 2 R 1
Part B 2 x 13 = 26
Q.N Ques Mark CO BT PO
O tions s
8.a Explain in detail about Array based Implementation of List 13 1 R 2
Or
Part – C 1 x 10 = 10
Q.NO Ques Marks CO BT PO
tions
10 Explain in detail about Stack –Linked List Implementation 10 1 U 1
Question Type Weightage Percentage
R 47(2m,2m,2m,2m,13m,13,13m) 61.84
U 27(2m,2m,13m,10m) 35.53
A 2(2m) 2.63
L 0 0
E 0 0
1. Define Data structures and its types
Data Structure can be defined as the group of data elements which provides an efficient way of storing and
organising data in the computer so that it can be used efficiently. Some examples of Data Structures are arrays,
Linked List, Stack, Queue, etc. Data Structures are widely used in almost every aspect of Computer Science i.e.
Operating System, Compiler Design, Artifical intelligence, Graphics and many more.
Data Structures are the main part of many computer science algorithms as they enable the programmers to handle
the data in an efficient way. It plays a vital role in enhancing the performance of a software or a program as the
main function of the software is to store and retrieve the user's data as fast as possible
abstract data type (ADT) is a mathematical model for data types. An abstract data type is defined by its
behavior (semantics) from the point of view of a user, of the data, specifically in terms of possible values,
possible operations on data of this type, and the behavior of these operations. This mathematical model contrasts
with data structures, which are concrete representations of data, and are the point of view of an implementer, not
a user.
Doubly Linked List is a variation of Linked list in which navigation is possible in both ways, either forward
and backward easily as compared to Single Linked List. Following are the important terms to understand the
concept of doubly linked list. Link − Each link of a linked list can store a data called an element.
The circular linked list is a linked list where all nodes are connected to form a circle. In a circular linked list,
the first node and the last node are connected to each other which forms a circle. There is no NULL at the end.
5.What is the advantage of using a doubly linked list for chaining over singly linked list?
Explanation: Using a doubly linked list reduces time complexity significantly. Though it uses more memory to store
the extra pointer.
It allows traversing in both forward and backward directions because of the next and previous pointers, unlike the
singly linked list, which allows traversing in only one direction.
Deletion of nodes is easier compared to a singly linked list. ...
Reversing a doubly linked list is also easy.
a+b*c+(d+e+f)/g
Array Representation
Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration.
Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration.
As per the above illustration, following are the important points to be considered.
Index starts with 0.
Array length is 10 which means it can store 10 elements.
Each element can be accessed via its index. For example, we can fetch an element at index 6 as 9.
Basic Operations
bool false
char 0
int 0
float 0.0
double 0.0f
void
wchar_t 0
Traverse Operation
Insertion Operation
Insert operation is to insert one or more data elements into an array. Based on the requirement, a new element can be added at
the beginning, end, or any given index of array.
Here, we see a practical implementation of insertion operation, where we add data at the end of the array −
Example
Following is the implementation of the above algorithm −
Live Demo
#include <stdio.h>
main() {
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
n = n + 1;
while( j >= k) {
LA[j+1] = LA[j];
j = j - 1;
}
LA[k] = item;
Deletion Operation
Deletion refers to removing an existing element from the array and re-organizing all elements of an array.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to delete
an element available at the Kth position of LA.
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop
Example
Following is the implementation of the above algorithm −
Live Demo
#include <stdio.h>
void main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5;
int i, j;
j = k;
while( j < n) {
LA[j-1] = LA[j];
j = j + 1;
}
n = n -1;
Search Operation
You can perform a search for an array element based on its value or its index.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to find
an element with a value of ITEM using sequential search.
1. Start
2. Set J = 0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop
Example
Following is the implementation of the above algorithm −
Live Demo
#include <stdio.h>
void main() {
int LA[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0, j = 0;
j = j + 1;
}
Update Operation
Update operation refers to updating an existing element from the array at a given index.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to
update an element available at the Kth position of LA.
1. Start
2. Set LA[K-1] = ITEM
3. Stop
Example
Following is the implementation of the above algorithm −
Live Demo
#include <stdio.h>
void main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5, item = 10;
int i, j;
LA[k-1] = item;
8.b. Explain in detail about Linked List Implementation of Doubly Linked List
Doubly Linked List is a variation of Linked list in which navigation is possible in both ways, either forward and backward
easily as compared to Single Linked List. Following are the important terms to understand the concept of doubly linked list.
Link − Each link of a linked list can store a data called an element.
Next − Each link of a linked list contains a link to the next link called Next.
Prev − Each link of a linked list contains a link to the previous link called Prev.
LinkedList − A Linked List contains the connection link to the first link called First and to the last link called Last.
As per the above illustration, following are the important points to be considered.
Doubly Linked List contains a link element called first and last.
Each link carries a data field(s) and two link fields called next and prev.
Each link is linked with its next link using its next link.
Each link is linked with its previous link using its previous link.
The last link carries a link as null to mark the end of the list.
Basic Operations
Insertion Operation
Following code demonstrates the insertion operation at the beginning of a doubly linked list.
Example
//insert link at the first location
void insertFirst(int key, int data) {
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty()) {
//make it the last link
last = link;
} else {
//update first prev link
head->prev = link;
}
Deletion Operation
Following code demonstrates the deletion operation at the beginning of a doubly linked list.
Example
//delete first item
struct node* deleteFirst() {
head = head->next;
Following code demonstrates the insertion operation at the last position of a doubly linked list.
Example
//insert link at the last location
void insertLast(int key, int data) {
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty()) {
//make it the last link
last = link;
} else {
//make link a new last link
last->next = link;
9.a. Explain in detail about Linked List Implementation of Singly Linked List
A singly linked list is a type of linked list that is unidirectional, that is, it can be traversed in only one direction from head to
the last node (tail).
Each element in a linked list is called a node. A single node contains data and a pointer to the next node which helps in
maintaining the structure of the list.
The first node is called the head; it points to the first node of the list and helps us access every other element in the list. The
last node, also sometimes called the tail, points to NULL which helps us in determining when the list ends.
You can determine and retrieve a specific node either from the front, the end, or anywhere in the list.
The worst case Time Complexity for retrieving a node from anywhere in the list is O(n).
Add a node to the List
You can add a node at the front, the end or anywhere in the linked list.
The worst case Time Complexity for performing these operations is as follows:
You can remove a node either from the front, the end or from anywhere in the list.
The worst case Time Complexity for performing this operation is as follows:
A polynomial is an expression that contains more than two terms. A term is made up of coefficient and exponent.
Example: P(x) = 4x3+6x2+7x+9
A polynomial may be represented using array or structure. A structure may be defined such that it contains two parts – one is
the coefficient and second is the corresponding exponent. The structure definition may be given as shown below:
Struct polynomial{
int coefficient;
int exponent;
};
The basic idea of polynomial addition is to add coefficient parts of the polynomials having same exponent.
Algorithm:
6.) Return k
7.) Exit
/* function prototypes */
int readPoly(struct poly []);
int addPoly(struct poly [],struct poly [],int ,int ,struct poly []);
void displayPoly( struct poly [],int terms);
int main()
{
int t1,t2,t3;
return 0;
}
int readPoly(struct poly p[10])
{
int t1,i;
int addPoly(struct poly p1[10],struct poly p2[10],int t1,int t2,struct poly p3[10])
{
int i,j,k;
i=0;
j=0;
k=0;
i++;
j++;
k++;
}
else if(p1[i].expo>p2[j].expo)
{
p3[k].coeff=p1[i].coeff;
p3[k].expo=p1[i].expo;
i++;
k++;
}
else
{
p3[k].coeff=p2[j].coeff;
p3[k].expo=p2[j].expo;
j++;
k++;
}
}
for(k=0;k<term-1;k++)
printf("%d(x^%d)+",p[k].coeff,p[k].expo);
printf("%d(x^%d)",p[term-1].coeff,p[term-1].expo);
}
Output
To implement a stack using the singly linked list concept, all the singly linked list operations are performed based on Stack
operations LIFO(last in first out) and with the help of that knowledge, we are going to implement a stack using a singly
linked list.
So we need to follow a simple rule in the implementation of a stack which is last in first out and all the operations can be
performed with the help of a top variable. Let us learn how to perform Pop, Push, Peek, and Display operations in the
following article:
In the stack Implementation, a stack contains a top pointer. which is the “head” of the stack where pushing and popping
items happens at the head of the list. The first node has a null in the link field and second node-link has the first node
address in the link field and so on and the last node address is in the “top” pointer.
The main advantage of using a linked list over arrays is that it is possible to implement a stack that can shrink or grow as
much as needed. Using an array will put a restriction on the maximum capacity of the array which can lead to stack
overflow. Here each new node will be dynamically allocated. so overflow is not possible.
Stack Operations:
1. push(): Insert a new element into the stack i.e just inserting a new element at the beginning of the linked list.
2. pop(): Return the top element of the Stack i.e simply deleting the first element from the linked list.
3. peek(): Return the top element.
4. display(): Print all elements in Stack.
C++
Java
Python3
C#
Javascript
#include <bits/stdc++.h>
struct Node {
int data;
Node* link;
};
Node* top;
if (!temp) {
exit(1);
temp->data = data;
temp->link = top;
top = temp;
int isEmpty()
int peek()
if (!isEmpty())
return top->data;
else
exit(1);
void pop()
Node* temp;
if (top == NULL) {
exit(1);
else {
temp = top;
top = top->link;
free(temp);
void display()
Node* temp;
if (top == NULL) {
exit(1);
else {
temp = top;
temp = temp->link;
int main()
{
push(11);
push(22);
push(33);
push(44);
display();
pop();
pop();
display();
return 0;
Output
44-> 33-> 22-> 11->
Top element is 44
22-> 11->
Top element is 22