Having some issues with my C program. What it needs to do: read the data file us
ID: 3917786 • Letter: H
Question
Having some issues with my C program. What it needs to do: read the data file using redirection (ex: ./a.out < queue-data.csv )
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
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);
}
}
Explanation / Answer
Given below is the fixed code.
Please do rate the answer if it was helpful. Thank you
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct listnode {
char name[50];
struct listnode* next;
};
void flushList(struct listnode** head);
void removeNode(struct listnode** head);
struct listnode* insertNode(struct listnode* head, char* name);
void printList (struct listnode* head);
int main (void)
{
char line[50];
char* token;
char del[]=", ";
struct listnode *head=NULL;
while( fgets(line,50,stdin)){
token = strtok(line,del);
if(strcmp(token,"add") == 0){
token = strtok(NULL, del);
printf("%10s", "add:");
head = insertNode(head,token);
}
else if(strcmp(token, "flush") == 0){
printf("%10s", "flush:");
flushList(&head);
printList(head);
}
else if(strcmp(token,"remove") == 0){
printf("%10s", "remove:");
removeNode(&head);
printList(head);
}
}
printf("flushing remaining names ");
flushList(&head);
}
void printList (struct listnode* head){
/*print the linked list*/
struct listnode* current = head;
if(head != NULL){
printf("%s", current->name);
current = current->next;
while(current != NULL){
printf(", %s", current->name);
current = current->next;
}
}
printf(" ");
}
struct listnode* insertNode(struct listnode* head, char* name){
/*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 && ptr->next != NULL){
if(strcmp(ptr->name, name) == 0){
printf("%s already in queue ", name);
return head;
}
ptr = ptr->next;
}
struct listnode* nd = malloc(sizeof(struct listnode));
strcpy(nd->name, name);
nd->next = NULL;
if(head == NULL)
head = nd;
else
ptr->next = nd;
printList(head);
return head;
}
void removeNode(struct listnode** head){
struct listnode* bye = *head;
/*removes the first node*/
if(bye == NULL){
printf("queue is empty ");
}
else{
*head = bye->next;
free(bye);
}
}
void flushList(struct listnode** head){
/*cleans out all the nodes*/
struct listnode* empty;
if (*head == NULL){
printf("The list is already empty.");
}
while(*head != NULL){
empty =*head;
*head =(*head)->next;
free(empty);
}
}
output
-----
add:Glenda
add:Glenda, Marco
flush:
add:Kendrik
add:Kendrik, Samuel
add:Kendrik, Samuel, Nicholas
add:Kendrik, Samuel, Nicholas, Alex
add:Samuel already in queue
add:Kendrik, Samuel, Nicholas, Alex, Alexander
remove:Samuel, Nicholas, Alex, Alexander
flushing remaining names
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.