Sunday, June 6, 2010

C Program to implement a circular queue as a linked list

Here's a simple C Program to implement a circular queue as a linked list





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

/* structure containing a data part and link part */
struct node
{
int data ;
struct node * link ;
} ;

void addcirq ( struct node **, struct node **, int ) ;
int delcirq ( struct node **, struct node ** ) ;
void cirq_display ( struct node * ) ;

void main( )
{
struct node *front, *rear ;

front = rear = NULL ;

addcirq ( &front, &rear, 10 ) ;
addcirq ( &front, &rear, 17 ) ;
addcirq ( &front, &rear, 18 ) ;
addcirq ( &front, &rear, 5 ) ;
addcirq ( &front, &rear, 30 ) ;
addcirq ( &front, &rear, 15 ) ;

clrscr( ) ;

printf ( "Before deletion:\n" ) ;
cirq_display ( front ) ;

delcirq ( &front, &rear ) ;
delcirq ( &front, &rear ) ;
delcirq ( &front, &rear ) ;

printf ( "\n\nAfter deletion:\n" ) ;
cirq_display ( front ) ;
}

/* adds a new element at the end of queue */
void addcirq ( struct node **f, struct node **r, int item )
{
struct node *q ;

/* create new node */
q = malloc ( sizeof ( struct node ) ) ;
q - > data = item ;

/* if the queue is empty */
if ( *f == NULL )
*f = q ;
else
( *r ) - > link = q ;

*r = q ;
( *r ) - > link = *f ;
}

/* removes an element from front of queue */
int delcirq ( struct node **f, struct node **r )
{
struct node *q ;
int item ;

/* if queue is empty */
if ( *f == NULL )
printf ( "queue is empty" ) ;
else
{
if ( *f == *r )
{
item = ( *f ) - > data ;
free ( *f ) ;
*f = NULL ;
*r = NULL ;
}
else
{
/* delete the node */
q = *f ;
item = q - > data ;
*f = ( *f ) - > link ;
( *r ) - > link = *f ;
free ( q ) ;
}
return ( item ) ;
}
return NULL ;
}

/* displays whole of the queue */
void cirq_display ( struct node *f )
{
struct node *q = f, *p = NULL ;

/* traverse the entire linked list */
while ( q != p )
{
printf ( "%d\t", q - > data ) ;

q = q - > link ;
p = f ;
}
}

Monday, April 19, 2010

Which is the Best Place to Find Saving Deals and Discount Coupon Offers?

More often than not, we tend to look for discount deals on various merchandise and goods while shopping online, or looking for services such as car rentals. But, just when you really need something urgently, you don’t seem to get hold of the right deal! Well, if this has happened to you in past, then here are some great deals on Enterprise Coupons.

Monday, February 15, 2010

Sorting a Structure of Multiple Keys

C Program that accepts a set of 5 records for students -

Ask user to enter name, age and height of a student. Sort these records in ascending order of their names. If the names are alike then sort according to their age.

/* Sorting of a structure of multiple keys. */

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

struct stud
{
      char name[25] ;
      int age ;
      float height ;
} ;

void main( )
{
      int i, j, choice ;
      struct stud s[5], temp ;

      float ff ( float ) ;

      clrscr( ) ;

      printf ( "Enter student's name, age and height in cm :-\n") ;
      for ( i = 0 ; i  < = 4 ; i++ )
      {
            fflush ( stdin ) ;
            gets ( s[i].name ) ;
            scanf ( "%d %f", &s[i].age, &s[i].height ) ;
      }

      clrscr( ) ;

      for ( i = 0 ; i  < = 3 ; i++ )
      {
            for ( j = 0 ; j  < = 3 - i ; j++ )
            {
                  if ( strcmp ( s[j].name, s[j + 1].name )  > = 0 )
                  {
                        if ( strcmp ( s[j].name, s[j + 1].name ) == 0 )
                        {
                              if ( s[j].age  >  s[j + 1].age )
                              {
                                    temp = s[j] ;
                                    s[j] = s[j + 1] ;
                                    s[j + 1] = temp ;
                              }
                        }
                        else
                        {
                              temp = s[j] ;
                              s[j] = s[j + 1] ;
                              s[j + 1] = temp ;
                        }
                  }
            }
      }

      printf ( "Records after sorting :-\n") ;
      printf ( "Students Name\t\tAge Height\n" ) ;

      for ( i = 0 ; i  < = 4 ; i++ )
      {
            printf ( "%-20s %2d %.2f\n", s[i].name, s[i].age, s[i].height ) ;
      }

      getch( ) ;
}

float ff ( float f )
{
      float *f1 = &f ;
    return *f1 ;
}

So, how is that for a Tutorial?