Question

Module 4 follow the materials available at Topic - Stacks and Queues. You should have a...

Module 4 follow the materials available at Topic - Stacks and Queues. You should have a good understanding of Lists at  Topic - Basic Data structures as a Queue and a Stack are simply implementation of a List with specific properties.

Assignment - Implement a Stack computer in Javascript (you will turn in a link to your program in JSFiddle). This is a simple computer that keeps a stack, when a number is entered it goes onto the top of the stack. When an operation is entered, the previous 2 numbers are operated on by the operation.

For example:

2 [enter] 2

5 [enter]   5 2

* [enter] * 5 2 -> collapses to 10

would leave at 10 at the top of the stack.

The program should use a simple input box, either a text field or prompt and display the contents of the Stack.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

this.peek-function ) return items[items.length-11 this.isEmpty- function () return items.length0; //Allocating size for arraythis.print -function0 alert (myStack Elements are: titems.toString ()).; //Decalration of the myStack object// var myStack//stack operation// myStack.push(element) myStack.print alert (Great Full!OUTPUT The page at https://fiddle jshell.net says: Stack Elements are Prevent this page from creating additional dialogs. OKhe page at hmps/fiddleishelnet says Login Sign up1 tack Eements are24 OK thot a JSFIDDLE . SFIDDLE The page at hsdl shellnetSFIDOLE The page at https//Sddle jshell.net saysJSFOOLE The page at https//iddlejshellnet says

//Class function decleration//

function myStack()

     {

          var items = [];

          //Push function//

          this.push = function(element){

          items.push(element);

     };

     //pop function//

this.pop = function()

     {

          return items.pop();

     };

this.peek = function()

     {

          return items[items.length-1];

     };

this.isEmpty = function()

     {

          return items.length == 0;

     };

     //Allocating size for array//

this.size = function()

     {

          return items.length;

     };

this.clear = function()

     {

          items = [];

     };

this.print = function()

     {

          alert("myStack Elements are:"+items.toString());

     };

}

//Decalration of the myStack object//

var myStack = new myStack();

//loop started//

while(1)

     {

          var element = prompt("Enter myStack element,(q or Q to exit)", "");

          //Condition//

          if(element == 'q' || element =='Q')

              {

                   break;

              }

          else

              {

              if(element=='*'|| element=='+' || element=='-' || element=='/')

                   {

                        //ensure if myStack is empty

                        if(myStack.isEmpty() || myStack.size<2 )

                             {

                                  alert("Invalid process, myStack is empty() or size is less than 2");

                             }  

                        else

                             {

                                  var op1 = myStack.pop();

                                  var op2 = myStack.pop();

                                  if(element=='*')

              {

                   var res = op1 * op2;

              }

     else if(element=='+')

          {

              var res = op1 + op2;

          }

     else if(element=='-')

          {

              var res = op1 - op2;

          }

     else if(element=='/')

          {

              var res = op1 / op2;

          }

     myStack.push(res);

     myStack.print();

          }   

}

else

     {

          //Stack operation//

          myStack.push(element);

          myStack.print();

     }

}

}  

alert("Great Full!");

Add a comment
Know the answer?
Add Answer to:
Module 4 follow the materials available at Topic - Stacks and Queues. You should have a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Test ADTs: Linked Lists, Stacks and Queues (Python and with proper indentation for better understanding please)....

    Test ADTs: Linked Lists, Stacks and Queues (Python and with proper indentation for better understanding please). Create a program that will choose 30 objects with an attribute of random number between 1 and 100. Place each object it in a queue. From that each object will exit the queue in the order that it entered two waiting stacks. Sort those stacks and create two doubly circular linked lists from those stacks. Merge those lists into one doubly circular linked list....

  • Overview For this assignment, we will practice using stacks and queues. In this exercise, you will...

    Overview For this assignment, we will practice using stacks and queues. In this exercise, you will create two stacks and a queue to keep track of cars using a parking lot. We will use the nature of the stacks and queues to solve the problem. Specifications Rules FIU has opened a new valet parking lot next to the PC building that requires a special decal to park. If the car does not have the correct parking decal, the driver should...

  • Overview For this assignment, we will practice using stacks and queues. In this exercise, you will...

    Overview For this assignment, we will practice using stacks and queues. In this exercise, you will create two stacks and a queue to keep track of cars using a parking lot. We will use the nature of the stacks and queues to solve the problem. Specifications Rules FIU has opened a new valet parking lot next to the PC building that requires a special decal to park. If the car does not have the correct parking decal, the driver should...

  • Page ot 9 2. Stacks/Queues: Write a method splitStack that takes a stack of integers as...

    Page ot 9 2. Stacks/Queues: Write a method splitStack that takes a stack of integers as a parameter and splits it into negatives and non-negatives. The numbers in the stack should be rearranged so that all the negatives appear on the bottom of the stack and all the non-negatives appear on the top. In other words, if after this method is called you were to pop numbers off the stack, you would first get all the nonnegative numbers and then...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • The purpose of this problem is to gain familiarity with stacks and queues. You have three...

    The purpose of this problem is to gain familiarity with stacks and queues. You have three jugs that can hold c1, c2, and c3 liters of water, respectively. Initially, jug 1 is full and the other two jugs are empty. You can repeat the following procedure any number of times: Choose two of the jugs and pour the contents of one into the other until either the first is empty or the second is full. Your goal is to end...

  • C++: Learning Outcomes Implement two stacks and use them to implement an infix to prefix expression...

    C++: Learning Outcomes Implement two stacks and use them to implement an infix to prefix expression convertor Stacks A stack is an abstract data type which uses a sequential container and limits access to that container to one end. You may enter or remove from the container, but only at one end. Using the Linked List data structure from your last homework assignment, implement a Stack of type string. The Stack should only have one data member: the Linked List....

  • Stacks are used by compilers to help in the process of evaluating expressions and generating machine...

    Stacks are used by compilers to help in the process of evaluating expressions and generating machine language code.In this exercise, we investigate how compilers evaluate arithmetic expressions consisting only of constants, operators and parentheses. Humans generally write expressions like 3 + 4and 7 / 9in which the operator (+ or / here) is written between its operands—this is called infix notation. Computers “prefer” postfix notation in which the operator is written to the right of its two operands. The preceding...

  • program should be in c language. thank you for helping me. 1 of 4 CSCI 281...

    program should be in c language. thank you for helping me. 1 of 4 CSCI 281 LAB 5 -STACKS AND A CALCULATOR ASSIGNMENT Using 2 stacks to implement a calculator CALCULATOR 1. User input of two types. Integer values and associated tokens(+ 2. As the user needs to enter the input in the following manner: +5/17-3 9+2 8+7 You should use a looping scanf to allow the user to enter digit, then a char, then a digit, then a car...

  • JAVA, please You must write a robust program meaning that your program should not crash with...

    JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT