data structure assignment

8
ANNEXURE-I Homework Title / No. : _____2_____________Course Code : __CSE2050_______ Course Instructor : __MR. RAJAN KAKKAR____Course Tutor (if applicable) : _________ Date of Allotment : ____13-09-2010________Date of submission : _____26-09-2010______ Student’s Roll No._____RC6801A07________Section No. : ______C6801______________ Declaration: I declare that this assignment is my individual work. I have not copied from any other student’s work or from any other source except where due acknowledgment is made explicitly in the text, nor has any part been written for me by another person. Student’s Signature : _____________ Evaluator’s comments: _________________________________________________________________ ____ Marks obtained : ___________ out of ______________________ Content of Homework should start from this page only:

Upload: gaganpreet-singh

Post on 03-Apr-2015

438 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DATA STRUCTURE ASSIGNMENT

ANNEXURE-I

Homework Title / No. : _____2_____________Course Code : __CSE2050_______

Course Instructor : __MR. RAJAN KAKKAR____Course Tutor (if applicable) : _________

Date of Allotment : ____13-09-2010________Date of submission : _____26-09-2010______

Student’s Roll No._____RC6801A07________Section No. : ______C6801______________

Declaration: I declare that this assignment is my individual work. I have not copied from any other student’s work or from any other source except where due acknowledgment is made explicitly in the text, nor has any part been written for me by another person.

Student’s Signature : _____________

Evaluator’s comments: _____________________________________________________________________

Marks obtained : ___________ out of ______________________

Content of Homework should start from this page only:

Page 2: DATA STRUCTURE ASSIGNMENT

PART – APART – A

QUES 1QUES 1: : Write advantages and disadvantages of using pointer. How the concept of pointers is useful in the implementation of data structures?

Ans 1- The main advantages of using pointers are: 1.) Function can return only one value. At the time of the same function the pointer help to recognize the variable. 2.) In the case of arrays we can decide the size of the array at runtime by allocating the necessary space. 3) The pointer has the address of the variable so it make the fast. 4) The pointer is necessary at the time of the run time memory.

Disadvantages of pointers:1) It is complex. 2) If sufficient memory is not available during runtime for the storage of pointers, the program may crash immediately. 3) If the programmer is not useful and consistent with the use of pointers, the program may crash.

The concept of pointers in the implementation of data structures is that if the link is broken at any node then the error is occurred. The pointer is related to the data structure .The data structure is the organization of the data that can be fetch easily at the line when it is needed. The pointer help to store the address, so at the time of fetching the data the pointer work. It tell the address of the data and we can receive data easily. In this way the pointer is useful in the data structure.

QUES 2: QUES 2: Elaborate the concept of “Fixed block storage allocation” and “Buddy system” in dynamic memory management.

Ans 2- Fixed block storage allocation: It means the block that is assign fix size is called the fixed block storage allocation. It is also called pool allocation, uses a free list of fixed-size blocks of memory . This works well for simple embedded systems

Page 3: DATA STRUCTURE ASSIGNMENT

Buddy System : The system in which the memory is allocated in the big blocks and the size of

that blocks is 2n then it is called the buddy system. The block is broken when the block that is

allocated is large than it should be then the block is broken in two parts. The all blocks are in the

link list, they have the particular size. The block is compared its buddy only when the block is

free. They are combined if they are both free.

QUES 3QUES 3: : Differentiate between static memory allocation and dynamic memory

allocation. Illustrate various memory management functions?

Ans 3-Static memory allocation : The static memory is the memory that is given or assign to the variable on the compile time. It is the opposite of the dynamic memory allocation. In this type of the memory allocation, the data is allocated logically. A single copy of the data is retained. Memory is assigned during compilation time.

Dyanamic memory allocation: The dynamic memory is the memory that is given or assign to the variable on the run time. It is the opposite of the static memory allocation. Memory management functions handle the allocation, reallocation, and deallocation of dynamic memory used by the encode/decode functions.

Various memory management functions are:

a.Alloc(): It allocate the memory.

b.Malloc(): It return the void pointer to NULL. It used to create a dynamic memory allocation in the C language.

ptr= malloc(no. of elements ,size of int)

c.Calloc(): It allocate the data on the unused space. It used to create memory allocation. In calloc we use 2 arguments i.e no. of arguments and size of it.

d.Free: it help to deallocate the memory.

e. Realloc()-This function works in the same way as the C realloc function. It reallocates an existing block of memory.

Page 4: DATA STRUCTURE ASSIGNMENT

Ptr = realloc(ptr,new)

QUES 4QUES 4: : Write different ways to manage records in memory?

Ans 4- We have to manage the record so that we can get back when ever we want. There are many ways to manage the records:

Array: In the C the array store the same kind of elements. With the help of the array we can store the data of the same type.

Pointer: The pointer is the variable that can store the address of the another variable. It is also helpful for the storing the data. It manage the records.

File: The file system is also helpful to manage record. We store the information in the file. The all info can be kept by the file.

PART – BPART – B

QUES 5:QUES 5: Illustrate the use of array of pointers and pointers to an array?

Ans 5-Array of pointers: A pointer is a variable that stores the memory address of another variable. So it's possible to have an array of pointers. In other words, an array of memory addresses:#include <stdio.h>int main() { int int_array[3] = {1,2,3}; int *ptr1 = &int_array[0]; int *ptr2 = &int_array[1];

}

We will use an array of character pointers to point to the strings declared as follows: char * nameptr[MAX];

Page 5: DATA STRUCTURE ASSIGNMENT

Pointers to an array: Now that we know that the name of an array holds the address of the first member of the array, we realize that we can declare a pointer of the same data type as the array and initialize it with the array.

int number[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };int *pNumbers = Number;

QUES 6QUES 6:: Give example to show the use of far pointer and dangling pointer problems?

Ans 6-Far pointerThe pointer in which a segment selector is available is called far pointer. It make possible to point. It point to addresses of the segment. It is used to access the far segment. Far pointer is used to address data stored in data spaces . It includes segment number.

Typically, such pointers are used with farmalloc() and such, to manage a heap separate from where all the rest of the data lives.

Dangling pointer

Dangling pointer means the pointer of invalid address. It means when any object is deleted then this kind of problem come. There is no element on the location. The element is deleted that is point by the dangling pointer.

The program has a dangling pointer

f() {struct s *p = 0;// p is localg(p);p->next->val =...; // p->next is dangling}g(struct s *p) {p->next = malloc(sizeof(struct s));create_10_Node_List(p);initialize(p);h(p);free_all_but_head(p);}

Page 6: DATA STRUCTURE ASSIGNMENT

QUES 7:QUES 7: Differentiate between linked list and arrays in terms of representations,

traversal and searching?

Ans 7- Linked list is the concept which is related with the pointer concept. The pointer is used in the linked list. But in the array the pointer is not used because there is no need of the link in the array.Array is the collection of same type of elements in continuous type. Traverse means-go to the every node . Search means- to find any element and give the location

Main differences between the two are:1) arrays are random access structures, where you can access elements in random/indexed manner, whereas list is a sequential access structure. This makes such algorithms like heap sort or binary search to work much faster on arrays2) arrays are static/fixed size whereas lists are dynamic size structures. It means that when creating an array (both on stack or heap), you have to specify its size. With lists, you just create an empty list and freely expand it.

QUES 8:QUES 8: Can we perform binary search in linked list, if no then illustrate the

reason?

Ans 8- No, we can’t binary search in link list. It is not possible to binary searching the link list because in the linked list there is no confirmation of the beginning and ending point. Suppose we have 10 nos. in the array, for any no. to be searched we'll check it first with the middle no. i.e. 5th no. If it matches OK, otherwise if it is less than it, we'll check the middle no. of 1 & 5 i.e. 3 and so on. If it is greater then we will check it with the middle no. of 5 & 10 i.e. 7 and so on. The search is carried till the no. is found or till middle no. equal to bounding no, i.e. no. not found.Coming to linked list, it is a way to store information without using arrays. The problem with array is we should know the maximum size in advance, in order to avoid it we use linked lists.i.e. we'll create a node containing information & the address of next node.