You are on page 1of 18

DATA STRUCTURES USING C/UNIT3

UNIT – II

2 marks:

1. Define searching. Name 2 types of searching techniques.


Searching refers to the operation of finding the location of a given item in a
collection of items. 2 types of searching techniques are

1)Binary search
2)linear search

2. What is the difference between linear and binary search?

In linear search input data need not to be in sorted. In binary search input data
need to be in sorted order.

3.Write one advantage and disadvantage of binary search.

Advantages:

1)It is better than a linear search algorithm since its run time complexity is
O(logN).

Disadvantages:

1)The list must be sorted


2) One must have direct access to the middle element in any sub list.

4. What do you mean by dynamic memory allocation? Write any 2 functions


used in ‘C’ for dynamic memory allocation.
The concept of dynamic memory allocation in c language enables the C
programmer to allocate memory at runtime. Dynamic memory allocation in c
language is possible by 4 functions of stdlib.h header file.

1. malloc()
2. calloc()
3. realloc()
4. free()

IISEM/BCA/SDC/NAMITHA Page 1
DATA STRUCTURES USING C/UNIT3

5. What is linkedlist?
A linked list is a linear data structure, in which the elements are not stored at
contiguous memory locations. A linked list is a linear data structure, in which
the elements are not stored at contiguous memory locations.
6. What is free storage list?
A free list is a data structure used in a scheme for dynamic memory
allocation. It operates by connecting unallocated regions of memory
together in a linked list, using the first word of each unallocated region as a
pointer to the next.
7. What is garbage collection?
Garbage collection is a term used in computer programming to describe the
process of finding and deleting objects which are no longer being
referenced by other objects. In other words, garbage collection is the
process of removing any objects which are not being used by any other
objects.
8. What role does the AVAIL list play in a linkedlist?
It is a list of free nodes in the memory. Whenever a new node is to be
inserted in the linked list, a free node is taken from the AVAIL List and is
inserted in the linked list. Similarly, whenever a node is deleted from the
linked list, it is inserted in the AVAIL List. So that it can be used in future.
9. How is singly linked list terminated? Give diagram.
The list is null-terminated. The empty list is simply the null pointer.

10. Define circularly linked list. Give its diagram.

A circular linked list is a type of linked list in which the first and the last
nodes are also connected to each other to form a circle.

11.What is two-way list? Give its diagram.

IISEM/BCA/SDC/NAMITHA Page 2
DATA STRUCTURES USING C/UNIT3

two-way list or 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.

12. Write the node diagram of two-way list.


N is divided into three parts:- information field(data), Forward link(next)-
which points to the next node and Backward link(prev)-which points to the
previous node.

13. What is underflow and overflow with respect to linked list?

When new data is to be inserted into the data structure but there is no
available space i.e. free storage list is empty this situation is called overflow.
When we want to delete data from a data structure that is empty this
situation is called underflow.

14. Write the advantages of linked list over arrays.

Advantages of Linked List over Array


1) Dynamic Data Structure:
2) No Memory Wastage:
3) Implementation:
4) Insertion and Deletion Operation:
1) Memory Usage:
2) Random Access:
3) Reverse Traversal:

IISEM/BCA/SDC/NAMITHA Page 3
DATA STRUCTURES USING C/UNIT3

Long Answer Questions

1) What is linear search? Write the algorithm for linear search.


(Linear Search) LINEAR(DATA,N,ITEM,LOC)
Here DATA is a linear array with N elements, and ITEM is a given item of
information.
This algorithm finds the location LOC of ITEM in DATA, or LOC:= 0 if the
search is
unsuccessful
1. Set DATA[N+1] := ITEM
2. Set LOC := 1
3. Repeat while DATA[LOC] != ITEM
Set LOC := LOC + 1
[ end of loop]
4. If LOC := N +1, then : Set LOC := 0
5. Exit.

2) Explain binary search with an example.

This is another method of accessing a list. The entries in a list are stored in the
increasing order.
This is an efficient technique for searching an ordered sequential list of elements.
In this method,
we first compare the key with the element in the middle position of the list. If there
is a match,
then the search is successful. If the element is less than the middle key, the desired
element must
lie in the lower half of the list. if it is greater, then the desired element will be in
the upper half of
the list. We repeat this procedure on the lower half or upper half the list.

IISEM/BCA/SDC/NAMITHA Page 4
DATA STRUCTURES USING C/UNIT3

3) Write an algorithm for binary search.

ALGORITHM
(Binary Search) BINARY(DATA, LB, UB, ITEM,LOC)
1. Set BEG := LB, END := UB and MID = INT((BEG + END)/2)
2. Repeat steps 3 and 4 while BEG <= END and DATA[MID]!=ITEM
3. If ITEM < DATA[MID], then:
Set END := MID – 1
else:
Set BEG := MID + 1
4. Set MID := INT((BEG + END)/ 2)
5. If DATA[MID] = ITEM, then:
set LOC := MID
else
Set LOC := NULL
6. exit

4) Explain any ‘2’ dynamic memory allocation functions used in ‘C’ with
syntax and example

The concept of dynamic memory allocation in c language enables the C


programmer to allocate memory at runtime. Dynamic memory allocation in c
language is possible by 4 functions of stdlib.h header file.

IISEM/BCA/SDC/NAMITHA Page 5
DATA STRUCTURES USING C/UNIT3

1. malloc()
2. calloc()

The malloc() function allocates single block of requested memory.

It doesn't initialize memory at execution time, so it has garbage value initially.

It returns NULL if memory is not sufficient.

The syntax of malloc() function is given below:

ptr=(cast-type*)malloc(byte-size)
Example:-

int *ptr ;
ptr = malloc (10 *sizeof(int))

The calloc() function allocates multiple block of requested memory.

It initially initialize all bytes to zero.

It returns NULL if memory is not sufficient.

The syntax of calloc() function is given below:

ptr=(cast-type*)calloc(number, byte-size)
Example:-

int *ptr ;
ptr = (int *) calloc (10, 2);

5) Write a note on memory allocation and de-allocation function


Memory allocation is the process of setting aside sections of memory in a
program to be used to store variables, and instances of structures and classes.

IISEM/BCA/SDC/NAMITHA Page 6
DATA STRUCTURES USING C/UNIT3

There are two types of memory allocations possible in C:

1. Compile-time or Static allocation.


2. Run-time or Dynamic allocation (using pointers).

C provides the following dynamic allocation and de-allocation functions :

 malloc( )
 calloc( )
 free( )
 realloc( )

The Malloc( ) Function


The malloc( ) function allocates a block of memory in bytes. The user should
explicitly give the block sixe it requires of the use. The malloc( ) is like a request
to the RAM of the system to allocate memoty.
Syntax:-

malloc (number of elements * size of each element) ;

Example:-

int *ptr ;
ptr = malloc (10 *sizeof(int))

The Calloc( ) Function


This function works exactly similar to malloc( ) function except for the fact that it
needs two arguments as against one argument required by malloc( ).
Example:-

int *ptr ;
ptr = (int *) calloc (10, 2);

The Free( ) Function


The free( ) function is used to de-allocate the previously allocated memory using
malloc( ) or calloc( ) functions.

IISEM/BCA/SDC/NAMITHA Page 7
DATA STRUCTURES USING C/UNIT3

Syntax:-

free (ptr_var); // Where ptr_var is the pointer in which the address of the allocated
memory block is assigned.

The Realloc( ) Function


This function is used to resize the size of memory block, which is already
allocated. It found use of in two situations :

 If the allocated memory block is insufficient for current application.


 If the allocated memory is much more than what is required by the current
application.

Syntax:-

ptr_var = realloc (ptr_var, new_size);


6) Write a note on dynamic memory management

When a C program is compiled, the compiler allocates memory to store different


data elements such as constants, variables (including pointer variables), arrays and
structures. This is referred to as compile-time or
static memory allocation. There are several limitations in such static memory
allocation:
1. This allocation is done in memory exclusively allocated to a program, which is
often limited in size.
2. A static array has fixed size. We cannot increase its size to handle situations
requiring more elements. As a result, we will tend to declare larger arrays than
required, leading to wastage of memory. Also, when fewer array elements are
required, we cannot reduce array size to save memory.
The C language provides a very simple solution to overcome these
limitations: dynamic memory allocation in which the memory is allocated at run-
time, i. e., during the execution of a program. Dynamic memory management
involves the use of pointers and four standard library functions, namely, malloc,
calloc, realloc and free.
7)What is linked list ? Explain the different types

IISEM/BCA/SDC/NAMITHA Page 8
DATA STRUCTURES USING C/UNIT3

A linked list is a non sequential collection of data items. For every data item in the
linked list,
there is an associated pointer that gives the memory location of the next data item
in the
linked list.

Types of linked list:Basically we can put linked lists into following 4 types.
• Singly linked list
• Doubly linked list
• Singly circular linked list
• Doubly circular linked list
1. Singly Linked List:Each node has a single link to another node is called Singly
Linked List.

Doubly Linked List


• Doubly linked list is a sequence of elements in which every node has link to its
previous node and next node.

Circular Linked List


• Circular linked list is similar to singly linked list. The only difference is that in
circular linked list, the last node points to the first node in the list.

IISEM/BCA/SDC/NAMITHA Page 9
DATA STRUCTURES USING C/UNIT3

Doubly Circular Linked List


• Doubly circular linked list is a linked data structure which consists of a set of
sequentially linked records called nodes.
• Doubly circular linked list can be conceptualized as two singly linked lists
formed
from the same data items, but in opposite sequential orders.

8) Explain the memory representation of linked list.

IISEM/BCA/SDC/NAMITHA Page 10
DATA STRUCTURES USING C/UNIT3

9) Write an algorithm for traversing a linked list.

Let LIST be a linked list in memory stored in linear arrays INFO and LINK with
START pointing to the first element and NULL indicating the end of LIST.
suppose
we want to traverse LIST in order to process each node exactly once.
• Algorithm uses a pointer variable PTR which points to the node that is currently
being processed. Accordingly, LINK[PTR] points to the
• Next node to be processed. until PTR=NULL, Which signals the end of the list.
Algorithm
1.PTR=START
2.While PTR != NULL

IISEM/BCA/SDC/NAMITHA Page 11
DATA STRUCTURES USING C/UNIT3

3.Process INFO[PTR]
4.PTR = LINK[PTR]
5.Return

10) Write an algorithm for searching an item in a linked list.

Let LIST be a linked list in memory. Suppose a specific ITEM of information is


given. searching
algorithms for finding the location LOC of the node where ITEM for appears in
LIST. Assume
element in list are not in sorted order.if ITEM doesn’t found SET LOC=NULL.

1.Set PTR:=START.

2.REPEAT STEP 3 WHILE(PTR!=NULL)

3.IF ITEM=INFO[PTR],THEN:

SET LOC:=PTR AND EXIT.

ELSE:

PTR:=LINK[PTR] [PTR now point to the next node]

[END IF]

[END of step 2LOOP]

4.SET LOC:=NULL [searching is unseccussfull]

5.EXIT.

11)Write an algorithm to insert an item(node) at the beginning of a linked list.

Insert a node at beginning


Suppose our linked list is not necessarily sorted and there is no reason to insert a
new
node in any special place in the list.Then the easiest place to insert the node is at
the
beginning of the list.
ALGORITHM:
INSFIRST(INFO,LINK,START,AVAIL,ITEM)
This algorithm insert ITEM as the first node in the list.
IISEM/BCA/SDC/NAMITHA Page 12
DATA STRUCTURES USING C/UNIT3

1.[OVERFLOW?]if AVAIL=NULL,then:
Write:OVRFLOW, and Exit.
2.[REMOVE FIRST NODE FROM AVAIL list]
Set NEW:=AVAIL and AVAIL:=LINK[NEW].
3.set INFO[NEW]:=ITEM[Copies new data into new node].
4.Set LINK[NEW]:=START.[New node now points to orginal first node]
5.Set START:=NEW [Changes START so it points to the new node.]
6.Exit.

12) Write an algorithm to insert an item after a given node of a linked list.

Insert a node after the node with a given location.


suppose we are given the value of loc where either LOC is the location of NODE
A in a
linked LIST OR LOC=NULL.The following is an algorithm which inserts ITEM
into
LIST do that ITEM Follows NODE A or when LOC=NULL.
ALGORITHM
INSLOC(INFO,LINK,START,AVAIL,LOC,ITEM)
1.IF(AVAIL=NULL),THEN WRITE:OVERFLOW AND EXIT.
2.[REMOVE A NODE FROM AVAIL]
NEW:=AVAIL AND AVAIL:=LINK[NEW]
3.SET INFO[NEW]:=ITEM
4.IF LOC=NULL,THEN: [FIRST LOCTION]
SET LINK[NEW]:=START AND START:=NEW.
5. ELSE:[INSERT A NODE WITH LOC.]
SET LINK[NEW]:=LINK[LOC] AND LINK[LOC]:=NEW.
[END IF]
6.EXIT

13) Write an algorithm to insert an item at the end of a linked list.

// algorithm to insert new node at the end


Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 10
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
IISEM/BCA/SDC/NAMITHA Page 13
DATA STRUCTURES USING C/UNIT3

Step 3: SET AVAIL = AVAIL -> NEXT


Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET NEW_NODE -> NEXT = NULL
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR -> NEXT != NULL
Step 8: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 9: SET PTR -> NEXT = NEW_NODE
Step 10: EXIT

14) Write an algorithm to delete the first node from the linked list.

Algorithm which deletes the first node


