-->

Friday, February 28, 2014

Merging the ordered Singly Linked List for Data Structure in 'C'

Merging the ordered Singly Linked List:

         Often it is necessary to merge two linked lists into a single linked list. Merging of two arrays consumes lots of time and storage whereas merging of two linked lists is simple. Just changing the address stored in link field of each node as and when required carries out the merging in case of linked list. Let us consider the example as follows before dealing with algorithm and program.



Both of the linked lists are in ascending order. First node address of each linked list is stored in ROOT1 and ROOT2 respectively. In order to merge these lists we can use another external pointer ‘ROOT’ to point to the first node of each list is compare. The node whose information is less become the first node of the merged list and the address of that node is stored in ‘ROOT’ .The process is continued till the end of each list.
In the above example the information of the first node of the second list is smaller than that of first node of the first linked list. So, ROOT is assigned with ROOT2 and ROOT2 is updated to point to the next node of the second linked list . Now ROOT2 points to a node with information 23.

Default selection RadiobuttonList Example in ASP.NET

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Default selection</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" 
            onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
            <asp:ListItem>Literal Control</asp:ListItem>
            <asp:ListItem>Label Control </asp:ListItem>
            <asp:ListItem>Localize control</asp:ListItem>
            <asp:ListItem>Multiview Control</asp:ListItem>
        </asp:RadioButtonList><br />
        <asp:Label ID="Label1" runat="server"></asp:Label>

    </div>
    </form>
</body>
</html>

Code behind code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (RadioButtonList1.SelectedIndex < 0)
        {
            RadioButtonList1.SelectedIndex = 0;
            Label1.Text += "Default selection in radio button list.";
        }
        else
        {
            Label1.Text = "Your item selected are : " + RadioButtonList1.SelectedItem.Text;
        }  
    }
}

Code generate the following output

Default selection RadiobuttonList Example in ASP.NET

Thursday, February 27, 2014

C# Programming: How to find pow of given number

You can easily find power of given number using Pow, which is static method of Math class. Here we take an example with integer number. Also gives II method , which is performed by programming.

Both method include in single program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 2, b = 4;   

            

            Console.WriteLine(" The power of given number is {0}",Math.Pow(a, b));
            // Second method
        int result= Fun(2, 6);
        Console.WriteLine(" The power of given number is {0}", result);
            Console.ReadKey();

        }

        private static int Fun(int a, int b)
        {
            if (b >= 1)
            {
                return (a * Fun(a, --b));
            }
            else
                return 1;
        }
      

    }
}

Code generate the following output

C# Programming: How to find pow of given number

c# programming : How to find largest number in given two number

You can easily find largest number between two number using Max, which is static method of Math class. This method is overload with multiple data types, such as Max(Byte, Byte) , Max(Decimal, Decimal) and many more data types. Here we take an example with integer number.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = -3, b = 52;
            Decimal deci = -4, deci1 = 53;

            Console.WriteLine(" the maximum number in between a= {0} and b={1} are {2}",a,b, Math.Max(a, b));

            Console.WriteLine(" the maximum number in between a= {0} and b={1} are {2}", deci, deci1, Math.Max(deci, deci1));

            Console.ReadKey();

        }
    }
}

Code generate the following output

c# programming : How to find largest number in given two number

C Program to reverse an ordered linked list for Data Structure for C Program

C Program to reverse an ordered linked list:

struct node
{
inf info;
struct node *link;
};typedef struct node sn;
sn*insert(sn *root, int i)
{
sn *new,*temp,*ptemp;
new=(sn*)malloc(sizeof(sn));
if(new= =NULL)
{
printf(“Memory allocation error…”);
exit(0);
}
new->info=I;new->link=NULL;
if(root= =NULL)
root=new;
else
if(i<root->info)
{
new->link=root;  root=new;
}
else
{
temp=root;
while(temp=NULL&&i>temp->info)
{
ptemp=temp;
temp=temp->link;
}
new->link=temp;
ptemp->link=new;
}return root; /*address of first node is returned*/
}
void traverse(sn *ptr)
{
while(ptr!=NULL)
{
printf(“%d”,ptr->info); ptr=ptr->link;
}
}
sn*reversell(sn *root)
{
sn *first=root,*pptr=root,*nptr;
nptr=nptr->link;
while(nptr!=NULL)
{
root=nptr;
nptr=nptr->link;
root->link=pptr;
pptr=root;
}
first->link=NULL;
return root;
}
main()
{
sn *root=NULL,*ptr;int info; char ch;
while(1)
{
printf(“/nEnter node information :”);
scanf(“%d”,&info);
root=insert(root,info);
printf(“\n Do you want to continue…(y/n)”);
ch=getche();
if(ch!=’y’)
 break;
}
printf(“\nOrdered (Asc)Singly Linked List :\n\n”);
treverse(root);getch();
printf(“\n\n The reversed (desc)linked list :\n\n”);
root=reversell(root);
traverse(root); getch();
}

The same reversing function also can be applied to the circular linked list by just changing the required condition to test the last node’s link. In normal linked list it is NULL. In circular linked list it become the address of the first node itself, stored in external pointer(root).

Reversing the ordered Singly Linked List for Data Structure in'C'

Special Operation on linked lists:

Reversing the ordered Singly Linked List:
              In order to reverse the ordered singly linked list, the root must point to the last node of the list. The first node’s address must be copied to the link of second node. Similarly the address of each node is copied in the link of the next node. It works like exchanging the links of each node with the address of the previous node. The algorithm is simple and self-explanatory.
The same is given as follows:

REVERSELL (ROOT)
FIRST<--ROOT [To store the first node’s address]
PPTR<--ROOT        [To store the address in link]
NPTR<--ROOT-->LINK [To store address of previous node]
Repeat While NPTR< >NULL
  ROOT<--NPTR
  NPTR<--NPTR-->LINK
  ROOT-->LINK<--PPTR
  PPTR<--ROOT
[End of while]
FIRST-->LINK<--NULL [To make first node as last]
Return ROOT
Exit.

Wednesday, February 26, 2014

How to Use Operators with Strings in Java Programming

Operator + with strings

You have used the operator + with numbers. When you use + with numbers, the result is also a number. However, if you use operator + with strings, it concatenates them e.g.

5 + 6 results in to 11.
“5” + “6” results in to “56”.
“17” + “A, V. Vihar” result in to “17 A, V. Vihar”
“abc” + “123” results in to “abc 123”
“” + 5 + “xyz” results in to “5xyz”
“” + 5 results in to “5”

(In above two expressions Java would internally convert 5 in to “5” first and then concatenate with “xyz” and “” respectively.)

Increment/Decrement Operators (++, --)

Java includes two useful operators not generally found in other computer languages (expect C and C++). These are the increment and decrement operators, ++ and --. The operators ++ adds 1 to its operand, and – subtracts one.
In other words,
a = a + 1;
is the same as
++a ; or a++;
And
a = a – 1
is the same as
--a ; or a --;

However, both the increment and decrements come in to two varieties: they may either precede of=r fallow the operand. The prefix version comes before the operand (as in ++ a or -- a) and the post-fix version comes after the operand (as in a++ or a--). The two version have the same effect upon the operand, but they differ when they take place in an expression.

© Copyright 2013 Computer Programming | All Right Reserved