In our last class we implemented methods of the Stack and Qu

In our last class, we implemented methods of the Stack and Queue, please look at the implementation of the methods (the implementation is available in the doc sharing) and specify their time complexity

. For Stack class public void push(char c) { } Time Complexity:

public char pop() { } Time Complexity:

public char peek() { } Time Complexity:

public boolean isEmpty() { } Time Complexity:

For Queue Class public void enqueue(char c) { } Time Complexity:

public void dequeue() { } Time Complexity:

public boolean isEmpty() { } Time Complexity:

Solution

An array is a random access data structure, where each element can be accessed directly and in constant time.

stack:

A stack is a recursive data structure. Here is a structural definition of a Stack:

The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.

class public void push(char c) { } Time Complexity:  O(1)

public char pop() { } Time Complexity: O(1)

public char peek() { } Time Complexity: O(1), peek (find-max or find-min), which returns the highest-priority element but does not modify the queue, and nearly always executes in O(1) time.

public boolean is Empty() { } Time Complexity: O(1), All operations except get-size() can be performed in { O(1)} time, get-size() runs in at worst {O(N)}.

Class public void enqueue(char c) { } Time Complexity: O(1)

public void dequeue() { } Time Complexity: O(1)

A stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. In the pushdown stacks only two operations are allowed: push the item into the stack, and pop the item out of the stack. A stack is a limited access data structure - elements can be added and removed from the stack only at the top. push adds an item to the top of the stack, pop removes the item from the top. A helpful analogy is to think of a stack of books; you can remove only the top book, also you can add a new book on the top.

A stack is a recursive data structure. Here is a structural definition of a Stack:

  • a stack is either empty or
    it consistes of a top and the rest which is a stack.
  • Queue: A queue is a container of objects (a linear collection) that are inserted and removed according to the first-in first-out (FIFO) principle. An excellent example of a queue is a line of students in the food court of the UC. New additions to a line made to the back of the queue, while removal (or serving) happens in the front. In the queue only two operations are allowed enqueue and dequeue. Enqueue means to insert an item into the back of the queue, dequeue means removing the front item. The picture demonstrates the FIFO access.
  • The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.

    class public void push(char c) { } Time Complexity:  O(1)

  • public char pop() { } Time Complexity: O(1)

  • public char peek() { } Time Complexity: O(1), peek (find-max or find-min), which returns the highest-priority element but does not modify the queue, and nearly always executes in O(1) time.

  • public boolean is Empty() { } Time Complexity: O(1), All operations except get-size() can be performed in { O(1)} time, get-size() runs in at worst {O(N)}.

  • Class public void enqueue(char c) { } Time Complexity: O(1)

  • public void dequeue() { } Time Complexity: O(1)

In our last class, we implemented methods of the Stack and Queue, please look at the implementation of the methods (the implementation is available in the doc s
In our last class, we implemented methods of the Stack and Queue, please look at the implementation of the methods (the implementation is available in the doc s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site