Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Just need a little help fixing a code. Everything runs fine for the most part, but there seems to be an issue when I use

Just need a little help fixing a code. Everything runs fine for the most part, but there seems to be an issue when I use the del command. It messes up the incrementing of the linked list. If you were to run the given code with the given input, you would see what I mean; the output does not match what it is supposed to be when using the prn command to print the current list. I have commented each function with what it should do.

The syntax for the insertAfter command is: ina num str 

where ina is the name of the command(insertAfter), num is a positive integer, and str represents the text.

The syntax for the insertBefore command is: inb num str

The syntax for the deleteNode command is: del num

The syntax for the replaceText command is: rep num str

If entering the following commands:

inb 2 best

ina 5 is

inb 1 laughing

ina 3 is

del 4

del 3

rep 2 is

ina 5 medicine

inb 3 best

ina 1 best

ina 2 best

prn

end

The output should be:

inb 2 best

Text inserted at the beginning

ina 5 is

Text inserted at the end

inb 1 laughing

ok

ina 3 is

Such text exists already

del 4

No such index

del 3

Deleted

rep 2 is

Replaced

ina 5 medicine

Text inserted at the end

inb 3 best

Ok

ina 1 best

Such text exists already

ina 2 the

Ok

prn

1 laughing

2 is

3 the

4 best

5 medicine

end

Please comment where/what you fixed; I would like to learn.

The code to be worked on:

#include #include #include // Link List Node struct Node{ int index; char *text; struct Node *next; }; // function to create a new node struct Node* newNode(int index,char *text) { // malloc is used to dynamically allocate the memory struct Node* temp = (struct Node*)malloc(sizeof(struct Node)); temp->index = index; temp->text = (char*)malloc(sizeof(char)*(strlen(text)+1)); strcpy(temp->text,text); temp->next = NULL; return temp; } //insertAfter function: //(a) A new node with the text specified in the command is inserted in the list after a node whose index is equal to the number specified in the command, indexes of the list should be changed to keep increasing order, and the following message must be printed "Ok". //(b) If the list contains a node whose text is identical to the text specified in the command, then no new node must be created and the following message must be printed "Such text exists already". //(c) If the list does not contain a node whose index is equal to the number specified in the command, then a new node must be inserted at the end of the list and the following message must be printed "Text inserted at the end". void insertAfter(struct Node** head, int index, char *text) { struct Node *temp; if( *head == NULL) { temp = newNode(1,text); temp->next = *head; *head = temp; return; } struct Node *cur = *head; while(cur->next!=NULL && strcmp(cur->text,text)!=0 && cur->index!=index) cur = cur->next; if(cur->index == index) { struct Node* temp = cur; while(temp && strcmp(temp->text,text)!=0) temp = temp->next; if(temp && strcmp(temp->text,text) == 0) { printf("Such text exists already "); return; } } if(strcmp(cur->text,text)==0 ) { printf("Such text exists already "); } else if(cur->index==index) { struct Node* nExt = cur->next; temp = newNode(index+1,text); cur->next = temp; temp->next = nExt; while(temp) { temp = temp->next; if(temp) temp->index = temp->index+1; } printf("Ok "); } else { temp = newNode(index,text); cur->next = temp; temp->next = NULL; printf("Text inserted at the End "); } } //insertBefore function: //(a) A new node with the text specified in the command is inserted in the list before a node whose index is equal to the number specified in the command, indexes of the list should be changed to keep increasing order, and the following message must be printed "Ok". //(b) If the list contains a node whose text is identical to the text specified in the command, then no new node must be created and the following message must be printed "Such text exists already". //(c) If the list does not contain a node whose index is equal to the number specified in the command, then a new node must be inserted at the beginning of the list and the following message must be printed "Text inserted at the beginning". void insertBefore(struct Node** head, int index, char *text) { struct Node* temp; if(*head == NULL) { temp = newNode(1,text); temp->next = *head; *head = temp; printf("Text inserted at the beginning "); return; } struct Node *cur = *head , *prev = NULL; while(cur!=NULL && strcmp(cur->text,text)!=0 && cur->index!=index) { prev = cur; cur = cur->next; }

