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!!!
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;
}