Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at position 0, its parent is at 1 and so on.       Example     head refers to 3 -> 2 -> 1 -> 0 -> NULL   positionFromTail = 2       Each of the data values matches its distance from the tail. The value 2 is at the desired position.       Function Description       Complete the getNode function in the editor below.       getNode has the following parameters:   · SinglyLinkedListNode pointer head: refers to the head of the list   · int positionFromTail: the item to retrieve       Returns       · int: the value at the desired position       Input Format       The first line contains an integer t, the number of test cases.   Each test case has the following format:   The first line contains an integer n, the number of elements in the linked list.   The next n lines contains an integer, the data value for an element of the linked list.   The last line contains an integer positionFromTail, the position from the tail to retrieve the   value of.       Constraints       · 1 ≤ t ≤ 10   · 1 ≤ n, m ≤ 1000   · 1 ≤ list1[i] ≤ 1000, where list[i] is the ith element of the linked list.   · 0 ≤ positionFromTail < n       Sample Input       2   1   1   0   3   3   2   1   2       Sample Output       1   3       Explanation       In the first case, there is one element in linked list with a value of 1. The last (only) element contains 1.   In the second case, the list is 3 -> 2 -> 1 -> NULL. The element with position of 2 from tail contains 3.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at position 0, its parent is at 1 and so on.

 

 

 

Example

 

 

head refers to 3 -> 2 -> 1 -> 0 -> NULL

 

positionFromTail = 2

 

 

 

Each of the data values matches its distance from the tail. The value 2 is at the desired position.

 

 

 

Function Description

 

 

 

Complete the getNode function in the editor below.

 

 

 

getNode has the following parameters:

 

· SinglyLinkedListNode pointer head: refers to the head of the list

 

· int positionFromTail: the item to retrieve

 

 

 

Returns

 

 

 

· int: the value at the desired position

 

 

 

Input Format

 

 

 

The first line contains an integer t, the number of test cases.

 

Each test case has the following format:

 

The first line contains an integer n, the number of elements in the linked list.

 

The next n lines contains an integer, the data value for an element of the linked list.

 

The last line contains an integer positionFromTail, the position from the tail to retrieve the

 

value of.

 

 

 

Constraints

 

 

 

· 1 ≤ t ≤ 10

 

· 1 ≤ n, m ≤ 1000

 

· 1 ≤ list1[i] ≤ 1000, where list[i] is the ith element of the linked list.

 

· 0 ≤ positionFromTail < n

 

 

 

Sample Input

 

 

 

2

 

1

 

1

 

0

 

3

 

3

 

2

 

1

 

2

 

 

 

Sample Output

 

 

 

1

 

3

 

 

 

Explanation

 

 

 

In the first case, there is one element in linked list with a value of 1. The last (only) element contains 1.

 

In the second case, the list is 3 -> 2 -> 1 -> NULL. The element with position of 2 from tail contains 3.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY