You are on page 1of 7

Data structure CSE2050 Assignment-2

Submitted To- Mr. Munish Kumar Mittal


Submitted By- Gaurav Sharma RB6803A24 Reg no-10800582

Q1: Advantages of Pointer: 1. Helps to store the addresses of different values in specific locations. 2. It helps in making the execution of the program fast, as when a user uses pointers, he is straightaway pointing out the memory location of any object. Pointing out a memory location decreases the execution time of any program. 3. We can use a same pointer for multiple values, and as such we can cut short the memory usage as well as the execution time of a program. Disadvantages: 1. Pointer adds up to the complexity of the program. The programmer has to take each and every care that the possible ambiguities that can arise in the execution while using pointers are removed. 2. Pointers, in some cases store the addresses of the data variables that can be easily hacked by the hackers. This can lead to the decreased security of our data. 3. Pointers are not able to hide the restricted memory locations. 4. Links need to be consistent. Any broken links may give up errors during time of execution.

Pointers in Data Structures: Data structure hugely depends upon the concept of pointer usage. The main reason behind this is that in data structures, we have to store the data in such a way that it is organised in a proper way, and that we can use the data whenever we need. As such, the address of the data in the memory is to be kept at a great importance. Pointers may help us to call our data by their addresses. This can help to reduce the execution time as well as memory usage. Also, the complexities can be removed by the apt use of pointers.

Q2: Fixed block storage: This concept deals with the usage of free memory blocks. This free memory is kept in the link lists. These link lists have blocks of equal memory size, and whenever any data is to be stored, it pools up the memory and used this fixed sized blocks

Buddy system: Whenever the memory storage is to be needed, the blocks assigned are of size 2^n. if the data stored is of lesser size, the blocks are broken into half, and as such the

memory is allocated. In order to compare a block to data, it must be free always. Whenever two blocks are free, they combine together once again.

Q3: Static Memory Allocation 1. It is the process of allocation of the memory at compilation time of the program. 2. It has a fixed duration, after which it terminated by itself Dynamic Memory Allocation 1. It is the process of allocating the memory at the run time 2. This type of memory has no fixed duration, and works till any garbage value is induced in it or the user himself terminates it. 3. External fragmentation of memory in case of file handling is possible.

3. No external fragmentation of the memory is possible

The various memory management functions are as: 1. ALLOC: it is used for the allocation of the memory. 2. MALLOC: this is used to switch the void pointer to NULL value. 3. CALLOC: this allocated the data on a free memory space.
4. FREE: this is used to free up all the memory. It deallocates the memory space and

clears it.

Q4: The following are the ways to manage records in a memory:


1. Simple variable storage: a simple way to store the data in the memory is to assign its

different values to the different variables. This is helpful if we have only a small amount of data. If we have large amount of data, equal number of variables will be needed, and as such the data handling will be too complex. 2. Array: this is a useful way to store a same type of data in an arranged manner with data locations of an array. This is useful only if the data is of same type. Different type of data cannot be stored in one array.

3. Pointers- These are the variables that store the address of other variables. These are

helpful in memory management as well as keeping a track of different variables.


4. Files- these are the collection of data. Files are much helpful in keeping a track of a

huge amount of data. Files can store large amount of data, and can be helpful in linking it to multiple applications.

Q5: Pointer to an array: when we store an array, we can use a single pointer that can store the address of each variable in itself. It is notable that the address allocated to each element is usually same. The pointer variable should be of the same data type as that of an array. Eg:

Void main() { int a[5]={1,2,3,4,5}; int *ptr; for(int i=0;i<5;i++) { ptr=&a[i]; cout<<ptr; } Getch(); }

Array of Pointers: When we use an array that stores the addresses of different variables, that is called as an array of pointers. The address of memory location for each element is different, and is usually in continuous locations. For eg:

Void main() { int a[5]={1,2,3,4,5};

int *ptr[5]; for(int i=0;i<5;i++) { Ptr[i]=&a[i]; } Getch(); }

Q6:

A far pointer is one which is a segment selector. This type of pointer is used to refer to the address of a particular segment of the memory.

Dangling pointer is the one which stores an invalid address. This type of pointer usually occurs when we have deleted some type of element, but we still address it in the main program.

Q7:

In terms of representation :

In terms of representation, array has just a data part. An array has a similar type of element storage in it. While the link list has an info as well as a link part. The link points to the immediate nodes.

Array

Link list

In terms of traversal: An array is usually traversed by the use of a loop, where the counter iterates the loop, and the locations of the array are traversed one by one. If we want to jump to a specific location, we can simply write that location in the array variable.

On the other hand, in link lists, we have to traverse through the link parts. Each node is connected to its previous as well as next node. As such, the data is traversed in a linear way.

In terms of searching: In an array, various types of searching techniques are possible like linear searching, binary searching etc. because there are a finite number of elements in array and middle position is known. In a link list, the middle position is not known. As such only linear searching is possible.

Q8: No, a binary search is not possible in a link list. A link list has a data as well as an address part. There is no continuous location for storage of data. As such we cannot judge the middle element or position in a link list. Binary search always starts form the middle element. So, any type of binary search is not possible.

You might also like