Sunday 17 February 2013

Important c program part2


  Program to implement a Linked Queue. 

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

struct Node
{
int item;
struct Node *next;
};

typedef struct Node *Nodeptr;

struct Queue
{
Nodeptr front, rear;
};

int empty( struct Queue *q )
{
if( q->front == NULL )
return 1;
else
return 0;
}

Nodeptr getnode()
{
return( ( Nodeptr ) malloc( sizeof( struct Node ) ) );
}

void insert( struct Queue *q, int x )
{
Nodeptr temp;
temp = getnode();

if( temp == NULL )
{
printf("The memory is full.\n");
return;
}

temp->item = x;
temp->next = NULL;

if( q->rear == NULL )
q->front = temp;
else
(q->rear)->next = temp;

q->rear = temp;
}

int del( struct Queue *q )
{
Nodeptr temp;
int x;

if( empty( q ) )
{
printf("The queue is empty.\n");
return( -999 );
}

temp = q->front;
x = (q->front)->item;
q->front = (q->front)->next;

if( empty( q ) )
q->rear = NULL;

free( temp );
return( x );
}

void display( struct Queue *q )
{
Nodeptr temp;
if( empty( q ) )
{
printf("The queue is empty.\n");
}
else
{
temp = q->front;
while( temp != NULL )
{
printf("%d -> ",temp->item);
temp = temp->next;
}
printf("END\n");
}
}

void main()
{
struct Queue *q;
char c;
int x;

clrscr();

   q->front = q->rear = NULL;

do
{
printf("\nMain Menu: "
  "\n    1. Empty,"
  "\n    2. Insert,"
  "\n    3. Delete,"
  "\n    4. Display,"
  "\n    5. Exit."
  "\nEnter your choice: ");
c = getche();
printf("\n");

switch( c )
{
case '1':
if( empty( q ) )
printf("The queue is empty.\n");
else
printf("The queue is not empty.\n");
break;

case '2':
printf("Enter item to insert: ");
scanf("%d",&x);
insert( q, x );
break;

case '3':
x = del( q );
if( x != -999 )
printf("The removed item: %d\n",x);
break;

case '4':
display( q );
break;
}
}
while( c != '5' );
}

...............................................................................................................................

Program to implement a Linked Stack. 

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

struct Node
{
int item;
struct Node *next;
};

typedef struct Node *Nodeptr;

struct Stack
{
Nodeptr top;
};

int empty( struct Stack *s )
{
if( s->top == NULL )
return 1;
else
return 0;
}

Nodeptr getnode()
{
return( ( Nodeptr ) malloc( sizeof( struct Node ) ) );
}

void push( struct Stack *s, int x )
{
Nodeptr temp;
temp = getnode();
if( temp == NULL )
printf("The memory is full.\n");
else
{
temp->item = x;
temp->next = s->top;
s->top = temp;
}
}

int pop( struct Stack *s )
{
Nodeptr temp;
int x;

if( empty( s ) )
{
printf("The linked stack is empty.\n");
x =  -999;
}
else
{
temp = s->top;
x = (s->top)->item;
s->top = (s->top)->next;
free( temp );
}
return( x );
}

void display( struct Stack *s )
{
Nodeptr temp;

if( empty( s ) )
{
printf("The stack is empty.\n");
return;
}

temp = s->top;
while( temp != NULL )
{
printf("%d -> ",temp->item );
temp = temp->next ;
}
printf("END.\n");
}

void main()
{
struct Stack *s;
char c;
int x;

clrscr();

s->top = NULL;

do
{
printf("\nMain Menu: "
  "\n    1. Empty,"
  "\n    2. Insert,"
  "\n    3. Delete,"
  "\n    4. Display,"
  "\n    5. Exit."
  "\nEnter your choice: ");
c = getche();
printf("\n");

switch( c )
{

case '1':
if( empty( s ) )
printf("The linked stack is empty.\n");
else
printf("The linked stack is not empty.\n");
break;

case '2':
printf("Enter item to insert: ");
scanf("%d",&x);
push( s, x );
break;

case '3':
x = pop( s );
if( x != -999 )
printf("The popped item: %d\n",x);
break;

case '4':
printf("The linked stack: ");
display( s );
break;
}
}
while( c != '5' );
}

....................................................................................................................................


/* Write a C program to evaluate a postfix expression. */

