Question

Write a c++ routine that returns true if two elements in a singly linked list of...

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 your list is: 1 → 3 → 4 → 14 → 5 and your target is 11, then you will just return false.

For this, you will need to implement the linked list class we discussed in class (for the purpose of this assignment, implementing the constructor, the add function, and the print function, should suffice). You can also instead use the STL list/iterator to implement your function.

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

In case of any queries, please revert back.

I will be implementing the linked list class for complete explanation.

#include <bits/stdc++.h>
#define MAX 10000
using namespace std;
  
//the Node of the Linked List
struct LinkedNode {
int value;
struct LinkedNode* next;
};
  
//Push to the Linked List as per Lifo order
void pushNode(struct LinkedNode** header, int new_value)
{
//we define the new node from the memory and allocate data and next
struct LinkedNode* new_node = (struct LinkedNode*)malloc(sizeof(struct LinkedNode));
new_node->value = new_value;
new_node->next = (*header);
//Move header to next node for continuation of linked list
(*header) = new_node;
}
  
//Checks if the Sum Exists in the list or not
bool pairSumCheck(struct LinkedNode* header, int sumValue)
{ //We define a set.
unordered_set<int> setA;
//Now, we allocate header to the ptr Node
struct LinkedNode* ptr = header;
//Performing the loop for the next nodes
while (ptr != NULL) {
//lets save value for each node
int currentValue = ptr->value;
// if sumValue-currentValue is equal to pointer value ie. last element of setA
// we exit the loop as we have found the sum.
if (setA.find(sumValue - currentValue) != setA.end())
{
cout <<" true "<<currentValue << " " << sumValue - currentValue;
return true;
}
// else we insert he pointer value to the set
setA.insert(ptr->value);
//We move on to the next iteration
ptr = ptr->next;
}
  
return false;
}
  
int main()
{
//Declare the linked List
struct LinkedNode* headerOfList = NULL;
  
// add elements to the list
pushNode(&headerOfList, 1);
pushNode(&headerOfList, 3);
pushNode(&headerOfList, 4);
pushNode(&headerOfList, 14);
pushNode(&headerOfList, 5);
  
/* function to print the result*/
bool result = pairSumCheck(headerOfList, 8);
if (result == false)
cout << "false";
  
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a c++ routine that returns true if two elements in a singly linked list of...
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
  • In the first exercise, you will override the add method in a subclass in order to...

    In the first exercise, you will override the add method in a subclass in order to improve its performance. In the second exercise, you will implement an Iterator for your new linked list class. Exercise 1 The add method of the linked list class discussed during lecture performs in O(N) time. What can be done to improve its performance to O(1)? What are the boundary cases? Define a new class that inherits from the CS20bLinkedList class introduced during the lecture....

  • Implement an application based on a singly linked list that maintains a list of the top...

    Implement an application based on a singly linked list that maintains a list of the top five performers in a video game. An entry on the list consists of a name and a score (you can make this into a blueprint class if you like), and the list must be maintained in descending order of scores. Here is an example of such a list when it only has three elements. ​Spike​120 ​Whiz​105 ​G-man​ 99 Use a class based on singly...

  • 2 Given a pointer to a singly linked list write a routine, which will travel through...

    2 Given a pointer to a singly linked list write a routine, which will travel through t creating a second list by taking every 3nd node from the first list and putting it ir newly created list. (For example: If the original list had 12 nodes it should remai nodes 1, 2, 4, 5, 7, 8, 10 and 11 while the 2nd list would be made up of nodes 3, 6, 12. Briefly explain: A. The advantage of implementing a...

  • how to implement a linked list object using only singly linked list toolkit. Then implement a...

    how to implement a linked list object using only singly linked list toolkit. Then implement a FREQUENCY function to count the ovcurrence of each element in the list. task#1: Add = operator to node: implement the assignment operator for the node such that setting a node = overwrites the value in the node with the value. task#2:Linked List class implement the methods of linked list. insert search and locate remove node* operator [] task#3: Implement the Frequency

  • Implement Singly Linked List detectLoop in Java. It would check whether the linked list contains a...

    Implement Singly Linked List detectLoop in Java. It would check whether the linked list contains a loop, return true if there exist a loop, false if not.

  • 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.)

  • 1. Create a class MLL, a singly linked, non-circular list where each node only has one...

    1. Create a class MLL, a singly linked, non-circular list where each node only has one link next and the list has a head and a tail link (think about implementation of node). The MLL class also implements the Iterable interface. The following should be implemented as well: 2. Add the methods to the MLL class: a. iterator() to implement the Iterable interface. This method returns an instance of MLLI initialized appropriately. b. addFirst( value) that adds a value to...

  • Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for ...

    Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...

  • Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code...

    Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...

  • java singly linked list write a routine, which will travel through the list second and third...

    java singly linked list write a routine, which will travel through the list second and third list by taking every 2nd node from the first list and 3rd node Given a pointer to creating from the first list and putting them into the two newly created lists. (For example: If the original list had 12 nodes it should remain with nodes 1, 4, 7, and 10. The 2nd list would be made up of nodes 2, 5, 8, and 11...

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