class notes
class notes
1) Searching
Successful search: If search key is 25, it is present in the above list and we return
its position 3 indicating “Successful search”.
Unsuccessful search: If search key is 50, it is not present in the above list and we
return -1 indicating “Unsuccessful search”
But, once the value of i is greater than or equal to 5, it is an indication that item is not
present and we return -1. The code for this can be written as:
return -1; /*Unsuccessful search */
Note: The terminal condition in the for loop i.e., i < 5 can be replaced by i < n for a
general case. Now, the code can be written as:
for (i = 0; i < n; i++)
{
1
if (key == a[i] ) return i; /* Successful search */
}
return – 1; /* Unsuccessful search */
Binary search
The general idea used in binary search is similar to the way we search for the telephone number of
a person in the telephone directory. Obviously, we do not use linear search. Instead, we open the
book from the middle and the name is compared with the element at the middle of the book. If the
name is found, the corresponding telephone number is retrieved and the searching has to be
stopped. If the name to be searched is less than the middle element, search towards left otherwise,
search towards right. The procedure is repeated till key item is found or the key item is not found.
2
After executing the statement, if key is same as a[mid], we return the position of key. If key is not
same as a[mid], it may be present either in the left part of the array or right part of the array and
leads to following 2 cases:
Case 1) key towards left of mid: If key is less than the middle element, the left part of
array has to be compared from low to mid-1 as shown in figure below:
i.e.,
4
1)malloc(size)
“What is the purpose of using malloc?” This function allows the program to allocate a block of
memory space as and when required and the exact amount needed during execution. The size of
the block is the number of bytes specified in the parameter.
#include <stdlib.h> /* Prototype definition of malloc() is available */
……..
ptr = (data_type *) malloc(size);
…….
where ,
ptr is a pointer variable of type data_type
data_type can be any of the basic data type or user defined data type
size is the number of bytes required
On successful allocation, the function returns the address of first byte of allocated memory.
Since address is returned, the return type is a void pointer. By type casting appropriately we can
use it to store integer, float etc.
If specified size of memory space is not available, the condition is called “overflow of
memory”. In such case, the function returns NULL.
“What will happen if the following program segment is executed?
int *ptr;
ptr = (int *) malloc (10);
2) calloc(n, size)
This function is used to allocate multiple blocks of memory. Here, calloc – stands for contiguous
allocation of multiple blocks and is mainly used to allocate memory for arrays. The number of
blocks is determined by the first parameter n. The size of each block is equal to the number of
bytes specified in the parameter i.e., size. Thus, total number of bytes allocated is n*size and all
bytes will be initialized to 0.
The syntax is shown below:
“What is the purpose of using calloc?”
#include <stdlib.h>
……..
……..
ptr = (data_type *) calloc(n, size);
……..
……..
where
ptr is a pointer variable of type data_type
data_type can be any of the basic data type or user defined data type
n is the number of blocks to be allocated
size is the number of bytes in each block
“What does this function return?” The function returns the following values:
On successful allocation, the function returns the address of first byte of allocated memory.
Since address is returned, the return type is a void pointer. By type casting appropriately we can
use it to store integer, float etc.
5
If specified size of memory is not available, the condition is called “overflow of memory”. In
such case, the function returns NULL.
What will happen if the following program segment is executed?
Solution: The compiler reserves the space for the variable ptr in the memory. No initialization is
done if ptr is a local variable (It is initialized to NULL if it is global variable). Now, using the
function calloc() we can request specified number of blocks of memory to be reserved. The
specified blocks of memory may be allocated or may not be allocated.
6
3) realloc(ptr, size)
if (ptr == NULL)
{ /* Memory is not allocated */
printf(“Insufficient memory\n”);
return;
}
7
Now, let us see “What does this function return?” The function returns the following
values:
On successful allocation, the function returns the address of first byte of allocated
memory.
If specified size of memory cannot be allocated, the condition is called “overflow
of memory”. In such case, the function returns NULL.
4) free(ptr)
Now, let us see “What is the purpose of using free()?”
This function is used to de-allocate (or free) the allocated block of memory which is allocated
using the functions calloc(), malloc() or realloc(). It is the responsibility of a programmer to de-
allocate memory whenever it is not required by the program and initialize ptr to NULL. The
syntax is shown below:
#include <stdlib.h> /* Prototype definition of free() is available */
……..
free(ptr);
ptr = NULL;
…….
…….
Here, ptr is a pointer to a memory block. Now, let us see how this function works. For
example, consider the following memory configuration where ptr points to a memory
block containing 200 integers ranging from 01 to 0200.
STACKS
8
“What is a stack?”
1) Push operation
Inserting an element into the stack is called push operation. Only one item is inserted at a time
and item has to be inserted only from top of the stack.
9
“What is stack overflow?”
Definition: When elements are being inserted, there is a possibility of stack being full. Once the
stack is full, it is not possible to insert any element. Trying to insert an element, even when the
stack is full results in overflow of stack.
For example, consider the stack shown in figure 6.1 with STACK_SIZE 4. After inserting 30, 20,
25 and 10 there is no space to insert any item. Then we say that stack is full. Trying to insert an
element into the stack when the stack is full is called overflow of stack.
“When we cannot insert item into the stack?” We cannot insert any item into the stack when top
has already reached STACK_SIZE–1 (Fig. 6.2.d). In such situation, we have to display
appropriate message as shown below using the following code:
10
“What is stack underflow?”
Definition: When elements are being deleted, there is a possibility of stack being empty. When
stack is empty, it is not possible to delete any item. Trying to delete an element from an empty
stack results in stack underflow.
For example, in the figure 6.3, after deleting 10, 25, 20 and 30 there are no elements in the stack
and stack is empty. Deleting an element from the empty stack results in stack underflow.
11
for (i = 0; i <= top; i++)
printf(“%d\n”, s[i]);
12
either postfix or prefix can be easily evaluated.
Recursion: A function which calls itself is called recursive function. Some of the problems
such as tower of Hanoi, problems involving tree manipulations etc., can be implemented very
efficiently using recursion. It is a very important facility available in variety of programming
languages such as C, C++ etc.,
Other applications: There are so many other applications where stacks can be used:
For example, to find whether the string is a palindrome, to check whether a given expression is
valid or not and so on.
Conversion of expressions
Definition: The sequence of operators and operands that reduces to a single value after
evaluation is called an expression. The operands consist of constants and variables whereas the
operators consist of symbols such as +, -, *, / and so on.
Representation of expressions
1) Infix expression
2) Postfix expression
3) Prefix expression
13
Precedence and associativity of the operators
14
Hence, equivalent postfix expression is A B C – D * + E ^ F +
15
/*CONVERSION FROM INFIX TO POSTFIX USING STACKS<EVALUATION OF
POSTFIX EXPRESSION USING STACKS PLEASE DO REFER THE WORKOUT
PROBLEMS*/
16