Posts

Showing posts from February, 2017

Swapping two nodes

Given a linked list and two characters to swap , WE NEED TO SWAP TWO NODES NOT THE VALUES given. Algorithm step 1- start traversing the linked list step 2- assign the nodes which contain the given values as t1 and t2.respectively step 3- assign the previous nodes before the nodes contain the given values as p1 and p2 step 4-assign p1 next node to t2 step 5-assign p2 next node to t1 step 6-assign t1 next node to t2 next node step 7-at last assign assign t2 next node to p2 step 8-end For example the given linked list be 1->2->3->4->5->null swap 3 and 5 according to the algorithm p1 node will be 2 t1 node will be 3 p2 node will be 4 t2 node will be 5 then while we execute the below code         p1.next=t2;         p2.next=t1;         t1.next=t2.next;         t2.next=p2; the nodes get swapped. output will be 1->2->5->4->3->null Code public class L...

Solution For LRU Stack

The objective is to i mplement a stack using LinkedList. You need to implement push and pop function. When push is called, insert the items in list at the head. When the stack is empty and pop is called return 0 The maximum stack size allowed is 5 Whenever a push is called Case 1 : Value is NOT present in stack: If the stack is full (has 5 items) then remove the oldest pushed item on the stack and push the new item. If the stack is not full, then push the new item Case 2 : Value is already present in stack: Since the item is already present in the stack move it to the top of the stack. Algorithm Step 1 :- Create a class(or structure ,depends on the programming language) with a data members for the creation of linked list. Step 2:- Implement push and pop function using a linked list as we do normally. Step 3:- In push function before pushing the element to stack check the size of the stack. ->if size is less than 5 ,then push the element normally. ->els...

Female co passenger hack

The female co-passenger hack. I came across this trick on the internet. When you need to book a ticket on IRCTC (Indian Railways), book two seats instea of one, and cancel one after some days. One in your name and one in fake Female name to get a girls in the compartment. The IRCTC algorithm works that way.