-->

Sunday, March 16, 2014

Searching for Data Structure in C Programming

Searching 

Searching is an operation that is applied on any of the data structure looking for the existence of an item or element within the same. So searching is simply a Boolean operation that returns TRUE if the element looking for exists in the data structure, otherwise to return FALSE. In almost every area of computing field searching is found. So understanding and implementation of searching is must for a computer programmer along with learning of data structure. Following are the few examples of searching:
. Searching a file in a directory or folder
. Searching name in a list of names
. Searching an address of a web site
. Searching a free partition to allocate memory to process
. Searching a free space in disk to allocate for a file
Basically searching operation is applied to every data structure to find out the existence of a member or element within it. The type of searching used for implementation depends on:
. Direct availability of index or address of elements
. Order of elements that are stored in data structure
We study here different types of searching like linear search and binary search applied on linear data structure, lists (array and linked list).

Double Ended Queue (DEQUE) in 'C'

Double Ended Queue (DEQUE)

DEQUE (pronounced as deck), Double Ended Queue, is a type of Queue in which additions and deletion are deletions are done from both the ends. So it is called as Double Ended Queue.

DEQUEs are of two types.

1. Input restricted DEQUE:
                        In this type of Double Ended Queue, the ADD (insertion) Operation is restricted to one end and the DELETE operation is done from both the ends. Only REAR is used for insertion operation and both FORNT and REAR are used for deletion operation. When an item is added REAR is updated accordingly and the item is inserted at REAR index. When an item is deleted from the FRONT end, FRONT is updated accordingly FRONT<--FRONT+1. When an item is deleted from REAR end, REAR is updated as REAR<--REAR-1.

2. Output restricted DEQUE:
                        It is opposite of Input restricted DEQUE. In this type Double Ended Queue, the DELETE operation is restricted to one end the insertion (ADD) operation is done to both the ends. Only FRONT is used for DELETE operation and both FRONT and REAR are used for ADD operation. When an item is added to the FRONT end, FRONT is updated accordingly FRONT<--FRONT-1, and the item is inserted at FRONT index. When an item is added to the item is inserted at REAR Index.

Saturday, March 15, 2014

How to access cell value from DataTable in ASP.NET

Introduction

Suppose, i have Database table with some rows and columns. Look like given below

How to access cell value from DataTable in ASP.NET

And i want to access cell value of it. Now, first discuss about DataTable, It is container, It Contains multiple Data Columns and multiple Data Rows. It is Main part of ADO.NET library. In this program, we will retrieve cell value of DataTable. Now, first load DataTable with any datasource using load( ) method. After that you can retrieve cell value of it easily.

Now Let's go for an example

Above mentioned snapshot, If we want to access jacob text from given table then consider this table as 2D array. Now, you can access this text from table using itemArray property of DataRow class. Like

string name =  instance of DataTable.Rows[0].ItemArray[1].ToString();
 Similarly, if want to access whole data of single column then must use loop, such as

for (int i = 0; i < table .Rows .Count; i++)
                {
                    
            string  name1 =   instance of DataTable.Rows[i].ItemArray[1].ToString(); +"<br/>";  
                    
                   
                }

Output of given mentioned code

How to access cell value from DataTable in ASP.NET


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class Default5 : System.Web.UI.Page
{
    string name1=string.Empty ;
    protected void Page_Load(object sender, EventArgs e)
    {
        using (SqlConnection con = new SqlConnection())
        {
            con.ConnectionString =ConfigurationManager.ConnectionStrings ["ConnectionString"].ToString ();
            con.Open ();
            using(SqlCommand cmd = new SqlCommand ())
{
                cmd.CommandText = "select * from Register";
                cmd.Connection =con;
                SqlDataReader rd=cmd.ExecuteReader ();
                DataTable table;
                table = new DataTable();
                table.Load(rd);
                for (int i = 0; i < table .Rows .Count; i++)
                {
                    
              name1 +=  table.Rows[i].ItemArray[1].ToString() +"<br/>";  
                    
                   
                }
                Label1.Text = name1;
}
        }

    }

    }

C function for an ADD operation

C function for an ADD operation:


void addlq(QUEUE *front, QUEUE *rear, int item)
{
QUEUE *temp = (QUEUE*) malloc (sizeof(QUEUE));
temp-->i=item;
temp-->link=0;
if (rear= =0)
{ rear=temp; front=temp; }
else
{ rear-->link=temp; rear=temp; }
}

To implement a Linear Queue using Linked List, in C, a self-referential struct QUEUE can be used similar to Linked List implemented STACK. The user define data type struct QUEUE contain one field to store the data say int i and the other field QUEUE *link to store the address of next node.
struct QUEUE
{
int i;
QUEUE *link;
};


Implementation of Circular Queue using 1-d array

Implementation of Circular Queue using 1-d array: 

  The Circular QUEUE can be represented graphically as consecutive blocks when it is implemented using one-dimensional circular array. N the maximum number of elements can be added into the QUEUE is called as size of QUEUE. To represent a QUEUE an array of size N can be used:

 In the above representation the size of QUEUE is 8. FORNT and REAR index variables can be used to do the DELETE and ADD operations respectively. Initially when the QUEUE is empty FORNT and REAR are assigned with a value 0, because of Lower Bound 1 of the array. When both are equal to 0, then the 
