Question

Simple singly-linked list question: write an iterative function position(p, target) that returns position (1, 2, 3,...

Simple singly-linked list question:

write an iterative function position(p, target) that returns position (1, 2, 3, or .......) of the target in the list to which p points. If target is not on the list the function returns -1.

please write the function in C++ thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code:

#include <stdio.h>

#include <stdlib.h>

struct listNode

{

struct listNode *nxt;

int val;

} * head;

/*

* ───────────────────────────────────────────── RETURNS THE NTH ELEMENT DATA ─────

*/

int position(struct listNode *p, int n)

{

if (p->val == n)

{

return n;

}

return position(p->nxt, n);

return -1;

}

/*

* ───────────────────────────────────────────────────────────── ADD THE DATA ─────

*/

void add(int num)

{

struct listNode *temp;

temp = (struct listNode *)malloc(sizeof(struct listNode));

temp->val = num;

if (head == NULL)

{

head = temp;

head->nxt = NULL;

}

else

{

temp->nxt = head;

head = temp;

}

}

int main()

{

add(1);

add(2);

add(3);

add(4);

add(5);

add(6);

add(7);

add(8);

add(9);

add(10);

int result = position(head, 5);

printf("%d",result);

}

Output:

5

Add a comment
Know the answer?
Add Answer to:
Simple singly-linked list question: write an iterative function position(p, target) that returns position (1, 2, 3,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Given the already build class and struct for singly linked list, write a recursive function...

    1. Given the already build class and struct for singly linked list, write a recursive function that finds the minimum value of Singly Linked List (please make use of a helper function) 2. Given the already built class and struct for singly linked list, write a recursive function that finds the sum of a singly linked list (please make sure of a helper function)

  • Write a method max() which take a generic singly linked list as an argument and returns...

    Write a method max() which take a generic singly linked list as an argument and returns the maximum element reference in the list.      If the list is empty, please throw an empty collection exception.      The method prototype is defined as follows. Please write down your code in the method body enclosed in braces: public static <T extends Comparable<T>> T max(SingleLinkedList<T> list) throws EmptyCollectionException { } 2) What is the big O notation of the max method? a) O(N)...

  • C programming Write an iterative and recursive version of a function to print out a linked...

    C programming Write an iterative and recursive version of a function to print out a linked list from head to tail and then do the same for printing a linked list from tail to head. Assume a singly linked list in all cases and access only to a head pointer at the time of the function call. struct node; typedef struct node Node; struct node int data; Node next;

  • Question 3: Reversing a singly-linked list of integers Suppose that you have a singly-linked list detined...

    Question 3: Reversing a singly-linked list of integers Suppose that you have a singly-linked list detined via the following data type (30) Integet itemt net: et stem in 1ist Complete the following function so that it reverses the list pointed to by the arguwent void reverse linkedlist(list itom tx shead)

  • Write a C# function bool HasCycle(Node head) that detects a cycle in a singly linked list....

    Write a C# function bool HasCycle(Node head) that detects a cycle in a singly linked list. It must return a boolean true if the list contains a cycle, or false otherwise. HasCycle has a reference to a Node object, that points to the head of a linked list, as parameter.

  • Question 2 (3 points) Given the following singly linked list (a list with four nodes), what...

    Question 2 (3 points) Given the following singly linked list (a list with four nodes), what will be the value stored at the last node when the following operations are executed? head->1111-151->11011->1211 Node curr = head; while(curr next !=null) { curr.item = curritem-head. item; curr = cur next; اسرة Node newNode = new Node(8): curr.next = newNode: 23 O 12 OS O21 Question 3 (3 points) Given a recursive method as shown below, what is the return value for P(5)...

  • Answer all questions 1- in circular singly linked list, previous pointer of the first node points...

    Answer all questions 1- in circular singly linked list, previous pointer of the first node points to which node A. First node B. Itself C. null D. Last node 2- Which of the following is NOT an applications of linked lists? A. Implementation of stacks and queues B. Dynamic memory allocation C. Manipulation of polynomials D. Keeping people at home during epidemics like corona virus 3- In a circular singly linked list? A. Components are all linked together in some...

  • Write a Python function to implement the quick sort algorithm over a singly linked list. The...

    Write a Python function to implement the quick sort algorithm over a singly linked list. The input of your function should be a reference pointing to the first node of a linked list, and the output of your function should also be a reference to the first node of a linked list, in which the data have been sorted into the ascending order. (You may use the LinkedQueue class we introduced in the lecture directly in your program.)

  • ngu Cons eY Ja Question 1 a) Write pseudo code to output a singly-linked list in...

    ngu Cons eY Ja Question 1 a) Write pseudo code to output a singly-linked list in reverse order when you are NOT allowed to allocate memory dynamically. What is the running time of the algorithm? b) Write pseudo code to output a singly-linked list in reverse order when you are ALLOWED to allocate memory dynamically. What is the running time of the algorithm? c) You have an increasingly-sorted circular list (using an array) of n elements that is full. The...

  • Given the following linked list structure called node: struct node { int val; struct node *...

    Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT