0% found this document useful (0 votes)
25 views20 pages

DS IAT - 1 Answerkey

The document discusses array based implementation of lists. It defines what an array is and the basic operations supported by arrays like traverse, insertion, deletion, search and update. It also explains how arrays are declared in C with an example and important points like indexes starting from 0 and how elements can be accessed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views20 pages

DS IAT - 1 Answerkey

The document discusses array based implementation of lists. It defines what an array is and the basic operations supported by arrays like traverse, insertion, deletion, search and update. It also explains how arrays are declared in C with an example and important points like indexes starting from 0 and how elements can be accessed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

ACADEMIC YEAR 2022-2023(ODD)

CS3301 DATA STRUCTURES INTERNAL ASSESSMENT TEST -1

DATE : 22/9/2022 TIME : 11.25 -12.55 PM (1.5 Hours)

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

8.b Explain in detail about Linked List Implementation of Doubly 13 1 R 1


Linked List
9.a Explain in detail about Linked List Implementation of Singly 13 1 R 1
Linked List
Or

9.b Explain in detail about Polynomial Manipulation with program 13 4 U 1

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

2. Define ADT and list out its various operations

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.

3. Difference between Array and Linked List

4. Define Doubly and Circular 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.

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.

6. Convert the following infix expression to postfix expression

a+b*c+(d+e+f)/g

7. Define STACK ADT


The abstract datatype is special kind of datatype, whose behavior is defined by a set of values and set of
operations. The keyword “Abstract” is used as we can use these datatypes, we can perform different operations.
But how those operations are working that is totally hidden from the user. The ADT is made of with primitive
datatypes, but operation logics are hidden.
Here we will see the stack ADT. These are few operations or functions of the Stack ADT.

 isFull(), This is used to check whether stack is full or not


 isEmpry(), This is used to check whether stack is empty or not
 push(x), This is used to push x into the stack
 pop(), This is used to delete one element from top of the stack
 peek(), This is used to get the top most element of the stack
 size(), this function is used to get number of elements present into the stack
8.a. Explain in detail about Array based Implementation of List
Array Implementation of list: Array: A set of data elements of same data type is called array. Array is a static data
structure i.e., the memory should be allocated in advance and the size is fixed. This will waste the memory space when
used space is less than the allocated space. An array implementation allows the following operations.
The basic operations are
a. Creation of a List.
b. Insertion of a data in the List
c. Deletion of a data from the List
d. Searching of a data in the list
Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data
structures make use of arrays to implement their algorithms. Following are the important terms to understand the
concept of Array.
 Element − Each item stored in an array is called an element.
 Index − Each location of an element in an array has a numerical index, which is used to identify the element.

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

Following are the basic operations supported by an array.


 Traverse − print all the array elements one by one.
 Insertion − Adds an element at the given index.
 Deletion − Deletes an element at the given index.
 Search − Searches an element using the given index or by the value.
 Update − Updates an element at the given index.
In C, when an array is initialized with size, then it assigns defaults values to its elements in following order.

Data Type Default Value

bool false

char 0

int 0

float 0.0

double 0.0f

void

wchar_t 0
Traverse Operation

This operation is to traverse through the elements of an array.


Example
Following program traverses and prints the elements of an array:
#include <stdio.h>
main() {
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
printf("The original array elements are :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8

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;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}

n = n + 1;

while( j >= k) {
LA[j+1] = LA[j];
j = j - 1;
}

LA[k] = item;

printf("The array elements after insertion :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}
}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after insertion :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 10
LA[4] = 7
LA[5] = 8
For other variations of array insertion operation click here

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;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}

j = k;

while( j < n) {
LA[j-1] = LA[j];
j = j + 1;
}

n = n -1;

printf("The array elements after deletion :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}
}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after deletion :
LA[0] = 1
LA[1] = 3
LA[2] = 7
LA[3] = 8

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;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}

while( j < n){


if( LA[j] == item ) {
break;
}

j = j + 1;
}

printf("Found element %d at position %d\n", item, j+1);


}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
Found element 5 at position 3

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;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}

LA[k-1] = item;

printf("The array elements after updation :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}
}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after updation :
LA[0] = 1
LA[1] = 3
LA[2] = 10
LA[3] = 7
LA[4] = 8

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.

Doubly Linked List Representation

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

Following are the basic operations supported by a list.


 Insertion − Adds an element at the beginning of the list.
 Deletion − Deletes an element at the beginning of the list.
 Insert Last − Adds an element at the end of the list.
 Delete Last − Deletes an element from the end of the list.
 Insert After − Adds an element after an item of the list.
 Delete − Deletes an element from the list using the key.
 Display forward − Displays the complete list in a forward manner.
 Display backward − Displays the complete list in a backward manner.

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;
}

//point it to old first link


link->next = head;

//point first to new first link


head = link;
}

Deletion Operation

Following code demonstrates the deletion operation at the beginning of a doubly linked list.
Example
//delete first item
struct node* deleteFirst() {

//save reference to first link


struct node *tempLink = head;
//if only one link
if(head->next == NULL) {
last = NULL;
} else {
head->next->prev = NULL;
}

head = head->next;

//return the deleted link


return tempLink;
}

Insertion at the End of an Operation

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;

//mark old last node as prev of new link


link->prev = last;
}

//point last to new last node


last = 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.

An example of a singly Linked List

Common Singly Linked List Operations:

Search for a node in the List

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:

 Add item to the front of the list: O(1)


 Add item to the end of the list: O(n)
 Add item to anywhere in the list: O(n)

Remove a node from the list

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:

 Remove item from the front of the list: O(1)


 Remove item from the end of the list: O(n)
 Remove item from anywhere in the list: O(n)

9.b. Explain in detail about Polynomial Manipulation with program


What is Polynomial?

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:

AddPoly(Struct Poly p1[10],Struct Poly p2[10],int t1,int t2,Struct Poly p3[10])

1.) [Initialize segment variables]


[Initialize Counter] Set i=0,j=0,k=0

2.) Repeat step 3 while i<t1 and j<t2

3.) If p1[i].expo=p2[j].expo, then


p3[i].coeff=p1[i].coeff+p2[i].coeff
p3[k].expo=p1[i].expo
[Increase counter] Set i=i+1,j=j+1,k=k+1
else if p1[i].expo > p2[j].expo, then
p3[k].coeff=p1[i].coeff
p3[k].expo=p1[i].expo
[Increase counter] Set i=i+1,k=k+1
else
p3[k].coeff=p2[j].coeff
p3[k].expo=p2[j].expo
Set j=j+1,k=k+1
[End of If]
[End of loop]

4.) Repeat while i<t1


p3[k].coeff=p1[i].coeff
p3[k].expo=p1[i].expo
Set i=i+1,k=k+1
[End of loop]

5.) Repeat while j<t2


p3[k].coeff=p2[j].coeff
p3[k].expo=p2[j].expo
Set j=j+1,k=k+1
[End of loop]

6.) Return k
7.) Exit

C program for Polynomial Addition Using Structure

/* program for addition of two polynomials


* polynomial are stored using structure
* and program uses array of structure
*/
#include<stdio.h>

/* declare structure for polynomial */


struct poly
{
int coeff;
int expo;
};
/* declare three arrays p1, p2, p3 of type structure poly.
* each polynomial can have maximum of ten terms
* addition result of p1 and p2 is stored in p3 */

struct poly p1[10],p2[10],p3[10];

/* 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;

/* read and display first polynomial */


t1=readPoly(p1);
printf(" \n First polynomial : ");
displayPoly(p1,t1);
/* read and display second polynomial */
t2=readPoly(p2);
printf(" \n Second polynomial : ");
displayPoly(p2,t2);

/* add two polynomials and display resultant polynomial */


t3=addPoly(p1,p2,t1,t2,p3);
printf(" \n\n Resultant polynomial after addition : ");
displayPoly(p3,t3);
printf("\n");

