Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#pragma warning(disable:4996)
- // C program to implement basic operations (search, insert and delete)
- // on a BST that handles duplicates by storing count with every node
- #include<stdio.h>
- #include<stdlib.h>
- #include <string.h>
- #define MAX_STRING_SIZE 100
- #define HASHSIZE 101 /* comment here */
- #define FILE_OUTPUT "calendar.txt"
- #define ALLOCSIZE 1000 /*size of available space*/
- static char allocbuf[ALLOCSIZE]; /* storage for alloc*/
- static char *allocp = allocbuf; /* next free position*/
- static struct map *hashtab[HASHSIZE]; /* pointer table */
- unsigned hash(char *); /* hashing function: form hash value for string s. used by both lookup and install*/
- char *strdup(char *);
- char *alloc(int n) { /* return a pointer to n characters*/
- if (allocbuf + ALLOCSIZE - allocp >= n) { /*it fits*/
- allocp += n;
- return allocp - n; /*old p*/
- } else /*not enough room*/
- return 0;
- }
- typedef struct map { /* creating a map structure */
- struct map *next;
- char *value;
- char *KEY;
- } map;
- /* lookup function takes a pointer to char - s as an argument and returns a pointer to map structure */
- map *lookup(char *s) {
- map *np;
- for (np = hashtab[hash(s)]; np != NULL; np = np->next) {
- if (strcmp(s, np->KEY) == 0) {
- return np;
- }
- }
- return NULL;
- }
- /* insert function takes a pointer to a char - KEY, value and returns a pointer to map structure */
- map *insert_map(char* KEY, char* value) {
- /* install: put (name, defn) in */
- /* Install uses lookup to determine whether the KEY being installed
- is already present. Proceeds to create a new entry or update*/
- map *np;
- unsigned hashval;
- if ((np = lookup(KEY)) == NULL) {
- np = (map *) malloc(sizeof (*np));
- if (np == NULL || (np->KEY = strdup(KEY)) == NULL) {
- return NULL;
- }
- hashval = hash(KEY);
- np->next = hashtab[hashval];
- hashtab[hashval] = np;
- } else {
- free((void *) np->value);
- }
- if ((np->value = strdup(value)) == NULL) { //map has no field value
- return NULL;
- }
- return np;
- }
- /* simple implementation of strdup*/
- char *strdup(char *s) {
- char *d = malloc(strlen(s) + 1); // Space for length plus null
- if (d == NULL) return NULL; // No memory
- strcpy(d, s); // Copy the characters
- return d; // Return the new string
- }
- unsigned hash(char *s) {
- /* hashing function: form hash value for string s */
- /* used by both lookup and install*/
- unsigned hashval;
- for (hashval = 0; *s != '\0'; s++) {
- hashval = *s + 31 * hashval;
- }
- return hashval % HASHSIZE;
- }
- int main() {
- char *value[10];
- int unique_key_arr[10] = {5122018, 4122018, 3122018, 2122018}; // Make dynamic
- char *value_arr[4] = {"Value1", "Value2", "Value3", "Value4"}; // an array of pointers
- char KEY[7];
- char *KEY_arr[4];
- for (int i = 0; i < 4; i++) {
- char* p; //pointer for value
- char* p2; //pointer for key
- if ((p = alloc(strlen(value_arr[i]) + 1)) == NULL) {
- return -1;
- } else {
- strcpy(p, value_arr[i]); //copying value to pointer to char
- value[i] = p; //p passed to value array
- printf("%s\n", value[i]);
- }
- //Convert integer to pointer to char
- sprintf(KEY, "%d", unique_key_arr[i]);
- KEY_arr[i] = KEY;
- printf("%s\n", KEY_arr[i]);
- //printf("I am here2 %s\n", KEY_arr[i]);
- }
- map * table[10]; //pointer to type map structure
- printf("\nBefore insert\n");
- for (int i = 0; i < 4; i++) {
- printf("%s, %s\n", KEY_arr[i], value[i]);
- table[i] = insert_map(KEY_arr[i], value[i]);
- }
- printf("\n");
- for (int i = 0; i < 4; i++) {
- printf("%s->%s \n", table[i]->KEY, table[i]->value);
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment