Question

In: Computer Science

Create a ValueGet() method that takes a linked list as input and an integer index and...

Create a ValueGet() method that takes a linked list as input and an integer index and returns the value stored in the node at that index position.

Sample Input:

01->100->300->214, index = 2

Output: 300

At index 2 the node has a value of 300


give me the full code.

Give the full code with c++

Solutions

Expert Solution

Sample Program:-

#include <iostream>

using namespace std; // Avoid need to use std:: qualification on cout and other things

typedef struct node {
   int value;
   node* next = NULL;
}node;

node *head = NULL;

int ValueGet(node *head, int index) {
   int count = 0; // The value at index 0 is pointed.
   while(head != NULL) { // Loop through all the element of the list.
       if(count == index) { // Check if we are pointing the index that we want to get currently.
           return head->value; // If yes then return the value.
       }
       head = head->next; // If not correct then we start pointing to the next node.
       count++; // Increment the index of node we are currently pointing.
   }
   cout << "The index does not exist." << endl; // If value is not found then tell that index is invalid.
   return 0;
}

int main() {
   node *p1, *p2, *p3, *p4;
   int value;

// Creating the linked list as in example.
   p1 = (node *)malloc(sizeof(node));
   p2 = (node *)malloc(sizeof(node));
   p3 = (node *)malloc(sizeof(node));
   p4 = (node *)malloc(sizeof(node));

// Setting values and pointers in the list.
   head = p1;
   p1->value = 01;
   p1->next = p2;
   p2->value = 100;
   p2->next = p3;
   p3->value = 300;
   p3->next = p4;
   p4->value = 214;
  
   value = ValueGet(head, 2); // Getting the value at index value 2.
   cout << value; // Printing the returned value.
}

Output :-

Hope this helps. If you have any queries or suggestions regarding the answers please leave them in the comments section so I can update and improve the answer. Thank you.


Related Solutions

Please use Python to create a method for a linked list that returns the index of...
Please use Python to create a method for a linked list that returns the index of a lookup value within the linked lust
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
write the method “getMaxValue” that finds and returns the maximum value in an integer linked list....
write the method “getMaxValue” that finds and returns the maximum value in an integer linked list. If the list is empty, then it should return 0. use the provided code below public class Question03 { public class ListNode//public for testing purposes { public int data;//public for testing purposes public ListNode link;//public for testing purposes public ListNode(int aData, ListNode aLink) { data = aData; link = aLink; } } public ListNode head;//public for testing purposes public int getMaxValue() { //----------------------------------------------------------------------------------- //Write...
In python I want to create a function that takes in a linked list. Using recursion...
In python I want to create a function that takes in a linked list. Using recursion only, I want to check if the linked list is sorted. How do i do this?
In python I have a linked list. I want to create one function that takes in...
In python I have a linked list. I want to create one function that takes in one parameter, head. In the function, cur = head and next_cur = head.next. I want to return head and next_cur, except at the end of the function they will return alternating values from head. For example, if the these are the values in the linked list: 2, 3, 5, 7, 11 after the function head should return: 2, 5, 11 and next_cur should return:...
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns...
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns a sorted copy of that ArrayList with no duplicates. Sample Input: {5, 7, 4, 6, 5, 6, 9, 7} Sample Output: {4, 5, 6, 7, 9}
What will be the final linked-list after executing the following method on the given input singly...
What will be the final linked-list after executing the following method on the given input singly linked-list? Consider that the singly linked-list does not have a tail reference. Input: 1->2->3->4->5->6->7->8->null                                                    void method(list){ if(list.head == null) return; Node slow_ref = list.head; Node fast_ref = list.head; Node prevS = null; Node prevF = null; while(fast_ref != null && fast_ref.next != null){ prevS = slow_ref; slow_ref = slow_ref.next; prevF = fast_ref; fast_ref = fast_ref.next.next; } prevS.next = slow_ref.next; prevF.next.next = slow_ref; slow_ref.next...
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string....
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic:...
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic: Ask for a number, add that number to the front of the list, print the list. Repeat until they enter -1 for the number. . Sample Input: 10, 15, 5, 2, 4, -1 Output: 4, 2, 5, 15, 10. Next sort all the numbers using selection sort and display them. Next give the user option to search for a specific number in the list....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT