0% found this document useful (0 votes)
9 views

DS Module- 2 (1)

Data structure module 2

Uploaded by

rahmafathima084
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

DS Module- 2 (1)

Data structure module 2

Uploaded by

rahmafathima084
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

3RD SEM BSc CS,

BCA DS –
MODULE 2
MAJLIS ARTS AND SCIENCE COLLEGE, PURAMANNUR

DEPARTMENT OF COMPUTER SCIENCE


DATA STRUCTURES USING C (3rd Semester Online
Study Material)
B.Sc Computer Science &
BCA
(Questions and answers based on second Module)

1. Convert ((A+B)*C)(E-F) to postfix


Ans: AB+C*EF-*
2. Define polish notation.
Ans:
An expression is called the prefix expression if the operator appears in the expression before the operands.
Prefix notation is also known as Polish Notation.
3. Define sparse matrix.
Ans: If most of the elements of the matrix have 0 (zero)value, then it is called a sparse matrix.
4. Define recursion.
Ans: A function calls itself is called recursion
5. Node is collection of --------
Ans: data and link
6. Clarify whether linked list is linear or non linear.
Ans: Linear
7. How to represent two way linked list.
Ans: A Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next
pointer and data which are there in singly linked list.

8. Define circular linked list.


Ans: Circular linked list is a linked list where all nodes are connected to form a circle. There is no NULL at the end.

9. What is linear array? What are the different operations on array.


Ans:
An array is a list of finite number of n homogeneous data elements.(i.e. data elements of the same type) such that
The elements of an array are referenced by an index.
Elements of array stored at contiguous memory locations.
This makes it easier to calculate the position of each element by simply adding an offset to a base value.

Traversal
Insertion
Deletion
Traversal
It prints all the array elements one after another.
Algorithm
LA is a linear array with lower bound LB and upper bound UB.
step 1: set k=LB
step 2: Repeat steps 3 and 4 while k<=UB
step 3: Apply visit to LA[k]
step 4: set k=k+1
step 5: exit
Insertion
It adds an element at given index.
Algorithm
INSERT(LA,N,K,ITEM)
LA is a linear array with N elements and K is a positive integer K<=N. This algorithm inserts an element ITEM into
the Kth position in LA.
Step 1: set J=N
step 2: Repeat steps 3 and 4 while J>=K
step 3: set LA[J+1]=LA[J]
step 4: set J=J-1
step 5: set LA[K]=ITEM
step 6: set N=N+1
step 7: exit
Deletion
Operation of removing one of the elements from LA.
Algorithm
DELETE(LA,N,K,ITEM)
LA is a linear array with N elements and K is a positive integer ,K<=N. This algorithm deletes the Kth element
from LA.
step 1: set ITEM=LA[K]
step 2: Repeat for J=K to N-1
step 3: set LA[J]=LA[J+1]
step 4: set N=N-1
step 5: exit

10. Write an algorithm for insertion in a singly linked list.

11. What is the main difference between array and linked list?
Ans:
An array is a list of finite number of n homogeneous data elements.(i.e. data elements of the same type) such that
The elements of an array are referenced by an index.
Elements of array stored at contiguous memory locations.
This makes it easier to calculate the position of each element by simply adding an offset to a base value.
Linked List is an ordered collection of elements of same type, which are connected to each other using pointers.
Linked List supports Sequential Access, which means to access any element/node in a linked list, we have to
sequentially traverse the complete linked list, upto that element.
Linked list can be Linear(Singly), Doubly or Circular linked list.

12. What are the different ways to allocate memory for arrays? Explain.
Ans:
Two possible arrangements
1. row major order
2. column major order
row major order

elements of an array are arranged sequentially row by row. Thus elements of first row occupies first set of
memory locations reserved for the array, elements of second row occupies the next set of memory and so on.
Eg: row major order A[2][3] 1 2 3
4 5 6
[0][0] [0][1] [0][2] [1][0] [1][1] [1][2]
22fdfsva
av , m

1 2 3 4 5 6

Elements
Memory location 200 202 204 206 208 210

column major order

Eg: column major order A[2][3] 1 2 3


4 5 6
[0][0] [1][0] [2][0] [0][1] [1][1] [2][1]
22fdfsva
av , m

1 4 2 5 3 6

Elements
Memory location 200 202 204 206 208 210
Given a 2D array of size M x N.
Row major address = B + w * (N * (i-L1) + j-L2)
Column major address = B + w * (M * (j-L2) + i-L1)
B : Base address of the array
w : Size of each element of the array
L1 : lower bound of row
L2 : lower bound of column

Find memory location in row major order

Eg:
find location of A[4]=A[1][0]
Row major address = B + w * (N * (i-L1) + j-L2)
here,
B=200
w =2
L1=L2=0 N=3
A[1 ][0] location=200+2[3*(1-0)+(0-0)]
=200+ 6=206

Find memory location in column major order

Eg:
find location of A[4]=A[1][0]
Column major address = B + w * (M * (j-L2) + (i-L1))
here,
B=200
w =2
L1=L2=0 M=2
A[1 ][0] location=200+2[2(0-0)+(1-0)]
=200+ 2=202

13. What are the different types of notations?


Infix :
An expression is called the Infix expression if the operator appears in between the operands in the expression.

Example : A+B
(A+B) * (C-D)
Prefix :
An expression is called the prefix expression if the operator appears in the expression before the operands.
Prefix notation is also known as Polish Notation.

Example :
infix prefix
A+B +AB
(A+B) * (C-D) *+AB-CD
Postfix :
An expression is called the postfix expression if the operator appears in the expression after the operands.
Postfix notation is also known as Reverse Polish Notation or Suffix.

Example :
infix postfix
A+B AB+
(A+B) * (C-D) AB+CD-*

14. Write an algorithm to convert infix to postfix conversion.


Ans:

15. Write an algorithm to insert an element in linear linked list.


Ans:
Overflow
Sometimes new data are to be inserted into a data structure but there is no available space, i.e, the free storage
list is empty. This situation is called overflow.
AVAIL = NULL
Underflow
START = NULL
INSERTION
FIRST

MIDDLE

LAST
Insert first
2) Insert middle

You might also like