SlideShare a Scribd company logo
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

PPT
Stack in Data Structure
PPTX
Stack,queue and linked list data structure.pptx
PPTX
Lecture 7 & 8: Stack & queue
PPTX
Module 2 ppt.pptx
PPTX
Stack and its operation implemented with array new - Copy.pptx
PPTX
Stack organization
PPT
Stacks
Stack in Data Structure
Stack,queue and linked list data structure.pptx
Lecture 7 & 8: Stack & queue
Module 2 ppt.pptx
Stack and its operation implemented with array new - Copy.pptx
Stack organization
Stacks

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

PPTX
5 data structures-stack
PPTX
dsc complexity about the error handeling in c plus plus.pptx
PPT
Stack data structures with definition and code
PPTX
DS UNIT1_STACKS.pptx
DOCX
Bubble sorting lab manual
5 data structures-stack
dsc complexity about the error handeling in c plus plus.pptx
Stack data structures with definition and code
DS UNIT1_STACKS.pptx
Bubble sorting lab manual
Ad

More from irshadoptical (20)

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

Recently uploaded (20)

PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
GDM (1) (1).pptx small presentation for students
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Cell Types and Its function , kingdom of life
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
01-Introduction-to-Information-Management.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Pharma ospi slides which help in ospi learning
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Supply Chain Operations Speaking Notes -ICLT Program
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Microbial diseases, their pathogenesis and prophylaxis
GDM (1) (1).pptx small presentation for students
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Cell Types and Its function , kingdom of life
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Chinmaya Tiranga quiz Grand Finale.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Module 4: Burden of Disease Tutorial Slides S2 2025
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
01-Introduction-to-Information-Management.pdf
A systematic review of self-coping strategies used by university students to ...
human mycosis Human fungal infections are called human mycosis..pptx
Microbial disease of the cardiovascular and lymphatic systems
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Anesthesia in Laparoscopic Surgery in India
Pharma ospi slides which help in ospi learning

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