#include <stdio.h>
#include <conio.h>
#include <math.h>

#define  STACKSIZE  20

struct Stack
{
int item[STACKSIZE];
int top;
};

int empty( struct Stack *s )
{
if( s->top == -1 )
return( 1 );
else
return( 0 );
}

void push( struct Stack *s, int x )
{
if( s->top == STACKSIZE - 1 )
{
printf("\nThe stack is full.");
getch();
exit( 0 );
}
else
s->item[ ++s->top ] = x;
}

int pop( struct Stack *s )
{
if( empty( s ) )
{
printf("\nThe stack is empty.");
getch();
exit( 0 );
}
return( s->item[ s->top-- ] );
}

int result( char postfix[] )
{
struct Stack *s;
char x;
int a, b, i, value;

s = ( struct Stack * ) malloc( sizeof( struct Stack ) );
s->top = -1;

for( i = 0 ; ( x = postfix[i] ) != '\0'; i++ )
{
if( isdigit( x ) )
{
push( s, (x - 48 ) );
}
else
{
b = pop( s );
a = pop( s );
switch( x )
{
case '+':
value = a + b;
break;
case '-':
value = a - b;
break;
case '*':
value = a * b;
break;
case '/':
value = a / b;
break;
case '%':
value = a % b;
break;
case '$':
value = pow( a, b );
break;
default:
printf("\n'%c' is an invalid operator.",x);
getch();
exit(0);
}

push( s, value );
}
}
return( pop( s ) );
}


void main()
{
char postfix[30];

clrscr();
printf("Enter a postfix expression: ");
scanf( "%s",postfix );

printf("\nThe result of the postfix expression => %d", result( postfix ) );
getch();
}

....................................................................................................................................

Program to implement a minimum priority queue

#include <stdio.h>
#include <conio.h>

#define  QSIZE  6

struct Queue
{
int item[QSIZE];
int front;
int rear;
};

int empty( struct Queue *pq )
{
if( pq->front == pq->rear )
return 1;
else
return 0;
}

void pqinsert( struct Queue *pq, int x )
{
if( pq->rear == QSIZE - 1 )
printf("The queue is full.");
else
pq->item[++pq->rear] = x;
}

int pqmindel( struct Queue *pq )
{
int i, min, index;

if( empty( pq ) )
{
printf("\nThe queue is empty.");
return( -999 );
}

min = pq->item[pq->front];
index = pq->front;
for( i = pq->front + 1; i <= pq->rear; i++ )
if( pq->item[i] < min )
{
min = pq->item[i];
index = i;
}

for( i = index; i < pq->rear; i++ )
pq->item[i] = pq->item[i+1];

pq->rear--;

return( min );
}

void display( struct Queue *pq )
{
int i;
if( empty( pq ) )
printf("\nThe queue is empty.");
else
{
printf("\nThe Pririty Queue: ");
for( i = pq->front + 1; i <= pq->rear; i++ )
printf("%d ",pq->item[i]);
}
}

void main()
{
struct Queue *pq;
char c;
int x;

clrscr();

pq->front = pq->rear = -1;

do
{
printf("\n\nMain Menu: "
  "\n    1. Empty,"
  "\n    2. Insert,"
  "\n    3. Delete,"
  "\n    4. Display,"
  "\n    5. Exit."
  "\nEnter your choice: ");
c = getche();

switch( c )
{
case '1':
if( empty( pq ) )
printf("\nThe queue is empty.");
else
printf("\nThe queue is not empty.");
break;

case '2':
printf("\nEnter item to insert: ");
scanf("%d",&x);
pqinsert( pq, x );
break;

case '3':
x = pqmindel( pq );
if( x != -999 )
printf("\nThe removed item: %d",x);
break;

case '4':
display( pq );
break;
}
}
while( c != '5' );
}

..............................................................................................................................................


Program to implement linear queue

#include <stdio.h>
#include <conio.h>

#define  QSIZE  6

struct Queue
{
int item[QSIZE];
int front;
int rear;
};

int empty( struct Queue *q )
{
if( q->front == q->rear )
return 1;
else
return 0;
}

void insert( struct Queue *q, int x )
{
if( q->rear == QSIZE - 1 )
printf("The queue is full.");
else
q->item[++q->rear] = x;
}

int del( struct Queue *q )
{
if( empty( q ) )
{
printf("\nThe queue is empty.");
return( -999 );
}
else
return( q->item[++q->front] );
}

