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

Comment everything and make sure it compiles and works!!! You are required to wr

ID: 3607141 • Letter: C

Question

Comment everything and make sure it compiles and works!!!


You are required to write an interactive C program that prompts the user for commands, accepts commands from the keyboard (stdin) and executes those commands. When a command requires output, it must be written to stdout. The program must continue to accept and process commands until the user types the end command The program deals with linked lists. Each node of such a list contains a string of length at most 255, a positive integer (i.e., an integer value 2 1) and a pointer to the next node of the list. For any node, the string and the integer stored in that node will be referred to as the text and the index for that node respectively. Initially, the list is empty. At all times, the existing list must satisfy the following requirements 1. The index is a number of the node in the list, i e. the first node has index 1, and when the list is scanned from the beginning to the end, the value of indexes is increasing by 1 2. The texts appearing in the list are all distinct; that is, no two nodes have the same text. The commands and their interpretations are as follows. (You should bear in mind that different parts of a command are separated by one or more spaces.) A. Command Insert After: The syntax for this command is as follows: ina num str Here, ina represents the name of the command num represents a positive integer number, and str represents a text. The interpretation of this command is as follows (a) A new node with the text specified in the command must be inserted in the list after a node whose index is equal to the number specified in the command, indexes of the list

Explanation / Answer

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

enum command{

ina, inb, del, rep, prn, end

};

typedef struct NODE_TAG{

char text[256];

struct NODE_TAG* index;

}NODE;

NODE *head = NULL;

enum command check_command(char *str){

if(str[0] == 'd') return del;

if(str[0] == 'r') return rep;

if(str[0] == 'p') return prn;

if(str[0] == 'e') return end;

if(str[2] == 'b') return inb;

return ina;

}

int check_str(char * str){

NODE * tmp = head;

while(tmp != NULL){

if(strcmp(tmp->text, str) ==0){

printf("ITEM already exists ");

return 1;

}

tmp = tmp->index;

}

return 0;

}

void insert_a(int index, char* str){

if(check_str(str)){

return;

}

NODE* temp = malloc(sizeof(NODE));

strcpy(temp->text, str);

temp->index = NULL;

if(head == NULL){

head = temp;

if(index == 1)

printf("OK ");

else

printf("Text inserted at the end ");

return;

}

NODE * tmp = head, *prev = head;

int count = 1;

while(tmp->index != NULL && count != index){

count++;

prev = tmp;

tmp = tmp->index;

}

if(count == index ){

temp->index = prev->index;

prev->index = temp;

if(prev == head){

head = temp;

}

printf("OK ");

return;

}

else {

tmp->index = temp;

printf("Text inserted at the end ");

return;

}

return;

}

void insert_b(int index, char * str){

if(check_str(str)){

return;

}

NODE* temp = malloc(sizeof(NODE));

strcpy(temp->text, str);

temp->index = NULL;

if(head == NULL || index == 1){

if(index == 1){

if(head != NULL) temp->index = head;

printf("OK ");

}

else

printf("Text inserted at the Begining ");

head = temp;

return;

}

NODE * tmp = head, *prev = head;

int count = 2;

while(tmp->index != NULL && count != index){

count++;

prev = tmp;

tmp = tmp->index;

}

if(count == index){

temp->index = prev->index;

prev->index = temp;

printf("OK ");

}

else{

temp->index = head;

head = temp;

printf("Text inserted at the Begining ");

}

return;

}

void delete_index(int ind){

NODE * temp = head, *tmp;

if(head == NULL ){

printf("No Item Exists ");

return;

}

if(ind == 1){

head = head->index;

free(temp);

printf("Deleted ");

return;

}

int count = 2;

while(count != ind && temp->index != NULL){

tmp = temp;

temp = temp->index;

count++;

}

if(count == ind){

if(temp->index != NULL){

tmp = temp->index;

temp->index = tmp->index;

free(tmp);

printf("Deleted ");

return;

}

else{

printf("No such index ");

}

}

else{

printf("NO such index ");

}

return;

}

void replace(int index, char* str){

NODE * temp = head;

int count = 1;

while(temp != NULL && count != index){

temp = temp->index;

}

if(temp == NULL){

printf("No such index ");

}

else{

strcpy(temp->text, str);

printf("Replaced ");

}

return;

}

void print_list(){

if(head == NULL){

printf("List is empty ");

return;

}

NODE * temp = head;

int ind = 1;

while(temp != NULL){

printf("%d %s ",ind, temp->text);

}

return;

}

int main(){

char cmnd[5];

char text[256];

int index;

while(1){

printf("Choose any command : 1. ina - Insert After 2. inb - Insert Before 3. del - Delete ");

printf("4. rep - Replace 5. prn - Print List 6. end - Exit from program ");

printf("Enter command : ");

scanf("%s", cmnd);

enum command cmd = check_command(cmnd);

switch(cmd){

case ina:

scanf("%d", &index);

fgets(text, 256, stdin);

insert_a(index, text);

break;

case inb:

scanf("%d", &index);

fgets(text, 256, stdin);

insert_b(index, text);

break;

case del:

scanf("%d", &index);

delete_index(index);

break;

case rep:

scanf("%d", &index);

fgets(text, 256, stdin);

replace(index, text);

break;

case prn:

print_list();

break;

case end:

exit(0);

default:

printf("Command is not correct ");

}

}

return 0;

}