All of deleted node will return the memory space of the deleted node N to the
beginning of the avail list.
Link[LOC]=avail and then AVAIL=LOC Where LOC is the location of the
deleted nodeN.
ALGORITHM
DEL(INFO,LINK,LIST,AVAIL,LOC,LOCP)
This algorithm delete a NODE N it present in LOC. And LOCP is its previous
node.
when N is staring which we are going to delete.
1.IF LOCP=NULL,THEN:
SET START:=LINK[START]. [Deletes first node.]
2.[Return deleted node to the AVAIL list.]
SET LINK[LOC]:=AVAIL AND AVAIL:=LOC
3.EXIT.

15) Write an algorithm to delete a node following a given node of a linked list.

Algorithm which delete node following a given node.


• All of deleted node will return the memory space of the deleted node N to the
beginning of the avail list.
IISEM/BCA/SDC/NAMITHA Page 14
DATA STRUCTURES USING C/UNIT3

Link[LOC]=avail and then AVAIL=LOC Where LOC is the location of the


deleted nodeN.
ALGORITHM
DEL(INFO,LINK,LIST,AVAIL,LOC,LOCP)
This algorithm delete a NODE N it present in LOC. And LOCP is its previous
node.
when N is staring which we are going to delete.
1.IF LOCP=NULL,THEN:
SET START:=LINK[START]. [Deletes first node.]
ELSE:
SET LINK[LOCP]=LINK[LOC] [Deletes node N.]
[End of IF structure].
2.[Return deleted node to the AVAIL list.]
SET LINK[LOC]:=AVAIL AND AVAIL:=LOC
3.EXIT.

16) Write a note on two-way lists or doubly linked list.

DOUBLY LINKED LISTS( two-way lists)


Two-Way List Two-way list,which can be traversed in two directions :
In the usual forward direction from the beginning of list to the end, or in backward
direction
from the end of the list to the beginning.
A two-way list is a linear collection of data element called nodes where each node
N is
divided into three parts:
– A information field INFO which contains the data of N
– A pointer field FORW which contains the location of the next node in the list
– A pointer field BACK which contains the location of the preceding node in the

Two-way header lists

IISEM/BCA/SDC/NAMITHA Page 15
DATA STRUCTURES USING C/UNIT3

The advantages of a two-way list and a circular header list may be combined into a
two-way circular header list as pictured. the list is circular because the two end
nodes point back to the header node.two-way list requires only one list pointer
variable START,which points to the header node.This is because the two pointers
in the header node point to the two ends of the list.

OPERATION ON TWO-WAY LIST


Traversing
Traverse: LIST in order to process each node exactly once. there is no advantage
in two-way list traversing operation.
Searching: the advantage of two-way list in searching is we can search ITEM in
the
backward direction if we have reason to suspect that ITEM appears near the end of
the list.
Deleting: Suppose delete node in two-way list then BACK[LOC] AND
FORW[LOC].
Changing pair pointers.
FORW[BACK[LOC]]:=FORW[LOC] AND
BACK[FORW[LOC]]:=BACK[LOC]
Inserting: suppose we are given the locations LOCA and LOCB of adjacent nodes
A and B
in LIST.we want to insert a given ITEM OF information between nodes A and B.
FORW[LOCA]:=NEW, FORW[NEW]:=LOCB
BACK[LOCB]:=NEW , BACK[NEW]:=LOCA

17) Write a note on header linked list

IISEM/BCA/SDC/NAMITHA Page 16
DATA STRUCTURES USING C/UNIT3

A header node is a special node that is found at the beginning of the list. A list
that contains this type of node, is called the header-linked list. This type of list is
useful when information other than that found in each node is needed.

Types of Header Linked List


1. Grounded Header Linked List
It is a list whose last node contains the NULL pointer. In the header linked list
the start pointer always points to the header node. start -> next =
NULL indicates that the grounded header linked list is empty. The operations
that are possible on this type of linked list are Insertion, Deletion, and
Traversing.

Circular Header Linked List


A list in which last node points back to the header node is called circular linked
list. The chains do not indicate first or last nodes. In this case, external pointers
provide a frame of reference because last node of a circular linked list does not
contain the NULL pointer. The possible operations on this type of linked list
are Insertion, Deletion and Traversing

IISEM/BCA/SDC/NAMITHA Page 17
DATA STRUCTURES USING C/UNIT3

18) Explain Circular linked list.

It is just a singly linked list in which the link field f the last node contains the
address of the first node of the list. That is the link field of the last node does not
point to NULL. It points to back to the beginning of the linked list.

Insertion into and deletion from circularly linked list follows the same pattern used
in a singly linked list.
The last node points to the first node .therefore when inserting or deleting the last
node,beside updating the end pointer in the header,we must also point the linked to
the first to the first node

IISEM/BCA/SDC/NAMITHA Page 18

You might also like