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 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
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?
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...
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:...
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...
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}
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT