Question

In: Computer Science

Give the peek algorithm (code) for a Stack that can hold 10 nodes. Include error checking,...

Give the peek algorithm (code) for a Stack that can hold 10 nodes. Include error checking, and assume that the array theData and the integer top are data members of the class Stack.

Give the algorithm for the structure Queue that keeps the front and rear “pointers” from progressing through all of memory (assume the Queue array has n elements).

Solutions

Expert Solution

Give the peek algorithm (code) for a Stack that can hold 10 nodes. Include error
checking, and assume that the array -theData- and the integer -top- are data members of
the class -Stack.

Algorithm peek(Stack stk)
//Input: Stack stk which is an array of 10 nodes, and the top pointing to top element of the stk.
//Output: The top element of the stack, if it exists. null otherwise.
//Given the Stack stk, will check if there are elements in the stack, and if there are....
//Will return the top element of the stack, and if there are not.... will return null.
//Note that this algorithm doesn't modify the Stack by any means....
   if(stk.top == -1)   //This is how an empty stack in the array representation is used.
       return NULL;
   else
       return stk.theData[top]  

=================================================================================

Give the algorithm for the structure -Queue- that keeps the front and rear "pointers"
from progressing through all of memory (assume the Queue array has n elements).

Algorithm isFull(Queue que)
//Input: Queue que which is an array of n elements, and rear pointing to the recently inserted
//       element in the array, and front pointing to the first element in the Queue.
//Output: Returns true if que is full, and false otherwise.
//Given the queue, if it is full, returns true, and false otherwise. This helps in
//keeping the rear from going beyond the allocation.
   if(rear == n)
       return true;
   else
       return false;  
Algorithm isEmpty(Queue que)
//Input: Queue que which is an array of n elements, and rear pointing to the recently inserted
//       element in the array, and front pointing to the first element in the Queue.
//Output: Returns true if que is empty, and false otherwise.
//Given the queue, if it is empty, returns true, and false otherwise. This helps in
//keeping the front from going beyond the allocation.
   if(rear == front)
       return true;
   else
       return false;

Thank you.


Related Solutions

C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
Develop and pseudocode for the code below: #include <algorithm> #include <iostream> #include <time.h> #include "CSVparser.hpp" using...
Develop and pseudocode for the code below: #include <algorithm> #include <iostream> #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() { amount = 0.0; } }; //============================================================================ // Static methods used for testing //============================================================================ /** * Display the bid information...
In simple Java language algorithm: Implement a static stack class of char. Your class should include...
In simple Java language algorithm: Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Using the stack class you created in problem 1), write a static method called parse that parses...
Need C++ code to be able to run, keep getting a constant value error #include #include...
Need C++ code to be able to run, keep getting a constant value error #include #include #include #include #include #include using namespace std; using namespace std::chrono; int c; void insertionSort(int* arr, int n) { for (int i = 1;i < n;i++) { int v = arr[i]; int j; for (j = i - 1;j > -1;j--) { c++; if (arr[j] > v) { arr[j + 1] = arr[j]; } else { break; } } arr[j + 1] = v; }...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
fix the code with constant expression error exrpession below in visual studio #include <iostream> #include <cstdlib>...
fix the code with constant expression error exrpession below in visual studio #include <iostream> #include <cstdlib> #include <ctime> void insertion_sort(int array[], int size, int start); void heap_sort(int B[], int n); void build_max_heap(int B[], int n); void max_heapify(int B[], int i, int n); void quick_sort(int B[], int p, int r); int partition(int B[], int p, int r); int main() {    int m = 10, Nf = 20000, Ns = 1000, delta = 1000, A[m][Nf];    for (int i = 0;...
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java...
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java program.
Give an english description of an algorithm which can determine whether or not characters can be...
Give an english description of an algorithm which can determine whether or not characters can be arranged into a palindrome when given an input string of X characters. Then, determine order of growth of this algorithm as a function of X. (These characters can only be any of the twenty six lower case alphabet letters.)
Write the code for postfix expression in C++ using a linked stack that can take numbers...
Write the code for postfix expression in C++ using a linked stack that can take numbers bigger than 9 (any size the user gives) and pushes the final result onto the top of the stack
*Please give the answers in pseudo code 1) Design an algorithm that will receive two integer...
*Please give the answers in pseudo code 1) Design an algorithm that will receive two integer items from a terminal operator, and display to the screen their sum, difference, product and quotient. Note that the quotient calculation (first integer divided by second integer) is only to be performed if the second integer does not equal zero. 2) Design an algorithm that will read two numbers and an integer code from the screen. The value of the integer code should be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT