SOLVE FOR store_digits GIVEN A CLASS LINK BELOW: def store_digits(n): \"\"\"Stor
ID: 3727028 • Letter: S
Question
SOLVE FOR store_digits GIVEN A CLASS LINK BELOW:
def store_digits(n):
"""Stores the digits of a positive number n in a linked list.
>>> s = store_digits(1)
>>> s
Link(1)
>>> store_digits(2345)
Link(2, Link(3, Link(4, Link(5))))
>>> store_digits(876)
Link(8, Link(7, Link(6)))
"""
class Link:
"""A linked list.
>>> s = Link(1)
>>> s.first
1
>>> s.rest is Link.empty
True
>>> s = Link(2, Link(3, Link(4)))
>>> s.second
3
>>> s.first = 5
>>> s.second = 6
>>> s.rest.rest = Link.empty
>>> s # Returns repr(s)
Link(5, Link(6))
>>> s.rest = Link(7, Link(Link(8, Link(9))))
>>> s
Link(5, Link(7, Link(Link(8, Link(9)))))
>>> print(s) # Prints str(s)
<5 7 <8 9>>
"""
empty = ()
def __init__(self, first, rest=empty):
assert rest is Link.empty or isinstance(rest, Link)
self.first = first
self.rest = rest
@property
def second(self):
return self.rest.first
@second.setter
def second(self, value):
self.rest.first = value
def __repr__(self):
if self.rest:
rest_str = ', ' + repr(self.rest)
else:
rest_str = ''
return 'Link({0}{1})'.format(repr(self.first), rest_str)
def __str__(self):
"""Returns a human-readable string representation of the Link
>>> s = Link(1, Link(2, Link(3, Link(4))))
>>> str(s)
'<1 2 3 4>'
>>> str(Link(1))
'<1>'
>>> str(Link.empty) # empty tuple
'()'
"""
string = '<'
while self.rest is not Link.empty:
string += str(self.first) + ' '
self = self.rest
return string + str(self.first) + '>'
Explanation / Answer
The above class can be explained using the below (Remember the below is not psuedo code or not an algorithm), it is just a explanation
. Get the input <Number>
While (input!=0)
if list empty
make input%10 to head;
else
make input % 10 to head;
make the list head to second element;
input = input/10;
end while;
Display the value between '<' list elements '>'
// please find the program for the same
#include <iostream>
#include <string>
using namespace std;
// Node class
class Node {
int value;
Node* nextNode;
public:
Node() {};
void SetData(int value_in) { value = value_in; };
void SetNext(Node* next_node) { nextNode = next_node; };
int Value() { return value; };
Node* NextNode() { return nextNode; };
};
class List {
Node *head;
public:
List() { head = NULL; };
string display();
void addHead(int data);
};
string List::display() {
Node *tmp = head;
string str="< ";
int result;
while(tmp!=NULL){
str +=" "+std::to_string(tmp->Value());
// cout<<str<<endl;
//cout<<tmp->Value();
tmp = tmp->NextNode();
}
str += " >";
return str;
}
/**
* Adding node to the linked list every time as a first node
*/
void List::addHead(int data) {
Node* newNode = new Node();
newNode->SetNext(NULL);
newNode->SetData(data);
newNode->SetNext(head);
head = newNode;
Node* tmp = head;
}
int main()
{
List list;
int number,r;
string result;
cout<<" Please enter a positive number ";
cin>>number;
if(number > 0){
while(number != 0){
r = number % 10;
list.addHead(r);
number = number/10;
}
}else cout<<" The entered number is not valid ";
result = list.display();
cout<<" the result : "<<result;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.