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
Create a method that takes a HashMap<Integer, Integer> and returns the sum of the keys of...
Create a method that takes a HashMap<Integer, Integer> and returns the sum of the keys of the HashMap.
Write a function that takes a binary search tree as input and produces a linked list...
Write a function that takes a binary search tree as input and produces a linked list of the entries, with the entries sorted (smallest entries at the front of the list and largest entries at the back). *Hint: use in-order traversal.* C++ there is no any structure
1 Write a Java method that takes an integer, n, as input and returns a reference...
1 Write a Java method that takes an integer, n, as input and returns a reference to an array of n random doubles between 100.0 and 200.0. Just write the method. 2. You have a class A: public class A { int i, double d; public A(double d) { this.d=d; this.i=10; } }
Create a C++ integer linked list program that performs the following methods below: Please create these...
Create a C++ integer linked list program that performs the following methods below: Please create these three source files: intList.h, intList.cpp, & intListTest.cpp. Implement recursive routines in the intList class to do the following: Print the list in reverse order Return the value in the middle node Return the average of all the odd values Remove every node containing an odd value Modify main (in IntListTest.cpp) so it does the following: Insert the numbers 1, 3, 4, 6, 7, 10,...
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...
Create a Java method that takes a String as input value and returns the number of...
Create a Java method that takes a String as input value and returns the number of vowels contained in that string.
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?
C++ Only Create a function named PrintStudents, which takes a string input filename and an integer...
C++ Only Create a function named PrintStudents, which takes a string input filename and an integer minimum score value and a string output file name as a parameters. The function will read the student scores and names from the file and output the names of the students with scores greater than or equal to the value given. This function returns the integer number of entries read from the file. If the input file cannot be opened, return -1 and do...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT