Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 3(18 points)-Write a program Submit lights.c (Create a linked list-0 poi

ID: 3735991 • Letter: P

Question

Problem 3(18 points)-Write a program Submit lights.c (Create a linked list-0 points without a linked list) Create a program that reads in a light information (a string of lights). Each line m that reads in a file of Christmas in the file represents a single light in the string of lights. nfo is stored in the following format: blue,2,0 where blue is the color, 2 is the brightness of the light and O is the size (0 means small and 1 means big) e program allows users to type in the following three commands: on, off and exit 1. When the user types on, each light should be output to screen (with the first letter of the color representing the light). The brightness dictates how many times the letter should be output to screen see sample run). If the lights are already on (meaning the user had typed on previously and not typed offyet), the program should output Lights are already on. to screen. When the user types off, instead of printing out each letter, the program should print - (see sample run). If the lights are already off, the program should output Lights are already off to screen. Note that the program starts with the lights in the off state If the user types exit, the program exits. 2. 3. You must use the following struct for your nodes (D O NOT MODIFY): typedef struct Nodef char* color; /*color of the light'/ int **details; /"holds the brightness and size*/ struct Node* next; JNode; Step 1: Define the following function: "This function takes the filename and creates a linked list of the string of lights. It returns the head of the linked list Node* light info(char *filename) Step 2: You must define at least one other meaningful function that solves a part of your problem (0 points if you do not do this). Random functions that just print a line do not count. You should mention in comments what this function does and the importance of the parameters and return type. *Hint-You should break down the given problem into smaller problems to figure out what needs a function.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXCHAR 1024

typedef struct Node{
    char *color;/*color of the light */
    int ** details;/*holds the brightness and size */
    struct Node *next;
}Node;
void fillNode(Node *new,char *str){
    char *color = (char *)malloc(MAXCHAR*sizeof(char));
    char *tmp = strtok(str,",");
    memcpy(color,tmp,strlen(tmp));
    new->color = color;
    new->details = (int**)malloc(2*sizeof(int *));
    new->details[0] = (int *)malloc(sizeof(int));
    new->details[1] = (int *)malloc(sizeof(int));
    *(new->details[0]) = atoi(strtok(NULL,","));
    *(new->details[1]) = atoi(strtok(NULL,","));
    new->next = NULL;
}
void push_back(Node **Head,Node *new){
    if(*Head == NULL){
        *Head = new;
    }else{
        Node *crnt = *Head;
        while(crnt->next!=NULL){
            crnt = crnt->next;
        }
        crnt->next = new;
    }
}
Node * light_info(char *filename){
    Node *Head = NULL;
    FILE *fp;
    char str[MAXCHAR];
    fp = fopen(filename, "r");
    if (fp == NULL){
        printf("Could not open file %s",filename);
        return NULL;
    }
    while (fgets(str, MAXCHAR, fp) != NULL){
        Node *new = (Node *)malloc(sizeof(Node));
        fillNode(new,str);
        push_back(&Head,new);
    }
    fclose(fp);
    return Head;
}
void print_linked_list(Node *Head){
    if(Head == NULL){
        return;}
    Node *crnt = Head;
    do{
        int i = 0;
        for(i = *(crnt->details[0]); i > 0; --i){
            printf("%c",*crnt->color);
        }printf(" ");
        crnt = crnt->next;
    }while(crnt!=NULL);printf(" ");
}
void free_all(Node *node){
    if(node!=NULL){
        free_all(node->next);
        node->next = NULL;
        free(node->color);
        free(node->details[0]);
        free(node->details[1]);
        free(node->details);
        free(node);
    }
}
int main(int argc, char** argv) {
    char str[4];
    int Switch = 0;
    Node *Head = light_info(argv[1]);
    for(;;){
      gets(str);
      if(strcmp(str,"on")==0){
          if(Switch == 0){
              printf("***Turning lights on ");
              print_linked_list(Head);
              Switch = 1;
          }else{
              printf("--Lights are already on ");
          }
      }else if(strcmp(str,"off")==0){
          if(Switch == 1 ){
              printf("***Turning lights off ");
              Switch = 0;
          }else{
              printf("--Lights are already off ");
          }
      }else if(strcmp(str,"exit")==0){
          printf("Exiting... ");
          break;
      }
    }
    free_all(Head);
    Head =NULL;/*not required but still making it so as the address gets freed in previous call*/
    return 0;
}

/*Note:give file name as command line argument*/

/*input file*/

/*blue,3,0
red,2,1
violet,1,0
yellow,3,1*/

/*output*/

/*

on
***Turning lights on
bbb rr v yyy
on
--Lights are already on
off
***Turning lights off
off
--Lights are already off
on
***Turning lights on
bbb rr v yyy
off
***Turning lights off
exit
Exiting...


*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote