Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I'm having some problems with my Dictionary.c code. When I try to run DictionaryClient.c, there is a segmentation fault when I call the delete()

Hi, I'm having some problems with my Dictionary.c code. When I try to run DictionaryClient.c, there is a segmentation fault when I call the delete() function. I've included the DictionaryClient.c and the Dictionary.h programs along with the expected output of DictionaryClient.c. Please help me rewrite my code.

Dictionary.c:

#include #include #include #include #include"Dictionary.h"

typedef struct NodeObj{ char* key; char* value; struct NodeObj* next; } NodeObj;

typedef NodeObj* Node;

Node newNode(char* k, char* v){ Node N = malloc(sizeof(NodeObj)); assert(N !=NULL); N->key = k; N->value = v; N->next = NULL; return (N); }

void freeNode(Node* pN){ if (pN !=NULL && *pN != NULL){ free(*pN); *pN = NULL; } }

typedef struct DictionaryObj{ Node head; int numItems; } DictionaryObj;

Dictionary newDictionary(void){ Dictionary D = malloc(sizeof(DictionaryObj)); assert(D !=NULL); D->head = NULL; D->numItems = 0; return D; }

void freeDictionary(Dictionary* pD){ if (pD != NULL && *pD != NULL){ if(!isEmpty(*pD)) makeEmpty(*pD); free(*pD); *pD =NULL; } }

int isEmpty(Dictionary D){ if (D ==NULL){ fprintf(stderr, "Stack Error: calling isEmpty() on NULL Dictionary reference " ); exit(EXIT_FAILURE); } return (D->numItems==0); }

int size(Dictionary D){ if (D ==NULL){ fprintf(stderr, "Stack Error: calling size() on NULL Dictionary reference " ); exit(EXIT_FAILURE); } return D->numItems; }

char* lookup(Dictionary D, char* k){ Node N = D->head; while(N != NULL){ if(strcmp(N->key, k)==0){ return N->value; } N = N->next; } return NULL; }

void insert(Dictionary D, char* k, char* v){ Node N = D->head; if(D==NULL){ fprintf(stderr, "Stack Error: calling insert() on NULL Dictionary reference "); exit(EXIT_FAILURE); } if(lookup(D, k) !=NULL){ fprintf(stderr, "error: key collusion "); exit(EXIT_FAILURE); } if(D->head == NULL){ N = Node(k, v); D->head = N; D->numItems ++; } else{ while(N->next != NULL){ N = N->next; } N->next = Node(k, v); D->numItems ++; } }

void delete(Dictionary D, char* k){ Node N = D->head; if(D==NULL){ fprintf(stderr, "Stack Error: calling delete() on NULL Dictionary reference "); exit(EXIT_FAILURE); } if(lookup(D, k) ==NULL){ fprintf(stderr, "error: key not found "); exit(EXIT_FAILURE); } if(D->numItems key, k) ==0){ Node temp = D->head; D->head = N->next; D->numItems --; freeNode(&temp); } else{ while(strcmp(N->key, k) !=0){ N = N->next; } Node temp = N-> next; N->next = N->next->next; freeNode(&temp); D->numItems --; } } }

void makeEmpty(Dictionary D){ if( D==NULL ){ fprintf(stderr, "Stack Error: calling makeEmpty() on NULL Dictionary reference "); exit(EXIT_FAILURE); } if( D->numItems==0 ){ fprintf(stderr, "Stack Error: calling makeEmpty() on empty Dictionary "); exit(EXIT_FAILURE); } D->numItems = 0; freeNode(&D->head);

}

void printDictionary(FILE* out, Dictionary D){ Node N; if( D==NULL ){ fprintf(stderr, "Stack Error: calling printDictionary() on NULL Dictionary reference "); exit(EXIT_FAILURE); } for(N=D->head; N!=NULL; N=N->next){ fprintf(out, "%s %s ", N->key, N->value); }

} image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Expected Output of DictionaryClient.c

image text in transcribed

// Dictionary.h // Header file for the Dictionary ADT #ifndef DICTIONARY H INCLUDE #define DICTIONARY H INCLUDE // Dictionary // Exported reference type typedef struct Dictionaryobj* Dictionary; // newDictionary() // constructor for the Dictionary type Dictionary newDictionary(void); // freeDictionary() // destructor for the Dictionary type void freeDictionary (Dictionary* pD); // sEmpty() // returns 1 (true) if s is empty. (false) otherwise // pre: none int isEmpty (Dictionary D); // size() // returns the number of (key, value) pairs in D // pre: none int size(Dictionary D); // lookup() // returns the value v such that (k, v) is in D, or returns NULL if no // such value v exists. // pre: none char* lookup (Dictionary D, char* k); /1 insert() // linseri:s mCM (keys, Villie) pair :i.fni.O )) // pre : lookupCD, k)-=NULL void insert (Dictionary D, char* k, char* v); // delete() // deletes pair with the key k // pre : lookupCD, k)!=NULL void delete(Dictionary D, char* k); // makeEmpty() // re-sets D to the empty state. // pre: none void makeEmpty(Dictionary D); // printDictionary() // prints a text representation of D to the file pointed to by out void printDictionary (FILE* out, Dictionary D); #endif // DictionaryClient.c // Test client for the Dictionary ADT #include #include #include #include"Dictionary" h" #define MAX LEN 180 int main(int argc, char* argv[]) Dictionary A- newDictionary(); char* k; char* v; char* word1[] - ("one","two", "three","four", "five", "six","seven"}; char* word2[]- ("foo", "bar", "blah","galumph", "happy", "sad", "blue"}; 1nt 1, insert(A, word1[i], word2[i]); printDictionary (stdout, A); v -lookup(A, k); printt("key:\"%s\" %s \"%s\"in", k, (v..NU L L ? "not found ": "value-''), v); // insert (A, "five", "glow"); // error: key collision delete(A, "one") delete(A, "three") delete(A, "seven"); printDictionary (stdout, A); k word1[i]; vlookup(A, k); printf("key=\"%s\" %s\"%s\"in", k, (v=NULL?"not found ": "value-"), v); /I delete(A, "one"); // error: key not found printf("%s ", printf("%d ", makeEmpty (A); printf("%s ", (isEmpty(A) ?"true": "false")); size(A)); (isEmpty(A) ? "true" : "false")); freeDictionary(&A); return(EXIT_SUCCESS); one foo two bar three blah four galumph five happy six sad seven blue key-"one" value-"foo" key-two" value-"bar" key-"three" value-"blah" key-"four" value-"galumph" key-"five" value-happy" Key SIx value- sad key- seven" value- blue two bar four galumph five happy six sad key-"one" not found "(null)" key-two" value-"bar" key-"three" not found "(null)" key-"four" value-"galumph" key-"five" value-"happy" Key SIx value- sad key-"seven" not found "(null)" false 4 true

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2005 For Windows Mobile Web Office And Database Applications Comprehensive

Authors: Gary B. Shelly, Thomas J. Cashman, Corinne Hoisington

1st Edition

0619254823, 978-0619254827

More Books

Students also viewed these Databases questions