Consider a queue data structure

Assignment Help Data Structure & Algorithms
Reference no: EM13163613

Consider a queue data structure, where the two operations of interest are enqueue (at the back of the queue) and dequeue (from the front of the queue). A queue is thus a FIFO (first in-first out) structure. Suppose we implement a queue by using two stacks, A and B, which support operations of push and pop, as follows:

enqueue(x): push x onto stack A

dequeue: if B is not empty return x = pop B

if B is empty

repeat until A is empty

x = pop A

push x onto stack B

return x = pop B

a) This implementation seems to correctly emulate a queue. Illustrate (by drawing the two stacks) how it works for the following sequence of enqueue and dequeue operations:

enqueue 5

enqueue 7

dequeue

enqueue 3

enqueue 6

enqueue 9

dequeue

dequeue

dequeue

dequeue

b) The actual cost of a push or a pop operation is1. What is the worst-case single operation possible after a sequence of n enqueue and dequeue operations, and what is its cost?

c) Explain how a simplistic worst-case analysis would lead to the conclusion that this algorithm would be O(n2).

d) Using the accounting method, assign the lowest (integer) amortized cost possible to the enqueue operation and to the dequeue operation so that after any sequence of n1 enqueues and n2 dequeues, n1?n2, the amortized cost is ? the actual cost. Explain why this works.

e) Show that the total amortized cost, which is an upper bound for the actual cost, over a sequence of n operations is O(n).

Reference no: EM13163613

Questions Cloud

With replacement order matters : Given an alphabet of size N=9. Write a c++ program that compares the number of possible sequences of the length L that can be generated inder the following assumptions: With replacement order matters, without replacement order matters, and without..
Write a program to keep track of a cd or dvd collection. : write a program to keep track of a CD or DVD collection. This can only work exclusively with either CDs or DVDs since some of the data is different. The data will be stored ina file. The data from the file will be stored ina text file as records. Eac..
Consider a 2-dimensional mesh : Consider a 2-dimensional mesh with n^2 nodes. The shortest distance that a message travels in the mesh is 1 link; the longest distance is 2(n-1) link
Convert meters to feet and inches. : 1. Write a program that can be used to convert meters to feet and inches. Allow the user to enter a metric meter value in a method .
Consider a queue data structure : Consider a queue data structure, where the two operations of interest are enqueue (at the back of the queue) and dequeue (from the front of the queue). A queue is thus a FIFO (first in-first out) structure. Suppose we implement a queue by using tw..
Find all irreducible polynomials : Find all irreducible polynomials
Consider the predator-prey models : Consider the predator-prey models developed early part of the 20 th  century in which the number of predators and preys may be predicted using the pair of ODEs
Create a crow''s foot erd using a specialization hierarchy : Given the following business scenario, create a Crow's Foot ERD using a specialization hierarchy if appropriate. Tiny Hospital keeps information on patients and hospital rooms
Define a certain reaction has an activation energy : A certain reaction has an activation energy of 60.0 kj/mol and a frequency factor of A= 3.10×1012 M^-1s^-1 . What is the rate constant k , of this reaction at 30.0 Deg celsius?

Reviews

Write a Review

Data Structure & Algorithms Questions & Answers

  Determine algorithm for cs curriculum consists of n courses

Determine an algorithm which works directly with this graph representation, and calculates minimum number of semesters necessary to complete the curriculum.

  Definition of a method isreverse

Provide the definition of a method, isReverse , whose two parameters are arrays of integers of equal size. The technique returns true if and only if one array is reverse of the other.

  Design a linked list structure

Design a linked list structure Music that contains data fields Name, Artist, Number_of_Songs, and a pointer to the list. Design the structure with three members and fill in data for each member.

  Create greedy algorithm-multiple breakpoint distance problem

Breakpoints between pi and p. Create greedy algorithm for Multiple Breakpoint Distance problem and estimate its approximation ratio.

  Kind of switching to configure switch to use

Your network's traffic load is very high all times, day and night. What kind of switching do you configure switch to use?

  Question about damaged database

Suppose if you were one of the users of a damaged database, discuss how would you be affected by such a failure and what measures could you take to prevent it?

  Recursive tree algorithms

Write a recursive function to determine if a binary tree is a binary search tree.

  Designing an algorithm for task-array of person numbers

You have been allotted task of designing an algorithm for following task. Someone has built the array of person numbers of all n students enrolled in 331 this fall.

  Perform an insertion sort on the file pointed

Using only the local data already supplied in FileSort, perform an insertion sort on the file pointed to by fd. Use lseeks for this; do not try to create any sort of array or list. An array-based version of insertion is supplied for your reference.

  Build a binary search tree

Build a binary search tree using the following set of numbers, preserving the orderin which they are given: 34,26,47,22,28,10,24,38,51,49,37,4,45,60,57,14.

  Explain feasibility analysis for jobs of lrt algorithm

Study feasibility analysis for jobs of LRT algorithm when preemption is allowed. Which scheduling algorithm is best suited for high speed networks and why? Distinguish between static and dynamic systems.

  Computing hash value for message

For a message, he computes the hash value H = (VChar 1 x VChar 2 x VChar 3 ...x VChar N) mod(26).

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd