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

CS3353 QB - Question Bank

CS3353 QB - question bank

Uploaded by

Omprakash D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views

CS3353 QB - Question Bank

CS3353 QB - question bank

Uploaded by

Omprakash D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

lOMoARcPSD|11267200

CS3353 QB - question bank

C Programming and Data Structures (Anna University)

Studocu is not sponsored or endorsed by any college or university


Downloaded by Omprakash D ([email protected])
lOMoARcPSD|11267200

CS3353
C PROGRAMMING
&
DATASTRUCTURES
Question bank

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

UNIT I
C PROGRAMMING FUNDAMENTALS

PART A
1. Define programming paradigm.
Programming paradigm is a style or way of programming. The are procedural, functional,
object-oriented, logic programming.
Some of the programming languages are
 Fortran
 Cobol
 Basic
 Pascal
 C

2. Give two examples for assignment statements.


Syntax for assignment : variable = expression / value ;Example : x=100;
y= a+b;

3. What are keywords? Give an example


 Keywords are reserved words, they have standard and predefinedmeaning.
 Keywords cannot be used as normal identifiers.
 Example : auto, break, char, continue, else, if, switch, struct, union.

4. What do you mean by variables in ‘C’ ?


A variable is an identifier that is used to represent some specified type ofinformation.
Syntax : data_type variable_name;Example :int marks;

5. Identify the use of ternary or conditional operator.


 ?: is known as conditional operator.It evaluates the first expression if the condition is true
otherwise the second expression is evaluated.
 Syntax : condition ? exp1 : exp2 ;
2

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

6. What is a compilation process?


Compiler converts source code into executable code. It includes
 Pre-processor
 Compilation
 Assembly
 Linking

7. Differentiate between an expression and a statement in C.

No. Expression Statements


i. Expression consists of It is defined as a set of declaration
operators and or sequence of actions.
operands.
ii. Example: a=29; ple: Assignment statementMark=73;
b=a+77;

8. What is the output of the programs given below?


#include <stdio.h>main( )
{
int a = 20, b = 10, c = 15, d = 5; int e;
e = (a + b) * c / d;
printf("Value of (a + b) * c / d is : %d\n", e );
}

OUTPUT :

Value of (a + b) * c / d is : 90

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

9. Classify the different types of storage classes .


There are mainly four types of storage classes. They are
 Automatic (auto)
 Static
 External (ext

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

10.Discover the meaning of C pre-processor


The reprocessor contains any operations in the processing language, it will be transformed
first.
1. The preprocessing language consists of
 Inclusion of header file
 Macro expansion
 Conditional compilation
 Line control
 Diagnostics

11.Invent the difference between ++a and a++.


++a is known as pre increment where the value is incremented by one and then the operation
is done.
 a++ is known as post increment where the operation is done first and then the value is
incremented by one.

12.Differentiate switch( ) and nested-if statement

N Switch( ) Nested if
o
.
i. The switch( ) can test only The if can evaluate relational or
constant values. logical expressions.
ii In switch( ) case nested if can In nested if statements, switch( )
. be used. case can be used

13.Summarize the various types of C operators.


 Arithmatic operators
 Relational operators
 Logical operators
 Increment or decrement operators
 Conditional or Ternary operators

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

 Bitwise operators
 Special operators (sizeof, & and * , . and -->)

14. Recommend the suitable example for infinite loop using while.

#include<stdio.h>void main()

int i = 1;

while( i<10 )

printf(“%d\n”,i);

getch( );

Here we are not updating the value of i. so after each iteration value of i remains same. As
a result, the condition (i<10) will always true so itwill print infinity loop.

15. How to create a two dimensional array?


Two dimensional arrays are stored in a row-column matrix, where the left index indicates the
row and right matrix indicates the column.

Syntax : data_type array_name[row_size][column_size];Example : int mat[3][3];

16..What are the different ways of initializing array?


 Values can be assigned to an array by normal declaration otherwise they hold garbage
6

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

values.
 Arrays can be initialized in following two ways :
i. At compile time
ii. At Run time

17..Define string.
 String is a sequence / array of characters enclosed with double quotes.
 Null character (‘\0’) is used to mark the end of the string

C O M P U T E R \0

Example : char word= “computer

18. Define Multi-dimensional array.


 Multi-dimensioned arrays have two or more index values which specify the element in the
array.

Declaration:
int m1[10][10];
static int m2[2][2] = { {0,1}, {2,3} };

19.Write a C program to find the length of given string.


PROGRAM
#include <stdio.h> int main()
{
char s[1000], i; printf("Enter a string: "); scanf("%s", s);
for(i = 0; s[i] != '\0'; ++i); printf("Length of string: %d", i); return 0;
}

OUTPUT
Enter a string: Programming in C Length of string:16

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

20. What is a function?


 Function is a set of instructions
 Self contained block
 Performs a specific task
 Used to avoid redundancy of code

21.What is the need for functions?


 To reduce the complexity of large programs
 To increase the readability
 To achieve reusability
 To avoid redundancy of code
 To save Memory

22.Compare actual parameter & formal argument


Actual argument: Specified in the function call statement. Used tosupply the input values to the
function either by copy or reference
Formal argument: Specified in the function definition statement. It takes either copy or address
of the actual arguments

23. What is a function prototype?


Function prototype is a function declaration statement. Syntax : return_type function_name(
parameters_list) Example: int factorial(int);

24.Differentiate call by value and call by reference.


Call by value: The values of the variables are passed by the calling function to the called
function.
Call by reference: The addresses of the variables are passed by the calling function to the
called function.

25. Write the advantages and disadvantages of recursion.


Recursion makes program elegant and cleaner. All algorithms can be defined recursively which
makes it easier to visualize and prove.
If the speed of the program is vital then, you should avoid using recursion.
Recursions use
8

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

more memory and are generally slow. Instead, you can use loop.

26.What is meant by Recursive function?


 If a function calls itself again and again, then that function is calledRecursive function.

Example:
void recursion()
{
recursion(); /* function calls itself */
}
int main()
{
recursion();

27.What are the steps in writing a function in a program?


Function Declaration (Prototype declaration):
 Every user-defined functions has to be declared before the main().
Function Callings:
 The user-defined functions can be called inside any functions like main(), userdefined
function, etc.
Function Definition:
 The function definition block is used to define the user-definedfunctions with statements.

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

PART B

1. List the different data types available in C.


2. Explain the different types of operators available in C.
3. Explain about various decision making statements available in C with illustrative programs
4. Write a C program to print the Fibonacci series of a given number.
5. Explain in detail about functions & Function prototypes?
6. Explain in detail about Recursion with its example
7. Tower of Hanoi with sample program?
8. What is an array? Discuss how to initialize a one dimensional and two dimensional arrays with
suitable example?

10

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

UNIT II
C PROGRAMMING - ADVANCED FEATURES

PART A

1. Define Structure in C
 C Structure is a collection of different data types which are grouped together and each
element in a C structure is called member.
 If you want to access structure members in C, structure variable should bedeclared.
 Many structure variables can be declared for same structure and memory will be allocated
for each separately.It is a best practice to initialize a structure to null while declaring, if we
don’tassign any values to structure members.

1. How to Declare a members in Structure?


A struct in C programming language is a structured (record) type[1] that aggregates a fixed
set of labeled objects, possibly of different types, into a single object. The syntax for a struct
declaration in C is:
syntax :
struct tag_name
{
type attribute; type attribute2;
/* ... */
};

2. What you meant by structure definition?


A structure type is usually defined near to the start of a file using a typedef statement.
typedef defines and names a new type, allowing its use throughout theprogram.
Typedefs usually occur just after the #define and #include statements in a file.
Example
.typedef struct { char
name[64];
char course[128];int age;
int year;

11

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

} student;

This defines a new type student variables of type student can be declared asfollows.
student st_rec;

3. What is meant by Union in C?


A union is a special data type available in C that enables you to store different data types in
the same memory location. You can define a union with many members, but only one
member can contain a value at any giventime.
Unions provide an efficient way of using the same memory location formulti-purpose.

4. How to define a union in C.


To define a union, you must use the union statement in very similar was as you did while
defining structure. The union statement defines a new data type, with more than one member
for your program. The format of the unionstatement is as follows:
union [union tag]
{
member definition;member definition;
...
member definition;
} [one or more union variables];

5. What are the uses of pointer?


a. Saves Memory Space
b. Used for dynamic memory allocation
c. Faster execution
d. Used to pass array of values to a function as a single argument

6. Define typedef .
a. The typedef keyword enables the programmer to create a new data type name by using an
existing data type.
b. By using typedef, no new data is created, rather an alternate name is given to a known data
type.

Syntax: typedef existing_data_type new_data_type;


12

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

7. What is an Address operator & Indirection operator?


Address operator: & -used to assign the address to a pointer variable,Referencing operator

8. How is pointer arithmetic done?


Pointer Arithmetic:
Valid operation
a. Pointer can be added with a constant
b. Pointer can be subtracted with a Constant
c. Pointer can be Incremented or DecrementedNot Valid
d. Two pointers can not be added,subtracted,multiplied or divided
Ex: int a=10
int *p=&a;p=p+1; 2000 10

2002

e. The pointer holds the address 2000. This value is added with 1.
f. The data type size of the constant is added with t

9. List the header files in ‘C’ language.


a. <stdio.h> contains standard I/O functions
b. <ctype.h> contains character handling functions
c. <stdlib.h> contains general utility functions
d. <string.h> contains string manipulation functions
e. <math.h> contains mathematical functions
f. <time.h> contains time manipulation functions

10. State the advantages of user defined functions over pre-defined function.
a. A user defined function allows the programmer to define the exact function of the module as
per requirement. This may not be the case with predefined function. It may or may not serve
the desired purpose completely.
b. A user defined function gives flexibility to the programmer to use optimal programming
instructions, which is not possible in predefined function.

11. Write the syntax for pointers to structure.


13

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

Struct S
{
char datatype1;int datatype2; float datatype3;
};
Struct S *sptr //sptr ia pointer to structure S

12. Is it better to use a macro or a function?


a. Macros are more efficient (and faster) than function, because their corresponding code is
inserted directly at the point where the macro iscalled.
b. There is no overhead involved in using a macro like there is in placing a call to a function.
However, macros are generally small and cannot handle large, complex coding constructs.
c. In cases where large, complex constructs are to handled, functions are more suited,
additionally; macros are expanded inline, which means that the code is replicated for each
occurrence of a macro.

13. Compare arrays and structures.

Arrays Structures

An array is a collection of data items of A structure is a collection of data items of


same data type. Arrays can only be different data types. Structures can be
declared. declared and defined.
There is no keyword for arrays. The keyword for structures is struct.

An array cannot have bit fields. A structure may contain bit fields.

An array name represents the address A structure name is known as tag. It is a


of the starting element. Shorthand notation of the declaration.

14

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

14. Difference between structure and union.

Structure Union

Every member has its own memory. All members use the same memory.

The keyword used is struct. The keyword used is union.

All members occupy separate memory


location, hence different interpretations Different interpretations for the same memory
of the same memory location are not location are possible.
possible. Consumes more space Conservation of memory is possible
compared to union.

15. What are the functions supports for dynamic memory allocation?
a. The functions supports for dynamic memory allocation are,
i. malloc()
ii. realloc()
iii. calloc()
iv. free()

16.What is the use of ‘typedef’’?


It is used to create a new data using the existing type.
Syntax: typedef data type name;
Example:
typedef int hours: hours hrs;/* Now, hours can be used as new datatype */

17 Write the syntax for pointers to structure.


Struct S
{
char datatype1;int datatype2; float datatype3;
};
Struct S *sptr; //sptr ia pointer to structure

18. Define self referential data structure


15

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

A self referential data structure is essentially a structure definition which includes at least one
member that is a pointer to the structure of its own kind. A chain of such structures can thus be
expressed as follows.
struct name {member 1;
member 2;
...
struct name *pointer;
};
The above illustrated structure prototype describes one node that comprises of two logical
segments. One of them stores data/information and the other one is a pointer indicating where
the next component can be found. .Several such inter-connected nodes create a chain of
structures.
19. Why files are needed?
When a program is terminated, the entire data is lost. Storing in a file will preserve your data
even if the program terminates.

20 .What are the types of Files ?


When dealing with files, there are two types of files you should knowabout:
i. Text files
ii. Binary files
Text files
Text files are the normal .txt files that you can easily create using Notepad or any simple text
editors.
When you open those files, you'll see all the contents within the file as plain text. You can
easily edit or delete the contents.
They take minimum effort to maintain, are easily readable, and provideleast security and takes
bigger storage space.
Binary files
Binary files are mostly the .bin files in your computer.
Instead of storing data in plain text, they store it in the binary form (0'sand 1's).
They can hold higher amount of data, are not readable easily and provides a better security
than text files.

21.Enlist the File Operations.


In C, you can perform four major operations on the file, either text orbinary:
16

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

1. Creating a new file


2. Opening an existing file
3. Closing a file
4. Reading from and writing information to a file

Working with files


When working with files, you need to declare a pointer of type file. This declaration is needed
for communication between the file and program. FILE *fptr;

22.How to open a file?


Opening a file is performed using the library function in the "stdio.h"
header file: fopen().
The syntax for opening a file in standard I/O is:ptr = fopen("fileopen","mode")

23.List the opening modes in standard I/O

r Open for reading. If the file does not exist, fopen()


returns NULL.
rb Open for reading in binaryIf the file does not exist, fopen()
mode. returns NULL.
If the file exists, its contents are
w Open for writing. overwritten. If
the file does not exist, it will be
created.
If the file exists, its contents are
wb Open for writing in binarymode. overwritten. If
the file does not exist, it will be
created.
Open for append. i.e, Data isIf the file does not exists, it will be
A added to created.
end of file.
Open for append in binaryIf the file does not exists, it will be
ab mode. i.e, created.
Data is added to end of file.

17

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

r+ Open for both reading and If the file does not exist, fopen()
writing. returns NULL.
Open for both reading and If the file does not exist, fopen()
rb+ writing in returns NULL.
binary mode.
If the file exists, its contents are
w+ Open for both reading and overwritten. If
writing. the file does not exist, it will be
created.
Open for both reading and If the file exists, its contents are
wb+ writing in overwritten. If
binary mode. the file does not exist, it will be
created.
a+ Open for both reading and If the file does not exists, it will be
appending. created.

Open for both reading and If the file does not exists, it will be
ab+ appending created.
in binary mode.

18

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

24. How to close a file?


The file (both text and binary) should be closed after reading/writing. Closing a file is performed
using library function fclose(). fclose(fptr); //fptr is the file pointer associated with file to be
closed.

i. Reading and writing to a text file


For reading and writing to a text file, we use the functions fprintf() andfscanf().
They are just the file versions of printf() and scanf(). The only differenceis that, fprint and
fscanf expects a pointer to the structure FILE.

25.What are two main ways a file can be organized?


Sequential Access — The data are placed in the file in a sequence like beads on a string.
Data are processed in sequence, one after another. To reach a particular item of data, all the
data that proceeds it first must be read.
Random Access — The data are placed into the file by going directly to the location in the file
assigned to each data item. Data are processed inany order.
A particular item of data can be reached by going directly to it, withoutlooking at any other data.

26. What is file?


A file is a semi-permanent, named collection of data.
A File is usuallystored on magnetic media, such as a hard disk or magnetic tape.
Semi-permanent means that data saved in files stays safe until it is deletedor modified.
Named means that a particular collection of data on a disk has a name, like mydata.dat and
access to the collection is done by using its name.

27.What are the various dynamic memory allocation functions?


malloc() - Used to allocate blocks of memory in required size of bytes. free () - Used to release
previously allocated memory space.
calloc() - Used to allocate memory space for an array of elements. realloac() - Used to modify
the size of the previously allocated memoryspace.

28.Write an example program for command line arguments.


19

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

#include <stdio.h>
int main( int argc, char *argv[] )
{
if( argc == 2 ) {
printf("The argument supplied is %s\n", argv[1]);
}
else if( argc > 2 )
{
printf("Too many arguments supplied.\n");
}
else
{
printf("One argument expected.\n");

20

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

21

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

29.Write the functions for random access file processing.


1. fseek()
2. ftell()
3. rewind()

30.Write short notes on fseek().fseek():


This function is used for seeking the pointer position in the file at thespecified byte.
Syntax: fseek( file pointer, displacement, pointer position);
Where
file pointer ----------------- It is the pointer which points to the file.
displacement ---------------- It is positive or negative.This is the number of bytes

which are skipped backward (if negative) or forward( if positive) from the current
position.This is attached with L because this is a long integer.

Pointer position:
This sets the pointer position in the file.

Value pointer position

0 Beginning 0

1 Current position

2 End of file

30. Give an example for fseek().


1) fseek( p,10L,0)
0 means pointer position is on beginning of the file,from this statement pointer position is
skipped 10 bytes from the beginning of the file.
2) fseek( p,5L,1)
1 means current position of the pointer position.From this statement pointer position is
skipped 5 bytes forward from the current position.
3) fseek(p,-5L,1)
From this statement pointer position is skipped 5 bytes backward from the current position.

22

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

31.Give an example for ftell().ftell()


This function returns the value of the current pointer position in the file.The value is count from the
beginning of the file.

Syntax: ftell(fptr); Where fptr - file pointer.

32.Give an example for rewind().rewind()


This function is used to move the file pointer to the beginning of thegiven file.
Syntax: rewind( fptr); Where fptr is a file pointer.

33.Write short notes on feof ().


The macro feof() is used for detecting whether the file pointer is at the end of file or not.It returns
nonzero if the file pointer is at the end of thefile otherwise it returns zero.

Syntax: feof(fptr);

Where fptr - file pointer .

23

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

PART B

1. What is a structure? Create a structure with data members of various types and declare two
structure variables. Write a program to read data into theseand print the same. Justify the need
for structured data type.
2. Write a C program to store the employee information using structure and search a particular
employee using Employee number.
3. Define and declare a nested structure to store date, which including day,month and year.
4. Define a structure called student tha
5.
6. t would contain name, regno and marks of five subjects and percentage. Write a C program to
read the details of name, regno and marks of five subjects for 30 students and calculate the
percentage and display the name, regno, marks of 30 subjects and percentageof each student.
7. Explain about array of structures and pointers in structures with exampleprogram
8. Write a C program to create mark sheet for students using self referentialstructure.
9. Discuss about dynamic memory allocation with suitable example C program.

24

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

UNIT III

LINEAR DATASTRUCTURES

PART A

1 What is a data structure?


● A data structure is a method for organizing and storing data which would allow
efficient
data retrieval and usage.
● A data structure is a way of organizing data that considers not only the items stored,
but
also their relationships to each other.
2 Why do we need data structures?
● Data structures allow us to achieve an important goal: component reuse.
● Once data structure has been implemented, it can be used again and again in
various applications.
3 List some common data structures.
● Stacks
● Queues
● Lists
● Trees
● Graphs
● Tables

25

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

4 How data structures are classified?


Data structures are classified into two categories based on how the data
items are
operated:
i. Primitive data structure
ii. Non-Primitive data structure
a. Linear data structure
b. Non-linear data structure

5 Differentiate linear and non-linear data structure.

Linear data structure Non-linear data structure


Data are arranged in linear or Data are not arranged in
sequential manner linear manner
Every items is related to its Every item is attached with
previous many other
and next item Items
Data items can be traversed Data items cannot be
in a traversed in a
single run. single run.
Implementation is easy Implementation is difficult.
Example: array, stack, Example: tree, graph
queue, linked
list
6 Define ADT (Abstract Data Type)
An abstract data type (ADT) is a set of operations and mathematical
abstractions , which
can be viewed as how the set of operations is implemented. Objects like lists, sets
and graphs, along with their operation, can be viewed as abstract data types, just

26

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

as integers, real numbers and Booleans.

Mention the features of ADT.


7 a. Modularity
i. Divide program into small functions
ii. Easy to debug and maintain
iii. Easy to modify
b. Reuse
i. Define some operations only once and reuse them in future
c. Easy to change the implementation

8 Define List ADT


A list is a sequence of zero or more elements of a given type. The list is
represented as
sequence of elements separated by comma. A1, A2, A3…..AN
Where N>0 and A is of type element

9 What are the ways of implementing linked list?


The list can be implemented in the following ways:
i. Array implementation
ii. Linked-list implementation
iii. Cursor implementation

27

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

10 What are the types of linked lists?


There are three types
i. Singly linked list
ii. Doubly linked list
iii. Circularly linked list

11 How the singly linked lists can be represented?

Each node has two elements


i. Data
ii. Next

12 How the doubly linked list can be represented?

Doubly linked list is a collection of nodes where nodes are connected by


forwarded and
backward link.
Each node has three fields:
1. Address of previous node
2. Data
3. Address of next node.

13 What are benefits of ADT?


a. Code is easier to understand
b. Implementation of ADT can be changed without requiring changes to the

28

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

program
that uses the ADT

14 When singly linked list can be represented as circular linked list?


In a singly linked list, all the nodes are connected with forward links to the
next nodes in
the list. The last node has a next field, NULL. In order to
implement the circularly linked

lists from singly linked lists, the last node’s next field is connected to the first
node.

15
When doubly linked list can be represented as circular linked list?
In a doubly linked list, all nodes are connected with forward and backward
links to the
next and previous nodes respectively. In order to implement circular linked lists
from
doubly linked lists, the first node’s previous field is connected to the last node and
the
last node’s next field is connected to the first node.

29

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

16 Where cursor implementation can be used?


The cursor implementation of lists is used by many languages such as
BASIC and
FORTRAN that do not support pointers. The two important features of the cursor
implementation of linked are as follows:
● The data are stored in a collection of structures. Each structure contains data and a
index to the next structure.
● A new structure can be obtained from the system’s global memory by a call to
cursor Space array.
List down the applications of List.
17 a. Representation of polynomial ADT
b. Used in radix and bubble sorting
c. In a FAT file system, the metadata of a large file is organized as a linked list of
FAT entries.
d. Simple memory allocators use a free list of unused memory regions, basically a
linked list with the list pointer inside the free memory itself.
18
What are the advantages of linked list?
a. Save memory space and easy to maintain
b. It is possible to retrieve the element at a particular index
c. It is possible to traverse the list in the order of increasing index.
d. It is possible to change the element at a particular index to a different
value,without affecting any other elements.

30

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

19 Mention the demerits of linked list


a. It is not possible to go backwards through the list
b. Unable to jump to the beginning of list from the end.
20 The polynomial equation can be represented with linked listas follows:

Coefficient Exponent Next node link

struct polynomial
{
int coefficient;int exponent;struct polynomial *next;
};

21 What are the operations performed in list?


The following operations can be performed on a list
i. Insertion
a. Insert at beginning
b. Insert at end
c. Insert after specific node
d. Insert before specific node
ii. Deletion
a. Delete at beginning
b. Delete at end
c. Delete after specific node
d. Delete before specific node
iii. Merging
iv. Traversal

31

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

22 What are the merits and demerits of array implementation of lists?


Merits
● Fast, random access of elements
● Memory efficient – very less amount of memory is required
Demerits
● Insertion and deletion operations are very slow since the elements should be
moved.

 Redundant memory space – difficult to estimate the size of array.

23 What is a circular linked list?


A circular linked list is a special type of linked list that supports traversing
from the end
of the list to the beginning by making the last node point back to
the head of the list.

32

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

24 What are the advantages in the array implementation of list?


a. Print list operation can be carried out at the linear time
b. Find Kth operation takes a constant time

25 What is the need for the header?


Header of the linked list is the first element in the list and it stores the
number of elements in the list. It points to the first data element of the list.

26 List three examples that uses linked list?


a. Polynomial ADT b.Radix sort c.Multi lists

27 List out the different ways to implement the list?


1. Array Based Implementation
2. Linked list Implementation
i. Singly linked list
ii. Doubly linked list
iii. Cursor based linked list

28 Write the routine for insertion operation of singly linked list.


Void Insert (ElementType X, List L, Position P)
{Position TmpCell; TmpCell=malloc(sizeof(struct Node)); if(TmpCell==NULL)
FatalError(“Out of space!!!”);
TmpCell->Element =X; TmpCell->Next=P->Next; P->Next=TmpCell;
}

29 Advantages of Array over Linked List.


1. Array has a specific address for each element stored in it and thus we can
access any memory directly.
2. As we know the position of the middle element and other elements are easily

33

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

accessible too, we can easily perform


BINARY SEARCH in array.

30 Disadvantages of Array over Linked List.


1. Total number of elements need to be mentioned or the memory allocation needs
to be done at the time of array creation
2. The size of array, once mentioned, cannot be increased in the program. If number
of elements entered exceeds the size of the array ARRAY OVERFLOW
EXCEPTION
occurs.

34

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

31 Advantages of Linked List over Array.


1. Size of the list doesn't need to be mentioned at the beginning of the program.
2. As the linked list doesn't have a size limit, we can go on adding new nodes
(elements) and increasing the size of the list to any extent.
32 Disadvantages of Linked List over Array.
1. Nodes do not have their own address. Only the address of the first node is
stored and in order to reach any node, we need to traverse the whole list from
beginning to the desired node.
2. As all Nodes don't have their particular address, BINARY SEARCH cannot be
performed

35

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

33 Define Stack.
A stack is an ordered list in which all insertions and deletions are
made at one end, called the top. It is an abstract data type and based
on the principle of
LIFO (Last In First Out).

34 What are the operations of the stack?

a. CreateStack/ InitStack(Stack) – creates an empty stack

b. Push(Item) – pushes an item on the top of the stack

c. Pop(Item) – removes the top most element from the stack

d. Top(Stack) – returns the first element from the stack

e. IsEmpty(Stack) – returns true if the stack is empty

35 Write the routine to push a element into a stack.

Push(Element X, Stack S)

{ if(IsFull(S)

{ Error(“Full Stack”); } else

S→Array[++S→TopOfStack]=X;

How the operations performed on linked list implementation of stack?


36 a. Push and pop operations at the head of the list.
b. New nodes should be inserted at the front of the list, so that they
become the top of the stack.
c. Nodes are removed from the front(top) of the stack.

36

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

36 What are the applications of stack?


The following are the applications of stacks
• Evaluating arithmetic expressions
• Balancing the parenthesis
• Towers of Hanoi
• Function calls Tree
traversal

37 What are the methods to implement stack in C?


The methods to implement stacks are:
● Array based
● Linked list based

38 How the stack is implemented by linked list?


It involves dynamically allocating memory space at run time while
performing stack
operations.
Since it consumes only that much amount of space is required for holding its
data
elements , it prevents wastage of memory space. struct stack
{
int element;
struct stack *next;
}*top;

39 Write the routine to pop a element from a stack.


int pop()
{ if(top==NULL)
{ printf(“\n Stack is empty.\n”);getch();exit(1);} else
{int temp;
temp=top→element; top=top→next; return temp; }}

37

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

40 Define queue.
It is a linear data structure that maintains a list of elements such
that insertion happens at
rear end and deletion happens at front end.
FIFO – First In First Out principle

41 What are the operations of a queue?


The operations of a queue are
● isEmpty()
● isFull()
● insert()
● delete()
● display()

42 Write the routine to insert a element onto a queue.


void insert(int element)
{
if(front==-1 )
{
front = rear = front +1; queue[front] =
element; return;
}
if(rear==99)
{
printf(“Queue is full”); getch();
return;
}
rear = rear +1; queue[rear]=element;
}

38

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

43 What are the types of queue?


The following are the types of queue:
● Double ended queue
● Circular queue
● Priority queue

44 Define double ended queue


● It is a special type of queue that allows insertion and deletion of
elements at both
Ends.
● It is also termed as DEQUE.

45 What are the methods to implement queue in C?


The methods to implement queues are:
● Array based
● Linked list based

39

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

46
How the queue is implemented by linked list?
• It is based on the dynamic memory management techniques which allow
allocation and
De-allocation of memory space at runtime.
Insert operation
It involves the following subtasks:
1. Reserving memory space of the size of a queue element in memory
2. Storing the added value at the new location
3. Linking the new element with existing queue
4. Updating the rear pointer
Delete operation
It involves the following subtasks:
1. Checking whether queue is empty
2. Retrieving the front most element of the queue
3. Updating the front pointer
4. Returning the retrieved value

47 Write the routine to delete a element from a queue


int del()
{int i;
if(front == NULL) /*checking whether the queue is empty*/
{return(-9999);} else
{i = front→element;front = front→next;return i;}
}

48 What are the applications of queue?


The following are the areas in which queues are applicable
a. Simulation
b. Batch processing in an operating systems
c. Multiprogramming platform systems
d. Queuing theory
e. Printer server routines
40

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

f. Scheduling algorithms like disk scheduling , CPU scheduling


g. I/O buffer requests

49
Define circular queue
A Circular queue is a queue whose start and end locations are logically
connected with
each other. That means the start location comes after the end location.

50
What are push and pop operations?
• Push – adding an element to the top of stack
• Pop – removing or deleting an element from the top of stack

41

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

51
What are enqueue and dequeue operations?
• Enqueue - adding an element to the queue at the rear end
If the queue is not full, this function adds an element to the back of the
queue, else it prints “OverFlow”.
void enqueue(int queue[], int element, int& rear, int arraySize) { if(rear ==
arraySize) // Queue is full
printf(“OverFlow\n”); else{
queue[rear] = element; // Add the element to the back rear++;
}
}
• Dequeue – removing or deleting an element from the queue at the front
end
If the queue is not empty, this function removes the element from the
front of the queue, else it prints “UnderFlow”. void dequeue(int queue[], int&
front, int rear) {
if(front == rear) // Queue is empty
printf(“UnderFlow\n”);
else {
queue[front] = 0; // Delete the front element front++;
}
}

42

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

52
STACK QUEUE
Insertion and deletion are made at Insertion at one end rear and
one end. deletion at other end front.
The element inserted last would be The element inserted first would be
removed first. So LIFO structure. removed first. So FIFO structure.

Full stack condition: Full stack condition: If(rear =


If(top==Maxsize) = Maxsize)
Physically and Logically full Logically full. Physically may or
stack may not be full.

How do you test for an empty queue?


To test for an empty queue, we have to check whether READ=HEAD where
REAR is a pointer pointing to the last node in a queue and HEAD is a pointer
that pointer to the dummy header. In the case of array implementation of
queue, the condition to be checked for an empty queue is READ<FRONT.

53 Define priority queue with diagram and give the operations.


Priority queue is a data structure that allows at least the following two
operations.
1. Insert-inserts an element at the end of the list called the rear.
2. DeleteMin-Finds, returns and removes the minimum element in the
priority Queue.

Operations: Insert, DeleteMin


How do you test for an empty stack?
54 To check if the stack is empty, we only need to check whether top
and bottom are the same number.
bool stack_empty(stack S) //@requires is_stack(S);
{ return S->top == S->bottom; }
43

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

55 What are the features of stacks?


● Dynamic data structures
● Do not have a fixed size
● Do not consume a fixed amount of memory
● Size of stack changes with
each push() and pop() operation.
Each push() and pop() operation increases and decreases the size of the
stack by 1, respectively.

56 Write a routine for IsEmpty condition of queue.


If a queue is empty, this function returns 'true', else it returns 'false'.
bool isEmpty(int front, int rear) { return (front == rear);
}

57

44

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

PART B

1.Explain the various operations of the list ADT with examples

2.Write the program for array implementation of lists

3.Write a C program for linked list implementation of list.

4.Explain the operations of singly linked lists

5.Explain the operations of doubly linked lists

6.Explain the operations of circularly linked lists

7.Explain the steps involved in insertion and deletion into a singly and doubly
linked list.

8..Explain Stack ADT and its operations

9.Explain array based implementation of stacks

10.Explain linked list implementation of stacks

11.Explain queue ADT

12.Explain array based implementation of queues

13.Explain linked list implementation of queues

45

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

UNIT IV

NON-LINEAR DATASTRUCTURES

S.
No.

1 Define non-linear data structure


Data structure which is capable of expressing more complex relationship
than that of physical adjacency is called non-linear data structure.

2 Define tree?
A tree is a data structure, which represents hierarchical relationship between
individual data items.

3 Define leaf?
In a directed tree any node which has out degree o is called a terminal
node or a leaf.

4 Explain the representations of priority queue.


Using Heap structure, Using Linked List

5 List out the steps involved in deleting a node from a binarysearch tree.
1. t has no right hand child node t->r == z
2. t has a right hand child but its right hand child node has no left sub tree
t->r->l == z
3.t has a right hand child node and the right hand child node has a left hand child
node t->r->l != z

6 Convert the infix expression (A-B/C)*(D/E-F) into a postfix.


Postfix: ABC/-DE/F-*

46

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

7 What are the steps to convert a general tree into binary tree?
* use the root of the general tree as the root of the binary tree
* determine the first child of the root. This is the leftmost node in the general tree at
the next
level
* insert this node. The child reference of the parent node refers to this node
* continue finding the first child of each parent node and insert it below the parent
node with the
child reference of the parent to this node.
* when no more first children exist in the path just used, move back to the parent of
the last node
entered and repeat the above process. In other words, determine the
first sibling of the last
node entered.
* complete the tree for all nodes. In order to locate where the node fits you must
search for the
first child at that level and then follow the sibling references to a nil where the next
sibling can
be inserted. The children of any sibling node can be inserted by locating the parent
and then
inserting the first child. Then the above process is repeated.

8 What is meant by directed tree?


directed tree is an acyclic diagraph which has one node called its root with in
degree o while all other nodes have in degree I.

9 What is a ordered tree?


In a directed tree if the ordering of the nodes at each level is prescribed then such
a tree is called ordered tree.
10 What are the applications of binary tree?
1. Binary tree is used in data processing.
2. File index schemes

47

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

3. Hierarchical database management system

11 What is meant by traversing?


Traversing a tree means processing it in such a way, that each node is visited only
once.

12 What are the different types of traversing?


The different types of traversing are
a. Pre-order traversal-yields prefix form of expression.
b. In-order traversal-yields infix form of expression.
c. Post-order traversal-yields postfix form of expression.

13 What are the two methods of binary tree implementation?

Two methods to implement a binary tree are


a. Linear representation.
b. Linked representation

14 What is a balance factor in AVL trees?


Balance factor of a node is defined to be the difference between the height of the
node's left subtree and the height of the node's right subtree.

15 What is meant by pivot node?


The node to be inserted travel down the appropriate branch track along the way of the
deepest level node on the branch that has a balance factor of +1 or -1 is called pivot
node.
What is the length of the path in a tree?
16 The length of the path is the number of edges on the path. In a tree there is exactly
one path form the root to each node.

48

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

17 Define expression trees?


aves of an expression tree are operands such as constants or variable names
and the other nodes contain operators.

18 What is meant by binary search tree?


Binary Search tree is a binary tree in which each internal node x stores an
element such that the element stored in the left sub tree of x are less than or equal to
x and elements stored in the right sub tree of x are greater than or equal to x.

19 What is the various representation of a binary tree?


Tree Representation
Array representation
Linked list representation

20 List the application of tree.


(i) Electrical Circuit
ii) Folder structure
a. Binary tree is used in data processing.
b. File index schemes
c. Hierarchical database management system

21 Define binary tree and give the binary tree node structure.

49

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

22 Give the pre & postfix form of the expression (a + ((b*(c-e))/f).

23 Define complete binary tree?


A complete binary tree of depth d is the strictly binary tree all of whose are
at level d.

24 Define hashing.
Hash function takes an identifier and computes the address of that identifier in the hash
table using some function

25 What is the need for hashing?


Hashing is used to perform insertions, deletions and find in constant average time.

26 Define hash function?


Hash function takes an identifier and computes the address of that identifier in the hash
table using some function.

27 List out the different types of hashing functions?


The different types of hashing functions are,
a. The division method
b. The mind square method
c. The folding method
d. Multiplicative hashing
e. Digit analysis

50

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

28 What are the problems in hashing?


a. Collision
b. Overflow

29 What are the problems in hashing?


When two keys compute in to the same location or address in the hash table through
any of the hashing function then it is termed collision.

30 Compare the various hashing techniques.


Technique Load Factor
Separate chaining - close to 1
Open Addressing - should not exceed 0.5
Rehashing - reasonable load factor

31 Define collision in hashing.


When two different keys or identifiers compute into the same location or address in the
hash table through any of the hashing functions, then it is termed Collision.

32 Define Double Hashing.


Double Hashing is a collision-resolution technique used in open addressing category.
In double hashing, we apply a second hash function to x and probe at a distance of
hash2 (x),
2hash2 (x)… , and so on.

33 What are applications of hashing?


The applications of hashing are,
● Compliers use hash table to keep track of declared variables on source code.
● Hash table is useful for any graph theory problem, where the nodes have
real names instead of numbers.
● Hash tables are used in programs that play games.
● Online spelling checkers use hashing.

51

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

34 What are the collision resolution methods?


The following are the collision resolution methods
● Separate chaining
● Open addressing
● Multiple hashing

35 Define separate chaining


It is an open hashing technique. A pointer field is added to each record location, when
an
overflow occurs; this pointer is set to point to overflow blocks making a linked list. In
this method, the table can never overflow, since the linked lists are only extended upon
the arrival of new
keys.

36 What is open addressing?


Open addressing is also called closed hashing, which is an alternative to resolve the
Collisions with linked lists. In this hashing system, if a collision occurs, alternative cells
are tired until an empty cell is found.
There are three strategies in open addressing:
● Linear probing
● Quadratic probing
● Double hashing

37 What is Rehashing?
If the table is close to full, the search time grows and may become equal to the table
size.
When the load factor exceeds a certain value (e.g. greater than 0.5) we do
Rehashing: Build a second table twice as large as the original and rehash there all the
keys of the original table.
Rehashing is expensive operation, with running time O(N) However, once done, the
new hash table will have good
performance.

52

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

38 What is Extendible Hashing?


Used when the amount of data is too large to fit in main memory and external storage
is used.
N records in total to store, M records in one disk block
The problem: in ordinary hashing several disk blocks may be examined to find an
element -
a time consuming process.
Extendible hashing: no more than two blocks are examined.

PART B

1 Define Tree. Explain the tree traversals with algorithms and


examples.

2 Construct an expression tree for the expression (a + b * c)


+((d * e + 1) * g). Give the outputs when you apply preorder, inorder and postorder
traversals.

3 Explain binary search tree ADT in detail.

4 Explain hashing

5 Explain open addressing and its types

6 detail about separate chaining.

7 Explain Rehashing in detail.

8 Explain Extendible hashing in detail.

53

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

UNIT V
SORTING AND SEARCHING TECHNIQUES

1 Define sorting
Sorting arranges the numerical and alphabetical data present in a list in a
specific order or sequence. There are a number of sorting techniques
available. The algorithms can be chosen based on the following factors
● Size of the data structure
● Algorithm efficiency
Programmer’s knowledge of the technique

2 Mention the types of sorting


● Internal sorting
● External sorting

3 What do you mean by internal and external sorting?


An internal sort is any data sorting process that takes place entirely within
the main memory of a computer. This is possible whenever the data to
be sorted is small enough to all be held in the main memory.
External sorting is a term for a class of sorting algorithms that can handle
massive amounts of data. External sorting is required when the data
being sorted do not fit into the main memory of a computing device
(usually RAM) and instead they
must reside in the slower external memory (usually a hard drive).

4 How the insertion sort is done with the array?


It sorts a list of elements by inserting each successive element in the
previously sorted
Sub list.
Consider an array to be sorted A[1],A[2],….A[n]

54

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

a. Pass 1: A[2] is compared with A[1] and placed them in sorted order.
b. Pass 2: A[3] is compared with both A[1] and A[2] and inserted at an appropriate
place. This makes A[1], A[2],A[3] as a sorted sub array.
c. Pass n-1: A[n] is compared with each element in the sub array
A [1], A [2] …A [n-1] and inserted at an appropriate position.

5 what is insertion sort? How many passes are required for theelements to be sorted ?
one of the simplest sorting algorithms is the insertion sort. Insertionsort consist of N-1 passes .
For pass P=1 through N-1 , insertion sort ensures that the elements in positions 0 through P-1
are in sorted order .It makes use of the fact that elements in position 0
through P-1 are already known to be in sorted order .

6 Write the function in C for insertion sort ?


void insertionsort(elementtype A[ ] , int N)
{
int j, p; elementtype tmp;
for(p=1 ; p <N ;p++ )
{
tmp = a[ p] ;
for ( j=p ; j>0 && a [ j -1 ] >tmp ;j--) a [ j ]=a [j-1 ] ;
a [ j ] = tmp ;
}}

7 Differentiate between merge sort and quick sort? Merge sort quick
sort
1. Divide and conquer strategy Divide and conquer strategy
2. Partition by position Partition by value

8 Mention some methods for choosing the pivot element in quick sort?
1. Choosing first element
2. Generate random number
3. Median of three

55

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

9 What are the three cases that arise during the left to right scanin quick sort?
1. I and j cross each other
2. I and j do not cross each other
3. I and j points the same position

10 What is the need of external sorting?


External sorting is required where the input is too large to fit into memory. So external
sorting Is necessary where the program is toolarge

11 What is sorting?
Sorting is the process of arranging the given items in a logical order. Sorting is an example
where the analysis can be preciselyperformed.

12 What is merge sort?


The merge sort algorithm is a classic divide conquer strategy. Theproblem is divided into
two arrays and merged into single array

13 What does internal sorting mean?


Internal sorting is a process of sorting the data in themain memory

14 What are the various factors to be considered in deciding asorting algorithm?


Factors to be considered in deciding a sorting algorithm are,
1. Programming time
2. Executing time for program
3. Memory or auxiliary space needed for the programsenvironment.

15 Is the heap sort always better than the quick sort?


No, the heap sort does not perform better than the quick sort.
Only when array is nearly sorted to begin with the heap sort algorithm gains an advantage. In
such a case, the quick deterioratesto its worst performance of O (n2).

56

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

16 Name some of the external sorting methods.


Some of the external sorting methods are,
1. Polyphone sorting
2. Oscillation sorting
3. Merge sorting
data with integer keys by grouping keys by the individual digits
which share the same significant position

17 Define searching
Searching refers to determining whether an element is present in a given list of
elements
or not. If the element is present, the search is considered as successful, otherwise it is
considered as an unsuccessful search. The choice of a searching technique is based on the
followingfactors
a. Order of elements in the list i.e., random or sorted
b. Size of the list

18 Mention the types of searching


The types are
● Linear search
● Binary search

19 What is meant by linear search?


Linear search or sequential search is a method for finding aparticular value in a list
that consists of checking every one of its elements, one at a timeand in sequence, until
the desired one is found.

57

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

20 What is binary search?


For binary search, the array should be arranged in ascending or descending order.
In each step, the algorithm compares the search key value with the middle element of the
array. If the key match, then a matching element has been found and its index, or Position, is
returned.
Otherwise, if the search key is less than the middle element, then the algorithm repeats its
action on the sub-array to the left of the middle element or, if the search key is greater, on the
sub-array to
the right.

PART B
1
Explain the sorting algorithms

2 Explain the searching algorithms


3 Write a C program to sort the elements using quick sort, insertion and merge sort.

4 Write a C program to perform searching operations using linear and binary search.

58

Downloaded by Omprakash D ([email protected])


lOMoARcPSD|11267200

59

Downloaded by Omprakash D ([email protected])

You might also like