C Programming Tutorial

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

#define TOP 1
#define BOT 2

struct node
{
    char plate [15] ;
    struct node *link ;
} *front[5], *rear[5] ;

char plate[15], temp[15] ;
int i ;

void add_dq ( struct node**, struct node**, int, char* ) ;
char* del_dq ( struct node**, struct node**, int ) ;
void push ( struct node**, char* ) ;
char* pop ( struct node** ) ;
void q_display ( struct node * ) ;

void main( )
{
    char ad ;
    int s, lane = -1, min, lc ;

    clrscr( );
    while ( 1 )
    {
        for ( i = 0 ; i < 5 ; i++ )
        {
            printf( \"lane %d: \", i ) ;
            q_display ( front[i] ) ;
        }

        printf( \"\\nArrival/Departure/Quit? ( A/D/Q ): \" ) ;
        ad = getch( ) ;

        if ( toupper ( ad ) == \'Q\' )
            exit ( 1 ) ;

        printf ( \"\\nEnter license plate num:\" ) ;
        gets ( plate ) ;
        ad = toupper ( ad ) ;

        if ( ad == \'A\' )  /* arrival of car */
        {
            lane = -1 ;  /* assume no lane is available */
            min = 10 ;
            for ( i = 0 ; i < 5 ; i++ )
            {
                s = count ( front[i] ) ;
                if ( s < min )
                {
                    min = s ;
                    lane = i ;
                }
            }

            if ( lane == -1 )
                printf ( \"\\nNo room available\" ) ;
            else
            {
                add_dq ( &front[ lane ], &rear[ lane ], BOT, plate ) ;
                printf ( \"\\npark car at lane %d slot %d\\n\", lane, s ) ;
            }
        }
        else
        {
            if ( ad == \'D\' )  /* departure of car */
            {
                for ( i = 0 ; i < 5 ; ++i )
                {
                    s = search ( front[i], plate ) ;
                    if ( s != -1 )
                    {
                        lane = i ;
                        break ;
                    }
                }
                if ( i == 5 )
                    printf ( \"\\nno such car!!\\n\" ) ;
                else
                {
                    printf ( \"\\ncar found at lane %d slot %d\\n\", lane, s ) ;
                    del_dq ( &front[ lane ], &rear[ lane ], s ) ;
                }
            }
            else
                if ( ad == \'Q\' )
                    exit ( 1 ) ;
        }
    }
}

/* adds a new element at the end of queue */
void add_dq ( struct node **f, struct node **r, int tb, char *p )
{
    struct node *q ;
    /* create new node */
    q = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
    strcpy ( q -> plate, p ) ;
    q -> link = NULL ;

    /* if the queue is empty */
    if ( *f == NULL )
        *f = q ;
    else
    {
        if ( tb == BOT )
            ( *r ) -> link = q ;
        else
        {
            q -> link = *f ;
            *f = q ;
            return ;
        }
    }
    *r = q ;
}

char* del_dq ( struct node **f, struct node **r, int n )
{
    struct node *q, *top = NULL ;
    /* if queue is empty */
    if ( *f == NULL )
        printf ( \"queue is empty\" ) ;
    else
    {
        if ( n == 0 )
        {
            strcpy ( temp, ( *f ) -> plate ) ;
            q = *f ;
            *f = ( *f ) -> link ;
            free ( q ) ;
            return temp ;
        }

        /* locate node */
        for ( i = 0 ; i < n ; i++ )
        {
            /* drive out cars */
            push ( &top, ( *f ) -> plate ) ;

            /* delete the node */
            q = *f ;
            *f = q -> link ;
            free ( q ) ;
        }

        /* delete the nth node */
        q = *f ;
        *f = q -> link ;
        free ( q ) ;

        for ( i = 0 ; i < n ; i++ )
        {
            strcpy ( temp, pop ( &top ) ) ;

            /* add the node */
            add_dq ( f, r, TOP, temp ) ;
        }
    }
}

int count ( struct node *q )
{
    int c = 0 ;

    /* traverse the entire linked list */
    while ( q!= NULL )
    {
        q = q -> link ;
        c++ ;
    }
    return c ;
}

int search ( struct node *q, char *p )
{
    int s = -1, c = 0 ;

    while ( q != NULL )
    {
        if ( strcmp ( p, q -> plate ) == 0 )
        {
            s = c ;
            break ;
        }
        else
        {
            q = q -> link ;
            c++ ;
        }
    }
    return ( s ) ;
}

/* adds a new element to the top of stack */
void push ( struct node **s, char* item )
{
    struct node *q ;
    q = ( struct node* ) malloc ( sizeof ( struct node ) ) ;
    strcpy ( q -> plate, item ) ;
    q -> link = *s ;
    *s = q ;
}

/* removes an element from top of stack */
char* pop ( struct node **s )
{
    struct node *q ;

    /* if stack is empty */
    if ( *s == NULL )
    {
        return NULL ;
    }
    else
    {
        q = *s ;
        strcpy ( temp, q -> plate ) ;
        *s = q -> link ;
        free ( q ) ;
        return ( temp ) ;
    }
}

void q_display ( struct node *q )
{
    while( q != NULL )
    {
        printf ( \"%s\", q -> plate ) ;
        q = q -> link ;
    }
    printf ( \"\\n\" ) ;
}

    Related Post:
  1. Program to display numbers using do while loop

  2. Program to sort a linked list by readjusting the links

  3. PROGRAM TO FIND ANSWER OF THE FOLLOWING SERIES. INPUT STEPS FROM KEYBOARD. SUM =12-32+52-72+.....

  4. Program of BAIRSTOWS METHOD

  5. Illustration of subscripted structure variables , ARRAYS OF STRUCTURES

  6. ACCESSING VARIABLES USING POINTERS

  7. Program to find sum of the elements of the array using self scheduling

  8. Program to assign the pointer variable to another pointer and display the contents of both pointer variables

  9. DEFINING, ASSIGNING AND ACCESSING VALUES TO STRUCTURE MEMBERS

  10. Program to find ascii value of entered text or word for each character

  11. Program to display menus like windows operating system and also can add as many as user want

  12. PROGRAM TO FIND ANSWER OF THE FOLLOWING SERIES. INPUT STEPS FROM KEYBOARD. SUM=X-X3/3!+X5/5!-X7/7!+...(TAKE VALUE OF X ALSO).

  13. Program to print triangle of numbers as shown in description

  14. Program to Activate and Deactivate a TSR that interchange the Small and Capital Letters after every 10 seconds

  15. Program to show the Basic Mouse Handling in Graphics Mode

  16. Program to evaluate fibonacci series and checking whether or not it is fibonacci number

  17. Program to interchange the Small and Captial Letters on the screen after every 5th second

  18. PROGRAM TO COMPUTE n! USING SELF-SCHEDULING

  19. Program to read two positive numbers and interchange its values using *(mul) and /(div) operators and without using any extra variable

  20. PROGRAM TO FIND ANSWER OF THE FOLLOWING SERIES. INPUT STEPS FROM KEYBORD. SUM=X-X3/3!+X5/5!-X7/7!+...(TAKE VALUE OF X ALSO).

 
 
Didn't find what you were looking for? Find more on A car garage simulation using de-queue (link list implementation)