Archive for January, 2013

Write a Program to Reverse Single List using Iterative Procedure

In the past we have already covered reversing a Linked List via Recursion.  Today we will discuss how we can reverse a Linked List using Iterative method.

Algorithm

  1. Take three pointer currentNode,nextNode and previousNode
  2. initialize currentNode to head of the linked List
  3. iterate over the linked list until currentNode is equal to null
    1. nextNode to previousNode
    2. previousNode to currentNode
    3. In the loop change currentNode to next Node
  4. return previousNode

Code

        public Node<T> ReverseIterative()
        {

            Node<T> currentNode = this.head;
            Node<T> previousNode, nextNode;
            previousNode = nextNode = null;

            while (currentNode != null)
            {
                nextNode = currentNode.Next;
                currentNode.Next = previousNode;
                previousNode = currentNode;
                currentNode = nextNode;

            }
            return previousNode;
        }

Write a Program for Printing Floyd’s triangle

Floyd’s triangle is a right-angled triangular array of natural numbers, used in computer science education. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner.

Source : http://en.wikipedia.org/wiki/Floyd’s_triangle

Code

        public static void PrintFloydTriangle(int rows)
        {
            int number = 1;
            for (int rowCounter = 0; rowCounter <= rows; rowCounter++)
            {
                for (int columnCounter = rowCounter; columnCounter >= 1; 
columnCounter--) { Console.Write(number++ + " "); } Console.WriteLine(""); } }
Test
Input : 6
Output


1

2 3

4 5 6

7 8 9 10

11 12 13 14 15

16 17 18 19 20 21

 

Input : 10


1

2 3

4 5 6

7 8 9 10

11 12 13 14 15

16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31 32 33 34 35 36

37 38 39 40 41 42 43 44 45

46 47 48 49 50 51 52 53 54 55

Write a Program to Find The count of a Given number in a Single Linked List

Algorithm

  1. Initialize number counter to 0
  2. Loop through the linked list
    1. if current node value is equal to number, increment the counter
  3. return counter

Code

        public int NumberCount(T number)
        {
            int count = 0;
            Node<T> temp = this.head;

            while (temp != null)
            {
                if (temp.Data.Equals(number))
                {
                    count = count + 1;
                }
                temp = temp.Next;
            }
            return count;
        }

Given an array of integers, write program to move all zeros at the end of the non zero elements.

  • Input : 1,0,2,0,3,0,0,4,5,6,7,0,0,0
  • Output : 1,2,3,4,5,6,7,0,0,0,0,0,0,0

Code:

        public  static void MoveZeroToEnd(int[] input)
        {
            Console.WriteLine(string.Join(",", input));
            int counter, jnonZeroIndex;
            counter = jnonZeroIndex = 0;
            int length = input.Length;

            for (counter = 0; counter < length; counter++)
            {
                if (input[counter] > 0)
                {
                    input[jnonZeroIndex] = input[counter];
                    jnonZeroIndex = jnonZeroIndex + 1;
                }
            }
            while (jnonZeroIndex < length)
            {
                input[jnonZeroIndex] = 0;
                jnonZeroIndex++;
            }
            Console.WriteLine(string.Join(",", input));

        }

Test

input Array : 1,0,2,0,3,0,0,4,5,6,7,0,0,0

Output Array : 1,2,3,4,5,6,7,0,0,0,0,0,0,0

input Array : 1,0,2,0,3,0,0,4,5,6,7,0,0,0,1,2,3,4,5,6,7,8,9,98

Output Array : 1,2,3,4,5,6,7,1,2,3,4,5,6,7,8,9,98,0,0,0,0,0,0,0