Linked List (Base 21)

This is a discussion on Linked List (Base 21) within the C Programming forums, part of the General Programming Boards category; We have to write a program that has base 21 numbers in an infile and have it output it in ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    2

    Linked List (Base 21)

    We have to write a program that has base 21 numbers in an infile and have it output it in a sorted list with it's decimal equivalent. Here is the code I have so far...

    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    /* --------------------------------------------------------------------- */
    
    struct NumNodeType
    {
        int dat;
        struct NumNodeType *next;
    };
    
    /* Define new type names:  BaseElem   and   BasePtr  
    * to refer to the list nodes.  These new names can
    * be used in place of struct NumNodeType and
    * struct NumNodeType *  repsectively.
    */
    typedef struct NumNodeType BaseElem;
    typedef struct NumNodeType * BasePtr;
    
    
    /* --------------------------------------------------------------------- */
    
    BasePtr GetNode( int val )
    {
        BasePtr node;
        node = malloc( sizeof(BaseElem) );
        node->dat = val;
        node->next = NULL;
        return node;    
    }
    
    
    int Length( BasePtr first )
    {
        BasePtr move;
        int len=0;
        move = first;
        while (move != NULL)
        {
            len++;
            move = move->next;
        }
        return len;
    }
    
    
    void PrintNode( BasePtr node )
    {
        printf("Val: &#37;d\n", node->dat);
    }
    
    void PrintList( BasePtr first )
    {
        BasePtr move;
        move = first;
        while (move != NULL)
        {
            PrintNode( move );
            move = move->next;
        }
    }
    
    void PrintNodeFile( FILE *ofp, BasePtr node )
    {
        fprintf(ofp, "Val: %d\n", node->dat);
    }
    
    void PrintListFile( FILE *ofp, BasePtr first )
    {
        BasePtr move;
        move = first;
        while (move != NULL)
        {
            PrintNodeFile( ofp,  move );
            move = move->next;
        }
    }
    
    int Compare( int x, int y)
    {
        if (x < y) return -1;
        else if (x == y) return 0;
        else if (x > y) return 1;
    }
    
    BasePtr FindInsLoc( BasePtr first, BasePtr node )
    {
        BasePtr move, prev;
        int val1, val2;
        int res = -1;
        val2 = node->dat;
        move = first;
        prev = move;
    
        val1 = move->dat;
        res = Compare( val1, val2 );
    
        while ( res < 0 && move != NULL)
        {
            prev = move;
            move = move->next;
            if (move != NULL)
            {
                val1 = move->dat;
                res = Compare( val1, val2 );
            }
        }    
        return prev;
    }
    
    BasePtr InsertNodeAfter( BasePtr first, BasePtr loc, BasePtr node)
    {
        //printf("Insert %d after %d\n", node->dat, loc->dat);
        node->next = loc->next;
        loc->next = node;
        return first;
    }
    
    
    /* --------------------------------------------------------------------- */
    
    int main( int argc, char *argv[] )
    {
        BasePtr head, tail, tmp;
        int i;
    
        FILE *infile, *outfile;
        /* verify that 2 parameters were passed */    
        if ( argc != 3)
        {
            printf("Usage: a.out infile outfile\n");
            return 1;
        }
        /* open input file */
        infile = fopen( argv[1], "r");
        if (!infile)
        {
            printf("ERROR: file %s not available\n", argv[1]);
            return 1;    
        }
        /* open output file */
        outfile = fopen( argv[2], "w");
        if (!outfile)
        {
            printf("ERROR: file %s not available\n", argv[2]);
            return 1;    
        }
    
        tmp = GetNode(1);
        head=tmp;
        tail=tmp;
    
        for (i = 2; i <= 7; ++i)
        {
            tmp = GetNode(i);
            tail->next = tmp;
            tail = tmp;
        }
    
        PrintList( head );
    
        fprintf(outfile, "Starting --\n");
        PrintListFile(outfile, head );
        fprintf(outfile, "Stopping --\n");
        i = Length( head );
        printf("List size: %d\n", i);
    
    
        
      /* insertion sort */
        srand( time(0) );
    
        BasePtr start, p, add;
        int x;
        tmp = GetNode(0);
        start=tmp;
    
        for (i = 1; i < 10; ++i)
        {
            x = rand() % 100 + 1;
            //printf("Inserting %d\n", x);
            add = GetNode(x);
            p = FindInsLoc( start, add );
            start = InsertNodeAfter( start, p, add );
            //printf("Version %d: \n", i);
            //PrintList( start );
        }
    
        printf("Sorted list ----- \n");
        PrintList( start );
    
        return 0;
    }

    Anyway, I am trying to do this part of the assignment (for the insertion sort)

    # Input: File containing a list of Base 21 numbers

    * Name of this file is supplied on the command line
    * First element in file is number of entries
    * You may assume all numbers have at most 4 digits

    # Output File containing a sorted list of Base 21 numbers and the decimal equivalent

    * Name of this file is supplied on the command line
    * First element in file is number of entries
    This program requires an infile and an outfile

    Any help would be greatly appericated. thanks guys.
    Last edited by ATXFG; 04-01-2007 at 09:17 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So what part of what you're trying to do can't you figure out? How to open a file? How to read from a file? How to write to a file? How to get a name from the command line arguments?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    2
    I am really not sure how to do any of that. I have always had the user enter numbers and scanned that into a variable and so forth, I have never really done this, with an infile and an outfile etc. Thanks for the reply.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read the FAQ. It covers working with command line arguments, working with files, and many other handy things.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 06:47 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21