Concept explainers
Explanation of Solution
Linked list:
Linked list is a linear and dynamic data structure which is used to organize data; it contains sequence of elements which are connected together in memory to form a chain. The every element of linked list is called as a node.
List head:
List head is a pointer used to point the first node in the linked list and it is also called as “head pointer”; if the “head pointer” points to a “NULL” value, then it is considered that the “list is empty”.
Graphical representation of linked list:
The graphical representation of a linked list is as follows:
Function to print the value stored in the first node of a list:
//Definition of printing the first node function
void printFirst(ListNode *ptr)
{
/*Check the list w...
Want to see the full answer?
Check out a sample textbook solutionChapter 17 Solutions
MyLab Programming with Pearson eText -- Standalone Access Card -- for Starting Out With C++: Early Objects (My Programming Lab)
- Write a user defined function named as “length_list” function that finds the length of a list of list_node_t nodes. This code should fit with the following code.Partial Code: #include <stdio.h> #include <stdlib.h> /* gives access to malloc */ #define SENT -1 typedef struct list_node_s { int digit; struct list_node_s *restp; } list_node_t; list_node_t *get_list(void); int main(void) { list_node_t *x;int y; x=get_list(); printf("%d\n",x->digit); printf("%d\n",x->restp->digit); printf("%d\n",x->restp->restp->digit); // your length_list function should be called from here printf("Length is: %d",y); } /* * Forms a linked list of an input list of integers * terminated by SENT */ list_node_t * get_list(void) { int data; list_node_t *ansp; [20]scanf("%d", &data); if (data == SENT) { ansp = NULL; } else { ansp = (list_node_t *)malloc(sizeof (list_node_t)); ansp->digit = data; ansp->restp = get_list(); } return (ansp);arrow_forwardstruct node{int num;node *next, *before;};start 18 27 36 45 54 63 The above-linked list is made of nodes of the type struct ex. Your task is now to Write a complete function code to a. Find the sum of all the values of the node in the linked list. b. Print the values in the linked list in reverse order. Use a temporary pointer temp for a and b. i dont need a full code just the list partarrow_forwardWrite a function mid to keep elements of an integer list between 5 and 10. One function only.mid [12,3,6,4,5,1] → [6,5]arrow_forward
- Write a function set_elements() that assigns 1 to the last and fifth elements of a list parameter. Ex: If the input is: One man's trash is another man's treasure then the output is: ['One', "man's", 'trash', 'is', 1, "man's", 1] ''' Your code goes here ''' vals_to_update = input().split() set_elements(vals_to_update) print(vals_to_update)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_forwardMax Absolute In List Function Lab Description Implement function max_abs_val(lst), which returns the maximum absolutevalue of the elements in list.For example, given a list lst: [-19, -3, 20, -1, 0, -25], the functionshould return 25. The name of the method should be max_abs_val and the method should take one parameter which is the list of values to test. Here is an example call to the function print(max_abs_val([-19, -3, 20, -1, 0, -25])) File Name maxabsinlst.py Score There are three tests each worth 2 points Note: You do not need any other code including the main method or any print statements. ONLY the max_abs_val method is required. Otherwise, the autograder will fail and be unable to grade your code. (I.e., do not include the above example in your code.) The above example should be used be test your code but deleted or comment out upon submission. PYTHON LABarrow_forward
- Use the following code which adds and prints elements of a simply linked list, and add a member function that will duplicate nonzero value nodes in a list. Call your function properly in main and print the list again. #include <iostream> using namespace std; struct node { int data; node *next; node(int d,node*n=0) { data=d; next=n; } }; class list { node *head; public: list() { head=0; } void print() { for (node *t=head;t!=0;t=t->next) cout<<t->data<<" "; cout<<endl; } void add(int el) { head=new node(el,head); } }; void main() { list l; l.add(0); l.add(7); l.add(1); l.add(0); l.add(3); l.print(); }arrow_forwardC Language In a linear linked list, write a function named changeFirstAndLast that swaps the node at the end of the list and the node at the beginning of the list. The function will take a list as a parameter and return the updated list.arrow_forwardThe following C function takes a single-linked list of integers as a parameter and rearranges the elements of the list. The function is called with the list containing the integers 1, 2, 3, 4, 5, 6, 7 in the given order. What will be the contents of the list after the function completes execution? struct node { int value; struct node *next; }; void rearrange(struct node *list) { struct node *p, * q; int temp; if ((!list) || !list->next) return; p = list; q = list->next; while(q) { temp = p->value; p->value = q->value; q->value = temp; p = q->next; q = p?p->next:0; } } a) 1, 2, 3, 4, 5, 6, 7b) 2, 1, 4, 3, 6, 5, 7c) 1, 3, 2, 5, 4, 7, 6d) 2, 3, 4, 5, 6, 7, 1arrow_forward
- JAVA CODE PLEASE Linked List Practice l by CodeChum Admin Write a function printNodes that takes in the head of a linked list and prints all the values of that linked list using a while loop. Print the values separated by a [space]->[space] In the main function, write a program that asks the user to input five integers and assign these values to the nodes then print the five nodes using the printNodes function. An initial code is provided for you. Just fill in the blanks. Input 1. One line containing an integer 2. One line containing an integer 3. One line containing an integer 4. One line containing an integer 5. One line containing an integer Output Enter·number·1:·1 Enter·number·2:·2 Enter·number·3:·3 Enter·number·4:·4 Enter·number·5:·5 1·->·2·->·3·->·4·->·5arrow_forwardGiven two lists(List1, List2) of INTs create a third list such that should contain only odd numbers from the first list and even numbers from the second list def mergeList(listOne, listTwo): thirdList = [] #complete the functionarrow_forwardprogram by using C language In the struct structure given below, the data of a student is stored in a singly linear linked list. Write a function named "secondHighest" that takes the starting address of the list as a parameter and returns the student number with the second highest grade? typedef struct node { int OgrNo; int not; struct node *next; };arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning