DS Unit 2 Solved QB
DS Unit 2 Solved QB
UNIT – II
2 marks:
1)Binary search
2)linear search
In linear search input data need not to be in sorted. In binary search input data
need to be in sorted order.
Advantages:
1)It is better than a linear search algorithm since its run time complexity is
O(logN).
Disadvantages:
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.
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.
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.
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.
IISEM/BCA/SDC/NAMITHA Page 3
DATA STRUCTURES USING C/UNIT3
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
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
IISEM/BCA/SDC/NAMITHA Page 5
DATA STRUCTURES USING C/UNIT3
1. malloc()
2. calloc()
ptr=(cast-type*)malloc(byte-size)
Example:-
int *ptr ;
ptr = malloc (10 *sizeof(int))
ptr=(cast-type*)calloc(number, byte-size)
Example:-
int *ptr ;
ptr = (int *) calloc (10, 2);
IISEM/BCA/SDC/NAMITHA Page 6
DATA STRUCTURES USING C/UNIT3
malloc( )
calloc( )
free( )
realloc( )
Example:-
int *ptr ;
ptr = malloc (10 *sizeof(int))
int *ptr ;
ptr = (int *) calloc (10, 2);
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.
Syntax:-
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.
IISEM/BCA/SDC/NAMITHA Page 9
DATA STRUCTURES USING C/UNIT3
IISEM/BCA/SDC/NAMITHA Page 10
DATA STRUCTURES USING C/UNIT3
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
1.Set PTR:=START.
3.IF ITEM=INFO[PTR],THEN:
ELSE:
[END IF]
5.EXIT.
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.
14) Write an algorithm to delete the first node from the linked list.
15) Write an algorithm to delete a node following a given node of a linked list.
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.
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.
IISEM/BCA/SDC/NAMITHA Page 17
DATA STRUCTURES USING C/UNIT3
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