return 0;
}
int readPoly(struct poly p[10])
{
int t1,i;

printf("\n\n Enter the total number of terms in the polynomial:");


scanf("%d",&t1);

printf("\n Enter the COEFFICIENT and EXPONENT in DESCENDING ORDER\n");


for(i=0;i<t1;i++)
{
printf(" Enter the Coefficient(%d): ",i+1);
scanf("%d",&p[i].coeff);
printf(" Enter the exponent(%d): ",i+1);
scanf("%d",&p[i].expo); /* only statement in loop */
}
return(t1);
}

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;

while(i<t1 && j<t2)


{
if(p1[i].expo==p2[j].expo)
{
p3[k].coeff=p1[i].coeff + p2[j].coeff;
p3[k].expo=p1[i].expo;

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 rest over terms of polynomial 1 */


while(i<t1)
{
p3[k].coeff=p1[i].coeff;
p3[k].expo=p1[i].expo;
i++;
k++;
}
/* for rest over terms of polynomial 2 */
while(j<t2)
{
p3[k].coeff=p2[j].coeff;
p3[k].expo=p2[j].expo;
j++;
k++;
}

return(k); /* k is number of terms in resultant polynomial*/


}

void displayPoly(struct poly p[10],int term)


{
int 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

Enter the total number of terms in the polynomial:4


Enter the COEFFICIENT and EXPONENT in DESCENDING ORDER
Enter the Coefficient(1): 3
Enter the exponent(1): 4
Enter the Coefficient(2): 7
Enter the exponent(2): 3
Enter the Coefficient(3): 5
Enter the exponent(3): 1
Enter the Coefficient(4): 8
Enter the exponent(4): 0

First polynomial : 3(x^4)+7(x^3)+5(x^1)+8(x^0)

Enter the total number of terms in the polynomial:5


Enter the COEFFICIENT and EXPONENT in DESCENDING ORDER
Enter the Coefficient(1): 7
Enter the exponent(1): 5
Enter the Coefficient(2): 6
Enter the exponent(2): 4
Enter the Coefficient(3): 8
Enter the exponent(3): 2
Enter the Coefficient(4): 9
Enter the exponent(4): 1
Enter the Coefficient(5): 2
Enter the exponent(5): 0
Second polynomial : 7(x^5)+6(x^4)+8(x^2)+9(x^1)+2(x^0)

Resultant polynomial after addition : 7(x^5)+9(x^4)+7(x^3)+8(x^2)+14(x^1)+10(x^0)

10.Explain in detail about Stack –Linked List Implementation

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.

Below is the implementation of the above operations:

 C++
 Java
 Python3
 C#
 Javascript

// C++ program to Implement a stack

// using singly linked list

#include <bits/stdc++.h>

using namespace std;

// Declare linked list node

struct Node {

int data;

Node* link;

};

Node* top;

// Utility function to add an element

// data in the stack insert at the beginning


void push(int data)

// Create new node temp and allocate memory in heap

Node* temp = new Node();

// Check if stack (heap) is full.

// Then inserting an element would

// lead to stack overflow

if (!temp) {

cout << "\nStack Overflow";

exit(1);

// Initialize data into temp data field

temp->data = data;

// Put top pointer reference into temp link

temp->link = top;

// Make temp as top of Stack

top = temp;

// Utility function to check if

// the stack is empty or not

int isEmpty()

// If top is NULL it means that

// there are no elements are in stack


return top == NULL;

// Utility function to return top element in a stack

int peek()

// If stack is not empty , return the top element

if (!isEmpty())

return top->data;

else

exit(1);

// Utility function to pop top

// element from the stack

void pop()

Node* temp;

// Check for stack underflow

if (top == NULL) {

cout << "\nStack Underflow" << endl;

exit(1);

else {

temp = top;
top = top->link;

free(temp);

void display()

Node* temp;

// Check for stack underflow

if (top == NULL) {

cout << "\nStack Underflow";

exit(1);

else {

temp = top;

while (temp != NULL) {

// Print node data

cout << temp->data << "-> ";

temp = temp->link;

int main()

{
push(11);

push(22);

push(33);

push(44);

display();

cout << "\nTop element is " << peek() << endl;

pop();

pop();

display();

cout << "\nTop element is " << peek() << endl;

return 0;

Output
44-> 33-> 22-> 11->
Top element is 44
22-> 11->
Top element is 22

You might also like