Please code in C language. Please use the starter code to help you solve the deleted node and the reverse list. Here is the starter code: #include #include #include #include   #include "linkedlist.h"   // print an error message by an error number, and return // the function does not exit from the program // the function does not return a value void error_message(enum ErrorNumber errno) {   char *messages[] = {       "OK",       "Memory allocaton failed.",       "Deleting a node is not supported.",       "The number is not on the list.",       "Sorting is not supported.",       "Reversing is not supported.",       "Token is too long.",       "A number should be specified after character d, a, or p.",       "Token is not recognized.",       "Invalid error number."};     if (errno < 0 || errno > ERR_END)     errno = ERR_END;   printf("linkedlist: %s\n", messages[errno]); }   node *new_node(int v) {   node *p = malloc(sizeof(node)); // Allocate memory   if (p == NULL) {     error_message(ERR_NOMEM);     exit(-1);   }     // Set the value in the node.   p->v = v; // you could do (*p).v   p->next = NULL;   return p; // return }   node *prepend(node *head, node *newnode) {   newnode->next = head;   return newnode; }   node *find_node(node *head, int v) {   while (head != NULL) {     if (head->v == v)       return head;     head = head->next;   }   return head; }   node *find_last(node *head) {   if (head != NULL) {     while (head->next != NULL)       head = head->next;   }   return head; }   node *append(node *head, node *newnode) {   node *p = find_last(head);     newnode->next = NULL;   if (p == NULL)     return newnode;   p->next = newnode;   return head; }   node *delete_list(node *head) {   while (head != NULL) {     node *p = head->next;     free(head);     head = p;   }   return head; }   void print_list(node *head) {   printf("[");   while (head) {     printf("%d, ", head->v);     head = head->next;   }   printf("]\n"); }   void print_node(node *p) {     printf("%p: v=%-5d next=%p\n", p, p->v, p->next); }   void print_list_details(node *head) {   while (head) {     print_node(head);     head = head->next;   } }   // functions that have not been implemented   node *delete_node(node *head, int v) {   // TODO   error_message(ERR_NODELETE);   return head; }   /*  * Given a pointer to the head node of an acyclic list, change the  * next links such that the nodes are linked in reverse order.  * Allocating new nodes or copying values from one node to another  * is not allowed, but you may use additional pointer variables.  * Return value is a pointer to the new head node.  */ node *reverse_list(node *head) {   // TODO   error_message(ERR_NOREVERSE);   return head;   }

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
icon
Concept explainers
Question

Please code in C language. Please use the starter code to help you solve the deleted node and the reverse list. Here is the starter code:

#include <stdio.h>

#include <ctype.h>

#include <stdlib.h>

#include <string.h>


 

#include "linkedlist.h"


 

// print an error message by an error number, and return

// the function does not exit from the program

// the function does not return a value

void error_message(enum ErrorNumber errno) {

  char *messages[] = {

      "OK",

      "Memory allocaton failed.",

      "Deleting a node is not supported.",

      "The number is not on the list.",

      "Sorting is not supported.",

      "Reversing is not supported.",

      "Token is too long.",

      "A number should be specified after character d, a, or p.",

      "Token is not recognized.",

      "Invalid error number."};


 

  if (errno < 0 || errno > ERR_END)

    errno = ERR_END;

  printf("linkedlist: %s\n", messages[errno]);

}


 

node *new_node(int v) {

  node *p = malloc(sizeof(node)); // Allocate memory

  if (p == NULL) {

    error_message(ERR_NOMEM);

    exit(-1);

  }


 

  // Set the value in the node.

  p->v = v; // you could do (*p).v

  p->next = NULL;

  return p; // return

}


 

node *prepend(node *head, node *newnode) {

  newnode->next = head;

  return newnode;

}


 

node *find_node(node *head, int v) {

  while (head != NULL) {

    if (head->v == v)

      return head;

    head = head->next;

  }

  return head;

}


 

node *find_last(node *head) {

  if (head != NULL) {

    while (head->next != NULL)

      head = head->next;

  }

  return head;

}


 

node *append(node *head, node *newnode) {

  node *p = find_last(head);


 

  newnode->next = NULL;

  if (p == NULL)

    return newnode;

  p->next = newnode;

  return head;

}


 

node *delete_list(node *head) {

  while (head != NULL) {

    node *p = head->next;

    free(head);

    head = p;

  }

  return head;

}


 

void print_list(node *head) {

  printf("[");

  while (head) {

    printf("%d, ", head->v);

    head = head->next;

  }

  printf("]\n");

}


 

void print_node(node *p) {

    printf("%p: v=%-5d next=%p\n", p, p->v, p->next);

}


 

void print_list_details(node *head) {

  while (head) {

    print_node(head);

    head = head->next;

  }

}


 

// functions that have not been implemented


 

node *delete_node(node *head, int v) {

  // TODO

  error_message(ERR_NODELETE);

  return head;

}


 

/*

 * Given a pointer to the head node of an acyclic list, change the

 * next links such that the nodes are linked in reverse order.

 * Allocating new nodes or copying values from one node to another

 * is not allowed, but you may use additional pointer variables.

 * Return value is a pointer to the new head node.

 */

node *reverse_list(node *head) {

  // TODO

  error_message(ERR_NOREVERSE);

  return head;


 

}

Part 1. Delete node. The linked list example we have studied in lecture maintains a singly linked list of
integers. The program can append and prepend integers to the list. Type help in the program to see a list
of commands. Note that if the number to be added is already on the list, the program prints the information
about the node that stores the number and does not add the number twice.
The list is displayed every time an integer is processed.
Currently, the program does not support the delete function. Complete the function delete_node () in
linkedlist.c so that the program can delete an integer from the list. The prototype of the function is:
node* delete_node (node * head, int v);
head is a pointer to the head node and v is the integer to be removed. The function returns the pointer
to the head node of the new list, which could be the same as head. If v is not on the list, the function prints
a message using the following function call: error_message (ERR_NOTFOUND).
Below is an example session using the delete feature.
$
1 2 3 4 5 6 7 8 9
./linkedlist-main
[1, ]
[1, 2, ]
[1, 2, 3, ]
[1, 2, 3, 4, ]
[1, 2, 3, 4, 5, ]
[1, 2, 3, 4, 5, 6, 1
5,
5, 6, 7, 8, ]
[1, 2, 3, 4,
[1, 2, 3, 4,
6, 7, ]
[1, 2, 3, 4, 5,
d5
[1, 2, 3, 4,
d3
[1, 2, 4, 6, 7, 8, 9, 1
d3
6, 7, 8, 9, ]
6, 7, 8, 9, ]
linkedlist: The number is not on the list.
[1, 2, 4, 6, 7, 8, 9, 1
Part 2. List reversal. In this exercise, we implement another function reverse_list() in linkedlist.
The prototype of the function is:
node reverse_list (node * head);
The function receives head as its sole argument, a pointer to the head node of the linked list. Assum
the list is acyclic. The function must change the next fields of each node so that the nodes end up linke
in reverse order. The function must return the address of the new head node (originally last in the list
You may use additional functions and local variables besides those already included in the starter code, bu
cannot change the values stored in the nodes or dynamically allocate new nodes. You can use either a loc
or recursion.
Below is an example session using the reversal feature.
$ ./linkedlist-main
1 2 3 4 5 6 7 8 9
[1, ]
[1, 2, ]
[1, 2, 3, ]
[1, 2, 3, 4, ]
[1, 2, 3, 4, 5, ]
[1, 2, 3, 4, 5, 6, ]
[1, 2, 3, 4, 5, 6, 7, 1
Transcribed Image Text:Part 1. Delete node. The linked list example we have studied in lecture maintains a singly linked list of integers. The program can append and prepend integers to the list. Type help in the program to see a list of commands. Note that if the number to be added is already on the list, the program prints the information about the node that stores the number and does not add the number twice. The list is displayed every time an integer is processed. Currently, the program does not support the delete function. Complete the function delete_node () in linkedlist.c so that the program can delete an integer from the list. The prototype of the function is: node* delete_node (node * head, int v); head is a pointer to the head node and v is the integer to be removed. The function returns the pointer to the head node of the new list, which could be the same as head. If v is not on the list, the function prints a message using the following function call: error_message (ERR_NOTFOUND). Below is an example session using the delete feature. $ 1 2 3 4 5 6 7 8 9 ./linkedlist-main [1, ] [1, 2, ] [1, 2, 3, ] [1, 2, 3, 4, ] [1, 2, 3, 4, 5, ] [1, 2, 3, 4, 5, 6, 1 5, 5, 6, 7, 8, ] [1, 2, 3, 4, [1, 2, 3, 4, 6, 7, ] [1, 2, 3, 4, 5, d5 [1, 2, 3, 4, d3 [1, 2, 4, 6, 7, 8, 9, 1 d3 6, 7, 8, 9, ] 6, 7, 8, 9, ] linkedlist: The number is not on the list. [1, 2, 4, 6, 7, 8, 9, 1 Part 2. List reversal. In this exercise, we implement another function reverse_list() in linkedlist. The prototype of the function is: node reverse_list (node * head); The function receives head as its sole argument, a pointer to the head node of the linked list. Assum the list is acyclic. The function must change the next fields of each node so that the nodes end up linke in reverse order. The function must return the address of the new head node (originally last in the list You may use additional functions and local variables besides those already included in the starter code, bu cannot change the values stored in the nodes or dynamically allocate new nodes. You can use either a loc or recursion. Below is an example session using the reversal feature. $ ./linkedlist-main 1 2 3 4 5 6 7 8 9 [1, ] [1, 2, ] [1, 2, 3, ] [1, 2, 3, 4, ] [1, 2, 3, 4, 5, ] [1, 2, 3, 4, 5, 6, ] [1, 2, 3, 4, 5, 6, 7, 1
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 7 images

Blurred answer
Knowledge Booster
Types of Linked List
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