ListyString and ListyNode Structs (ListyString.h) For your linked lists, you mus
ID: 3599716 • Letter: L
Question
ListyString and ListyNode Structs (ListyString.h)
For your linked lists, you must use the structs we have specified in ListyString.h without any modifications. You must #include the header file from ListyString.c like so:
#include "ListyString.h"
The node struct you will use for your linked lists is defined in ListyString.h as follows:
typedef struct ListyNode {
char data; // Each node holds a single character.
struct ListyNode *next; // Pointer to next node in linked list.
} ListyNode;
Additionally, there is a ListyString struct that you will use to store the head of each linked list string, along with the length of that list:
typedef struct ListyString {
struct ListyNode *head; // Pointer to head of string's linked list.
int length; // Length of this string / linked list.
} ListyString;
Input Files
One of the required functions for this program needs to open and process an input file. The first line of the input file will always be a single string that contains at least 1 character and no more than 1023 characters, and no spaces. The first thing you should do when processing an input file is to read in that string and convert it to a ListyString (i.e., a linked list string). That will become your working string, and you will manipulate it according to the remaining commands in the input file.
Each of the remaining lines in the file will correspond to one of the following string manipulation commands, which you will apply to your working string in order to achieve the desired output for your program:
Important note: For the first three commands listed in this table, key is always a single character, and str is a string. Both key and str are guaranteed to contain alphanumeric characters only (A-Z, a-z, and 0-9). Not counting the need for a null terminator (‘’), str can range from 1 to 1023 characters (inclusively). So, with the null terminator, you might need up to 1024 characters to store str as a char array when reading from the input file.
Another important note: If one of the above commands modifies your working string, you should also ensure that the length member of that ListyString struct gets updated.
Function Requirements
int main(int argc, char **argv);
Description: You have to write a main() function for this program. It should only do the following three things: (1) capture the name of an input file (passed as a command line argument), (2) call the processInputFile() function (passing it the name of the input file to be processed), and (3) return zero.
Returns: 0 (zero).
int processInputFile(char *filename);
Description: Read and process the input file (whose name is specified by the string filename) according to the description above in Section 3, “Input Files.” To perform the string manipulations described in that section, you should call the corresponding required functions listed below. In the event that a bad filename is passed to this function (i.e., the specified file does not exist), this function should simply return 1 without producing any output.
Output: This function should only produce output if the input file has “?” and/or “!” commands. For details, see Section 3 (“Input Files”), or refer to the input/output files included with this assignment. Note that this function should not produce any output if the input file does not exist.
Returns: If the specified input file does not exist, return 1. Otherwise, return 0.
ListyString *createListyString(char *str);
Description: Convert str to a ListyString by first dynamically allocating a new ListyString struct, and then converting str to a linked list string whose head node will be stored inside that ListyString struct. Be sure to update the length member of your new ListyString, as well.
Special Considerations: str may contain any number of characters, and it may contain nonalphanumeric characters. If str is NULL or an empty string (“”), simply return a new
ListyString whose head is initialized to NULL and whose length is initialized to zero.
Runtime Requirement: This should be an O(k) function, where k is the length of str.
Returns: A pointer to the new ListyString. Ideally, this function would return NULL if any calls to malloc() failed, but I do not intend to test your code in an environment where malloc() would fail, so you are not required to check whether malloc() returns NULL.
int listyLength(ListyString *listy);
Description: Return the length of the ListyString (i.e., the length of listy’s linked list).
Runtime Requirement: The runtime of this function must be O(1).
Returns: The length of the string (i.e., the length of the linked list contained within listy). If listy is NULL, return -1. If listy is non-NULL, but listy->head is NULL, return zero.
Explanation / Answer
ListyString.h
#ifndef __LISTY_STRING_H
#define __LISTY_STRING_H
typedef struct node
{
// each node holds a single character
char data;
// pointer to next node in linked list
struct node *next;
} node;
// Functional Prototypes
node *stringToList(char *str);
node *replaceChar(node *head, char key, char *str);
node *reverseList(node *head);
void printList(node *head);
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.