Transcribed TextTranscribed Text

Lab 5 In this lab, you need to use the knowledge from file processing, strings, pointers, memory management, and library calls. In specific, you are going to read the symbol.txt file and store the stock symbol as a key and company name as data in a hash table where the symbol and name are separated by a comma. After inserting the data to a hash table, read the stock symbol from the standard input and retrieve the company name from the hash table. If the stock symbol is equal to “QUIT”, exit the program. The following is an example run. Dr.L>> ./lab5 symbol.txt Input a stock symbol: IBM International Business Machines Corporation Input a stock symbol: JPM JP Morgan Chase & Co. Input a stock symbol: F Ford Motor Company Input a stock symbol: QUIT ** Note that you may need to release the memory from the hash table by calling - hdestroy()

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

* File:   lab5.c
* Author:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <math.h>

struct Node {
    char * name;
    char * symbol;
    int hashCode;
    struct Node * next;

typedef struct Node Node;

struct List {
    Node * head;
    int size;
typedef struct List List;

struct HashTable {
    List * table;
    int size;

typedef struct HashTable HashTable;

// function prototypes

void listAdd(List * list, Node * node);
int getHashCode(char * symbol);
Node * getNode(char * chunk);
List readFile(char * path);
HashTable hashTableSetUp(List * list);
char * getName(HashTable ht, char * input);
void runMenu(HashTable ht);

void freeNode(Node * n);
void freeList(List list);
void hdestroy(HashTable ht);

int main(int argc, char** argv) {

    // check for the argument
    if (argc != 2) {
       return (EXIT_FAILURE);
    // read file
    List list = readFile(argv[1]);
    // set up the table
    HashTable ht = hashTableSetUp(&list);
    // run menu
    // destroy date

    return (EXIT_SUCCESS);

* destroy the hash table
* @param ht
void hdestroy(HashTable ht) {
    int i;
    // free each list oh the table
    for (i = 0; i < ht.size; i++) {

* free content of a list
* @param list
void freeList(List list) {
    Node * n = list.head;
    Node * p;
    // free each node of the list
    while (n != NULL) {
       p = n;
       n = n->next;

* free data of a node
* @param n
void freeNode(Node * n) {
    if (n != NULL) {

* get user's input
* show the result on screen
* @param ht
void runMenu(HashTable ht) {
    char input[64];
    char * name;

    while (1) {
       printf("Input a stock symbol: ");
       scanf("%s", input);

       // stop when user enters QUIT
       if (strcmp(input, "QUIT") == 0) {

       // get the name from hash table
       name = getName(ht, input);

       // show result
       if (name == NULL) {
            printf(" Not found\n");
       } else {
            printf("%s\n", name);

* get name from the table
* @param ht the hash table
* @param input symbol string
* @return
char * getName(HashTable ht, char * input) {

    // calculate the hash code
    int hashCode = getHashCode(input);
    // get the expected index
    int index = hashCode % ht.size;
    // get the header of that cell
    Node * n = ht.table[index].head;

    // move the end of the list
    while (n != NULL) {
       // the same hash code
       // and the same symbol
       // in the worst case, 2 different symbol could have the same hash code
       if (n->hashCode == hashCode
                && strcmp(n->symbol, input) == 0) {
            return n->name;

By purchasing this solution you'll be able to access the following files:

for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats