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; }
Types of Linked List
A sequence of data elements connected through links is called a linked list (LL). The elements of a linked list are nodes containing data and a reference to the next node in the list. In a linked list, the elements are stored in a non-contiguous manner and the linear order in maintained by means of a pointer associated with each node in the list which is used to point to the subsequent node in the list.
Linked List
When a set of items is organized sequentially, it is termed as list. Linked list is a list whose order is given by links from one item to the next. It contains a link to the structure containing the next item so we can say that it is a completely different way to represent a list. In linked list, each structure of the list is known as node and it consists of two fields (one for containing the item and other one is for containing the next item address).
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;
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 7 images