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...
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;...
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
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.)
1. Add code to the constructor which instantiates a testArray that will hold 10 integers. 2....
1. Add code to the constructor which instantiates a testArray that will hold 10 integers. 2. Add code to the generateRandomArray method which will fill testArray with random integers between 1 and 10 3. Add code to the printArray method which will print each value in testArray. 4. Write an accessor method, getArray which will return the testArray Here's the code starter code: import java.util.ArrayList; public class TestArrays { private int[] testArray; public TestArrays() { } public void generateRandomArray() {...
can someone tell me why I'm getting the error code on Eclipse IDE: Error: Main method...
can someone tell me why I'm getting the error code on Eclipse IDE: Error: Main method is not static in class StaticInitializationBlock, please define the main method as:    public static void main(String[] args) This is what I'm working on class A { static int i; static { System.out.println(1); i = 100; } } public class StaticInitializationBlock { static { System.out.println(2); } public static void main(String[] args) { System.out.println(3); System.out.println(A.i); } }
How can the Hamming error correcting code be improved? what are the improvements and implications?
How can the Hamming error correcting code be improved? what are the improvements and implications?
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1); sine =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT