/* Student Name: Student NETID: Email Address: Problems Solved: */ /* PROGRAM : ohash.c PURPOSE : insert/delete into an open hash table with doubly-linked lists and no headers UNIX COMP: gcc -o ohash ohash.c PC COMP : TURBO C++ (tcc) or Borland TEST : ohash ohash.dat INTERACT : ohash REDIRECT : ohash #include #define N 7 typedef int element_type; typedef struct list_node *node_ptr; struct list_node { node_ptr next; node_ptr prev; element_type key; }; node_ptr hashtable[N]; /* Ex1 : initialize the hash table */ void init_hash() { } int hash(int x) { return (x % N); } /* Ex2 : find the key in the list. return NULL if not found */ node_ptr find_hash(element_type key) { return NULL; } /* Ex3 : insert the key into the list. return 0 if there is a duplicate, otherwise, insert at the head and return 1. Maintain both next and prev pointers */ int insert_hash(element_type key) { return 0; } /* Ex4 : delete the key from the list. return 0 if it doesn't exist, otherwise, delete and return 1. Maintain both next and prev pointers. */ int delete_hash(element_type key) { return 0; } void display_hash() { int i; node_ptr p; printf("\nOpen Hash Table:\n"); for (i=0; i %d ",p->key); p = p->next; } printf("\n"); } printf(" +-+\n"); } void cmd_interpreter(FILE *fp) { char cmd, xtra; element_type x; init_hash(); printf("> "); while (fscanf(fp,"%c",&cmd) != EOF) { switch (cmd) { case 'q' : exit(0); case 'h' : printf("h(elp; q(uit; i(nsert x; d(isplay; D(elete x\n"); break; case 'i' : fscanf(fp,"%d",&x); insert_hash(x); break; case 'D' : fscanf(fp,"%d",&x); delete_hash(x); break; case 'd' : display_hash(); break; } fscanf(fp,"%c",&xtra); printf("> "); } } int main(int argc, char *argv[]) { FILE *fp; if (argc == 1) fp = stdin; else { fp = fopen(argv[1],"r"); if (fp == NULL) { printf("No File: %s\n",argv[1]); return 1; } } cmd_interpreter(fp); return 0; }