Question

In: Computer Science

*Need to write the pseudocode for this given C++ code*: public:     slist(): head_(nullptr), size_(0) {}...

*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);
    }
   

Solutions

Expert Solution

// 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


Related Solutions

write pseudocode for the following problems not c code Pseudocode only Write a C program to...
write pseudocode for the following problems not c code Pseudocode only Write a C program to print all natural numbers from 1 to n. - using while loop Write a C program to print all natural numbers in reverse (from n to 1). - using while loop Write a C program to print all alphabets from a to z. - using while loop Write a C program to print all even numbers between 1 to 100. - using while loop...
Using pseudocode or C++ code, write code to print “small” if the magnitude M of an...
Using pseudocode or C++ code, write code to print “small” if the magnitude M of an earthquake is in the range [0, 3), “medium” if M is in the range [3, 6), “large” if M is in the range [6, 9) and “epic” if M is greater than or equal to 9, where M is input by a user via the keyboard. (in c++)
write the pseudocode and code to solve the following: you are given a list. determine the...
write the pseudocode and code to solve the following: you are given a list. determine the sum of all the elements in the list without using the built in puthon function sum(). Take your code and create your own function and name it my_sum_algo() which will return the sum of numbers PS please write comments in the code for my understanding, and please write jn jn PYTHON 3 languge. Thank you, have a great day !
WRITE THIS JAVA CODE IN PSEUDOCODE!! import java.util.Scanner; public class License { public static void main(String[]...
WRITE THIS JAVA CODE IN PSEUDOCODE!! import java.util.Scanner; public class License { public static void main(String[] args) { char correctAnswers[] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'}; char userAnswers[] = new char[correctAnswers.length]; Scanner scanner = new Scanner(System.in); for (int i = 0; i < userAnswers.length; i++) { String answer = ""; System.out.printf("Question #%d. Enter your answer( A, B, C or D): ", i + 1); do...
Using pseudocode or C++ code, write a function to compute and return the product of two...
Using pseudocode or C++ code, write a function to compute and return the product of two sums. The first is the sum of the elements of the first half of an array A. The second is the sum of the elements of the second half of A. The function receives A and N ≥ 2, the size of A, as parameters. (in c++)
I need this code translated to C code. Thank you. public class FourColorTheorem { public static...
I need this code translated to C code. Thank you. public class FourColorTheorem { public static boolean isPrime(int num) { // Corner case if (num <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < num; i++) if (num % i == 0) return false; return true; } public static void main(String[] args) { int squares[] = new int[100]; for (int i = 1; i < squares.length; i++) squares[i-1] = i * i;...
1. Write pseudocode for the following program. You do not have to write actual C++ code....
1. Write pseudocode for the following program. You do not have to write actual C++ code. Assume you have a text file, that has been encrypted by adding 10 to the ASCII value of each character in the message. Design a decryption program that would reverse this process, and display the original message to the console.to the new array. 2.Write the code for the specified program. Use proper style and naming. Test and upload your code as a CPP file....
Write the following Java code into Pseudocode import java.util.*; public class Main { // Searching module...
Write the following Java code into Pseudocode import java.util.*; public class Main { // Searching module public static void score_search(int s,int score[]) { // Initialise flag as 0 int flag=0; // Looping till the end of the array for(int j=0;j<10;j++) { // If the element is found in the array if(s==score[j]) { // Update flag to 1 flag=1; } } // In case flag is 1 element is found if(flag==1) { System.out.println("golf score found"); } // // In case flag...
Given the below pseudocode, write the proper code that implements it using MARIE's assembly language:               ...
Given the below pseudocode, write the proper code that implements it using MARIE's assembly language:                    Input a number and store it in X; if X > 1 then    Y := X + X;    X := 0; endif; Y := Y + 1; Output the value of Y; N.B: You should include the MARIE code in your Answer, with an explanation of each instruction in your code beside it. Example:              Subt One         /Subtract 1 from AC Add a...
How do you translate this pseudocode go regular code in C++ int iMin = 0,i =...
How do you translate this pseudocode go regular code in C++ int iMin = 0,i = 0; for(j = 0; j < n - 1; j++) int iMin = j; for(i = j + 1; i < n; i++) if(a[i] < a[iMin]) iMin = i; if(iMin != j) swap(a[i], a[iMin]);
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT