Provide an explanation that justifies the running time of each function in terms of n, where n is the number of students in the list. You can assume the name of each student is of length O(1). HERE ARE THE FUNCTIONS FOR YOU TO EXPLAIN THE RUNNING TIME OF: FUNCTION 1: bool insert_student(int id, char name[], struct slist* lst) { char* studentName = find_student(id, lst); if (studentName == NULL) { // no student with the given id is found, // so we can perform insertion here   // creating a new student node struct snode* new_node = (struct snode*) malloc(sizeof(struct snode*));   new_node->id = id; new_node->name = (char*) malloc(strlen(name)*sizeof(char)); strcpy(new_node->name, name); new_node->next = NULL;   // if the first node is null if (lst->front == NULL) { lst->front = new_node; } else { struct snode* prevNode = NULL; struct snode* nextNode = lst->front;   bool is_node_inserted = false;   do { if (id < nextNode->id) { if (prevNode == NULL) { // new node will be the front node new_node->next = nextNode; lst->front = new_node; } else { // node will be inserted between prevNode // and nextNode prevNode->next = new_node; new_node->next = nextNode; } is_node_inserted = true; break; } prevNode = nextNode; nextNode = nextNode->next; } while (nextNode != NULL);   if (!is_node_inserted) { // then this node is the last node // prevNode will hold the last node of the list prevNode->next = new_node; }   }   return true; } else { // student with the same id found // no insertion is possible return false; } } FUNCTION 2: bool remove_student(int id, struct slist* lst) { char* foundName = find_student(id, lst); if (foundName != NULL) { // the student is present then we can remove the student if (lst->front->id == id) { // if the first node is the id to be removed // then set node as first node // and remove the first node struct snode* tempNode = lst->front; lst->front = tempNode->next; free(tempNode); } else { // getting the current node and the next node struct snode* currentNode = lst->front; struct snode* nextNode = currentNode->next; // this bool value is required in case when the last node // is the node to be deleted bool was_student_deleted = false;   while (nextNode->next != NULL){ // if the id of next node matches the id to be found if (nextNode->id == id) { // then remove the next node // and set the next of next node as // the next of the current node struct snode* tempNode = nextNode; currentNode->next = tempNode->next; was_student_deleted = true; free(tempNode); break; } currentNode = nextNode; nextNode = nextNode->next; }   FUNCTION 3: void free_list(struct slist* lst) { free(lst->front); free(lst); }   FUNCTION 4: bool check_for_sorted_list(struct slist* out) { struct snode* currentNode = out->front; int currentId = -1;   while (currentNode != NULL) { if (currentId > currentNode->id) { return false; } else { currentId = currentNode->id; } currentNode = currentNode->next; }   return true; }   FUNCTION 5: struct slist* create_list() { struct slist* new_list = (struct slist*) malloc(sizeof(struct slist*)); new_list->front = NULL; return new_list; } FUNCTION 6:   if (!was_student_deleted) { struct snode* tempNode = currentNode->next; currentNode->next = NULL; free(tempNode); } } return true; } else { // the student is not present return false; } }   FUNCTION 7: char * find_student(int id, struct slist * lst){ struct snode* nodecurrent = lst -> front;   while (nodecurrent != NULL) { // id matches nodecurrent if (nodecurrent -> id == id) { return nodecurrent -> name; }   nodecurrent = nodecurrent -> next; }   return NULL; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Provide an explanation that justifies the running time of each function in terms of n, where n is the number of students in the list. You can assume the name of each student is of length O(1).

HERE ARE THE FUNCTIONS FOR YOU TO EXPLAIN THE RUNNING TIME OF:

FUNCTION 1:

bool insert_student(int id, char name[], struct slist* lst) {

char* studentName = find_student(id, lst);

if (studentName == NULL) {

// no student with the given id is found,

// so we can perform insertion here

 

// creating a new student node

struct snode* new_node = (struct snode*) malloc(sizeof(struct snode*));

 

new_node->id = id;

new_node->name = (char*) malloc(strlen(name)*sizeof(char));

strcpy(new_node->name, name);

new_node->next = NULL;

 

// if the first node is null

if (lst->front == NULL) {

lst->front = new_node;

} else {

struct snode* prevNode = NULL;

struct snode* nextNode = lst->front;

 

bool is_node_inserted = false;

 

do {

if (id < nextNode->id) {

if (prevNode == NULL) {

// new node will be the front node

new_node->next = nextNode;

lst->front = new_node;

} else {

// node will be inserted between prevNode

// and nextNode

prevNode->next = new_node;

new_node->next = nextNode;

}

is_node_inserted = true;

break;

}

prevNode = nextNode;

nextNode = nextNode->next;

} while (nextNode != NULL);

 

if (!is_node_inserted) {

// then this node is the last node

// prevNode will hold the last node of the list

prevNode->next = new_node;

}

 

}

 

return true;

} else {

// student with the same id found

// no insertion is possible

return false;

}

}

FUNCTION 2:

bool remove_student(int id, struct slist* lst) {

char* foundName = find_student(id, lst);

if (foundName != NULL) {

// the student is present then we can remove the student

if (lst->front->id == id) {

// if the first node is the id to be removed

// then set node as first node

// and remove the first node

struct snode* tempNode = lst->front;

lst->front = tempNode->next;

free(tempNode);

} else {

// getting the current node and the next node

struct snode* currentNode = lst->front;

struct snode* nextNode = currentNode->next;

// this bool value is required in case when the last node

// is the node to be deleted

bool was_student_deleted = false;

 

while (nextNode->next != NULL){

// if the id of next node matches the id to be found

if (nextNode->id == id) {

// then remove the next node

// and set the next of next node as

// the next of the current node

struct snode* tempNode = nextNode;

currentNode->next = tempNode->next;

was_student_deleted = true;

free(tempNode);

break;

}

currentNode = nextNode;

nextNode = nextNode->next;

}

 

FUNCTION 3:

void free_list(struct slist* lst) {

free(lst->front);

free(lst);

}

 

FUNCTION 4:

bool check_for_sorted_list(struct slist* out) {

struct snode* currentNode = out->front;

int currentId = -1;

 

while (currentNode != NULL) {

if (currentId > currentNode->id) {

return false;

} else {

currentId = currentNode->id;

}

currentNode = currentNode->next;

}

 

return true;

}

 

FUNCTION 5:

struct slist* create_list() {

struct slist* new_list = (struct slist*) malloc(sizeof(struct slist*));

new_list->front = NULL;

return new_list;

}

FUNCTION 6:

 

if (!was_student_deleted) {

struct snode* tempNode = currentNode->next;

currentNode->next = NULL;

free(tempNode);

}

}

return true;

} else {

// the student is not present

return false;

}

}

 

FUNCTION 7:

char * find_student(int id, struct slist * lst){

struct snode* nodecurrent = lst -> front;

 

while (nodecurrent != NULL) {

// id matches nodecurrent

if (nodecurrent -> id == id) {

return nodecurrent -> name;

}

 

nodecurrent = nodecurrent -> next;

}

 

return NULL;

}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Lists
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education