Thread: Help with a linked list array

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    20

    Help with a linked list array

    Help all! :-) I'm trying to initialize a hash table for a C program and somehow I keep getting errors when it comes to the add() function. I have an array of linked lists and can't seem to get the initilizations right.. Could anyone help me?

    PHP Code:
    [CODE]#include <stdio.h>

    FILE *in_fptr/* Input File Pointer; points to the file
    containing the input to the program */
    *out_fptr/* Output File Pointer; points to the file to
    contain all the output of the program */
    *fopen();

    #define TRUE 1
    #define FALSE 0
    #define NAME_LENGTH 10 /* maximum number of characters in name */
    #define COM_LENGTH 3 /* number of characters in a command */
    #define BKT_SIZE 19 /* number of entries in bucket directory */
    #define FIRST_FACTOR 2 /* factor used in hashing for the first uppercase letter */
    #define LAST_FACTOR 3 /* factor used in hashing for the last uppercase letter */
    #define MAXSTRING 100 /* maximum length a file name can be */

    /* an entry (node) in hash table */
    struct ht_entry {
    char ht_name[NAME_LENGTH 1];
    int ref_count/* number of references to the name */
    struct ht_entry *next;
    };

    struct ht_entry *hash_tbl[BKT_SIZE]; /* bucket directory (hash index) */

    // Function prototypes
    void init();
    int search(char *name_ptrint *ind_ptr);
    int hash(char *name_ptr);
    void add(char *name_ptr,int *ind_ptr);
    void tbl_flush();

    /********************************************************************/

    main()
    {
    init();

    printf("\n\n");

    system("PAUSE");
    }
    // end of main

    /********************************************************************/

    void init()
    {
    /*
    This routine is used by the main routine.
    It initializes hash_tbl.
    It opens the two files (sets in_fptr and out_fptr).
    */
    char inp_name[NAME_LENGTH 1], /* the name in the input */
    input_filename[MAXSTRING], /* the name of the input file */
    command[COM_LENGTH 1]; /* the command in the input */
    int hash_index;

    //gets the name of the input file
    printf("\nInput the file name to open (including .txt): ");
    scanf("%s"input_filename);
    fflush(stdin);

    //opens file stream with input_filename to read
    if((in_fptr fopen(input_filename"r")) == NULL)
    {
    printf("Can't open %s\n"input_filename);
    system("PAUSE");
    exit(
    1);
    }

    //opens file stream with the output file to write the output
    if((out_fptr fopen("output_file.txt""w")) == NULL)
    {
    printf("Can't open output_file\n");
    system("PAUSE");
    exit(
    1);
    }

    //inputs titles to the output file
    fprintf(out_fptr" Input Data\n");
    fprintf(out_fptr" Command Name\n");

    //prints contents of input_filename file to output file
    while((fscanf(in_fptr"%s"command))!=EOF)
    {
    fflush(stdin);

    fscanf(in_fptr"%s"inp_name);
    fflush(stdin);

    fprintf(out_fptr" %s"command);

    if((
    strcmp(command"DEF")==0))
    {
    //this is where you'd have to search if DEF command exists to define it
    fprintf(out_fptr"%s"" This is a DEF command. ");
    hash_index hash(inp_name);

    if((
    search(command, &hash_index))) /* if DEF exists increment it */
    {
    printf("Didn't work"); //increment it
    }
    else 
    /* else add it */
    {
    add(command, &hash_index); //add it
    }
    fprintf(out_fptr"%s"" Hash Index = ");
    fprintf(out_fptr"%i"hash_index);
    /**** SEARCH for definition ****/
    }
    else
    {
    //this is where you'd have to search if DEF command exists to use it
    fprintf(out_fptr"%s"" This is a USE command. ");
    /**** SEARCH for definition ****/
    }

    fprintf(out_fptr" %s\n"inp_name);

    }
    // end while loop

    }/* end of init */

    /********************************************************************/

    int search(char *name_ptrint *ind_ptr/* name to be searched , hash index (index into the hash table) */
    {
    return 
    FALSE;

    }
    /* end of search */

    /********************************************************************/

    int hash(char *name_ptr/* name to be hashed */
    {
    char current/* current letter being evaluated */
    int i/* loop indexer */
    int first_upper=0/* number digit of first uppercase letter */
    last_upper/* number digit of last uppercase letter */
    hash_index/* hash index value to be returned */

    for(i=0i<(strlen(name_ptr)); i++) /* loop for length of name */
    {
    current=(int)name_ptr[i]; /* parse current letter to int */
    if(current>64 && current 91 && first_upper==0/* if uppercase letter set first and last values */
    {
    first_upper=current-64;
    last_upper=first_upper;
    }
    else if(
    current>64 && current 91/* if uppercase set last value */
    {
    last_upper=current-64;
    }
    }
    //end for

    hash_index=(first_upper FIRST_FACTOR last_upper LAST_FACTOR) % BKT_SIZE/* sets hash index value */

    return hash_index;

    }
    // end of hash

    /********************************************************************/

    void add(char *name_ptr,int *ind_ptr)
    {

    /* PROBLEMS SEEM TO BE HERE WHEN ADDING OR INITIALIZING HASH TABLE */
    /* Could someone tell me how to initialize these or link the *tmp to the hash_tbl[]? */

    struct ht_entry *tmp;
    tmp malloc(sizeof(*tmp));



    hash_tbl[*ind_ptr]->ref_count=1;
    if(
    hash_tbl[*ind_ptr]==NULL)
    {

    printf("ref_count = %i "hash_tbl[*ind_ptr]->ref_count);
    printf("It's NULL!!");

    }
    else
    {
    printf("It's not null!");
    }

    //while(strcmp((hash_tbl[*ind_ptr]->ht_name), name_ptr)==0){

    //}

    printf("It's been added!\n");
    }
    //end add


    /*******************************************************************/

    void tbl_flush()
    {

    }
    /* end of tbl_flush */
    [/CODE

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    what are the errors you are getting?

    you need to include
    Code:
    #include <stdlib.h>
    #include <string.h>
    also,
    Code:
    fflush(stdin);
    is a no no
    see here
    Last edited by sand_man; 09-25-2004 at 10:25 AM.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Your code is poorly written I'm afraid. You use quite a few non-portable and undefined constructs sprinkled with bad style, and the logic just seems confused. I would also recommend using a consistent amount of whitespace, preferably between logical tokens. Everything scrunched together is difficult to read. I cleaned your code up and fixed the add function, which is definitely a start. Enjoy.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    FILE *in_fptr, *out_fptr; 
    
    #define TRUE 1 
    #define FALSE 0 
    #define NAME_LENGTH 10 /* maximum number of characters in name */ 
    #define COM_LENGTH 3 /* number of characters in a command */ 
    #define BKT_SIZE 19 /* number of entries in bucket directory */ 
    #define FIRST_FACTOR 2 /* factor used in hashing for the first uppercase letter */ 
    #define LAST_FACTOR 3 /* factor used in hashing for the last uppercase letter */ 
    #define MAXSTRING 100 /* maximum length a file name can be */ 
     
    struct ht_entry { 
      char ht_name[NAME_LENGTH + 1]; 
      int ref_count;
      struct ht_entry *next; 
    }; 
    
    struct ht_entry *hash_tbl[BKT_SIZE];
     
    void init(); 
    int search(char *name_ptr, size_t ind_ptr); 
    int hash(char *name_ptr); 
    void add(char *name_ptr, size_t ind_ptr); 
    
    int main(void)
    {
      init();
    
      return 0;
    }
    
    void init() 
    { 
      char inp_name[NAME_LENGTH + 1];
      char input_filename[MAXSTRING];
      char command[COM_LENGTH + 1];
      size_t hash_index; 
    
      printf("Input the file name to open (including .txt): "); 
      if (scanf("%s", input_filename) != 1)
      {
        printf("Input error\n"); 
        exit(EXIT_FAILURE); 
      }
    
      if ((in_fptr = fopen(input_filename, "r")) == NULL) 
      { 
        printf("Can't open %s\n", input_filename); 
        exit(EXIT_FAILURE); 
      } 
    
      if ((out_fptr = fopen("output_file.txt", "w")) == NULL) 
      { 
        printf("Can't open output_file\n"); 
        exit(EXIT_FAILURE); 
      }
    
      fprintf(out_fptr, " Input Data\n"); 
      fprintf(out_fptr, " Command Name\n"); 
    
      while ((fscanf(in_fptr, "%s", command)) == 1) 
      {
        fscanf(in_fptr, "%s", inp_name);
        fprintf(out_fptr, " %s", command);
    
        if (strcmp(command, "DEF") == 0) 
        { 
          fprintf(out_fptr, "%s", " This is a DEF command. "); 
          hash_index = hash(inp_name); 
    
          if (search(command, hash_index))
          { 
            printf("Didn't work");
          } 
          else
          { 
            add(command, hash_index);
          } 
          fprintf(out_fptr, "%s", " Hash Index = "); 
          fprintf(out_fptr, "%i", hash_index); 
        } 
        else 
        { 
          fprintf(out_fptr, "%s", " This is a USE command. "); 
        } 
    
        fprintf(out_fptr, " %s\n", inp_name); 
      }
    
      fclose(in_fptr);
      fclose(out_fptr);
    }
    
    int search(char *name_ptr, size_t ind)
    {
      return FALSE;
    }
    
    int hash(char *name_ptr)
    { 
      char current;
      size_t i;
      size_t first_upper = 0, last_upper = 0;
      size_t hash_index;
    
      for (i = 0; i < strlen(name_ptr); i++)
      { 
        current = name_ptr[i];
        if (current > 64 && current < 91 && first_upper == 0)
        { 
          first_upper = current - 64; 
          last_upper = first_upper; 
        } 
        else if (current > 64 && current < 91)
        { 
          last_upper = current - 64; 
        } 
      }
    
      hash_index = (first_upper * FIRST_FACTOR + last_upper * LAST_FACTOR) % BKT_SIZE;
    
      return hash_index; 
    }
    
    void add(char *name_ptr, size_t ind)
    {
      struct ht_entry *new_def = malloc(sizeof *new_def);
    
      if (new_def == NULL)
      {
        return;
      }
    
      strcpy(new_def->ht_name, name_ptr);
      new_def->ref_count = 1;
      new_def->next = hash_tbl[ind];
      hash_tbl[ind] = new_def;
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    Thank you very much! I'm a little new to programming so hopefully I can improve. I appreciate the mods greatly! I just need to keep practicing and maybe pick up some good habits...

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I just need to keep practicing and maybe pick up some good habits...
    Spend enough time lurking and helping around here and you'll be a guru in no time.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  4. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM