Codelab question!
This is c++.
Consider the following code: // Linked Lists: TRAVERSE
struct ListNode { int data; struct ListNode *next; };
Assume that a linked list has been created and head points to a sentinel node. A sentinel node is an empty data node in the beginning of the list. It sometimes holds a sentinel value. The use of sentinel nodes is a popular trick to simplify the insert and delete operations.
You may also assume that the list is not empty. Each node in the list contains an integer representing a digit in a number. The data in the sentinel node is -1.
For instance, n = 234 will be stored as {-1, 4, 3, 2} in the linked list.
Write a function based on the list traversal named computeNumFromList that is passed the pointer to the first node in the list, it prints the digits in the list one per line, and it returns a long integer, the number calculated from its digits in the list (234 see the above example). You may assume that long int will hold the converted number (no need to check for numeric overflow).
This is what I got :
long int computeNumFromList(ListNode *root)
{
long int ans = 0;
root = root->next;
long int ten=1;
while(root!=nullptr)
{
ans = ans + ten*root->data;
root = root->next;
ten = ten*10;
}
return ans;
}
Some how codelab still said that I am incorrect. Could someone please help me? :(
From the problem statement:
"..... named computeNumFromList that is passed the pointer to the first node in the list, it prints the digits in the list one per line, and it returns a long integer, .... "
You missed the print statement. Also, it is not clear whether this function is meant to handle negative numbers or not. In case it needs to handle negative numbers, you can add another node containing -1 (besides the sentinel) while creating the list to make it independent, otherwise you would have to check the original number and convert the returned result from the function.
-------------------- Code ------------------------
// This code handles negative numbers as well
#include <iostream>
struct ListNode
{
int data;
struct ListNode *next;
ListNode() : data{0}, next{nullptr}
{}
};
void print_list(ListNode *root)
{
while(root != nullptr)
{
std::cout <<
root->data << "\n";
root =
root->next;
}
}
void process_number(long int number, ListNode *root)
{
if(root != nullptr)
{
if(number < 0)
{
root->next = new ListNode;
root = root->next;
root->data = -1;
number = -number;
}
while(number >
0)
{
root->next = new ListNode;
root = root->next;
root->data = number % 10;
number = number / 10;
}
}
}
long int computeNumFromList(ListNode *root)
{
long int ans = 0;
bool neg = false;
root = root->next;
long int ten=1;
while(root!=nullptr)
{
if(root->data ==
-1)
{
neg = true;
root = root->next;
}
std::cout <<
root->data << "\n";
ans = ans +
ten*root->data;
root =
root->next;
ten = ten*10;
}
return neg ? -ans : ans;
}
int main()
{
int number;
std::cin >> number;
ListNode *root = new ListNode;
root->data = -1;
root->next = nullptr;
process_number(number, root);
std::cout << computeNumFromList(root)
<< "\n";
}
---------------- Output ----------------
Codelab question! This is c++. Consider the following code: // Linked Lists: TRAVERSE struct ListNode {...
A linked list is constructed of nodes described by the following structure: struct node{ char data; struct node *next; }; Assume a linked list containing a sentinel node is constructed from the above nodes. Write a function named "count"-prototyped as int count(struct node*sent)- that accepts a pointer to the sentinel node; counts the number of data (non-sentinel) nodes containing the character 'A'; and returns that count as the function value.
#include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...
I need help completing this with C++ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseList(ListNode* head) { } };
A linked list of integers is built using the following struct: struct node { int data; struct node *next; }; Define a function named max that returns the maximum integer in a list. The function takes one arguments, a pointer to the head of the list. The function returns an integer, which is the maximum value. If the list is empty, return zero. NOTE: You know nothing about the values in the list. They could all be negative!
***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful. Also note that the function must work no matter how many nodes...
In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode { public: T data; ListNode<T>* next; ListNode(T data) { this->data = data;...
This is a code for linked list, it is about adding a node in the middle of a list, I am really confused why int i = 2? can't it be 0? Add to the Middle • Allocate memory and store data for new node Traverse to node just before the required position of new node Change next pointers to include new node in between struct node *newNode; newNode = malloc(sizeof(struct node)); newNode->data = 4; struct node *temp head; for(int...
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...
Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...
Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation of the Table ADT. Make sure that you apply the concepts of design by contract (DbC) to your implementation. Once you have fully implemented the table, create a main.c file that implements a testing framework for your table. Your table implementation must ensure that values inserted are unique, and internally sorted within a linked list. table.h #ifndef _TABLE_H #define _TABLE_H //----------------------------------------------------------------------------- // CONSTANTS AND...