Sie sind auf Seite 1von 3

c dipak's notes Pointer is a user defined data type which creates special types of variables which can hold

the address of primitive data type like char, int, float, double or user defined data type like function, pointer etc. or derived data type like array, structure. Pointers allow you to dynamically request memory to store off information for use later. They allow you to create linked lists and other algorithmically oriented data structures. In the case of arrays, we can decide the size of th array at runtime by allocating the necessary space. Coming to the disadvantages of pointers 1.) If sufficient memory is not available during runtime for the storage of pointers, the program may crash (least possible) http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-using-C-C

For example when you make an array you must allocate memory for a certain number of elements. If you want to add more elements to the array than you allocated for you must create a new array and copy the old array into the new array. This can take lots of time. You can prevent this by allocating lots of space initially but then you might allocate more than you need wasting memory. With a linked list you can start with space for just one element allocated. And add on new elements easily without the need to do any copying and reallocating.

The limitation of linked list is that it consumes extra space when compared to a array since each node must also contain the address of the next item in the list to search for a single item in a linked list is cumbersome and time consuming. question: q1)palindrome property

steps>>1) Find the middle of the linked list using two pointers. 2) Reverse the linked list from the middle(second half). 3) Now two pointers, 1st one from start of list, 2nd one from middle of list. Compare the list elements by moving pointers each time by one. Check for palindrome property. q2)given a linked list. You are givn two numbers M and N. You have to skip first M nodes and delete Next N Nodes, and continue this till you reach end of linked list sol>>while(ptrloc and ptr !=NULL) for loop to M-1 steps both pointers move together checking for NULL, ptrloc for loop ptr moves N times checking for NULL, ptrloc[link]=ptr[link],ptr=ptr[link],ptrloc=ptr.. q3)input linked list is : 1->9->3->8->5->7->7 do you see any pattern in this input ? odd placed nodes are in increasing order and even placed nodes are in decreasing order. write a code that gives the the following linkedlist: output linked list should be 1->3->5->7->7->8->9 ?? can it be done inplace ? sol>>count the no. of elements, a)if odd leave 1st one and exchange the rest alternate, b)if even leave 1st and last exchange the rest alternate, do it n-1/2 times stepa a and b... q4)reversing a linked list sol>>technique1... In this way, a new linked list will be created and all the items of the first linked list will be added to the new linked list in reverse order. technique 2

In this method, we will swap linked list node objects (references to the data). Swapping starts from the first nodes object and the first nodes object is swapped with the last nodes object. Then, the second nodes object is swapped with the one before the last nodes object.

Das könnte Ihnen auch gefallen