if(cur!=NULL && cur->index == index) { struct Node* temp = cur; while(temp && strcmp(temp->text,text)!=0) temp = temp->next; if(temp && strcmp(temp->text,text) == 0) { printf("Such text exists already "); return; } } if(cur!=NULL && strcmp(cur->text,text) == 0) { printf("Such text exists already "); } else if(cur!=NULL && cur->index==index) { if(cur == *head) temp = newNode(1,text); else temp = newNode(index-1,text); if(prev==NULL) { temp->next = *head; *head = temp; } else { prev->next = temp; temp->next = cur; } cur = temp; while(cur) { int val = cur->index; cur = cur->next; if(cur) cur->index = val+1; } printf("Ok "); } else { temp = newNode(1,text); temp->next = *head; *head = temp; cur = temp; index=0; while(cur) { if(cur) cur->index = index+1; if(cur) cur = cur->next; } printf("Text inserted at the beginning "); } } //deleteNode function: //(a) If the list contains a node whose index is equal to the number specified in the command, then the node must be removed from the list, indexes of the list should be changed to keep increasing order, and the following message must be printed "Deleted". //(b) If the list does not contain a node whose index is equal to the number specified in the command, then the program must leave the list unchanged and the following message must be printed "No such index". void deleteNode(struct Node** head, int index) { struct Node* temp; if(*head == NULL) { printf("No Such index "); return; } struct Node *cur = *head , *prev = NULL; while(cur!=NULL && cur->index!=index) { prev = cur; cur = cur->next; } if(cur!=NULL && cur->index==index) { if(prev==NULL) { temp = *head; *head = (*head)->next; free(temp); } else { prev->next = cur->next; free(cur); } printf("Deleted "); } else printf("No Such Index "); } // replaceText function: //(a) If the list contains a node whose index is equal to the number specified in the command, then the node text must be replaced with the text specified in the command, and the following message must be printed "Replaced". //(b) If the list does not contain a node whose index is equal to the number specified in the command, then the program must leave the list unchanged, and the following message must be printed "No such index". void replaceText(struct Node* head, int index, char *text) { if(head==NULL) { printf("No Such Index "); return; } struct Node *cur = head; while(cur!=NULL && cur->index!=index) cur = cur->next; if(cur==NULL) { printf("No Such Index "); } else { free(cur->text); cur->text = (char*)malloc(sizeof(char)*(strlen(text+1))); strcpy(cur->text,text); printf("Replaced "); } } // function to print all the node's data of linkedlist void printList(struct Node* node) { if(node==NULL) { printf("List is Empty "); return; } while(node) { printf("%d\t\t%s ",node->index,node->text); node = node->next; } } // main method to test above functions int main() { struct Node* head = NULL; char cmd[4]; char name[256]; int index; while(1){ scanf("%s",&cmd); if(strcmp(cmd,"ina")=='\0') { scanf("%d %s",&index,&name); insertAfter(&head , index,name); } else if(strcmp(cmd,"inb")=='\0') { scanf("%d %s",&index,&name); insertBefore(&head,index,name); } else if(strcmp(cmd,"del")=='\0') { scanf("%d",&index); deleteNode(&head, index); } else if(strcmp(cmd,"rep")=='\0') { scanf("%d %s",&index,&name); replaceText(head,index,name); } else if(strcmp(cmd,"prn")=='\0') { printList(head); } else break; } return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Spatial And Temporal Databases 10th International Symposium Sstd 2007 Boston Ma Usa July 2007 Proceedings Lncs 4605

Authors: Dimitris Papadias ,Donghui Zhang ,George Kollios

2007th Edition

3540735399, 978-3540735397

More Books

Students also viewed these Databases questions