Monday, 8 February 2016

suman microsoft linked list

Problem 1 
        /// <summary>          /// Add node at the end in link list          /// </summary>          /// <param name="data"></param>          public void Add(int data)          {              Node newNode = new Node()              {                  Value = data,                  Next = null              };               if (head == null)              {                  head = newNode;              }              else              {                  Node temp = head;                  while (temp.Next != null)                  {                      temp = temp.Next;                  }                  temp.Next = newNode;              }          } 
Problem 2 
/// <summary>          /// Reverse Link List (Iterative Method)          /// </summary>          public void ReverseIterative()          {              Node current = head;              Node prev = null, next;               if (head == null || head.Next == null)                  return;               while (current != null)              {                  next = current.Next;                  current.Next = prev;                  prev = current;                  current = next;              }               head = prev;          } 
Problem 3 
/// <summary>          /// Write a program to check if given linklist is palindrome          /// </summary>          /// <param name="list1"></param>          /// <returns></returns>          public static bool IsPalindrome(Node list1)          {              Node slow = list1;              Node fast = list1;               Stack<int> stack = new Stack<int>();               /* Push elements from first half of link list onto stack. When fast runner               * (which is moving at 2x speed) reaches the end of linked list, then we know we are at the middle*/              while (fast != null && fast.Next != null)              {                  stack.Push(slow.Value);                  slow = slow.Next;                  fast = fast.Next.Next;              }               // Has odd number of elements, so skip the middle one              if (fast != null)              {                  slow = slow.Next;              }               while (slow != null)              {                  // if values are different then its not a palindrome                  int value = stack.Pop();                  if (slow.Value != value)                      return false;                   slow = slow.Next;              }               return true;          } 
Problem 4 
/// <summary>          /// Write a program to delete node from linklist          /// </summary>          /// <param name="n"></param>          /// <param name="head"></param>          public static void DeleteNode(int n, Node head)          {              Node temp = head;              if (temp == null)                  return;               if (temp.Value == n)              {                  head = temp.Next;              }              else              {                  while (temp.Next != null)                  {                      if (temp.Next.Value == n)                      {                          temp.Next = temp.Next.Next;                          break;                      }                      temp = temp.Next;                  }              }          } 
Problem 5 
/// <summary>          /// Reverse Link List (Recursive Method)          /// </summary>          public void ReverseRecursive(Node p)          {              if (p.Next == null)              {                  //head is global pointer                  head = p;                  return;              }              ReverseRecursive(p.Next);              p.Next.Next = p;              p.Next = null;          } 
Problem 6 
/// <summary>          /// Write a Function to swap nodes x and y in linked list by changing links          /// Input:  10->15->12->13->20->14,  x = 12, y = 20          /// Output: 10->15->20->13->12->14          /// </summary>          /// <param name="?"></param>          public void swapNodes(int x, int y)          {              // Nothing to do if x and y are same              if (x == y) return;               // Search for x (keep track of prevX and CurrX)              Node prevX = null, currX = head;              while (currX != null && currX.Value != x)              {                  prevX = currX;                  currX = currX.Next;              }               // Search for y (keep track of prevY and CurrY              Node prevY = null, currY = head;              while (currY != null && currY.Value != y)              {                  prevY = currY;                  currY = currY.Next;              }               // If either x or y is not present, nothing to do              if (currX == null || currY == null)                  return;               // If x is not head of linked list              if (prevX != null)                  prevX.Next = currY;              else // Else make y as new head                  head = currY;               // If y is not head of linked list              if (prevY != null)                  prevY.Next = currX;              else  // Else make x as new head                  head = currX;               // Swap next pointers              Node temp = currY.Next;              currY.Next = currX.Next;              currX.Next = temp;          } 
Problem 7 
/// <summary>          /// Function to reverse all even positioned node and append at the end          /// odd is the head node of given linked list.          /// Input List:  1->2->3->4->5->6          /// Output List: 1->3->5->6->4->2          /// </summary>          /// <param name="odd"></param>          void Rearrange(Node odd)          {              // If linked list has less than 3 nodes, no change is required              if (odd == NULL || odd.Next == null || odd.Next.Next == null)                  return;               // even points to the beginning of even list              Node even = odd.Next;               // Remove the first even node              odd.Next = odd.Next.Next;               // odd points to next node in odd list              odd = odd.Next;               // Set terminator for even list              even.Next = null;               // Traverse the  list              while (odd != null && odd.Next != null)              {                  // Store the next node in odd list                  Node* temp = odd.Next.Next;                   // Link the next even node at the beginning of even list                  odd.Next.Next = even;                  even = odd.Next;                   // Remove the even node from middle                  odd.Next = temp;                   // Move odd to the next odd node                  if (temp != null)                      odd = temp;              }               // Append the even list at the end of odd list              odd.Next = even;          } 
Problem 8 
/// <summary>          /// Find if there is loop in single link list          /// </summary>          /// <returns></returns>          public bool IsLoopExists()          {              Node slow, fast;              slow = head;              fast = head;               while (slow != null && fast != null && fast.Next != null)              {                  slow = slow.Next;                  fast = fast.Next.Next;                   if (slow == fast)                  {                      return true;                  }              }               return false;          } 
Problem 9 
// Function to sort a linked list of 0s, 1s and 2s          //1) Traverse the list and count the number of 0s, 1s and 2s. Let the counts be n1, n2 and n3 respectively.          //2) Traverse the list again, fill the first n1 nodes with 0, then n2 nodes with 1 and finally n3 nodes with 2.          void sortList()          {              int[] count = { 0, 0, 0 };  // Initialize count of '0', '1' and '2' as 0              Node ptr = head;               /* count total number of '0', '1' and '2'               * count[0] will store total number of '0's               * count[1] will store total number of '1's               * count[2] will store total number of '2's  */              while (ptr != null)              {                  count[ptr.Value] += 1;                  ptr = ptr.Next;              }               int i = 0;              ptr = head;               /* Let say count[0] = n1, count[1] = n2 and count[2] = n3               * now start traversing list from head node,               * 1) fill the list with 0, till n1 > 0               * 2) fill the list with 1, till n2 > 0               * 3) fill the list with 2, till n3 > 0  */              while (ptr != null)              {                  if (count[i] == 0)                      ++i;                  else                  {                      ptr.Value = i;                      --count[i];                      ptr = ptr.Next;                  }              }          } 

Problem 10 
/// <summary>         /// The function removes duplicates from a sorted list          /// </summary>         public void RemoveDuplicateFromSortedList()         {             Node current = head;             Node next = null;               /* do nothing if the list is empty */             if (current == null)                 return;                          /* Traverse the list till last node */             while (current.Next != null)             {                 /* Compare current node with next node */                 if (current.Value == current.Next.Value)                 {                     next = current.Next.Next;                     //delete(current.Next);                     current.Next = next;                 }                 else                 {                     current = current.Next;                 }             }         }  

Problem 11 
/// <summary>         /// The function removes duplicates from a unsorted list          /// </summary>         public void RemoveDuplicateFromUnsortedList()         {             Node ptr1 = head;             Node ptr2 = null;                /* Traverse the list till last node */             while (ptr1 != null && ptr1.Next != null)             {                 ptr2 = ptr1;                   while (ptr2.Next != null)                 {                     /* Compare current node with next node */                     if (ptr1.Value == ptr2.Next.Value)                     {                         ptr2.Next = ptr2.Next.Next;                     }                     else                     {                         ptr2 = ptr2.Next;                     }                 }                   ptr1 = ptr1.Next;             }         }    


No comments:

Post a Comment