In: Computer Science
*Need to write the pseudocode for this given C++ code*:
public:
slist(): head_(nullptr), size_(0) {}
void prepend(int data)
{
snode* newnode = new
snode(data, head_);
head_ = newnode;
++size_;
}
bool empty() const
{
return size_ == 0;
}
size_t size() const{
return size_;
}
friend std::ostream& operator
<<(std::ostream& os, const slist sli){
if(sli.empty()){
return os << "list empty\n";
}
snode* p =
sli.head_;
while(p!=
nullptr){
os << *p << "\n";
p=p->next_;
}
return os;
}
int get_largest() const
{
snode*p = head_;
int largest =
p->data_;
while(p !=
nullptr){
if (p->data_ > largest)
{
largest = p->data_;
}
p = p->next_;
}
return largest;
}
bool is_greater(int n){
return
is_greater_helper(head_,n);
}
bool is_greater_helper(snode* p ,int n){
if(p->next_ ==
nullptr){return n > p->data_;}
return n >
p->data_ && is_greater_helper(p->next_, n);
}
// Pseudocode to create class snode and slist
class snode
number data_; // number type data field
snode next_; // next_ of type snode pointing to the
next node
//constructor to create a snode with provided
values
function snode(int data, snode next)
data_ = data;
next_ = next;
end snode
end class snode
class slist
snode head_; // head_ of type snode pointing to
node at the start of the slist
number size_ ; // size_ of type number to store the
number of elements of the slist
// constructor to create an empty slist
function slist()
head_ = null; // head_ is null i.e
empty at the start
size_ = 0; // set number of
elements in slist to 0
end slist
function prepend(int data)
snode newnode = snode(data,head_);
// create a newnode of type snode with data data and next as
head_
head_ = newnode; // set head_ to
point to newnode
size_ = size_ + 1; // increment
size
end prepend
function empty()
if(size_ == 0) then // if size of
list = 0 then return true else false
return
true;
else
return
false;
end if
end empty
function size()
return size_; // return the number
of elements in the list
end size
function printList() // print the list
if(empty()) then // if list is
empty print empty list
print("list
empty");
else
snode p = head_;
// set p to start of list i.e head node
while(p != null)
// loop that continues till we read end of list
do
print(p.data_,"\n"); // print the data
p = p.next_; // go to next node
end
while
end printList
function get_largest() // function to get the largest
data of the list
snode p = head_; // set p to start
of the list i.e head
int largest = p.data_ ; // set
largest to data at head
// loop that continues till we read
end of list
while(p != null)
do
if(p.data_ >
largest) then // if data at p > largest, then set largest to
data at p
largest = p.data_;
end if
p = p.next_; //
go to next node
end while
return largest ; // return the
largest data
end get_largest
function is_greater(number n)
return is_greater_helper(head_,
n)
end is_greater
function is_greater_helper(snode node, number
n)
// if p is the last node
if(p.next_ == null) then
if(n >
p.data_) then // return true if n > data at p else false
return true;
else
return false;
end
if
else // if p is not the last node
if((n >
p.data_) and (is_greater_helper(p.next_,n) == true)) // if n >
data at p and n > all the other nodes of list
return true; // then return true else
false
else
return false;
end if;
end if;
end is_greater_helper
end class slist