Skip to main content

Posts

Delete a node from doubly linked list

Deletion operation in DLL is simpler when compared to SLL. Because we don't have to go in search of previous node of to-be-deleted node.  Here is how you delete a node Link previous node of node of to-be-deleted to next node. Link next node of node of to-be-deleted to previous node. Free the memory of node of to-be-deleted Simple, isn't it. The code can go like this. prevnode = delnode->prev; nextnode = delnode->next; prevnode->next = nextnode; nextnode->prev = prevnode; free(delnode); And that is it. The node delnode is deleted. But we should always consider boundary conditions. What happens if we are trying to delete the first node or last node? If first node is to be deleted, its previous node is NULL. Hence step 3 should not be used.  And also, once head is deleted, nextnode becomes head . Similarly if last node is to be deleted, nextnode is NULL. Hence step 4 is as strict NO NO. And we should set prevnode to tail. After we put these things together, we have...

Doubly Linked List

Doubly Linked List is a list where each node has two links - one to the next node and another to the previous node. Because of two links , operations on this list are considerably easie r . But t here is an overhead of one more pointer in each node. First we need to have a node for the list. As I mentioned earlier, the node will have data - in this case a simple integer and two pointers. struct node { int n; struct node * next; struct node * prev; }; typedef struct node * NODEPTR; We have type def ined NODEPTR as we will be use it often. Next we have to define two pointers - head of the list and tail of the list. And let us not forget to initialize them to NULL. int main () { NODEPTR head,tail; head = tail = NULL ; Our create_node function will be similar to that of singly linked list . Only difference is we have to initialize newnode->prev as well . To NULL. L et us write functions to insert a node to  list. Let ...

Insert a node into sorted list

A singly linked list is a data structure where each node is linked to the next node. In such a list, if you have to add a new node, it can be done in a simple way such as     find the last node     link last node to new node Which can be done using code such as NODEPTR insertnode(NODEPTR head, NODEPTR newnode) { if(head==NULL) { head = newnode; } else { NODEPTR temp = head; while(temp->next !=NULL)//till we have not reached last node { temp = temp->next; } // now temp is our last node. add new node after this temp->next = newnode; } But if we have a sorted list and we want to add a new node to this list we should traverse to first node temp which is greater than new node add the new node to previous node of this prev->next = newnode; newnode->next = temp; To find previous node, each time store current node in prev before moving to next node We also need ...

Search for a node in Linked List

Normally searching can be done using linear search or binary search. But in a linked list, jumping to an intermediate node is not possible. Hence searching should be done sequentially. Let us assume we have a linked list of names. struct node { char name[30]; struct node * next_ptr; }; typedef struct node * NODEPTR; In fact it would be better optimized, if we use a dynamic memory for name. For sake of simplicity, let us leave that. Now we have a node pointer called head, which points to the head of the list. And we also have a string search_string. We need to write a function to search this string, and return the pointer to node containing this string. If search string is not found, we must return NULL. NODEPTR head; ----- ----- NODEPTR search(NODEPTR head, char *str) { NODEPTR temp; for(temp=head;temp!=NULL;temp = temp->next_ptr) { if(strcmp(temp->name,str)==0) return temp; } return NULL; } We start from first n...

Function to add a node to binary search tree

Binary search tree (BST) is an ordered tree where each node has smaller values on the left sub tree and larger values on the right subtree. This is similar to binary search algorithm. And another useful fact is if you traverse the node in in-order method, the values of the tree will be in ascending order. Binary Search Tree with root as 50 Now as you can see from the diagram on the left child and their children of root (which in this case is 50), values are 40, 20 and 45. All are less than 50. On the other hand, on the right sub tree 50, the values are 60, 55 and 78 - larger than 50. And this rule follows for each and every node, not just root. Now if we have to  add a node - let us 43, where shall we add it, in such a way that the tree will still remain as BST. The new node must be a leaf node (i.e. the node with no child nodes) Let us traverse from root and find out where this new node fits. 43 is smaller than 50. Hence we must branch to left of 50. Now we get 40. 4...

Deletion of a node from linked list given only that node

We have considered how to create and print the list and delete a node from a list. But often interviewers ask you a question, given a node of the list, how do you delete that node? The problem here is we do not know the head of the list. We just the know one node which we must delete. Well, the solution is much simpler than it appears. Suppose 12 is the node which should be deleted. As we can see from the diagram, we are deleting the next node of 99 by the usual method of linking n to next of next of 12. But the requirement was not to delete next node  but the node with 12 itself. So we will retain the data of that node (99) by copying it to previous node. And now the node with 99 is marked for deletion. Which can be easily deleted as follows. We are assuming that n is the node with 12 as value. n -> data = n -> next -> data; //copy the data n -> next = n -> next -> next; //link to next free( n -> next) ; //delete next of n   ...

Function to sort an array using insertion sort

Insertion sort is slightly better sorting method among O(n2) algorithms. It is effecient if the elements are almost sorted and if the size is small. Insertion sort basically works by splitting your elements into two parts. Sorted half and unsorted half. To begin with, Sorted half is empty and unsorted half has all elements. Then one element is removed from unsorted half and added to the sorted half. When adding , this element is inserted at the correct location. This process is continued till we have all the elements in sorted half and unsorted half is empty. Now let us try to understand it with a diagram. First 17 is removed from unsorted array and added to sorted array. Since there is one element there, it is sorted. Next 4 which is front most element now in unsored array is removed and added to sorted array. But 4 is smaller than 17, so 17 is pushed to right and its place is taken by 4.    Next 32 is removed from the front of unsorted array and added to sorted array. But he...