Programming in C
4th Edition
ISBN: 9780321776419
Author: Stephen G. Kochan
Publisher: Addison-Wesley
expand_more
expand_more
format_list_bulleted
Textbook Question
Chapter 10, Problem 4E
Write a function called r to remove an e from a linked list. The sole argument to the procedure should be a pointer to the list. Have the function remove the entry after the one pointed to by the argument. (Why can’t you remove the entry pointed to by the argument?) You need to use the special structure you set up in exercise 3 to handle the special case of removing the first element from the list.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write a function that gets a linked list of ints, and reverses it. For example
- on input 1 -> 2 -> 3 -> 4, after the function finishes the execution, the list
becomes 4 -> 3 -> 2 -> 1
- If the list has one element, then it doesn’t change
- If the list is empty, then it doesn’t change
You may use the data fields in the struct and the functions provided in LL.h and LL.c.
// reverses a linked list
void LL_reverse(LL_t* list);
Test for the Function:
void test_q3() {
LL_t* lst = LLcreate();
LL_add_to_tail(lst, 1);
LL_add_to_tail(lst, 3);
LL_add_to_tail(lst, 8);
LL_add_to_tail(lst, 4);
LL_add_to_tail(lst, 3);
LL_reverse(lst);
intcorrect[] = {3,4,8,3,1};
inti;
node_t* n = lst->head;
for(i=0;i<5;i++) {
if (n==NULL) {
printf("Q3 ERROR: node %d==NULL unexpected\n", i);
return;
}
if (n->data != correct[i]) {
printf("Q3 ERROR: node%d->data==%d, expected %d\n", i, n->data, correct[i]);
return;
}
n = n->next;
}
if (n==NULL)
printf("Q3 ok\n");
}
Support…
Write a function that gets a linked list of ints, and reverses it. For example
- on input 1 -> 2 -> 3 -> 4, after the function finishes the execution, the list
becomes 4 -> 3 -> 2 -> 1
- If the list has one element, then it doesn’t change
- If the list is empty, then it doesn’t change
You may use the data fields in the struct and the functions provided in LL.h and LL.c.
// reverses a linked list
void LL_reverse(LL_t* list);
Test for the Function;
void test_q3() {
LL_t* lst = LLcreate();
LL_add_to_tail(lst, 1);
LL_add_to_tail(lst, 3);
LL_add_to_tail(lst, 8);
LL_add_to_tail(lst, 4);
LL_add_to_tail(lst, 3);
LL_reverse(lst);
intcorrect[] = {3,4,8,3,1};
inti;
node_t* n = lst->head;
for(i=0;i<5;i++) {
if (n==NULL) {
printf("Q3 ERROR: node %d==NULL unexpected\n", i);
return;
}
if (n->data != correct[i]) {
printf("Q3 ERROR: node%d->data==%d, expected %d\n", i, n->data, correct[i]);
return;
}
n = n->next;
}
if (n==NULL)
printf("Q3 ok\n");
}
Support File…
Write in C++
Now, what if Sam wants to insert Eevee after Pikachu but he doesn't know where Pikachu is located in his list. Let's help him search for a Pokemon by its name and insert another Pokemon directly after it. Write a function secondPlace() that takes six parameters and inserts a new Pokemon into a list right after another specified pokemon. Once you find the specified Pokemon, use the insertAfter() function from the previous sub-question to add the new Pokemon. The function should return the new number of strings in the array.
Note
The same Pokemon may appear in the list multiple times (Sam has a bad memory). Assume that Sam wants every instance of the Pokemon he is searching for to be followed by the Pokemon he is attempting to insert.
If the array is already full or if the number of strings that are going to be added to the array plus the current number of strings exceeds the total size of the array, the array should remain unchanged.
Assume all inputs are lowercase…
Chapter 10 Solutions
Programming in C
Ch. 10 - Type in and run the 15 programs presented in this...Ch. 10 - Write a function called to insert a new entry into...Ch. 10 - Prob. 3ECh. 10 - Write a function called r to remove an e from a...Ch. 10 - Prob. 5ECh. 10 - Prob. 6ECh. 10 - Prob. 7ECh. 10 - Prob. 8ECh. 10 - Prob. 9ECh. 10 - Prob. 10E
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
Write a program that implements your algorithm from Exercise 3.
Java: An Introduction to Problem Solving and Programming (8th Edition)
Consider the integration and configuration process model shown in Figure 2.3. Explain why it is essential to re...
Software Engineering (10th Edition)
Which loop should you use in situations where you want the loop to repeat until the boolean expression is false...
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Drivers License Exam The local Drivers License Office has asked you to write a program that grades the written ...
Starting Out with Java: Early Objects (6th Edition)
T F: A local variable may be accessed by any other procedure in the same Form file.
Starting Out With Visual Basic (7th Edition)
Knowledge Booster
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
- Use clojure, define a function called remove-second that takes a list, and returns a new list that is the same as the input list, but with the second value removed. For example, given '(3 1 4), remove-second should return the list '(3 4).arrow_forwardWe have a list. For example [2,4,6,6,8,2,12,54,15]. Without repeating the elements in the list a new list that does not repeat its elements by creating and using the function that writes Create.arrow_forwardWrite a function that takes a list removal(L), delete from it all the elements whose indicesare divisible by 3. And return the resulting list after removing unwanted elements.Example:>>>removal(['A', 'B', 'C', 'D', 'E'])['B', 'C','E'arrow_forward
- Write a function with the signature below that returns the sum of the last k elements of a singly linked list that contains integers. int returnSumOfLastKNodes(Node* head, int k) Example: 10 -> 5->8->15->11->9->23 10 represents the head node, returnSumOfLastKNodes(Node* head, 4) will return 58.arrow_forwardYou are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty. Function Description Complete the insertNodeAtTail function in the editor below. insertNodeAtTail has the following parameters: SinglyLinkedListNode pointer head: a reference to the head of a list int data: the data value for the node to insert Returns SinglyLinkedListNode pointer: reference to the head of the modified linked list Input Format The first line contains an integer , the number of elements in the linked list.The next lines contain an integer each, the value that needs to be inserted at tail. Constraints Sample Input STDIN Function ----- -------- 5 size of linked list n = 5 141 linked list data values…arrow_forwardWrite a function that changes the places of the first and second nodes of a linked list. I.e. the First node will become the second node, and the second node will become the first node. THE FUNCTION WILL NOT EXCHANGE DATA, IT WILL CHANGE LINKS.arrow_forward
- Write a C++ Function that returns the data of the middle node in a linked list and in case the linked list contains only one node return the data inside this node then returns the sum of all the nodes in linked list then returns the maximum data value in the linked listarrow_forwardcomplete a C program which delete the first and last nodes of Linked List by using below functions: void Firstnodedelete(); void Lastnodedelete();arrow_forwardConsider the following function that takes reference to head of a Doubly Linked List as parameter. Assume that anode of doubly linked list has previous pointer as prev and next pointer as next.void fun(struct node **head_ref){struct node *temp = NULL;struct node *current = *head_ref;while (current != NULL){temp = current->prev;current->prev = current->next;current->next = temp;current = current->prev;}if(temp != NULL )*head_ref = temp->prev;}Assume that reference of head of following doubly linked list is passed to above function 1 <--> 2 <--> 3 <--> 4 <--> 5 <-->6. What should be the modified linked list after the function call? Note: solve as soon as possiblearrow_forward
- Write a function merge that merges two lists into one, alternating elements from each list until the end of one of the lists has been reached, then appending the remaining elements of the other list. For example, merging the lists containing A B C and D E F G H should yield the list A D B E C F G H. c++arrow_forwardImplements clone which duplicates a list. Pay attention, because if there are sublists, they must be duplicated as well. Understand the implementation of the following function, which recursively displays the ids of each node in a list Develop your solution as follows: First copy the nodes of the current list (self) Create a new list with the copied nodes Loop through the nodes of the new list checking the value field If this field is also a list (use isinstance as in the show_ids function) then it calls clone on that list and substitutes the value. Complete the code: def L4(*args,**kwargs): class L4_class(L): def clone(self): def clone_node(node): return <... YOUR CODE HERE ...> r = <... YOUR CODE HERE...> return r return L4_class(*args,**kwargs)arrow_forwardA linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return . Otherwise, return . Example refers to the list of nodes The numbers shown are the node numbers, not their data values. There is no cycle in this list so return . refers to the list of nodes There is a cycle where node 3 points back to node 1, so return . Function Description Complete the has_cycle function in the editor below. It has the following parameter: SinglyLinkedListNode pointer head: a reference to the head of the list Returns int: if there is a cycle or if there is not Note: If the list is empty, will be null. Input Format The code stub reads from stdin and passes the appropriate argument to your function. The custom test cases format will not be described for this question due to its complexity. Expand the section for the main function and review the…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Introduction to Linked List; Author: Neso Academy;https://www.youtube.com/watch?v=R9PTBwOzceo;License: Standard YouTube License, CC-BY
Linked list | Single, Double & Circular | Data Structures | Lec-23 | Bhanu Priya; Author: Education 4u;https://www.youtube.com/watch?v=IiL_wwFIuaA;License: Standard Youtube License