SlideShare a Scribd company logo
Given operation are: push, pop, top, isEmpty
Lets assume that 'S' is given stack
1. Set num to the second element from the top of the stack, leaving the stack without its top
elements.
setSecondElement(S, num):
if(S.isEmpty())
print "stack is empty"
S.pop()
if(S.isEmpty())
print "stack is empty"
num = S.top()
2. Given an integer n, set the nth element from the top of the stack, leaving the stack unchanged.
setNthWithoutChange(S, num, n):
create tempStack
// popping first n elements from S and pushing into temp stack
for i=1 to n:
if(S.isEmpty()):
print "stack contains less element than n"
break;
tempStack.push(S.pop())
// if we have sufficient elements
if i==n :
num= tempStack.top()
// pop all elements from tempstack and push back into S
while(!tempStack.isEmpty()):
S.push(tempStack.pop())
3. Set num to the bottom element of the stack, leaving the stack empty.
setBottomElementWithChange(S, num):
if(S.isEmpty()):
print "stack is empty"
else:
num = S.pop() // setting first element to num
while(!S.isEmpty())
num = S.pop()
4. Set num to the bottom element of the stack, leaving the stack unchanged.
setBottomElementWithoutChange(S, num):
if(S.isEmpty()):
print "stack is empty"
else:
create a tempStack
while(!S.isEmpty())
tempStack.push(S.pop())
num = tempStack.top()
// pushing back into S
while(!tempStack.isEmpty())
S.push(tempStack.pop())
5. Set num to the nth element from the bottom of the stack leaving the stack unchanged.
if(S.isEmpty()):
print "stack is empty"
else:
create a tempStack
while(!S.isEmpty())
tempStack.push(S.pop())
int count = 0;
while(!tempStack.isEmpty())
temp = tempStack.pop()
count = count + 1
if(count == n)
num = temp
S.push(temp)
if count < n:
print "stack do not have sufficient element"
Solution
Given operation are: push, pop, top, isEmpty
Lets assume that 'S' is given stack
1. Set num to the second element from the top of the stack, leaving the stack without its top
elements.
setSecondElement(S, num):
if(S.isEmpty())
print "stack is empty"
S.pop()
if(S.isEmpty())
print "stack is empty"
num = S.top()
2. Given an integer n, set the nth element from the top of the stack, leaving the stack unchanged.
setNthWithoutChange(S, num, n):
create tempStack
// popping first n elements from S and pushing into temp stack
for i=1 to n:
if(S.isEmpty()):
print "stack contains less element than n"
break;
tempStack.push(S.pop())
// if we have sufficient elements
if i==n :
num= tempStack.top()
// pop all elements from tempstack and push back into S
while(!tempStack.isEmpty()):
S.push(tempStack.pop())
3. Set num to the bottom element of the stack, leaving the stack empty.
setBottomElementWithChange(S, num):
if(S.isEmpty()):
print "stack is empty"
else:
num = S.pop() // setting first element to num
while(!S.isEmpty())
num = S.pop()
4. Set num to the bottom element of the stack, leaving the stack unchanged.
setBottomElementWithoutChange(S, num):
if(S.isEmpty()):
print "stack is empty"
else:
create a tempStack
while(!S.isEmpty())
tempStack.push(S.pop())
num = tempStack.top()
// pushing back into S
while(!tempStack.isEmpty())
S.push(tempStack.pop())
5. Set num to the nth element from the bottom of the stack leaving the stack unchanged.
if(S.isEmpty()):
print "stack is empty"
else:
create a tempStack
while(!S.isEmpty())
tempStack.push(S.pop())
int count = 0;
while(!tempStack.isEmpty())
temp = tempStack.pop()
count = count + 1
if(count == n)
num = temp
S.push(temp)
if count < n:
print "stack do not have sufficient element"

More Related Content

Similar to Given operation are push, pop, top, isEmptyLets assume that .pdf

DSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUEDSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUE
swathirajstar
 
I have a stack in Java populated with integers. Im trying to compa.pdf
I have a stack in Java populated with integers. Im trying to compa.pdfI have a stack in Java populated with integers. Im trying to compa.pdf
I have a stack in Java populated with integers. Im trying to compa.pdf
JUSTSTYLISH3B2MOHALI
 
04 stacks
04 stacks04 stacks
04 stacks
Rajan Gautam
 
2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers
kinan keshkeh
 
Stack
StackStack
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
maamir farooq
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
arorastores
 
STACK.docx
STACK.docxSTACK.docx
STACK.docx
VandanaGoyal21
 
Stacks queues
Stacks queuesStacks queues
Stacks queues
Rajendran
 
To insert an element at given position in arrayInsert(stack s1, s.pdf
To insert an element at given position in arrayInsert(stack s1, s.pdfTo insert an element at given position in arrayInsert(stack s1, s.pdf
To insert an element at given position in arrayInsert(stack s1, s.pdf
anuragperipheral
 
Lect 15-16 Zaheer Abbas
Lect 15-16 Zaheer  AbbasLect 15-16 Zaheer  Abbas
Lect 15-16 Zaheer Abbas
Information Technology Center
 
Given the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docxGiven the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docx
shericehewat
 
Algo>Stacks
Algo>StacksAlgo>Stacks
Algo>Stacks
Ain-ul-Moiz Khawaja
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
Stacks
StacksStacks
Stacks
Sujeet Singh
 
Develop a stack in c with dynamic memory allocation. Use the followi.pdf
Develop a stack in c with dynamic memory allocation. Use the followi.pdfDevelop a stack in c with dynamic memory allocation. Use the followi.pdf
Develop a stack in c with dynamic memory allocation. Use the followi.pdf
fckindswear
 
Stacks
StacksStacks
Stacks
Sadaf Ismail
 
Stack push pop
Stack push popStack push pop
Stack push pop
A. S. M. Shafi
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
rupam100
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
tameemyousaf
 

Similar to Given operation are push, pop, top, isEmptyLets assume that .pdf (20)

DSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUEDSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUE
 
I have a stack in Java populated with integers. Im trying to compa.pdf
I have a stack in Java populated with integers. Im trying to compa.pdfI have a stack in Java populated with integers. Im trying to compa.pdf
I have a stack in Java populated with integers. Im trying to compa.pdf
 
04 stacks
04 stacks04 stacks
04 stacks
 
2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers
 
Stack
StackStack
Stack
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
 
STACK.docx
STACK.docxSTACK.docx
STACK.docx
 
Stacks queues
Stacks queuesStacks queues
Stacks queues
 
To insert an element at given position in arrayInsert(stack s1, s.pdf
To insert an element at given position in arrayInsert(stack s1, s.pdfTo insert an element at given position in arrayInsert(stack s1, s.pdf
To insert an element at given position in arrayInsert(stack s1, s.pdf
 
Lect 15-16 Zaheer Abbas
Lect 15-16 Zaheer  AbbasLect 15-16 Zaheer  Abbas
Lect 15-16 Zaheer Abbas
 
Given the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docxGiven the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docx
 
Algo>Stacks
Algo>StacksAlgo>Stacks
Algo>Stacks
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Stacks
StacksStacks
Stacks
 
Develop a stack in c with dynamic memory allocation. Use the followi.pdf
Develop a stack in c with dynamic memory allocation. Use the followi.pdfDevelop a stack in c with dynamic memory allocation. Use the followi.pdf
Develop a stack in c with dynamic memory allocation. Use the followi.pdf
 
Stacks
StacksStacks
Stacks
 
Stack push pop
Stack push popStack push pop
Stack push pop
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 

More from anandanand521251

When ionic compounds dissolve in water, they dissociate into ions..pdf
When ionic compounds dissolve in water, they dissociate into ions..pdfWhen ionic compounds dissolve in water, they dissociate into ions..pdf
When ionic compounds dissolve in water, they dissociate into ions..pdf
anandanand521251
 
Let the discrete random variable X denote the number of times an ev.pdf
 Let the discrete random variable X denote the number of times an ev.pdf Let the discrete random variable X denote the number of times an ev.pdf
Let the discrete random variable X denote the number of times an ev.pdf
anandanand521251
 
The reaction of sodium hydroxide with sulfuric ac.pdf
                     The reaction of sodium hydroxide with sulfuric ac.pdf                     The reaction of sodium hydroxide with sulfuric ac.pdf
The reaction of sodium hydroxide with sulfuric ac.pdf
anandanand521251
 
What is a constructorAns)A constructor is also type of method whi.pdf
What is a constructorAns)A constructor is also type of method whi.pdfWhat is a constructorAns)A constructor is also type of method whi.pdf
What is a constructorAns)A constructor is also type of method whi.pdf
anandanand521251
 
d. The number of moles of O2 would increase. as t.pdf
                     d. The number of moles of O2 would increase. as t.pdf                     d. The number of moles of O2 would increase. as t.pdf
d. The number of moles of O2 would increase. as t.pdf
anandanand521251
 
The main difference between DBA and Data Scientists 1) DBA stands.pdf
The main difference between DBA and Data Scientists 1) DBA stands.pdfThe main difference between DBA and Data Scientists 1) DBA stands.pdf
The main difference between DBA and Data Scientists 1) DBA stands.pdf
anandanand521251
 
The encoding format of the given decimal numbers are as fallowsFor.pdf
The encoding format of the given decimal numbers are as fallowsFor.pdfThe encoding format of the given decimal numbers are as fallowsFor.pdf
The encoding format of the given decimal numbers are as fallowsFor.pdf
anandanand521251
 
Carbon dioxide enters the waters of the ocean by .pdf
                     Carbon dioxide enters the waters of the ocean by .pdf                     Carbon dioxide enters the waters of the ocean by .pdf
Carbon dioxide enters the waters of the ocean by .pdf
anandanand521251
 
In computer security, a vulnerability is a weakness which allows an .pdf
In computer security, a vulnerability is a weakness which allows an .pdfIn computer security, a vulnerability is a weakness which allows an .pdf
In computer security, a vulnerability is a weakness which allows an .pdf
anandanand521251
 
QuestionEvaluate lim as x approaches infinity 2x-11+2xSolution.pdf
QuestionEvaluate lim as x approaches infinity 2x-11+2xSolution.pdfQuestionEvaluate lim as x approaches infinity 2x-11+2xSolution.pdf
QuestionEvaluate lim as x approaches infinity 2x-11+2xSolution.pdf
anandanand521251
 
Option (E) Pb is the correct answer.This is because lead occurs in.pdf
Option (E) Pb is the correct answer.This is because lead occurs in.pdfOption (E) Pb is the correct answer.This is because lead occurs in.pdf
Option (E) Pb is the correct answer.This is because lead occurs in.pdf
anandanand521251
 
NH4+ is acid as it give a proton tobecome NH3, a baseNO2- is a bas.pdf
NH4+ is acid as it give a proton tobecome NH3, a baseNO2- is a bas.pdfNH4+ is acid as it give a proton tobecome NH3, a baseNO2- is a bas.pdf
NH4+ is acid as it give a proton tobecome NH3, a baseNO2- is a bas.pdf
anandanand521251
 
Jessica and Leah bought identical boxes of stationary. Leah used her.pdf
Jessica and Leah bought identical boxes of stationary. Leah used her.pdfJessica and Leah bought identical boxes of stationary. Leah used her.pdf
Jessica and Leah bought identical boxes of stationary. Leah used her.pdf
anandanand521251
 
ln e^35applying logarithmic property35 ln eln e = 135 1.pdf
ln e^35applying logarithmic property35 ln eln e = 135  1.pdfln e^35applying logarithmic property35 ln eln e = 135  1.pdf
ln e^35applying logarithmic property35 ln eln e = 135 1.pdf
anandanand521251
 
hi,Now its clear,I had gone through all your solution and it is co.pdf
hi,Now its clear,I had gone through all your solution and it is co.pdfhi,Now its clear,I had gone through all your solution and it is co.pdf
hi,Now its clear,I had gone through all your solution and it is co.pdf
anandanand521251
 
falsethey have volume but no shapeSolutionfalsethey have v.pdf
falsethey have volume but no shapeSolutionfalsethey have v.pdffalsethey have volume but no shapeSolutionfalsethey have v.pdf
falsethey have volume but no shapeSolutionfalsethey have v.pdf
anandanand521251
 
Conc. H2SO4, heatSolutionConc. H2SO4, heat.pdf
Conc. H2SO4, heatSolutionConc. H2SO4, heat.pdfConc. H2SO4, heatSolutionConc. H2SO4, heat.pdf
Conc. H2SO4, heatSolutionConc. H2SO4, heat.pdf
anandanand521251
 
C is correct. The Data Link layer is responsible for encapsulation, .pdf
C is correct. The Data Link layer is responsible for encapsulation, .pdfC is correct. The Data Link layer is responsible for encapsulation, .pdf
C is correct. The Data Link layer is responsible for encapsulation, .pdf
anandanand521251
 
AnswerThe hormones and structuresorgansPituitary glandFemal.pdf
AnswerThe hormones and structuresorgansPituitary glandFemal.pdfAnswerThe hormones and structuresorgansPituitary glandFemal.pdf
AnswerThe hormones and structuresorgansPituitary glandFemal.pdf
anandanand521251
 
a) Pronciples that guarantee high cache rateb) Types of cache for.pdf
a) Pronciples that guarantee high cache rateb) Types of cache for.pdfa) Pronciples that guarantee high cache rateb) Types of cache for.pdf
a) Pronciples that guarantee high cache rateb) Types of cache for.pdf
anandanand521251
 

More from anandanand521251 (20)

When ionic compounds dissolve in water, they dissociate into ions..pdf
When ionic compounds dissolve in water, they dissociate into ions..pdfWhen ionic compounds dissolve in water, they dissociate into ions..pdf
When ionic compounds dissolve in water, they dissociate into ions..pdf
 
Let the discrete random variable X denote the number of times an ev.pdf
 Let the discrete random variable X denote the number of times an ev.pdf Let the discrete random variable X denote the number of times an ev.pdf
Let the discrete random variable X denote the number of times an ev.pdf
 
The reaction of sodium hydroxide with sulfuric ac.pdf
                     The reaction of sodium hydroxide with sulfuric ac.pdf                     The reaction of sodium hydroxide with sulfuric ac.pdf
The reaction of sodium hydroxide with sulfuric ac.pdf
 
What is a constructorAns)A constructor is also type of method whi.pdf
What is a constructorAns)A constructor is also type of method whi.pdfWhat is a constructorAns)A constructor is also type of method whi.pdf
What is a constructorAns)A constructor is also type of method whi.pdf
 
d. The number of moles of O2 would increase. as t.pdf
                     d. The number of moles of O2 would increase. as t.pdf                     d. The number of moles of O2 would increase. as t.pdf
d. The number of moles of O2 would increase. as t.pdf
 
The main difference between DBA and Data Scientists 1) DBA stands.pdf
The main difference between DBA and Data Scientists 1) DBA stands.pdfThe main difference between DBA and Data Scientists 1) DBA stands.pdf
The main difference between DBA and Data Scientists 1) DBA stands.pdf
 
The encoding format of the given decimal numbers are as fallowsFor.pdf
The encoding format of the given decimal numbers are as fallowsFor.pdfThe encoding format of the given decimal numbers are as fallowsFor.pdf
The encoding format of the given decimal numbers are as fallowsFor.pdf
 
Carbon dioxide enters the waters of the ocean by .pdf
                     Carbon dioxide enters the waters of the ocean by .pdf                     Carbon dioxide enters the waters of the ocean by .pdf
Carbon dioxide enters the waters of the ocean by .pdf
 
In computer security, a vulnerability is a weakness which allows an .pdf
In computer security, a vulnerability is a weakness which allows an .pdfIn computer security, a vulnerability is a weakness which allows an .pdf
In computer security, a vulnerability is a weakness which allows an .pdf
 
QuestionEvaluate lim as x approaches infinity 2x-11+2xSolution.pdf
QuestionEvaluate lim as x approaches infinity 2x-11+2xSolution.pdfQuestionEvaluate lim as x approaches infinity 2x-11+2xSolution.pdf
QuestionEvaluate lim as x approaches infinity 2x-11+2xSolution.pdf
 
Option (E) Pb is the correct answer.This is because lead occurs in.pdf
Option (E) Pb is the correct answer.This is because lead occurs in.pdfOption (E) Pb is the correct answer.This is because lead occurs in.pdf
Option (E) Pb is the correct answer.This is because lead occurs in.pdf
 
NH4+ is acid as it give a proton tobecome NH3, a baseNO2- is a bas.pdf
NH4+ is acid as it give a proton tobecome NH3, a baseNO2- is a bas.pdfNH4+ is acid as it give a proton tobecome NH3, a baseNO2- is a bas.pdf
NH4+ is acid as it give a proton tobecome NH3, a baseNO2- is a bas.pdf
 
Jessica and Leah bought identical boxes of stationary. Leah used her.pdf
Jessica and Leah bought identical boxes of stationary. Leah used her.pdfJessica and Leah bought identical boxes of stationary. Leah used her.pdf
Jessica and Leah bought identical boxes of stationary. Leah used her.pdf
 
ln e^35applying logarithmic property35 ln eln e = 135 1.pdf
ln e^35applying logarithmic property35 ln eln e = 135  1.pdfln e^35applying logarithmic property35 ln eln e = 135  1.pdf
ln e^35applying logarithmic property35 ln eln e = 135 1.pdf
 
hi,Now its clear,I had gone through all your solution and it is co.pdf
hi,Now its clear,I had gone through all your solution and it is co.pdfhi,Now its clear,I had gone through all your solution and it is co.pdf
hi,Now its clear,I had gone through all your solution and it is co.pdf
 
falsethey have volume but no shapeSolutionfalsethey have v.pdf
falsethey have volume but no shapeSolutionfalsethey have v.pdffalsethey have volume but no shapeSolutionfalsethey have v.pdf
falsethey have volume but no shapeSolutionfalsethey have v.pdf
 
Conc. H2SO4, heatSolutionConc. H2SO4, heat.pdf
Conc. H2SO4, heatSolutionConc. H2SO4, heat.pdfConc. H2SO4, heatSolutionConc. H2SO4, heat.pdf
Conc. H2SO4, heatSolutionConc. H2SO4, heat.pdf
 
C is correct. The Data Link layer is responsible for encapsulation, .pdf
C is correct. The Data Link layer is responsible for encapsulation, .pdfC is correct. The Data Link layer is responsible for encapsulation, .pdf
C is correct. The Data Link layer is responsible for encapsulation, .pdf
 
AnswerThe hormones and structuresorgansPituitary glandFemal.pdf
AnswerThe hormones and structuresorgansPituitary glandFemal.pdfAnswerThe hormones and structuresorgansPituitary glandFemal.pdf
AnswerThe hormones and structuresorgansPituitary glandFemal.pdf
 
a) Pronciples that guarantee high cache rateb) Types of cache for.pdf
a) Pronciples that guarantee high cache rateb) Types of cache for.pdfa) Pronciples that guarantee high cache rateb) Types of cache for.pdf
a) Pronciples that guarantee high cache rateb) Types of cache for.pdf
 

Recently uploaded

Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 

Recently uploaded (20)

Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 

Given operation are push, pop, top, isEmptyLets assume that .pdf

  • 1. Given operation are: push, pop, top, isEmpty Lets assume that 'S' is given stack 1. Set num to the second element from the top of the stack, leaving the stack without its top elements. setSecondElement(S, num): if(S.isEmpty()) print "stack is empty" S.pop() if(S.isEmpty()) print "stack is empty" num = S.top() 2. Given an integer n, set the nth element from the top of the stack, leaving the stack unchanged. setNthWithoutChange(S, num, n): create tempStack // popping first n elements from S and pushing into temp stack for i=1 to n: if(S.isEmpty()): print "stack contains less element than n" break; tempStack.push(S.pop()) // if we have sufficient elements if i==n : num= tempStack.top() // pop all elements from tempstack and push back into S while(!tempStack.isEmpty()): S.push(tempStack.pop()) 3. Set num to the bottom element of the stack, leaving the stack empty. setBottomElementWithChange(S, num): if(S.isEmpty()): print "stack is empty" else: num = S.pop() // setting first element to num
  • 2. while(!S.isEmpty()) num = S.pop() 4. Set num to the bottom element of the stack, leaving the stack unchanged. setBottomElementWithoutChange(S, num): if(S.isEmpty()): print "stack is empty" else: create a tempStack while(!S.isEmpty()) tempStack.push(S.pop()) num = tempStack.top() // pushing back into S while(!tempStack.isEmpty()) S.push(tempStack.pop()) 5. Set num to the nth element from the bottom of the stack leaving the stack unchanged. if(S.isEmpty()): print "stack is empty" else: create a tempStack while(!S.isEmpty()) tempStack.push(S.pop()) int count = 0; while(!tempStack.isEmpty()) temp = tempStack.pop() count = count + 1 if(count == n) num = temp S.push(temp) if count < n: print "stack do not have sufficient element" Solution Given operation are: push, pop, top, isEmpty Lets assume that 'S' is given stack
  • 3. 1. Set num to the second element from the top of the stack, leaving the stack without its top elements. setSecondElement(S, num): if(S.isEmpty()) print "stack is empty" S.pop() if(S.isEmpty()) print "stack is empty" num = S.top() 2. Given an integer n, set the nth element from the top of the stack, leaving the stack unchanged. setNthWithoutChange(S, num, n): create tempStack // popping first n elements from S and pushing into temp stack for i=1 to n: if(S.isEmpty()): print "stack contains less element than n" break; tempStack.push(S.pop()) // if we have sufficient elements if i==n : num= tempStack.top() // pop all elements from tempstack and push back into S while(!tempStack.isEmpty()): S.push(tempStack.pop()) 3. Set num to the bottom element of the stack, leaving the stack empty. setBottomElementWithChange(S, num): if(S.isEmpty()): print "stack is empty" else: num = S.pop() // setting first element to num while(!S.isEmpty()) num = S.pop() 4. Set num to the bottom element of the stack, leaving the stack unchanged. setBottomElementWithoutChange(S, num):
  • 4. if(S.isEmpty()): print "stack is empty" else: create a tempStack while(!S.isEmpty()) tempStack.push(S.pop()) num = tempStack.top() // pushing back into S while(!tempStack.isEmpty()) S.push(tempStack.pop()) 5. Set num to the nth element from the bottom of the stack leaving the stack unchanged. if(S.isEmpty()): print "stack is empty" else: create a tempStack while(!S.isEmpty()) tempStack.push(S.pop()) int count = 0; while(!tempStack.isEmpty()) temp = tempStack.pop() count = count + 1 if(count == n) num = temp S.push(temp) if count < n: print "stack do not have sufficient element"