QUEUE is said to be empty.
              When first element is added into QUEUE, REAR and FRONT are both incremented by 1 and the element to be added is placed at REAR index of QUEUE, QUEUE is the name of the array i.e. FORNT<--1, REAR<--1 and QUEUE [REAR]<--ITEM. ITEM is the element to be added to QUEUE. In the further addition operations, the REAR is incremented by 1 and the ITEM is copied at REAR index. When the REAR=N, then REAR is set equal to 1 and addition is done at REAR index. When the REAR is equal to N and FRONT=1 OR FORNT=REAR+1, QUEUE is    said to be full. If any addition is done when the QUEUE is full, ‘overflow’ occurs.
             When the element is to be deleted from the QUEUE, the element stored in the QUEUE with FRONT index is deleted by copying it in ITEM and the incrementing FRONT by 1. When FRONT=N then FORNT is set equal to 1. When FRONT is equal to 0, the QUEUE is empty. When the QUEUE is empty and if the deletion operation is done ‘underflow’, occurs.
Example: Consider a QUEUE of size 8. It is initially represented as follows:



        In order to implement the Circular QUEUE using one-dimensional array, an array of size N is used with name QUEUE and FRONT and REAR as the indices to represent the deletion end and addition end respectively. Following algorithms are used for the purpose.

Bind GridView in ASP.NET

Introduction

Grid view is a Data Source Control. Which represents the Data in form of a Table. Its architecture like a table. It have multiple rows and columns. Diagrammatic representations of Grid View shown below:
GridView in ASP.NET


This table have 5 rows and 3 columns.

How to Bind Grid View using Programming


 Before Bind Database table with Grid view, must connect with Database engine. DOTNET provides a Namespace, such as System.Data.SqlClient, which connects front-end to back-end. This Namespace contains multiple classes such as SqlConnection, SqlCommand and etc.
Front-end can connect with back-end using SqlConnection class. First of all, we create an object for it.
We can access the Data member and member function of class with the help of that object. such as

Step-1

using System. Data. SqlClient;

// Create an instance of SqlConnection class
SqlConnection con = new SqlConnection( );

// Access connection string using con instance.

con. ConnectionString = " Connection String Parameter ";

// Initialize connection string with database parameter

Some ConnectionString parameters which passes inside the ConnectionString. like
Data Source : Server Name;
Database Name : Name of your Database;
User-name : Database user name;
Password :  Password of your Database

For example

connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"

Step-2
Now, open the state of connection. Because state of connection close by default. So call open method from instance of SqlConnection class, such as

con.open( );

After making connection, you can access or delete data from Database table. CommandText property of SqlCommand class provide interface with Database table. You can access this property by instance of SqlCommand class, such as

Step-3

SqlCommand cmd = new SqlCommand( );
cmd. CommandText = " Select * from [Table-name]";

This Query retrieves whole table of database. If you want to access particular column from table, must include column name inside the query. Like

cmd. CommandText = "Select column-name from [Table-name]";

Step-4

Now, Connect SqlCommand class with Connection object using connection property of SqlCommand class.
cmd. Connection = con;

Step-5

After fetching the data from database table, Read must from SqlDataReader class, which is inside in System.Data.SqlClient Namespace. Like

SqlDataReader rd= cmd. ExecuteReader( );

Step-6

Bind GridView with DataReader. like

Gridview1. DataSource = rd;
Gridview1. DataBind( );


Complete Source code of Design pattern 

 <form id="form1" runat="server">

    <div>

        <asp:GridView ID="GridView1" runat="server">

        </asp:GridView>    

 </div>

    </form>

Complete Business logic

using System.Data.SqlClient;

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select * from [usertable]";
        cmd.Connection = con;
        SqlDataReader rd = cmd.ExecuteReader();
        GridView1.DataSource = rd;
        GridView1.DataBind();

    }
   
}

Code Generate the following output

Friday, March 14, 2014

Circular Queue for Data Structure in 'C'

Circular Queue:

Circular is a Queue in which the elements are arranged in a circular manner i.e. after the last element the first element comes. Instead of considering a Queue simply as linear, the blocks can be treated to frame a circle and can be felt after the last block there comes the first block. So after index N, 1 is considered as the next index and operations are done accordingly. Circular array is used to implement the circular queue. Circular array is similar to linear array and the only difference is after index N, 1 is treated as the next index.
           
Circular Queue for Data Structure in 'C'
   
 Here in the above representation after the last index N, there comes 1 as the next index. Only here is the difference between the linear array and circular array exits.
                    When addition of items is done in circular queue, another condition is tested. If REAR=N then REAR is set equal to 1 and insertion is done at REAR otherwise REAR is set equal to REAR+1 andinsertion is done. In this case the overflow occurs when FRONT=1 and REAR=N. The overflow also occurs when FRONT=REAR+1. So in circular queue, the queue is said to be full if FRONT=REAR+1 or FRONT=1 and REAR=N.
                   Similarly while doing deletion, when FRONT=N, FRONT is set equal to 1 after deletion. When FRONT=REAR then there is a single element in the Queue. In that case when deletion is done both FRONT and REAR are set equal to 0. While doing deletion underflow occurs when FRONT=0.

                  Only the above mentioned changes are kept in mind while implementing the Circular Queue using one-dimensional array. The remaining part of the algorithms is same as Linear Queue.

© Copyright 2013 Computer Programming | All Right Reserved