void display( struct Queue *q )
{
int i;
if( empty( q ) )
printf("\nThe queue is full.");
else
{
printf("\nThe Queue: ");
for( i = q->front + 1; i <= q->rear; i++ )
printf("%d ",q->item[i]);
}
}

void main()
{
struct Queue q;
char c;
int x;

clrscr();

q.front = q.rear = -1;

do
{
printf("\n\nMain Menu: "
  "\n    1. Empty,"
  "\n    2. Insert,"
  "\n    3. Delete,"
  "\n    4. Display,"
  "\n    5. Exit."
  "\nEnter your choice: ");
c = getche();

switch( c )
{
case '1':
if( empty( &q ) )
printf("\nThe queue is empty.");
else
printf("\nThe queue is not empty.");
break;

case '2':
printf("\nEnter item to insert: ");
scanf("%d",&x);
insert( &q, x );
break;

case '3':
x = del( &q );
if( x != -999 )
printf("\nThe removed item: %d",x);
break;

case '4':
display( &q );
break;
}
}
while( c != '5' );
}


Posted on 10:19 | Categories:

Friday 15 February 2013

Importatnt Programs in C#


Demo on Class , Members and Object

using System;

namespace FreshersLike
{
    class Test
    {
        public int ns;            //Non-Static Member
        public static int s;    //Static Member
    }

    class MembersDemo
    {
        public static  void Main()
        {
            Test t1 = new Test();
            Test t2;

            t2 = new Test();
            t1.ns = 100;

            t2.ns = 1000;
            // t1.s = 500; Error

            Test.s = 500;
            Console.WriteLine("{0} , {1} , {2} ", t1.ns , t2.ns , Test.s);
        }
    }
}
................................................................................................................................


Demo on Optional Paramter & Named Parameter Passing.

using System;

namespace FreshersLike
{
    class Employee
    {

        private int Empno;
        private string Ename , Job;

        private decimal Salary, Commission;

        public void SetEmployee(int eno, string ename, decimal comm = 0, decimal salary = 10000, string job = "SALESMAN")
        {
            Empno = eno;

            Ename = ename;

            Job = job;

            Salary = salary;

            Commission = comm;
        }

        public void ShowEmployee()
        {
            Console.WriteLine("Employee # : {0} \nEmployee Name : {1} \nSalary : Rs. {2} \nCommission : Rs. {3} \nJob : {4} ", Empno , Ename , Salary , Commission , Job );
        }
    }

    class OptionalParameterDemo
    {
        public static void Main()
        {
            Employee emp = new Employee();

            //emp.SetEmployee(101, "Ravi", salary: 50000, job: "Manager");

            emp.SetEmployee(101, "Kumar", 1000);

            emp.ShowEmployee();
        }
    }
}

.........................................................................................................................................

Demo on Parsing


using System;

namespace FreshersLike
{
    class ParsingDemo
    {
        public static void Main()
        {
            int Empno;
            string Ename;
            decimal Salary;
            DateTime Hiredate;

            Console.Write("Enter Employee # : ");
            Empno = int.Parse(Console.ReadLine());

            Console.Write("Enter Employee Name : ");
            Ename = Console.ReadLine();

            Console.Write("Enter Salary : ");
            Salary = decimal.Parse(Console.ReadLine());

            Hiredate = DateTime.Now; //Assigns the current system data and time 

            Console.Clear(); //Clears the Screen

            Console.ForegroundColor = ConsoleColor.Yellow; //Sets the Fore color of text

            Console.WriteLine("Employee # : {0} \nEmployee Name : {1} \nSalary : Rs. {2} \nDate of Joining : {3} ", Empno, Ename, Salary, Hiredate);
        }
    }
}

........................................................................................................................................


//Demo on Type Casting

using System;

namespace CSIntro303
{
    class TypeCastingDemo
    {
        public static void Main()
        {
            int no1, no2;
            float result1, result2;

            Console.WriteLine("Enter Two Numbers Below : " );
            no1 = int.Parse(Console.ReadLine());
            no2 = int.Parse(Console.ReadLine());

            result1 = no1 / no2;
            result2 = (float)no1 / no2; //Type Casting

            Console.WriteLine("{0} / {1} = {2} " , no1,no2,result1);
            Console.WriteLine("{0} / {1} = {2} ", no1, no2, result2);
        }
    }
}
















Wednesday 13 February 2013

Important Programs in C




Program to implement circular queue 

#include <stdio.h>
#include <conio.h>
#define  QSIZE  5

struct Queue
{
int item[QSIZE];
int front, rear;
};

int empty( struct Queue *q )
{
if( q->front == q->rear )
return( 1 );
return(0);
}

void insert( struct Queue *q, int x )
{
int temp;
temp = q->rear;
q->rear = ( q->rear + 1 ) % QSIZE;

if( q->rear == q->front )
{
printf("\nThe queue is full.");
q->rear = temp;
}
else
q->item[ q->rear] = x;
}

int del( struct Queue *q )
{
if( empty(q) )
{
printf("\nThe queue is empty.");
return( -9999 );
}

q->front = ( q->front + 1 ) % QSIZE;
return( q->item[ q->front ] );
}

void operation()
{
struct Queue *q;
char c;
int x, value;

q = ( struct Queue * ) malloc( sizeof( struct Queue ) );

do
{
printf("\n\nMain Menu:\n"
   "    1. Empty,\n"
   "    2. Insert,\n"
   "    3. Delete,\n"
   "    4. Display,\n"
   "    5. Exit.\n"
   "Enter your choice: ");
c = getche();

switch( c )
{
case '1':
if( empty(q) )
printf("\nThe queue is empty.");
else
printf("\nThe queue is not empty.");
break;

case '2':
printf("\nEnter the item to insert: ");
scanf("%d",&x);
insert( q, x );
break;

case '3':
if( ( value = del( q ) ) != -9999 )
printf("The deleted item: %d",value );
break;
}
}
while( c != '5' );
}

void main()
{
clrscr();
operation();
}

...................................................................................................................................
Program To implement FourLink

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

struct Node
{
int item;
struct Node *next;
};

typedef struct Node *Nodeptr;

Nodeptr getnode()
{
return( (Nodeptr) malloc( sizeof( struct Node ) ) );
}

Nodeptr create( int x )
{
Nodeptr temp;
temp = getnode();
if( temp == NULL )
{
printf("The memory is full.\n");
}
else
{
temp->item = x;
temp->next = NULL;
}
return( temp );
}

Nodeptr insert(  Nodeptr p, int x )
{
Nodeptr temp;

temp = getnode();
temp->item = x;
temp->next = NULL;

p->next = temp;
return( temp );
}

Nodeptr dividing( Nodeptr list, int x )
{
Nodeptr l, p, temp;
l = NULL;
while( list != NULL )
{
if( list->item == x )
{
if( l == NULL )
{
l = getnode();
l->item = list->item;
temp = l;
}
else
{
p = getnode();
p->item = list->item;
l->next = p;
l = p;
}
x = x + 4;
}
list = list->next;
}
l->next = NULL;
return( temp );
}

void display( Nodeptr temp )
{
if( temp != NULL )
{
printf("%d -> ",temp->item );
display( temp->next );
}
else
{
printf("END.\n");
}
}

void main()
{
Nodeptr list, temp, L[4];
int i, x;

clrscr();

printf("Creating the Main Linked List:-\n"
   "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
   "Enter the numbers to insert ( at end type -999 ):  ");
scanf("%d",&x);
list = create( x );
temp = list;
while( 1 )
{
scanf("%d",&x);
if( x == -999 )
break;
temp = insert( temp, x );
}

   for( i = 0; i < 4; i++ )
{
L[i] = dividing( list, i + 1 );
printf("\nDisplaying the linked list L[%d]:-\n"
   "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
   "The linked list: ", i + 1);
display( L[i] );
}

printf("\nDisplaying the main linked list:-"
   "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
   "\nThe linked list: ");
display( list );

getch();
}

..................................................................................................................................
Program To implement Heap Sort

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void buildheap( int a[], int n )
{
int i, s, f, elt;

for( i = 1; i < n; i++ )
{
elt = a[i];
s = i;
f = ( s - 1 ) / 2;

while( s > 0 && a[f] < elt )
{
a[s] = a[f];
s = f;
f = ( s - 1 ) / 2;
}

a[s] = elt;
}
}

void heapsort( int a[], int n )
{
int i, s, f, ivalue;
buildheap( a, n );

for( i = n - 1; i > 0; i-- )
{
ivalue = a[i];
a[i] = a[0];
f = 0;

if( i == 1 )
s = -1;
else
s = 1;

if( a[s] < a[s+1] && i > 2 )
s = 2;

while( s >= 0 && ivalue < a[s] )
{
a[f] = a[s];
f = s;
s = f * 2 + 1;

if( s + 1 <= i - 1 && a[s] < a[s+1] )
s = s + 1;

if( s > i - 1 )
s = -1;
}

a[f] = ivalue;
}
}

void main()
{
int *item, n, i;

clrscr();
printf("Enter total number of items: ");
scanf("%d",&n);

item = ( int * ) malloc( n * sizeof( int ) );

printf("Enter the items: ");
for( i = 0; i < n; i++ )
scanf("%d", (item+i) );

heapsort( item, n );

printf("\nThe sorted list => ");
for( i = 0; i < n; i++ )
printf("%d ", *(item+i) );

getch();
}
................................................................................................................................

Implementation Of HuffMan Coding

 #include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

#define  SIZE  10

struct Node
{
int info;
struct Node *father, *left, *right;
};

typedef struct Node *Nodeptr;

struct Pqueue
{
Nodeptr item[SIZE];
int front, rear;
};


int empty( struct Pqueue *pq )
{
if( pq->front == pq->rear )
return 1;
else
return 0;
}

void pqinsert( struct Pqueue *pq, Nodeptr x )
{
if( pq->rear == SIZE - 1 )
printf("The queue is full.");
else
pq->item[++pq->rear] = x;
}

Nodeptr pqmindel( struct Pqueue *pq )
{
int i, index;
Nodeptr min;

if( empty( pq ) )
{
printf("UNDERFLOW. The queue is empty.");
getch();
exit( 1 );
}

min = pq->item[0];
index = 0;

for( i = 1; i <= pq->rear; i++ )
if( ( pq->item[i] )->info < min->info )
{
min = pq->item[i];
index = i;
}

for( i = index; i < pq->rear; i++ )
pq->item[i] = pq->item[i+1];

pq->rear--;

return( min );
}

Nodeptr getnode()
{
return( ( Nodeptr ) malloc( sizeof ( struct Node ) ) );
}

Nodeptr maketree( int a )
{
Nodeptr temp;
temp = getnode();
temp->info = a;
temp->father = temp->left = temp->right = NULL;
return( temp );
}

void setleft( Nodeptr p, Nodeptr x )
{
if( p == NULL )
printf("Inavlid insertion.");
else if( p->left != NULL )
printf("Left node already present.");
else
{
p->left =  x ;
x->father = p;
}
}

void setright( Nodeptr p, Nodeptr x )
{
if( p == NULL )
printf("Inavlid insertion.");
else if( p->right != NULL )
printf("Right node already present.");
else
{
p->right = x;
x->father = p;
}
}

int isleft( Nodeptr p )
{
if( ( p->father )->left == p )
return( 1 );
else
return( 0 );
}


void huffmantree( int frcq[], int n )
{
struct Pqueue *root;
Nodeptr tree, p, p1, p2, pos[SIZE];
char code[20][15], temp[15];
int i, j, k;

root->rear = root->front = -1;
for( i = 0; i < n; i++ )
{
p = maketree( frcq[i] );
pos[i] = p;
pqinsert( root, p );
}

while( root->rear > 0  )
{
p1 = pqmindel( root );
p2 = pqmindel( root );

p = maketree( p1->info + p2->info );

setleft( p, p1 );
setright( p, p2 );

pqinsert( root, p );
}

tree = pqmindel( root );

for( i = 0; i < n; i++ )
{
j = 0;
p = pos[i];
code[i][j] = '\0';

while( p != tree )
{
if( isleft( p ) )
code[i][++j] = '0';
else
code[i][++j] = '1';

p = p->father;
}

for( k = 0; j >= 0; k++, j-- )
temp[k] = code[i][j];

strcpy( code[i], temp );
}

for( i = 0; i < n; i++ )
printf("\nCode of frequency %d => %s",frcq[i],code[i]);
}

void main()
{
int *frcq, i, n;

printf("Enter total number of frequencies: ");
scanf("%d",&n);

frcq = ( int * ) malloc( n * sizeof( int ) );

printf("Enter the frequencies: ");
for( i = 0; i < n; i++ )
scanf("%d",(frcq+i));


huffmantree( frcq, n );

getch();
}



Posted on 00:02 | Categories: