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
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
Simple singly-linked list question: write an iterative function position(p, target) that returns position (1, 2, 3,...
Write a c++ routine that returns true if two elements in a singly linked list of integers add up to a given target (similar to the two sum problem 1). Your function should return true and print the individual numbers, or return false. Examples: - If your list is: 1 → 3 → 4 → 14 → 5 and your target is 8, then you will return true and print: 3, 5 (since 3 + 5 = 8). - If...
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 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 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;
C++ Write a function that takes in as input a singly linked list that contains the digits to an integer in reverse order. For example: 0 → 0 → 1 = 100 or 3 → 2 → 1 = 123. Return the list as the number. You may assume there will be no leading zeros in the list. For example: 3 → 2 → 0. You may assume there will not be any negative integers.
Write a C function that iterates through a singly linked list and converts it to a doubly linked list. Your solution must NOT create a second list. You must come up with the appropriate parameters and return type. Note: the nodes in the singly linked list have a field for a previous pointer, but that field has not initially been set in any of the nodes. You must do this as part of your solution!
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. 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 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 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...