SlideShare a Scribd company logo
1 of 6
Download to read offline
In practice, a RAM is used to simulate the operation of a stack by manipulating the address value
in the MAR (memory address register). Here, the data values in the stack do not actually shift
during PUSH and POP operations. Rather, the address of the RAM location corresponding to the
data input and output is changed appropriately. Determine the sequence of operations needed for
PUSH and POP.
Solution
For example, if 1, 2, 3, 4, 5 is a push sequence, 4, 5, 3, 2, 1 is a corresponding pop sequence, but
the sequence 4, 3, 5, 1, 2 is not.
Analysis: An intuitive thought on this problem is to create an auxiliary stack. We push the
numbers in the first sequence one by one, and try to pop them out according to the order in the
second sequence.
Take the sequence 4, 5, 3, 2, 1 as an example to analyze the process to push and pop. The first
number to be popped is 4, so we need to push it into a stack. The pushing order is defined in the
first sequence, where there are numbers 1, 2 and 3 prior to 4. Therefore, numbers 1, 2, and 3 are
pushed into a stack before 4 is pushed. At this time, there are 4 numbers in a stack, which are 1,
2, 3 and 4, with 4 on top. When 4 is popped, numbers 1, 2 and 3 are left. The next number to be
popped is 5, which is not on top of stack, so we have to push numbers in the first sequence into
stack until 5 is pushed. When number 5 is on top of a stack, we can pop it. The next three
numbers to be popped are 3, 2 and 1. Since these numbers are on top of a stack before pop
operations, they can be popped directly. The whole process to push and pop is summarized in
Table 1.
Step
Operation
Stack Status
Popped
Step
Operation
Stack Status
Popped
1
Push 1
1
6
Push 5
1, 2, 3, 5
2
Push 2
1, 2
7
Pop
1, 2, 3
5
3
Push 3
1, 2, 3
8
Pop
1, 2
3
4
Push 4
1, 2, 3, 4
9
Pop
1
2
5
Pop
1, 2, 3
4
10
Pop
1
Table 1: The process to push and pop with a push sequence 1, 2, 3, 4, 5 and pop sequence 4, 5, 3,
2, 1
Let us continue to analyze another pop sequence 4, 3, 5, 1, 2. The process to pop the first number
4 is similar to the process above. After the number 4 is popped, 3 is on the top of stack and it can
be popped. The next number to be popped is 5. Since it is not on top, we have to push numbers in
the first sequence until the number 5 is pushed. The number 5 can be popped when it is pushed
onto the top of a stack. After 5 is popped out, there are only two numbers 1 and 2 left in stack.
The next number to be popped is 1, but it is not on the top of stack. We have to push numbers in
the first sequence until 1 is pushed. However, all numbers in the first sequence have been
pushed. Therefore, the sequence 4, 3, 5, 1, 2 is not a pop sequence of the stack with push
sequence 1, 2, 3, 4, 5. The whole process to push and pop is summarized in Table 2.
Step
Operation
Stack Status
Popped
Step
Operation
Stack Status
Popped
1
Push 1
1
6
Pop
1, 2
3
2
Push 2
1, 2
7
Push 5
1, 2, 5
3
Push 3
1, 2, 3
8
Pop
1, 2
5
4
Push 4
1, 2, 3, 4
The next number to be popped is 1, which is neither on the top of stack, nor in the remaining
numbers of push sequence.
5
Pop
1, 2, 3
4
Table 1: The process to push and pop with a push sequence 1, 2, 3, 4, 5 and pop sequence 4, 3, 5,
1, 2
According to the analysis above, we get a solution to check whether a sequence is a pop
sequence of a stack or not. If the number to be popped is currently on top of stack, just pop it. If
it is not on the top of stack, we have to push remaining numbers into the auxiliary stack until we
meet the number to be popped. If the next number to be popped is not remaining in the push
sequence, it is not a pop sequence of a stack. The following is some sample code based on this
solution:
bool IsPopOrder(const int* pPush, const int* pPop, int nLength)
{
bool bPossible = false;
if(pPush != NULL && pPop != NULL && nLength > 0)
{
const int* pNextPush = pPush;
const int* pNextPop = pPop;
std::stack stackData;
while(pNextPop - pPop < nLength)
{
// When the number to be popped is not on top of stack,
// push some numbers in the push sequence into stack
while(stackData.empty() || stackData.top() != *pNextPop)
{
// If all numbers have been pushed, break
if(pNextPush - pPush == nLength)
break;
stackData.push(*pNextPush);
pNextPush ++;
}
if(stackData.top() != *pNextPop)
break;
stackData.pop();
pNextPop ++;
}
if(stackData.empty() && pNextPop - pPop == nLength)
bPossible = true;
}
return bPossible;
}
Step
Operation
Stack Status
Popped
Step
Operation
Stack Status
Popped
1
Push 1
1
6
Push 5
1, 2, 3, 5
2
Push 2
1, 2
7
Pop
1, 2, 3
5
3
Push 3
1, 2, 3
8
Pop
1, 2
3
4
Push 4
1, 2, 3, 4
9
Pop
1
2
5
Pop
1, 2, 3
4
10
Pop
1

More Related Content

More from irshadoptical

i need help to edit the two methods below so that i can have them wo.pdf
i need help to edit the two methods below so that i can have them wo.pdfi need help to edit the two methods below so that i can have them wo.pdf
i need help to edit the two methods below so that i can have them wo.pdfirshadoptical
 
Help!! Which of the following correctly lists the simple molecules t.pdf
Help!! Which of the following correctly lists the simple molecules t.pdfHelp!! Which of the following correctly lists the simple molecules t.pdf
Help!! Which of the following correctly lists the simple molecules t.pdfirshadoptical
 
How does feasibility differ in an agile environment in comparison to.pdf
How does feasibility differ in an agile environment in comparison to.pdfHow does feasibility differ in an agile environment in comparison to.pdf
How does feasibility differ in an agile environment in comparison to.pdfirshadoptical
 
describe a real-life example of selection against a homozygous reces.pdf
describe a real-life example of selection against a homozygous reces.pdfdescribe a real-life example of selection against a homozygous reces.pdf
describe a real-life example of selection against a homozygous reces.pdfirshadoptical
 
Define a motor unit. What roles do motor units play in graded contrac.pdf
Define a motor unit. What roles do motor units play in graded contrac.pdfDefine a motor unit. What roles do motor units play in graded contrac.pdf
Define a motor unit. What roles do motor units play in graded contrac.pdfirshadoptical
 
An antibody is a protein that has which level of protein structure .pdf
An antibody is a protein that has which level of protein structure  .pdfAn antibody is a protein that has which level of protein structure  .pdf
An antibody is a protein that has which level of protein structure .pdfirshadoptical
 
C++ PROGRAMThis program builds on the code below#include iostr.pdf
C++ PROGRAMThis program builds on the code below#include iostr.pdfC++ PROGRAMThis program builds on the code below#include iostr.pdf
C++ PROGRAMThis program builds on the code below#include iostr.pdfirshadoptical
 
You are in charge of the investigation regarding possible exposur.pdf
You are in charge of the investigation regarding possible exposur.pdfYou are in charge of the investigation regarding possible exposur.pdf
You are in charge of the investigation regarding possible exposur.pdfirshadoptical
 
Which of the following is not a product of the light reactions of ph.pdf
Which of the following is not a product of the light reactions of ph.pdfWhich of the following is not a product of the light reactions of ph.pdf
Which of the following is not a product of the light reactions of ph.pdfirshadoptical
 
Write a line of code to create a Print Writer to write to the file .pdf
Write a line of code to create a Print Writer to write to the file .pdfWrite a line of code to create a Print Writer to write to the file .pdf
Write a line of code to create a Print Writer to write to the file .pdfirshadoptical
 
what is the difference bettween cotranslational import and posttrans.pdf
what is the difference bettween cotranslational import and posttrans.pdfwhat is the difference bettween cotranslational import and posttrans.pdf
what is the difference bettween cotranslational import and posttrans.pdfirshadoptical
 
What is the meaning of critical periods in neonatal development Wha.pdf
What is the meaning of critical periods in neonatal development Wha.pdfWhat is the meaning of critical periods in neonatal development Wha.pdf
What is the meaning of critical periods in neonatal development Wha.pdfirshadoptical
 
What are the major differences between foreign bonds and Eurobonds .pdf
What are the major differences between foreign bonds and Eurobonds .pdfWhat are the major differences between foreign bonds and Eurobonds .pdf
What are the major differences between foreign bonds and Eurobonds .pdfirshadoptical
 
What is meant by ecological fallacySolutionAnswerThe term .pdf
What is meant by ecological fallacySolutionAnswerThe term .pdfWhat is meant by ecological fallacySolutionAnswerThe term .pdf
What is meant by ecological fallacySolutionAnswerThe term .pdfirshadoptical
 
What are the two main branches of Non-Euclidean Geometry What is the.pdf
What are the two main branches of Non-Euclidean Geometry What is the.pdfWhat are the two main branches of Non-Euclidean Geometry What is the.pdf
What are the two main branches of Non-Euclidean Geometry What is the.pdfirshadoptical
 
By what transport method does oxygen enter the blood from the alveol.pdf
By what transport method does oxygen enter the blood from the alveol.pdfBy what transport method does oxygen enter the blood from the alveol.pdf
By what transport method does oxygen enter the blood from the alveol.pdfirshadoptical
 
All of these are examples of evidence used to support the hypothesis.pdf
All of these are examples of evidence used to support the hypothesis.pdfAll of these are examples of evidence used to support the hypothesis.pdf
All of these are examples of evidence used to support the hypothesis.pdfirshadoptical
 
A 28 year-old biologist makes a trip to study life forms in a distan.pdf
A 28 year-old biologist makes a trip to study life forms in a distan.pdfA 28 year-old biologist makes a trip to study life forms in a distan.pdf
A 28 year-old biologist makes a trip to study life forms in a distan.pdfirshadoptical
 
4. Name the following compounds CO2 CH CH3 CH20H CH3COOH 5. Water ha.pdf
4. Name the following compounds CO2 CH CH3 CH20H CH3COOH 5. Water ha.pdf4. Name the following compounds CO2 CH CH3 CH20H CH3COOH 5. Water ha.pdf
4. Name the following compounds CO2 CH CH3 CH20H CH3COOH 5. Water ha.pdfirshadoptical
 
4. When examining whether group differences occur on more than one d.pdf
4. When examining whether group differences occur on more than one d.pdf4. When examining whether group differences occur on more than one d.pdf
4. When examining whether group differences occur on more than one d.pdfirshadoptical
 

More from irshadoptical (20)

i need help to edit the two methods below so that i can have them wo.pdf
i need help to edit the two methods below so that i can have them wo.pdfi need help to edit the two methods below so that i can have them wo.pdf
i need help to edit the two methods below so that i can have them wo.pdf
 
Help!! Which of the following correctly lists the simple molecules t.pdf
Help!! Which of the following correctly lists the simple molecules t.pdfHelp!! Which of the following correctly lists the simple molecules t.pdf
Help!! Which of the following correctly lists the simple molecules t.pdf
 
How does feasibility differ in an agile environment in comparison to.pdf
How does feasibility differ in an agile environment in comparison to.pdfHow does feasibility differ in an agile environment in comparison to.pdf
How does feasibility differ in an agile environment in comparison to.pdf
 
describe a real-life example of selection against a homozygous reces.pdf
describe a real-life example of selection against a homozygous reces.pdfdescribe a real-life example of selection against a homozygous reces.pdf
describe a real-life example of selection against a homozygous reces.pdf
 
Define a motor unit. What roles do motor units play in graded contrac.pdf
Define a motor unit. What roles do motor units play in graded contrac.pdfDefine a motor unit. What roles do motor units play in graded contrac.pdf
Define a motor unit. What roles do motor units play in graded contrac.pdf
 
An antibody is a protein that has which level of protein structure .pdf
An antibody is a protein that has which level of protein structure  .pdfAn antibody is a protein that has which level of protein structure  .pdf
An antibody is a protein that has which level of protein structure .pdf
 
C++ PROGRAMThis program builds on the code below#include iostr.pdf
C++ PROGRAMThis program builds on the code below#include iostr.pdfC++ PROGRAMThis program builds on the code below#include iostr.pdf
C++ PROGRAMThis program builds on the code below#include iostr.pdf
 
You are in charge of the investigation regarding possible exposur.pdf
You are in charge of the investigation regarding possible exposur.pdfYou are in charge of the investigation regarding possible exposur.pdf
You are in charge of the investigation regarding possible exposur.pdf
 
Which of the following is not a product of the light reactions of ph.pdf
Which of the following is not a product of the light reactions of ph.pdfWhich of the following is not a product of the light reactions of ph.pdf
Which of the following is not a product of the light reactions of ph.pdf
 
Write a line of code to create a Print Writer to write to the file .pdf
Write a line of code to create a Print Writer to write to the file .pdfWrite a line of code to create a Print Writer to write to the file .pdf
Write a line of code to create a Print Writer to write to the file .pdf
 
what is the difference bettween cotranslational import and posttrans.pdf
what is the difference bettween cotranslational import and posttrans.pdfwhat is the difference bettween cotranslational import and posttrans.pdf
what is the difference bettween cotranslational import and posttrans.pdf
 
What is the meaning of critical periods in neonatal development Wha.pdf
What is the meaning of critical periods in neonatal development Wha.pdfWhat is the meaning of critical periods in neonatal development Wha.pdf
What is the meaning of critical periods in neonatal development Wha.pdf
 
What are the major differences between foreign bonds and Eurobonds .pdf
What are the major differences between foreign bonds and Eurobonds .pdfWhat are the major differences between foreign bonds and Eurobonds .pdf
What are the major differences between foreign bonds and Eurobonds .pdf
 
What is meant by ecological fallacySolutionAnswerThe term .pdf
What is meant by ecological fallacySolutionAnswerThe term .pdfWhat is meant by ecological fallacySolutionAnswerThe term .pdf
What is meant by ecological fallacySolutionAnswerThe term .pdf
 
What are the two main branches of Non-Euclidean Geometry What is the.pdf
What are the two main branches of Non-Euclidean Geometry What is the.pdfWhat are the two main branches of Non-Euclidean Geometry What is the.pdf
What are the two main branches of Non-Euclidean Geometry What is the.pdf
 
By what transport method does oxygen enter the blood from the alveol.pdf
By what transport method does oxygen enter the blood from the alveol.pdfBy what transport method does oxygen enter the blood from the alveol.pdf
By what transport method does oxygen enter the blood from the alveol.pdf
 
All of these are examples of evidence used to support the hypothesis.pdf
All of these are examples of evidence used to support the hypothesis.pdfAll of these are examples of evidence used to support the hypothesis.pdf
All of these are examples of evidence used to support the hypothesis.pdf
 
A 28 year-old biologist makes a trip to study life forms in a distan.pdf
A 28 year-old biologist makes a trip to study life forms in a distan.pdfA 28 year-old biologist makes a trip to study life forms in a distan.pdf
A 28 year-old biologist makes a trip to study life forms in a distan.pdf
 
4. Name the following compounds CO2 CH CH3 CH20H CH3COOH 5. Water ha.pdf
4. Name the following compounds CO2 CH CH3 CH20H CH3COOH 5. Water ha.pdf4. Name the following compounds CO2 CH CH3 CH20H CH3COOH 5. Water ha.pdf
4. Name the following compounds CO2 CH CH3 CH20H CH3COOH 5. Water ha.pdf
 
4. When examining whether group differences occur on more than one d.pdf
4. When examining whether group differences occur on more than one d.pdf4. When examining whether group differences occur on more than one d.pdf
4. When examining whether group differences occur on more than one d.pdf
 

Recently uploaded

Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsISCOPE Publication
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint23600690
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxCeline George
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽中 央社
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptxPoojaSen20
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxMohamed Rizk Khodair
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesAmanpreetKaur157993
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...Nguyen Thanh Tu Collection
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project researchCaitlinCummins3
 

Recently uploaded (20)

Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS Publications
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 

In practice, a RAM is used to simulate the operation of a stack by m.pdf

  • 1. In practice, a RAM is used to simulate the operation of a stack by manipulating the address value in the MAR (memory address register). Here, the data values in the stack do not actually shift during PUSH and POP operations. Rather, the address of the RAM location corresponding to the data input and output is changed appropriately. Determine the sequence of operations needed for PUSH and POP. Solution For example, if 1, 2, 3, 4, 5 is a push sequence, 4, 5, 3, 2, 1 is a corresponding pop sequence, but the sequence 4, 3, 5, 1, 2 is not. Analysis: An intuitive thought on this problem is to create an auxiliary stack. We push the numbers in the first sequence one by one, and try to pop them out according to the order in the second sequence. Take the sequence 4, 5, 3, 2, 1 as an example to analyze the process to push and pop. The first number to be popped is 4, so we need to push it into a stack. The pushing order is defined in the first sequence, where there are numbers 1, 2 and 3 prior to 4. Therefore, numbers 1, 2, and 3 are pushed into a stack before 4 is pushed. At this time, there are 4 numbers in a stack, which are 1, 2, 3 and 4, with 4 on top. When 4 is popped, numbers 1, 2 and 3 are left. The next number to be popped is 5, which is not on top of stack, so we have to push numbers in the first sequence into stack until 5 is pushed. When number 5 is on top of a stack, we can pop it. The next three numbers to be popped are 3, 2 and 1. Since these numbers are on top of a stack before pop operations, they can be popped directly. The whole process to push and pop is summarized in Table 1. Step Operation Stack Status Popped Step Operation Stack Status Popped 1 Push 1 1 6
  • 2. Push 5 1, 2, 3, 5 2 Push 2 1, 2 7 Pop 1, 2, 3 5 3 Push 3 1, 2, 3 8 Pop 1, 2 3 4 Push 4 1, 2, 3, 4 9 Pop 1 2 5 Pop 1, 2, 3 4 10 Pop 1 Table 1: The process to push and pop with a push sequence 1, 2, 3, 4, 5 and pop sequence 4, 5, 3, 2, 1 Let us continue to analyze another pop sequence 4, 3, 5, 1, 2. The process to pop the first number 4 is similar to the process above. After the number 4 is popped, 3 is on the top of stack and it can be popped. The next number to be popped is 5. Since it is not on top, we have to push numbers in the first sequence until the number 5 is pushed. The number 5 can be popped when it is pushed
  • 3. onto the top of a stack. After 5 is popped out, there are only two numbers 1 and 2 left in stack. The next number to be popped is 1, but it is not on the top of stack. We have to push numbers in the first sequence until 1 is pushed. However, all numbers in the first sequence have been pushed. Therefore, the sequence 4, 3, 5, 1, 2 is not a pop sequence of the stack with push sequence 1, 2, 3, 4, 5. The whole process to push and pop is summarized in Table 2. Step Operation Stack Status Popped Step Operation Stack Status Popped 1 Push 1 1 6 Pop 1, 2 3 2 Push 2 1, 2 7 Push 5 1, 2, 5 3 Push 3 1, 2, 3 8 Pop 1, 2 5 4 Push 4 1, 2, 3, 4
  • 4. The next number to be popped is 1, which is neither on the top of stack, nor in the remaining numbers of push sequence. 5 Pop 1, 2, 3 4 Table 1: The process to push and pop with a push sequence 1, 2, 3, 4, 5 and pop sequence 4, 3, 5, 1, 2 According to the analysis above, we get a solution to check whether a sequence is a pop sequence of a stack or not. If the number to be popped is currently on top of stack, just pop it. If it is not on the top of stack, we have to push remaining numbers into the auxiliary stack until we meet the number to be popped. If the next number to be popped is not remaining in the push sequence, it is not a pop sequence of a stack. The following is some sample code based on this solution: bool IsPopOrder(const int* pPush, const int* pPop, int nLength) { bool bPossible = false; if(pPush != NULL && pPop != NULL && nLength > 0) { const int* pNextPush = pPush; const int* pNextPop = pPop; std::stack stackData; while(pNextPop - pPop < nLength) { // When the number to be popped is not on top of stack, // push some numbers in the push sequence into stack while(stackData.empty() || stackData.top() != *pNextPop) { // If all numbers have been pushed, break if(pNextPush - pPush == nLength) break; stackData.push(*pNextPush); pNextPush ++; } if(stackData.top() != *pNextPop) break;
  • 5. stackData.pop(); pNextPop ++; } if(stackData.empty() && pNextPop - pPop == nLength) bPossible = true; } return bPossible; } Step Operation Stack Status Popped Step Operation Stack Status Popped 1 Push 1 1 6 Push 5 1, 2, 3, 5 2 Push 2 1, 2 7 Pop 1, 2, 3 5 3 Push 3 1, 2, 3 8 Pop 1, 2 3
  • 6. 4 Push 4 1, 2, 3, 4 9 Pop 1 2 5 Pop 1, 2, 3 4 10 Pop 1