Ask Question
19 May, 17:30

Let s be a stack. Consider the following program:

s. push (1);

s. push (2);

s. push (3);

s. pop ();

s. push (4);

s. pop ();

s. pop ();

int x = s. peek ();

What is the value of x after the assignment statement?

+4
Answers (1)
  1. 19 May, 19:11
    0
    Hi

    In the first scenario we have

    S = []

    then:

    S=[1],

    S=[2,1]

    S=[3,2,1]

    S=[2,1]

    S=[4,2,1]

    S=[2,1]

    S=[1]

    So when it's call the method peek to S, X will be assigned 1

    Explanation:

    Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO (Last In First Out). There are many real-life examples of a stack. Consider an example of plates stacked over one another in the canteen. (Taken from wikipedia)

    - The method push is to put the element on the top of the stack

    - The method pop is take out the top element of the stack

    - The method peek is to see what is the top element of the stack
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Let s be a stack. Consider the following program: s. push (1); s. push (2); s. push (3); s. pop (); s. push (4); s. pop (); s. pop (); int ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers