Archive for September, 2013

Get List of Tables Present in SQL Server Database

Today I have to get a list of all tables present in database. I used following query to get the list of all tables present in SQL Server Database.

SELECT TABLE_NAME  
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' order by TABLE_NAME 

Write A Program To Reverse a Doubly Linked List

Today we have to write a program for reversing a Doubly linked list. A doubly-linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes. The beginning and ending nodes’ previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list. If there is only one sentinel node, then the list is circularly linked via the sentinel node. It can be conceptualized as two singly linked lists formed from the same data items, but in opposite sequential orders.

image

Source : http://en.wikipedia.org/wiki/Doubly-linked_list

Code

        public DoubleNode<T> Reverse()
        {
            DoubleNode<T> newHead = null;
            while (head != null)
            {
                DoubleNode<T> temp = head;
                head = head.Next;
                temp.Next = newHead;
                newHead = temp;
            }
            return newHead;
        }

Write a Program for Swapping Every Second Element of LinkList

Input : 0->1->2->3->4->5->6->7->8->9->

output: 1->0->3->2->5->4->7->6->9->8->

Code :

        public void SwapEverySecondElement()
        {
            var p = head;
            var q = p.Next;

            while (q != null)
            {
                var temp = p.Data;
                p.Data = q.Data;
                q.Data = temp;
                p = q.Next;
                if (p != null)
                {
                    q = p.Next;

                }
                else
                {
                    q = null;
                }

            }

        }

Test

Input

0->1->2->3->4->5->6->7->8->9->

Output

1->0->3->2->5->4->7->6->9->8->

——————————————————————————–

Input

A->B->C->D->E->F->G->H->I->J->K->L->M->N->O->P->Q->R->S->T->U->V->W->X->Y->Z->

Output

B->A->D->C->F->E->H->G->J->I->L->K->N->M->P->O->R->Q->T->S->V->U->X->W->Z->Y->

Write a Program to Print String in Triangular Format

Today we will write a program for printing a given string’s in triangle.

For Example

input : Independence

Output

image

Code

For First Image

        public static void PrintStringInTriangle(this string input)
        {
            int length = input.Length;

            for (int i = 0; i < length; i++)
            {
                for (int spaceCounter =0; spaceCounter <=length-i; spaceCounter++)
                {
                    Console.Write(" ");
                }
                for (int charCounter = 0; charCounter <=i; charCounter++)
                {
                    Console.Write(input[charCounter]);
                    Console.Write(" ");
                }
                Console.WriteLine("");
            }


        }

For Second Image

        public static void PrintStringInLeftTriangle(this string input)
        {
            int length = input.Length;

            for (int i = 0; i < length; i++)
            {
                for (int spaceCounter = 0; spaceCounter <= length - i; spaceCounter++)
                {
                    Console.Write(" ");
                }
                for (int charCounter = 0; charCounter <= i; charCounter++)
                {
                    Console.Write(input[charCounter]);
                     
                }
                Console.WriteLine("");
            }


        }

For Third Image

        public static void PrintStringInRightTriangle(this string input)
        {
            int length = input.Length;

            for (int i = 0; i < length; i++)
            {
                for (int spaceCounter = 0; spaceCounter <= length; spaceCounter++)
                {
                    Console.Write(" ");
                }
                for (int charCounter = 0; charCounter <= i; charCounter++)
                {
                    Console.Write(input[charCounter]);

                }
                Console.WriteLine("");
            }


        }