Double Link List Code


The Concept of link list and Double link list is clearly explained in Wikipedia and here.I just want to show you the correct code for making doubly link list because sometimes algorithm is explained great but code is not given their.

We first have to make struct and define the structure we want.I just have inserted a character in a node and its next and previous nodes.

So our insert method which would insert a node will be like this. 

void Insert(char a)
{
if(first == NULL)
{ first = new Node();
first->nxt = NULL;
first->prv = NULL;
first->a = a;
last = first;
}
else
{ last->nxt = new Node();
last->nxt->a = a;
last->nxt->prv = last;
last = last->nxt;
last->prv->nxt = last;
}
last->nxt = NULL;
}

And the delete method would be like this.

void Delete(char b)
{
Node * temp = first;
int i = 1;
while(temp!= NULL)
{ if(temp->a == b)
{ temp->prv->nxt = temp->nxt;
temp->nxt->prv = temp->prv;
break;
}
temp = temp->nxt;
}
}

And another method would be which would show All the nodes.

void ShowAll()
{
Node * temp = first;
int i = 1;
while(temp!= NULL)
{
printf("\nAt poistion %d we have %c ",i,temp->a);
temp = temp->nxt;
}
cout << "\nEnd Of List"<< endl;
}

Here is the link from where you can download the CPP file of this code.Download

Continue Reading...

Own Windows Tools With C# (Part 1): Custom Monitor Turn Off


Hey friends, i m going to start new part in this blog where we will make some windows tools by using windows libraries. it customize application made just make windows more easy to use. if you have any idea about it share it with us.

Now i m going to make a application that will shutdown my monitor on one click. Some time we need to turn off monitor we press button of power or monitor but by using this custom application you can easily turn off your monitor and no power issue will be happened.

How to make it???

Create New windows Projects. Form style depend on you what you want. Drag button from tool box double click on button, then you get button click event.

Now write following code to add
interopservices
using System.Runtime.InteropServices;

then following code write as class level.
public int WM_SYSCOMMAND = 0x0112;
public int SC_MONITORPOWER = 0xF170;
[DllImport("user32.dll")]
private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);

in above code we are calling user32.dll, this library holds all operation like this.


Now come to the button click event and write this code..

SendMessage(this.Handle.ToInt32(), WM_SYSCOMMAND, SC_MONITORPOWER, 2);

Now enjoy your own application with windows component.
Download Sample
if you like the post response me in form of comment. and contact me for further detail. Soon i will back with new custom tool. Take Care
Continue Reading...

Insertion Sort


Insertion sort is a simple sorting algorithm, a comparison sort in which the sorted array (or list) is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.....


However, insertion sort provides several advantages:
simple implementation,efficient for (quite) small data sets,efficient for data sets that are already,substantially sorted: the time complexity is O(n + d),
where d is the number of inversions, more efficient in practice than most other simple quadratic (i.e., O(n2)) algorithms such as selection sort or bubble sort; the average running time is n2/4, and the running time is linear in the best case stable (i.e., does not change the relative order of elements with equal keys) in-place (i.e., only requires a constant amount O(1) of additional memory space)
online (i.e., can sort a list as it receives it)

#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int temp;
int j;
int a[10]={10,9,8,7,6,5,4,3,2,1};
for(int i=1;i<=10;i++)
{
temp=a[i]; j=i-1;
while(j>0&&a[j]>temp)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
for(int k=1;k<=10;k++)
printf("%d\n",a[k]);
getche();
}
Continue Reading...

Booth Algorithm Code in C#


One of the simplest algorithm for binary numbers multiplication.The concept and the theory of the booth algorithm is explained fine on Wikipedia and here.That's why I am just giving here a simple code in C# for the booth multiplication of 2 4 bits binary numbers.

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

