473,473 Members | 2,155 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Amortize cost analyse

2 New Member
Hi,

I'm really stuck with this analyse of amortize cost exercise :
---------------
Show how to implement a queue with two stacks such that both Enqueue and Dequeue
takes amortized time
O(1)

Can you help me please?

Thanks
Oct 6 '13 #1
2 4058
weaknessforcats
9,208 Recognized Expert Moderator Expert
A stack is a queue. Particularly, it is a LIFO (last-in-first-out) queue.

You PUSH onto the top of the stack. This is the enqueue.

You POP from the top of the stack. This is the dequeue.

You PEEK to view the top of the stack, leaving the element there.

So if you want to calculate A+B you use two stacks. An operand stack for the values A and B and an operator stack for the +.

1) push A onto the operand stack
2) push + onto the operator stack
3) push B onto the operand stack
Then:
4) pop the + from the operator stack.
5) Since + needs two operands, pop two values from the operand stack.
6) add the values
7) push the sum onto the operand stack.
8) at this point the operator stack is empty and the sum of A and B is the top entry in the operand stack.

You can use this scheme with Polish notation also.
Oct 6 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Johan Holst Nielsen | last post by:
Hi, Is there any Python packages to analyse or get some information out of an PDF document... Like where the text are placed - what text are placed - fonts, embedded PDFs/fonts/images etc. ...
2
by: Patrick Fischer | last post by:
Hello Hello I looks for a possibility to analyse html while browsing. Like a DOM Inspector. While the page is loading the analyser check the page, make a DOM tree and I can get the DOM tree. ...
3
by: Phil Endecott | last post by:
Dear PostgreSQL experts, This is with version 7.4.2. My database has grown a bit recently, mostly in number of tables but also their size, and I started to see ANALYSE failing with this...
8
by: novice | last post by:
Hi geeks, Can any body explain me how to analyse int the pollowing code This is the question I was asked in the interview... char *s ={ "hello", "basic", "world", "program"}; char...
0
by: =?ISO-8859-1?Q?Konrad_M=FChler?= | last post by:
Hallo, ich bin auf der Suche nach einem Tool, mit dem ich unter Visual Studio 7 oder 8 eine Performance Analyse auf meinem Code durchführen kann, um zu ermitteln, wo wieviel Zeit verloren geht....
0
by: Petr Jakes | last post by:
On the local radio station here in the Czech they announced simple contest: If listeners will hear Elton John's Sacrifice followed immediately by Madonna's Frozen they have to call to the...
1
by: Naha | last post by:
Hi, I am starting a new project where I have to create a office monitoring system, whereby I need to capture images from a webcam and analyse these images using Java advanced imaging in order to...
4
by: ramyamuthusamy | last post by:
Hi I want to know how to analyse the network traffic using java,,
4
by: finelady | last post by:
Hello, I am new on perl and want to do one script who will ask for the name of the log file to analyse and will give the statictics : 1- the covered period of the log (start-end) by date and hours;...
4
haridhasekar
by: haridhasekar | last post by:
hi all, pls analyse the code .. temme why it is used so ... is there any alternate method to validate whether it is a numeric....if so temme..... function isnumeric(e,obj) { var keynum;...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.