Skip to main content

Posts

Convert colum to row without using unpivot operator

In this post I will show you how to convert column to row in SQL.For this post I am going to use following table which have three colum stu_name,sibject and marks.SELECT stu_name, MAX(CASE WHEN subject = 'ECO' THEN marks ELSE 0 END) ECO, MAX(CASE WHEN subject = 'HIS' THEN marks ELSE 0 END) HIS, MAX(CASE WHEN subject = 'MAT' THEN marks ELSE 0 END) MAT, MAX(CASE WHEN subject = 'GEO' THEN marks ELSE 0 END) GEO, MAX(CASE WHEN subject = 'SCI' THEN marks ELSE 0 END) SCI FROM col_to_rows GROUP BY stu_name

How to implement Stack using linked list

Stacks are easier to implement using linked lists. There is no need to set a limit on the size of the stackusing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LinkedlistUsingStack { public class Node { public int Data { get; set; } public Node Next { get; set; } public Node(int data) { this.Data = data; } } public class Stack { private Node _stackTop; public void Push(int newValue) { Node newNode = new Node(newValue); newNode.Next = _stackTop; _stackTop = newNode; } public bool IsEmpty() { return _stackTop == null; } public int Pop() { Node temp = _stackTop; var data = temp.Data; _stackTop = temp.Next; return data; } public void Print() { …

How to Find if Linked List contains Loops or Cycles in C#

In this post I will show you how to detect loop in linked list.We can find the loop in the linked list via Floyd’s Cycle-Finding Algorithm, explained here.The algorithm is pretty straightforward: We start at the beginning of the linked list with two pointers. The first pointer is incremented through each node of the list. The second pointer moves twice as fast, and skips every other node. If the linked list contains a loop, these two pointers will eventually meet at the same node, thus indicating that the linked list contains a loop. .using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Algo { public class Node { public Node Next { get; set; } public int Value { get; set; } public Node(int value) { this.Value = value; } } public class LinkedList { private Node _head; public LinkedList() { } public vo…

Reverse a given linked list in c#

In this post I will show you how to reverse a  given linked list in c#.For example a given linked list as show below10->20->30-NULLthen output should be30->20->10-NULLusing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Algo { public class List { public List Next { get; set; } public int Value { get; set; } public List(int value) { Value = value; } public override string ToString() { List current = this; StringBuilder builder = new StringBuilder(); while (current!=null) { builder.Append(current.Value + "->"); current = current.Next; } return builder.ToString(); } public List ReverseList() { List prev = null; List next = null; List current = this; …

How to check a tree is a binary tree of not

This is continuation of my post How to create binary tree in c#.In this post I will show you how to check a given tree is a binary tree or not. public bool isBST() { return (IsBST(_root)); } private bool IsBST(Node node) { if (node == null) return (true); // do the subtrees contain values that do not // agree with the node? if (node.Left != null && FindMaxValue(node.Left) > node.Data) return (false); if (node.Right != null && FindMinValue(node.Right) <= node.Data) return (false); // check that the subtrees themselves are ok return (IsBST(node.Left) && IsBST(node.Right)); }

Mirror of a binary tree

This is continuation of my post How to create binary tree in c#.In this post I will show you how convert a given binary tree into it’s mirror image./**
Changes the tree into its mirror image. So the tree...
       4
      / \
     2   5
    / \
   1   3 is changed to...
       4
      / \
     5   2
        / \
       3   1Strategy:Uses a recursive helper that recurs over the tree, swapping the left/right pointers. public void MirrorTree() { MirrorTree(_root); } private void MirrorTree(Node node) { if (node != null) { // do the sub-trees MirrorTree(node.Left); MirrorTree(node.Right); // swap the left/right pointers Node temp = node.Left; node.Left = node.Right; node.Right = temp; } }

How to print out all path from root to leaf of a given binary tree

This is continuation of my post How to create binary tree in c#.In this post I will show you how to print all path from root to leaf of a given binary tree in c#.Let’s consider following binary tree.             *       4
             *    2     5
             *  1    3     For above tree there are three path from root to leafPath 1 : 4 2 1Path 2: 4 2 3Path 3: 4 5 public void PrintPath() { int[] path = new int[100]; printPaths(_root, path, 0); } private void printPaths(Node node, int[] path, int pathLen) { if (node == null) return; // append this node to the path array path[pathLen] = node.Data; pathLen++; // it's a leaf, so print the path that led to here if (node.Left == null && node.Right == null) { PrintArray(path, pathLen); } else { // otherwise try both subtre…