namespace Booth_s_Algorithm
{
    class Program
    {
        static void Main(string[] args)
        {
            string A = "0000";
            string M, Q;
            string Q_1 = "0";

            Console.Write("Please enter a 4 bit Multiplicant = ");
            M = Console.ReadLine();

            Console.Write("Please enter a  4 bit Multiplier = ");
            Q = Console.ReadLine();

            for (int i = 0; i < 4; i++)
            {
                string Qo_Q = Q[3] + Q_1;
                if (Qo_Q == "11" || Qo_Q == "00")
                {
                    //shift
                    Q_1 = Q[3].ToString();
                    string NewQ = A[3].ToString() + Q[0] + Q[1] + Q[2];
                    Q = NewQ;

                    string NewA = A[0].ToString() + A[0].ToString() + A[1].ToString() + A[2].ToString();
                    A = NewA;

                }
                else if (Qo_Q == "01")
                {
                    A = Add(A, M);
                    Q_1 = Q[3].ToString();
                    string NewQ = A[3].ToString() + Q[0] + Q[1] + Q[2];
                    Q = NewQ;

                    string NewA = A[0].ToString() + A[0].ToString() + A[1].ToString() + A[2].ToString();
                    A = NewA;
                }

                else if (Qo_Q == "10")
                {
                    A = Subtract(A, M);
                    Q_1 = Q[3].ToString();
                    string NewQ = A[3].ToString() + Q[0] + Q[1] + Q[2];
                    Q = NewQ;

                    string NewA = A[0].ToString() + A[0].ToString() + A[1].ToString() + A[2].ToString();
                    A = NewA;
                }
            }

            Console.WriteLine("\n\nA = {0}, Q = {1}, Q_1 = {2} ", A, Q, Q_1);                            

        }
        static string Add(string A, string M)
        {
            string answer;
            string carry = "0";
            string bin0 = "0", bin1 = "0", bin2 = "0", bin3 = "0";
            for (int i = 3; i >= 0; i--)
            {
                if (A[i] == '0' && M[i] == '0' && carry == "0")
                {
                    if (i == 3)
                        bin3 = "0";
                    else
                        if (i == 2)
                            bin2 = "0";
                        else
                            if (i == 1)
                                bin1 = "0";
                            else
                                if (i == 0)
                                    bin0 = "0";

                }
                else
                    if (A[i] == '0' && M[i] == '1' && carry == "0")
                    {
                        if (i == 3)
                            bin3 = "1";
                        else
                            if (i == 2)
                                bin3 = "1";
                            else
                                if (i == 1)
                                    bin2 = "1";
                                else
                                    if (i == 0)
                                        bin1 = "1";
                    }
                else
                        if (A[i] == '1' && M[i] == '0' && carry == "0")
                        {
                            if (i == 3)
                                bin3 = "1";
                            else
                                if (i == 2)
                                    bin2 = "1";
                                else
                                    if (i == 1)
                                        bin1 = "1";
                                    else
                                        if (i == 0)
                                            bin0 = "1";
                        }
                else
                            if (A[i] == '0' && M[i] == '0' && carry == "1")
                            {
                                carry = "0";
                                if (i == 3)
                                    bin3 = "1";
                                else
                                    if (i == 2)
                                        bin2 = "1";
                                    else
                                        if (i == 1)
                                            bin1 = "1";
                                        else
                                            if (i == 0)
                                                bin0 = "1";
                            }
                else
                                if (A[i] == '0' && M[i] == '1' && carry == "1")
                                {
                                    carry = "1";
                                    if (i == 3)
                                        bin3 = "0";
                                    else
                                        if (i == 2)
                                            bin2 = "0";
                                        else
                                            if (i == 1)
                                                bin1 = "0";
                                            else
                                                if (i == 0)
                                                    bin0 = "0";
                                }
                                else
                                    if (A[i] == '1' && M[i] == '0' && carry == "1")
                                    {
                                        carry = "1";
                                        if (i == 3)
                                            bin3 = "0";
                                        else
                                            if (i == 2)
                                                bin2 = "0";
                                            else
                                                if (i == 1)
                                                    bin1 = "0";
                                                else
                                                    if (i == 0)
                                                        bin0 = "0";
                                    }
                                    else if (A[i] == '1' && M[i] == '1' && carry == "0")
                                    {
                                        carry = "1";
                                        if (i == 3)
                                            bin3 = "0";
                                        else
                                            if (i == 2)
                                                bin2 = "0";
                                            else
                                                if (i == 1)
                                                    bin1 = "0";
                                                else
                                                    if (i == 0)
                                                        bin0 = "0";
                                    }
                                    else if (A[i] == '1' && M[i] == '1' && carry == "1")
                                    {
                                        carry = "1";
                                        if (i == 3)
                                            bin3 = "1";
                                        else
                                            if (i == 2)
                                                bin2 = "1";
                                            else
                                                if (i == 1)
                                                    bin1 = "1";
                                                else
                                                    if (i == 0)
                                                        bin0 = "1";
                                    }

            }
            answer = bin0 + bin1 + bin2 + bin3;
            return answer;
        }

        static string Subtract(string A, string M)
        {
            bool shift = false;
            char bin3 = '0', bin2 = '0', bin1 = '0', bin0 = '0';

            for (int i = 3; i >= 0; i--)
            {
                if (shift)
                {
                    if (M[i] == '0')
                    {
                        if (i == 3)
                            bin3 = '1';
                        else if (i == 2)
                            bin2 = '1';
                        else if (i == 1)
                            bin1 = '1';
                        else if (i == 0)
                            bin0 = '1';
                    }
                    else
                        if (M[i] == '1')
                        {
                            if (i == 3)
                                bin3 = '0';
                            else if (i == 2)
                                bin2 = '0';
                            else if (i == 1)
                                bin1 = '0';
                            else if (i == 0)
                                bin0 = '0';
                        }

                }
                else
                {
                    if (M[i] == '1')
                    {
                        if (i == 3)
                            bin3 = '1';
                        else if (i == 2)
                            bin2 = '1';
                        else if (i == 1)
                            bin1 = '1';
                        else if (i == 0)
                            bin0 = '1';
                    }
                    else
                        if (M[i] == '0')
                        {
                            if (i == 3)
                                bin3 = '0';
                            else if (i == 2)
                                bin2 = '0';
                            else if (i == 1)
                                bin1 = '0';
                            else if (i == 0)
                                bin0 = '0';
                        }
                }

                if (M[i] == '1')
                    shift = true;
            }

            string ans = bin0.ToString()+bin1.ToString()+bin2.ToString()+bin3.ToString();

            return ans;
        }
    }
}

Continue Reading...

Insert,Delete and Update through datagrid


Inert,Delete,Update through data grid

In this application we can insert delete and update all through data grid.One thing is to remember is that I have used WPF tool kit Data grid here so if you don't know how to use it then see this post. The main thing that would help us to do this is dataset, as we know when ever we do any editing in our grid then that editing is also done in the dataset so by using the elements in the datagrid we can update , delete or insert data though datagrid.


Below this you can see the code behind the insert button.

        private void Insert_Click(object sender, EventArgs e)
        {
            for (int i = number; i < ds.Tables[0].Rows.Count; i++)
            {
                int rg = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[0]);
                string name = ds.Tables[0].Rows[i].ItemArray[1].ToString();

Insert(Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[0]), ds.Tables[0].Rows[i].ItemArray[1].ToString(),
ds.Tables[0].Rows[i].ItemArray[2].ToString(), ds.Tables[0].Rows[i].ItemArray[3].ToString());

             }
        }


Here is the code of Update Button.The thing to remember in updating the data is that I have taken two data sets and one dataset is updated in the datagrid up the other will be not changed , so in the loop I check every column of each row by comparing those two data sets and if any change is found then the change is made.



private void button4_Click(object sender, EventArgs e)
 {
   bool k = false;
   for (int i = 0; i < number; i++)
   {
   if ((ds.Tables[0].Rows[i].ItemArray[1].ToString() != ps.Tables[0].Rows[i].ItemArray[1].ToString())
   || (ds.Tables[0].Rows[i].ItemArray[2].ToString() != ps.Tables[0].Rows[i].ItemArray[2].ToString())
|| (ds.Tables[0].Rows[i].ItemArray[3].ToString() != ps.Tables[0].Rows[i].ItemArray[3].ToString()))
    {
      k = UpdateData(Convert.ToInt32(ps.Tables[0].Rows[i].ItemArray[0].ToString()), ds.Tables[0].Rows[i].ItemArray[1].ToString(),
ps.Tables[0].Rows[i].ItemArray[2].ToString(), ps.Tables[0].Rows[i].ItemArray[3].ToString());
     MessageBox.Show("Updated");
          }             
    }
 }


And the Code for the Delete button is given below.



      private void DeleteButton_Click(object sender, EventArgs e)
  {
  int i = Convert.ToInt32(controlGrid1.dataGrid1.SelectedIndex.ToString());
  bool d = Delete(Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[0]));
     if (d)
      {
          MessageBox.Show("Deleted");
          UPDATE_dataGrid();
       } 
   else
           MessageBox.Show("Could not delete");
}


Here is the download file for this project and also data base is given.The folders to download are DBMS_Assignment and databasefiles.


download


Continue Reading...
 
|  I Code Help. Blogger Template By Lawnydesignz Powered by Blogger