Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXSTRING 100
/*Create linked list node structure.*/
struct node {
char name[MAXSTRING];
int refcount;
struct node *next;
};
int calcindex(char j[100]);
struct node* addnode(struct node *j, char k[100], int l);
int lookup(char j[100], struct node *k);
void changeref(char j[100], struct node *k);
int print(struct node *j, FILE *k);
int main(void)
{
/*Necessary variables.*/
FILE *ifp, *ofp;
char item[MAXSTRING];
char command[MAXSTRING];
int index;
int search;
int i;
/*Create array[19] of these nodes.*/
struct node *strarray[19] = {0};
/*Open input file.*/
ifp = fopen("inputfile.txt", "r");
/*Open output file*/
ofp = fopen("outputfile.txt", "w");
/*Do until command equals end of input file.*/
while (fscanf(ifp, "%s", &command) != EOF) {
/*Get string from file.*/
fscanf(ifp, "%s", &item);
/*Print command and string to output file.*/
fprintf(ofp, "%s %s", command, item);
/*Check to see if command is Dec or Use.*/
/*If the command is Dec:*/
if (strcmp(command, "DEC") == 0) {
/*Use calculate index function.*/
index = calcindex(item);
/*Check indexed linked list for name by searching list at specific index*/
search = lookup(item, strarray[index]);
/*If name exists in list*/
if (search == 1){
/*Print error message "***Duplicate Name***" to output file*/
fprintf(ofp, " ***Duplicate Name***");
/*Start a new line.*/
fprintf(ofp, "\n");
}
/*If name does not exist in list*/
else {
/*Use add node function with proper name, reference count, and pointer.*/
strarray[index] = addnode(strarray[index], item, strarray[index]->refcount);
/*Start new line on output file.*/
fprintf(ofp, "\n");
}
}
/*If the command is Use:*/
else {
/*Use calculate index function.*/
index = calcindex(item);
/*Check indexed linked list for name by searching list at specific index*/
search = lookup(item, strarray[index]);
/*If name exists in list*/
if (search == 1) {
/*Reference count +1*/
changeref(item, strarray[index]);
/*Start new line on output file.*/
fprintf(ofp, "\n");
}
/*If name does not exist in list*/
else {
/*Print error message "***Undefined Name***" to output file*/
fprintf(ofp, " ***Undefined Name***");
/*Start a new line.*/
fprintf(ofp, "\n");
}
}
}
/*Cycle through array.*/
for (i = 0; i < 19; i++) {
/*If current linked list is empty*/
if (strarray[i] == NULL) {
/*Continue*/
continue;
}
/*If current linked list has data*/
else {
/*Print current array field.*/
fprintf(ofp, "%d ", i);
/*Use print function*/
print(strarray[i], ofp);
}
}
/*Close input file.*/
fclose(ifp);
/*Close output file.*/
fclose(ofp);
/*End program.*/
return 0;
}
int calcindex(char value[]) {
int total = 0;
size_t i;
/*Until the end of the string*/
for (i=0; i<strlen(value); i++) {
/*If current character is a letter*/
if (isalpha(value[i])) {
/*If current character is uppercase*/
if (isupper(value[i])) {
/*Add to total this value*/
total = value[i] - 'A' + 1;
}
/*If current character is lowercase*/
else {
/*Add to total this value*/
total = value[i] - 'a' + 1;
}
}
/*If current character is a number*/
else {
/*Add to total this value*/
total = value[i] - '0';
}
}
/*Return total*/
return total;
}
struct node* addnode(struct node *list, char n[], int rc) {
struct node *pNew = NULL;
struct node *created_list = NULL;
/*Creating default node*/
pNew = malloc(sizeof(struct node));
strcpy(pNew->name, n);
pNew->refcount = rc;
pNew->next = NULL;
/*If new node is first node in list*/
created_list = list;
if (list == NULL)
return pNew;
/*If otherwise*/
while (list->next != NULL)
list = list->next;
list->next = pNew;
/*Return updated list*/
return created_list;
}
int lookup(char value[], struct node *head) {
/*If list node is empty, return 0*/
if (head == NULL)
return 0;
/*If list node contains the value, return 1*/
else if (strcmp(value, head->name) == 0)
return 1;
/*Otherwise continue down the list*/
else
return(lookup(value, head->next));
}
void changeref(char value[], struct node *head) {
int rc;
/*If list node contains the value, raise reference count by 1*/
if (strcmp(value, head->name) == 0) {
rc = head->refcount;
rc += 1;
head->refcount = rc;
}
/*Otherwise continue down the list*/
else {
changeref(value, head->next);
}
}
int print(struct node *list, FILE *ofp) {
struct node *current = list;
/*While current node is not empty*/
while (current != NULL)
{
/*Print out the node information*/
fprintf(ofp, " %s %d\n", current->name, current->refcount);
/*Continue to the next node*/
current = current->next;
}
return 1;
}