Code:
#include <stdio.h>
#include <stdlib.h> // needed for malloc, free
struct node { // a node in the linked list
char data;
struct node *next;
};
int menu(void); // display list of choices, get user's choice
struct node *addString(struct node *, const char *, int *, struct node *[]); // Add a string (char array) to the list
void output(struct node *f, int *, struct node *[]); // Output a string based on a users selected index
void stringLength(struct node *f, int *, struct node *[]); // Output the length of a string based on a users selected index
struct node *concatenate(struct node *f, int *, int *, int *, struct node *[]); // concatenate 2 strings together
struct node *insertChar(struct node *, int *, char *, int *, struct node *[]); // Insert a character at a given position of a string
struct node *del(struct node *, int *, int *, struct node *[]); // Delete a character at a given position of a string
struct node *destroy(struct node *, int *, struct node *[]); // deallocate a user specified list
struct node *destroyAll(struct node *, struct node *[]); // deallocate all lists
int main(void)
{
struct node *front; // front points to the front of our list
struct node *linkedListStrings[5] = {NULL, NULL, NULL, NULL, NULL}; //an array of 5 struct node pointers
int optionsResponse; // User input for main menu options
int index = 0; // Index of the first or only string used
int index2; // Index of the second string used
int index3; // Index of the third string used (concatination)
int position; // The posiiton in the string to insert characters
char insertedCharacter; // The users input character to be inserted into the linked list
char response[20]; // user input for sub menu options
int i;
front = NULL; // initialize list to NULL
do // display menu, do command, quit when user inputs choice 8
{
optionsResponse = menu();
switch(optionsResponse)
{
case 1 : // Adding a new string
for(i=0;i<5;i++)
printf("%d\t%p\n", i, linkedListStrings[i]);
printf ("Enter a new string: ");
scanf("%s", response);
printf ("Enter a node to place it in [0-4]: ");
scanf("%d", &index);
addString(front, response, &index, linkedListStrings);
printf("\n");
for(i=0;i<5;i++)
printf("%d\t%p\n", i, linkedListStrings[i]);
break;
case 2 :
for(i=0;i<5;i++)
printf("%d\t%p\n", i, linkedListStrings[i]);
// printing a string from a selected index
printf ("Enter the index of the string that you would like to output [0-4]: ");
scanf("%d", &index);
output(front, &index, linkedListStrings);
for(i=0;i<5;i++)
printf("%d\t%p\n", i, linkedListStrings[i]);
break;
case 3 :
for(i=0;i<5;i++)
printf("%d\t%p\n", i, linkedListStrings[i]);
// printing the length of a string from a selected index
printf ("Enter the index of the string [0-4]: ");
scanf("%d", &index);
stringLength(front, &index, linkedListStrings);
for(i=0;i<5;i++)
printf("%d\t%p\n", i, linkedListStrings[i]);
break;
case 4 : for(i=0;i<5;i++)
printf("%d\t%p\n", i, linkedListStrings[i]);
// concatenating two strings together and selecting an index to place the new string
printf ("Enter the index of the first string [0-4]: ");
scanf("%d", &index);
printf ("Enter the index of the second string [0-4]: ");
scanf("%d", &index2);
printf ("Enter an index to place the concatenated string [0-4]: ");
scanf("%d", &index3);
concatenate(front, &index, &index2, &index3, linkedListStrings);
for(i=0;i<5;i++)
printf("%d\t%p\n", i, linkedListStrings[i]);
break;
case 5 : for(i=0;i<5;i++)
printf("%d\t%p\n", i, linkedListStrings[i]);
// Inserting a character into a position of specifed string
printf ("Enter the index of the string [0-4]: ");
scanf("%d", &index);
getchar();
printf ("Enter a character to be inserted into the string: ");
scanf("%c", &insertedCharacter);
printf ("\nEnter the position in the string at \nwhich you would like to insert a character: ");
scanf("%d", &position);
insertChar(front, &index, &insertedCharacter, &position, linkedListStrings);
for(i=0;i<5;i++)
printf("%d\t%p\n", i, linkedListStrings[i]);
break;
case 6 : for(i=0;i<5;i++)
printf("%d\t%p\n", i, linkedListStrings[i]);
// Deleting a character from a specified position
printf ("Enter the index of the string [0-4]: ");
scanf("%d", &index);
printf ("Enter the position in the string corresponding \nto the character you would like to delete: ");
scanf("%d", &position);
del(front, &index, &position, linkedListStrings);
for(i=0;i<5;i++)
printf("%d\t%p\n", i, linkedListStrings[i]);
break;
case 7 : for(i=0;i<5;i++)
printf("%d\t%p\n", i, linkedListStrings[i]);
// destroying/deleting a string
printf ("Enter the index of the string to be destroyed [0-4]: ");
scanf("%d", &index);
destroy(front, &index, linkedListStrings);
for(i=0;i<5;i++)
printf("%d\t%p\n", i, linkedListStrings[i]);
break;
case 8 : for(i=0;i<5;i++)
printf("%d\t%p\n", i, linkedListStrings[i]);
// destroying/deleting a string
destroyAll(front, linkedListStrings);
for(i=0;i<5;i++)
printf("%d\t%p\n", i, linkedListStrings[i]);
break;
}
} while(optionsResponse != 9);
destroyAll(front, linkedListStrings);
}
int menu(void) // display menu and get user's choice
{
int temp;
printf("Please select from one of the following String menu options: \n\n");
printf("1.\tCreate a new string\n");
printf("2.\tOutput a string\n");
printf("3.\tOutput the length of a string\n");
printf("4.\tConcatenate two strings together\n");
printf("5.\tInsert a character at a given position of a string\n");
printf("6.\tDelete a character at a given position of a string\n");
printf("7.\tDestroy a string\n");
printf("8.\tDestroy all strings\n");
printf("9.\tQuit\n\n");
printf("I choose option number: ");
scanf("%d", &temp);
return temp;
}
struct node *addString(struct node *f, const char *response, int *index, struct node *linkedListStrings[])// Add a string (char array) to the list
{
int i = 0; // i will act as a counter to print values from the array
// create a new node, store response as the datum
struct node *temp = (struct node *)malloc(sizeof(struct node));
struct node *front = f; // front points to the front of our list
int indexValue;
indexValue = *index;
if (linkedListStrings[*index] != NULL) // Tests to make sure a node was not already created at the index specified
{
printf("Error - A string already exists at location %d", indexValue);
}
else
{
/* While the character in the char array is not '\0' iterate through
the array and add each value to the node */
f = linkedListStrings[*index];
while(response[i] != '\0')
{
if(f == NULL) // if f is NULL store the value in the first node
{
front = f = (struct node *)malloc(sizeof(struct node));
f->data = response[i++];
f->next = NULL;
}
else if(f->next == NULL) // If f is not null, find the next NULL node and store data to it
{
temp = (struct node *)malloc(sizeof(struct node));
temp->data = response[i++];
temp->next = NULL;
f->next = temp;
}
else // If f is not null and the next node in the list is not null, move to the next node
{
f = f->next;
}
}
linkedListStrings[indexValue] = front; // front of the list as it has not been modified
}
return *linkedListStrings;
}
void output(struct node *f, int *index, struct node *linkedListStrings[]) // Output a string based on a users selected index
{
struct node *front = f; // front points to the front of our list
int indexValue;
indexValue = *index;
f = linkedListStrings[*index];
if (f == NULL) // Error checking to prevent the user from selecting a NULL index
{
printf("A string has not been created for index %d\n", indexValue);
}
else
{
printf("String at index %d is: ", *index);
while(f != NULL) // iterate through the list, printing each node's datum
{
printf("%c", f->data);
f = f->next;
}
}
printf("\n\n");
}
struct node *concatenate(struct node *f, int *index, int *index2, int *index3, struct node *linkedListStrings[]) // concatenate 2 strings together
{
struct node *front = f; // front points to the front of our list
struct node *node1; // points to linkedListStrings[*index]
struct node *node2; // points to linkedListStrings[index2]
struct node *node3; // points to linkedListStrings[index3]
struct node *temp; // temporary value holder
struct node *frontNode3; // Points to the front of node3 (linkedListStrings[index3])
int indexValue, index2Value, index3Value;
indexValue = *index;
index2Value = *index2;
index3Value = *index3;
f = node1 = linkedListStrings[indexValue];
node2 = linkedListStrings[index2Value];
//frontNode3 = node3 = malloc(sizeof(struct node));
frontNode3 = node3 = (struct node *)malloc(sizeof(struct node)); // ADDED
if(linkedListStrings[indexValue] != NULL) // Make sure that the first user specified linked list is not empty
{
if (linkedListStrings[index2Value] != NULL)// Make sure that the second user specified linked list is not empty
{
if (linkedListStrings[index3Value] == NULL) // Make sure that the location to place the concatenated strings is empty
{
while(f != NULL) // Add linkedListStrings[*index] to the user specified location
{
node3->data = f->data;
//temp = malloc(sizeof(struct node));
temp = (struct node *)malloc(sizeof(struct node)); // ADDED
node3->next = temp;
f = f->next;
node3 = node3->next;
}
f = node2;
while(f != NULL) // Add linkedListStrings[index2] at the end of linkedListStrings[*index] of the user specified location
{
node3->data = f->data;
//temp = malloc(sizeof(struct node));
temp = (struct node *)malloc(sizeof(struct node)); // ADDED
f = f->next;
if(f != NULL)
{
node3->next = temp;
node3 = node3->next;
}
}
linkedListStrings[index3Value] = frontNode3; // front of the list 3 as it has not been modified
}
else // Error - linkedListStrings[index3] != NULL
{
printf("A string already exists at index %d \n", index3Value);
}
}
else // Error - linkedListStrings[index2] = NULL
{
printf("A string has not been created for index %d\n", index2Value);
}
}
else // Error - linkedListStrings[*index] = NULL
{
printf("A string has not been created for index %d\n", indexValue);
}
return *linkedListStrings;
}
struct node *insertChar(struct node *f, int *index, char *insertedCharacter, int *position, struct node *linkedListStrings[]) // Insert a character at a given position of a string
{
struct node *front = f; // front points to the front of our list
struct node *temp;
int iter; // Used to iterate through the linked lists nodes
int indexValue;
indexValue = *index;
f = linkedListStrings[*index];
if(linkedListStrings[*index] != NULL)
{
/* Iterate from 1 position from the specified position where
we insert the new character */
for(iter = *position-1; iter > 0; iter--)
{
if(f != NULL) // Go to the next node while f is not null
{
f = f->next;
}
else // Error checking
{
printf("Location does not exist!\n");
iter = 0;
}
}
if(f != NULL)
{
temp = malloc(sizeof(struct node));
temp->data = *insertedCharacter;
if(position > 0) // place the new character at its user specified position
{
temp->next = f->next;
f->next = temp;
}
else // If position is 0, place the new character at the beginning of the list
{
front = temp;
temp->next = f;
}
linkedListStrings[indexValue] = front; // front of the list as it has not been modified
}
}
else // error checking - If the index the user specified was NULL
{
printf("A string has not been created for location %d\n", indexValue);
}
return *linkedListStrings;
}
struct node *del(struct node *f, int *index, int *position, struct node *linkedListStrings[]) // Delete a character at a given position of a string
{
struct node *front = f; // front points to the front of our list
struct node *temp;
int iter; // Used to iterate through the linked lists nodes
int indexValue;
indexValue = *index;
f = linkedListStrings[indexValue];
if(linkedListStrings[indexValue] != NULL)
{
/* Iterate from 1 position from the specified position where
we delete the character */
for(iter = *position-1; iter > 0; iter--)
{
if(f != NULL)
{
f = f->next; // Go to the next node while f is not null
}
else // Error checking
{
printf("Error - No such position.");
iter = 0;
}
}
if(f != NULL)
{
if(position > 0) // delete the character at its user specified position
{
temp = f->next;
f->next = temp->next;
free(temp);
}
else // if position was 0, destroy the string
{
front = f->next;
free(f);
}
linkedListStrings[indexValue] = front; // front of the list as it has not been modified
}
}
else // Error checking - if(linkedListStrings[*index] = NULL), cannot delete anything
{
printf("A string has not been created for index %d\n", indexValue);
}
return *linkedListStrings;
}