Question
Having some issues with my C program. What it needs to do: read the data file using redirection (ex: ./a.out 1)You know the following about
Having some issues with my C program. What it needs to do: read the data file using redirection (ex: ./a.out
1)You know the following about the data and can use it in your logic: Each line begins with one of the following actions: {add, remove, flush}. When the action is add, it will be followed by a name with a comma separating the two tokens. A line can be stored in 50 bytes. Nothing else about the lines within the file should be hard-coded to this particular data, including the number of lines in the file.
2) For each possible action, your program should print the action. When the action is add: (a) if the name is already in the queue, state so. (b) if the name is not in the queue, add it to the tail end of the queue and then print all of the names currently in the queue. When the action is remove:(a) if the queue is empty, state so. (b) if the queue is not empty, remove the node from the head end of the queue and then print all of the names currently in the queue (if any remain). This action should also free the memory for the node. When the action is flush: (a) if the queue is empty, state so. (b) if the queue is not empty, remove all remaining nodes from the queue and free their memory.
3). After the last action in the file has been processed, if any names remain in the queue they should be flushed and you should state that this is happening.
Attached is what the compiler is telling me is wrong, what my EXPECTED output should be, and the file.csv
My program: *****************************************************************************************************************************************
#include
#include
#include
struct listnode {
char g[50];
struct listnode* next;
};
void flushList(struct listnode* head);
void removeNode(struct listnode* head);
struct listnode* insertNode(struct listnode* head, char* x);
void printList (struct listnode* head,char g);
int main (void)
{
char line[50];
char* token, *del=",";
struct listnode *head=NULL, *tail, *temp;
fgets(line,50,stdin);
token = strtok(line,del);
while(token != NULL){
if(token = "add"){
token = strtok(NULL, del);
temp = insertNode(head,token);/*makes pointer from int w/o cast */
if(head==NULL){
head = temp;}
else{
tail->next = temp;/*incompatable pointer */
tail = temp;}
printf("add: ");
printList(head);
printf(" ");
}
else if(token = "flush"){
flushList(&head);
printf("flush: ");
printList(head);
printf(" ");
}
else if(token = "remove"){
removeNode(head);
}
token = strtok(NULL,del);
}
}
void printList (struct listnode* head){/*conflicting types for PrintLIst*/
/*print the linked list*/
while(head != NULL){
printf("%s", head->g);
head = head->next;/*incompatible pointer type*/
}
printf(" ");
}
struct listnode* insertNode(struct listnode* head, char* x){
/*adds a new node to the end of the list IF it isn't in the list already*/
struct listnode* ptr = head;
while(ptr != NULL){
if((ptr->g)== x){
printf("This name already exist here.");
return ptr;
}
ptr = ptr->next;
}
struct listnode* nd = malloc(sizeOf(struct listnode));
nd -> g = x;
nd->next = NULL;
return nd;
}
void removeNode(struct listnode* head){
struct listnode* bye = head;
/*removes the first node*/
if(bye == NULL){
printf("Removing: This is an empty list.");
}
else{
bye = bye -> next;
free(head);
}
}
void flushList(struct listnode** head){
/*cleans out all the nodes*/
struct listnode* empty;
if (head == NULL){
printf("Flushing: The list is already empty.");
}
while(head != NULL){
empty =*head;
*head =(* head)->next;
free(empty);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started