Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C Programming Help

Options
  • 21-04-2005 10:36pm
    #1
    Closed Accounts Posts: 579 ✭✭✭


    Sorry for the annoyance again, this should more than likely be my last coding problem for a few months at least, you guys have been good to me in the past so I figured I'd give it a shot. I'm trying to create a Queue to store garage information : so far I've got this: -

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

    struct queueNode { /* self-referential structure */
    char data;
    struct queueNode *nextPtr;
    };

    typedef struct queueNode QueueNode;
    typedef QueueNode *QueueNodePtr;

    /* function prototypes */
    void printQueue( QueueNodePtr );
    int isEmpty( QueueNodePtr );
    char dequeue( QueueNodePtr *, QueueNodePtr * );
    void enqueue( QueueNodePtr *, QueueNodePtr *, char );
    void instructions( void );

    int main()
    {
    char make[80];
    char model[80];
    int year;
    float price;
    char reg[80];
    int mileage;

    QueueNodePtr headPtr = NULL, tailPtr = NULL;
    int choice;

    instructions();
    printf( "? " );
    scanf( "%d", &choice );

    while ( choice != 3 ) {

    switch( choice ) {

    case 1:
    printf( "Enter a make: " );
    scanf( "\n%s",make);


    printf( "Enter a model: " );
    scanf( "\n%s",model);


    printf( "Enter a year: " );
    scanf( "\n%i",&year);


    printf( "Enter a price: " );
    scanf( "\n%fl", &price);

    printf( "Enter a car reg: " );
    scanf( "\n%s", reg);


    printf( "Enter a mileage: " );
    scanf( "\n%i", &mileage);



    enqueue( &headPtr, &tailPtr,make,model,year,price,reg,mileage);
    printQueue( headPtr);
    break;
    case 2:
    if ( !isEmpty( headPtr ) ) {
    mileage = dequeue( &headPtr, &tailPtr );
    printf( "%i has been dequeued.\n", mileage );
    }

    printQueue( headPtr );
    break;

    default:
    printf( "Invalid choice.\n\n" );
    instructions();
    break;
    }

    printf( "? " );
    scanf( "%d", &choice );
    }

    printf( "End of run.\n" );
    return 0;
    }

    void instructions( void )
    {
    printf ( "Enter your choice:\n"
    " 1 to add an item to the queue\n"
    " 2 to remove an item from the queue\n"
    " 3 to end\n" );
    }

    void enqueue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr,char value )
    {
    QueueNodePtr newPtr;

    newPtr = malloc( sizeof( QueueNode ) );

    if ( newPtr != NULL ) {
    newPtr->data = value;
    newPtr->nextPtr = NULL;

    if ( isEmpty( *headPtr ) )
    *headPtr = newPtr;
    else
    ( *tailPtr )->nextPtr = newPtr;

    *tailPtr = newPtr;
    }
    else
    printf( "%c not inserted. No memory available.\n",
    value );
    }

    char dequeue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr )
    {
    char value;
    QueueNodePtr tempPtr;

    value = ( *headPtr )->data;
    tempPtr = *headPtr;
    *headPtr = ( *headPtr )->nextPtr;

    if ( *headPtr == NULL )
    *tailPtr = NULL;

    free( tempPtr );
    return value;
    }

    int isEmpty( QueueNodePtr headPtr )
    {
    return headPtr == NULL;
    }

    void printQueue( QueueNodePtr currentPtr )
    {
    if ( currentPtr == NULL )
    printf( "Queue is empty.\n\n" );
    else {
    printf( "The queue is:\n" );

    while ( currentPtr != NULL ) {
    printf( "%c --> ", currentPtr->data );
    currentPtr = currentPtr->nextPtr;
    }

    printf( "NULL\n\n" );
    }
    }

    I tried putting it into a structure and reading the structure but I kept getting errors, any help as usual would be greatly appreciated, Thanks


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Ah, Christ, my eyes! Code tags, people! Especially for lengthy posts.


  • Closed Accounts Posts: 579 ✭✭✭Magnolia_Fan


    what do you mean by code tags, sorry...this is the second or third time I've posted such long code, I figured it could easily be copied and pasted into a word document and then into C


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    [ code] [ /code ]

    Preserves the indentation, makes it easier to read.


  • Registered Users Posts: 5,335 ✭✭✭Cake Fiend


    Holy cow, that's a mess! My brain hurts from trying to understand that code.

    OK, it looks like you're trying to program a simple database using a single-linked list (or Queue as you call it).

    You need to have a good understanding of pointers before you go near linked lists - it doesn't look like you do.

    There are way too many problems with this code to list out in one post, so I've attached a very basic database I programmed for you to look at (change the .txt extension to .c). My code has no buffer overrun protection in it, so bear that in mind (also remember never to do that yourself :) )

    Post again if there is anything you don't understand in my code, or if there are things about yours that you still don't get. First and foremost though, make sure your pointer knowledge is up to scratch.


  • Closed Accounts Posts: 579 ✭✭✭Magnolia_Fan


    Is there anyway of doing it without using Files?


  • Advertisement
  • Registered Users Posts: 5,335 ✭✭✭Cake Fiend


    Yes, I only put in file i/o in there so that the database would be saved. Change 'head = read_data(head);' to 'head = NULL;' and remove 'write_data(head);' from the end of main() and it should completely bypass the file i/o functions. Apart from the reading on load and writing on exit, everything is done in RAM.

    How are you getting on with it so far?


  • Closed Accounts Posts: 1,575 ✭✭✭elivsvonchiaing


    struct queueNode { /* self-referential structure */
    char data;
    /* should be char *data */
    struct queueNode *nextPtr;
    };
    ...
    ...

    void enqueue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr,char value )
    /* should be void enqueue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr,char *value ) */

    {
    QueueNodePtr newPtr;
    ++ char *buff;

    newPtr = malloc( sizeof( QueueNode ) );
    ++ buff = malloc(strlen(value));
    ++ strcpy(buff, value);

    if ( newPtr != NULL ) {
    newPtr->data = buff;
    ....


    Just what I spotted immediately - might be more probs though...


  • Closed Accounts Posts: 579 ✭✭✭Magnolia_Fan


    Well, I'll try that tommorow its a little late now, I took a look at your example today, its actually a helluva lot cooler than the way we do it in out tutorials but I think if I handed it up the lecturer may be upset. I was kind of caught up with a VB.Net project and neglected C for a while so my pointers ain't up to scratch. I tried tinkering with yours but of course I'd screw it up. But with your new instructions I'd say I should get it working.
    P.S Hey Thanks alot guys, your always great help on this site...you'll get me passed my exams!


Advertisement