SlideShare a Scribd company logo
1 of 7
1
Stack.java
importjava.util.*;
/**
* A LIFOstack of generic<CODE>Object</CODE>s.
*
* @author JimGlenn
* @version0.1 12/05/2005
*/
publicclassStack
{
/**
* The listthatholdsthe elementsonthisstack. The endof the
* lististhe topof the stack.
*/
private Listelements;
/**
* Createsan emptystack.
*/
publicStack()
{
elements=newArrayList();
}
/**
* Addsan itemto the top of thisstack.
*
* @param toAddthe itemto add
*/
publicvoidpush(ObjecttoAdd)
{
elements.add(toAdd);
}
/**
* Removesthe itematthe top of thisstack.
*
* @return the itemat the top of thisstack
*/
publicObjectpop()
{
returnelements.remove(elements.size() - 1);
}
2
/**
* Returnsthe itemat the top of thisstack. That itemisnot
* removed.
*
* @return the itemat the top of thisstack
*/
publicObjectpeek()
{
returnelements.get(elements.size()- 1);
}
/**
* Returnsthe numberof elementsonthisstack.
*
* @return the size of thisstack
*/
publicintsize()
{
returnelements.size();
}
}
3
Queue.java
importjava.util.*;
/**
* A FIFOqueue of generic<CODE>Object</CODE>s.
*
* @author JimGlenn
* @version0.1 12/05/2005
*/
publicclassQueue
{
/**
* The listthatholdsthe elementsonthisqueue. The frontof the
* lististhe frontof the queue.
*/
private Listelements;
/**
* Createsan emptyqueue.
*/
publicQueue()
{
elements=newLinkedList();
}
/**
* Addsan itemto the endof thisqueue.
*
* @param toAddthe itemto add
*/
publicvoidenqueue(ObjecttoAdd)
{
elements.add(toAdd);
}
/**
* Removesthe itematthe front of thisqueue.
*
* @return the itemat the frontof thisqueue
*/
publicObjectdequeue()
{
returnelements.remove(0);
}
4
/**
* Returnsthe itemat the front of thisqueue. Thatitemisnot
* removed.
*
* @return the itemat the frontof thisqueue
*/
publicObjectfirst()
{
returnelements.get(0);
}
/**
* Returnsthe numberof elementsonthisqueue.
*
* @return the size of thisqueue
*/
publicintsize()
{
returnelements.size();
}
}
5
Postfix.java
importjava.util.*;
importjava.io.*;
/**
* Anarithmeticexpressioninpostfix form.
*
* @author JimGlenn
* @version0.2 12/05/2005
* @version0.1 11/22/2004
*/
publicclassPostfix
{
private Stringexp;
/**
* Createsa postfix expressionfromthe givenprefix expression.
* Thisversiondoesnothandle errorsanddoesnot allow
* integerliterals.
*
* @param infix alegal infix expression
*/
publicPostfix(Stringinfix)
{
exp= "";
// create a queue containingthe tokensinthe infix expression;
// we do thisbecause StringTokenizerhasnoequivalentof peek
Queue queue =newQueue();
StringTokenizertok=newStringTokenizer(infix);
while (tok.hasMoreTokens())
queue.enqueue(tok.nextToken());
// create the operatorstack
Stack stack = newStack();
while (stack.size() +queue.size() >0)
{
if (queue.size()==0)
exp= exp+ stack.pop() +" ";
else if (isIdentifier((String)(queue.first())))
exp= exp+ queue.dequeue()+" ";
else if (queue.first().equals("("))
stack.push(queue.dequeue());
else if (stack.size()==0)
6
stack.push(queue.dequeue());
else if (getPrecedence((String)(queue.first()))
> getPrecedence((String)(stack.peek())))
stack.push(queue.dequeue());
else if (stack.peek().equals("(")
&& queue.first().equals(")"))
{
stack.pop();
queue.dequeue();
}
else
exp= exp+ stack.pop() +" ";
}
}
/**
* Precedence levelsforarithmeticoperatorsandparentheses.
* Levelsare spacedoutto make it easiertoadd operatorswith
* in betweenprecedences.
*/
private staticfinal intADDITIVE= 0;
private staticfinal intMULTIPLICATIVE= 10;
private staticfinal intPARENTHESES= -10;
/**
* Returnsthe precedence of the givenoperator.
*
* @param op an operatoror parenthesis
* @return the precedence of the givenoperator.
*/
private staticintgetPrecedence(Stringop)
{
if (op.equals("+") ||op.equals("-"))
returnADDITIVE;
else if (op.equals("*") ||op.equals("/"))
returnMULTIPLICATIVE;
else if (op.equals("(") ||op.equals(")"))
returnPARENTHESES;
else
throw newIllegalArgumentException();
}
/**
* Determinesif the givenstringisanidentifier.
*
* @param id a string
* @return true iff the givenstringisan identifier
*/
7
private staticbooleanisIdentifier(Stringid)
{
returnCharacter.isJavaIdentifierStart(id.charAt(0));
}
/**
* Returnsa stringrepresentationof thisexpression.
*
* @return a stringrepresentationof thisexpression.
*/
publicStringtoString()
{
returnexp;
}
publicstaticvoidmain(String[] args) throwsIOException
{
BufferedReaderin
= newBufferedReader(newInputStreamReader(System.in));
System.out.println("Enteralegal infix expression:");
System.out.println(newPostfix(in.readLine()));
}
}

More Related Content

What's hot

6 new ES6 features
6 new ES6 features6 new ES6 features
6 new ES6 features
Kyle Dorman
ย 
Contact Book Version 1.0
Contact Book Version 1.0Contact Book Version 1.0
Contact Book Version 1.0
Salman Mushtaq
ย 
Controlfile
ControlfileControlfile
Controlfile
dkeerthan
ย 

What's hot (20)

The Ring programming language version 1.4.1 book - Part 22 of 31
The Ring programming language version 1.4.1 book - Part 22 of 31The Ring programming language version 1.4.1 book - Part 22 of 31
The Ring programming language version 1.4.1 book - Part 22 of 31
ย 
Python Async IO Horizon
Python Async IO HorizonPython Async IO Horizon
Python Async IO Horizon
ย 
A Playful Introduction to Rx
A Playful Introduction to RxA Playful Introduction to Rx
A Playful Introduction to Rx
ย 
The Ring programming language version 1.6 book - Part 81 of 189
The Ring programming language version 1.6 book - Part 81 of 189The Ring programming language version 1.6 book - Part 81 of 189
The Ring programming language version 1.6 book - Part 81 of 189
ย 
A deep dive into PEP-3156 and the new asyncio module
A deep dive into PEP-3156 and the new asyncio moduleA deep dive into PEP-3156 and the new asyncio module
A deep dive into PEP-3156 and the new asyncio module
ย 
The Ring programming language version 1.5.2 book - Part 26 of 181
The Ring programming language version 1.5.2 book - Part 26 of 181The Ring programming language version 1.5.2 book - Part 26 of 181
The Ring programming language version 1.5.2 book - Part 26 of 181
ย 
6 new ES6 features
6 new ES6 features6 new ES6 features
6 new ES6 features
ย 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java Bytecodes
ย 
Operating System Assignment Help
Operating System Assignment HelpOperating System Assignment Help
Operating System Assignment Help
ย 
Rx for Android & iOS by Harin Trivedi
Rx for Android & iOS  by Harin TrivediRx for Android & iOS  by Harin Trivedi
Rx for Android & iOS by Harin Trivedi
ย 
Riga Dev Day 2016 - Having fun with Javassist
Riga Dev Day 2016 - Having fun with JavassistRiga Dev Day 2016 - Having fun with Javassist
Riga Dev Day 2016 - Having fun with Javassist
ย 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
ย 
devday2012
devday2012devday2012
devday2012
ย 
Contact Book Version 1.0
Contact Book Version 1.0Contact Book Version 1.0
Contact Book Version 1.0
ย 
Functional Programming in Scala in a Nutshell: Review of Functional Programmi...
Functional Programming in Scala in a Nutshell: Review of Functional Programmi...Functional Programming in Scala in a Nutshell: Review of Functional Programmi...
Functional Programming in Scala in a Nutshell: Review of Functional Programmi...
ย 
Lambda ะฒั‹ั€ะฐะถะตะฝะธั ะธ Java 8
Lambda ะฒั‹ั€ะฐะถะตะฝะธั ะธ Java 8Lambda ะฒั‹ั€ะฐะถะตะฝะธั ะธ Java 8
Lambda ะฒั‹ั€ะฐะถะตะฝะธั ะธ Java 8
ย 
OTcl and C++ linkages in NS2
OTcl and C++ linkages in NS2OTcl and C++ linkages in NS2
OTcl and C++ linkages in NS2
ย 
Controlfile
ControlfileControlfile
Controlfile
ย 
asyncio internals
asyncio internalsasyncio internals
asyncio internals
ย 
Metaprogramming and Reflection in Common Lisp
Metaprogramming and Reflection in Common LispMetaprogramming and Reflection in Common Lisp
Metaprogramming and Reflection in Common Lisp
ย 

Similar to Posfix

New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxNew folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
curwenmichaela
ย 
A linked stack is implemented using a standard Node class as follows.pdf
A linked stack is implemented using a standard Node class as follows.pdfA linked stack is implemented using a standard Node class as follows.pdf
A linked stack is implemented using a standard Node class as follows.pdf
kisgstin23
ย 
StackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfStackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdf
ARCHANASTOREKOTA
ย 
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
Modifications highlighted in bold lettersDropOutStack.javaim.pdfModifications highlighted in bold lettersDropOutStack.javaim.pdf
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
Lalkamal2
ย 
(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual
Chandrapriya Jayabal
ย 
1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf
saradashata
ย 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
VictorXUQGloverl
ย 
we using java code DynamicArrayjava Replace all .pdf
we using java code   DynamicArrayjava   Replace all .pdfwe using java code   DynamicArrayjava   Replace all .pdf
we using java code DynamicArrayjava Replace all .pdf
gudduraza28
ย 
java write a program to evaluate the postfix expressionthe program.pdf
java write a program to evaluate the postfix expressionthe program.pdfjava write a program to evaluate the postfix expressionthe program.pdf
java write a program to evaluate the postfix expressionthe program.pdf
arjuntelecom26
ย 
EmptyCollectionException-java -- - Represents the situation in which.docx
EmptyCollectionException-java --  - Represents the situation in which.docxEmptyCollectionException-java --  - Represents the situation in which.docx
EmptyCollectionException-java -- - Represents the situation in which.docx
BlakeSGMHemmingss
ย 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdf
amazing2001
ย 
Please do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdfPlease do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdf
aioils
ย 
please read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdfplease read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdf
aggarwalopticalsco
ย 
AnswerNote Driver class is not given to test the DoubleArraySeq..pdf
AnswerNote Driver class is not given to test the DoubleArraySeq..pdfAnswerNote Driver class is not given to test the DoubleArraySeq..pdf
AnswerNote Driver class is not given to test the DoubleArraySeq..pdf
nipuns1983
ย 
Implement the ADT stack by using an array stack to contain its entri.pdf
Implement the ADT stack by using an array stack to contain its entri.pdfImplement the ADT stack by using an array stack to contain its entri.pdf
Implement the ADT stack by using an array stack to contain its entri.pdf
SIGMATAX1
ย 
@author Derek Harter @cwid 123 45 678 @class .docx
@author Derek Harter  @cwid   123 45 678  @class  .docx@author Derek Harter  @cwid   123 45 678  @class  .docx
@author Derek Harter @cwid 123 45 678 @class .docx
adkinspaige22
ย 
please read below it will tell you what we are using L.pdf
please read below it will tell you what we are using   L.pdfplease read below it will tell you what we are using   L.pdf
please read below it will tell you what we are using L.pdf
ankit11134
ย 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
Conint29
ย 

Similar to Posfix (20)

New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxNew folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
ย 
A linked stack is implemented using a standard Node class as follows.pdf
A linked stack is implemented using a standard Node class as follows.pdfA linked stack is implemented using a standard Node class as follows.pdf
A linked stack is implemented using a standard Node class as follows.pdf
ย 
StackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfStackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdf
ย 
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
Modifications highlighted in bold lettersDropOutStack.javaim.pdfModifications highlighted in bold lettersDropOutStack.javaim.pdf
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
ย 
(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual
ย 
1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf
ย 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
ย 
we using java code DynamicArrayjava Replace all .pdf
we using java code   DynamicArrayjava   Replace all .pdfwe using java code   DynamicArrayjava   Replace all .pdf
we using java code DynamicArrayjava Replace all .pdf
ย 
java write a program to evaluate the postfix expressionthe program.pdf
java write a program to evaluate the postfix expressionthe program.pdfjava write a program to evaluate the postfix expressionthe program.pdf
java write a program to evaluate the postfix expressionthe program.pdf
ย 
Please review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdfPlease review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdf
ย 
EmptyCollectionException-java -- - Represents the situation in which.docx
EmptyCollectionException-java --  - Represents the situation in which.docxEmptyCollectionException-java --  - Represents the situation in which.docx
EmptyCollectionException-java -- - Represents the situation in which.docx
ย 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdf
ย 
Please do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdfPlease do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdf
ย 
please read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdfplease read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdf
ย 
AnswerNote Driver class is not given to test the DoubleArraySeq..pdf
AnswerNote Driver class is not given to test the DoubleArraySeq..pdfAnswerNote Driver class is not given to test the DoubleArraySeq..pdf
AnswerNote Driver class is not given to test the DoubleArraySeq..pdf
ย 
Implement the ADT stack by using an array stack to contain its entri.pdf
Implement the ADT stack by using an array stack to contain its entri.pdfImplement the ADT stack by using an array stack to contain its entri.pdf
Implement the ADT stack by using an array stack to contain its entri.pdf
ย 
Stack Implementation
Stack ImplementationStack Implementation
Stack Implementation
ย 
@author Derek Harter @cwid 123 45 678 @class .docx
@author Derek Harter  @cwid   123 45 678  @class  .docx@author Derek Harter  @cwid   123 45 678  @class  .docx
@author Derek Harter @cwid 123 45 678 @class .docx
ย 
please read below it will tell you what we are using L.pdf
please read below it will tell you what we are using   L.pdfplease read below it will tell you what we are using   L.pdf
please read below it will tell you what we are using L.pdf
ย 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
ย 

More from Fajar Baskoro

Membangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan AppsheetMembangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan Appsheet
Fajar Baskoro
ย 

More from Fajar Baskoro (20)

Generasi Terampil Digital Skill-2023.pptx
Generasi Terampil Digital Skill-2023.pptxGenerasi Terampil Digital Skill-2023.pptx
Generasi Terampil Digital Skill-2023.pptx
ย 
Cara Membuat Kursus Online Wordpress-tutorstarter
Cara Membuat Kursus Online Wordpress-tutorstarterCara Membuat Kursus Online Wordpress-tutorstarter
Cara Membuat Kursus Online Wordpress-tutorstarter
ย 
PPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
PPT-Kick Off Double Track 2024 melaksanakan Festival RamadhanPPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
PPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
ย 
Buku Inovasi 2023 - 2024 konsep capaian KUS
Buku Inovasi 2023 - 2024 konsep capaian  KUSBuku Inovasi 2023 - 2024 konsep capaian  KUS
Buku Inovasi 2023 - 2024 konsep capaian KUS
ย 
Pemaparan Sosialisasi Program Dual Track 2024.pptx
Pemaparan Sosialisasi Program Dual Track 2024.pptxPemaparan Sosialisasi Program Dual Track 2024.pptx
Pemaparan Sosialisasi Program Dual Track 2024.pptx
ย 
Executive Millennial Entrepreneur Award 2023-1a-1.pdf
Executive Millennial Entrepreneur Award  2023-1a-1.pdfExecutive Millennial Entrepreneur Award  2023-1a-1.pdf
Executive Millennial Entrepreneur Award 2023-1a-1.pdf
ย 
1-Executive Millennial Entrepreneur Award 2023-1-cetak.pptx
1-Executive Millennial Entrepreneur Award  2023-1-cetak.pptx1-Executive Millennial Entrepreneur Award  2023-1-cetak.pptx
1-Executive Millennial Entrepreneur Award 2023-1-cetak.pptx
ย 
Executive Millennial Entrepreneur Award 2023-1.pptx
Executive Millennial Entrepreneur Award  2023-1.pptxExecutive Millennial Entrepreneur Award  2023-1.pptx
Executive Millennial Entrepreneur Award 2023-1.pptx
ย 
Pemrograman Mobile - JetPack Compose1.pptx
Pemrograman Mobile - JetPack Compose1.pptxPemrograman Mobile - JetPack Compose1.pptx
Pemrograman Mobile - JetPack Compose1.pptx
ย 
Evaluasi KPP Program Dual Track Provinsi Kaltim
Evaluasi KPP Program Dual Track Provinsi KaltimEvaluasi KPP Program Dual Track Provinsi Kaltim
Evaluasi KPP Program Dual Track Provinsi Kaltim
ย 
foto tenda digital skill program dari sekolah
foto tenda digital skill program dari sekolahfoto tenda digital skill program dari sekolah
foto tenda digital skill program dari sekolah
ย 
Meraih Peluang di Gig Economy yang cocok bagi remaja
Meraih Peluang di Gig Economy yang cocok bagi remajaMeraih Peluang di Gig Economy yang cocok bagi remaja
Meraih Peluang di Gig Economy yang cocok bagi remaja
ย 
Membangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan AppsheetMembangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan Appsheet
ย 
epl1.pdf
epl1.pdfepl1.pdf
epl1.pdf
ย 
user.docx
user.docxuser.docx
user.docx
ย 
Dtmart.pptx
Dtmart.pptxDtmart.pptx
Dtmart.pptx
ย 
DualTrack-2023.pptx
DualTrack-2023.pptxDualTrack-2023.pptx
DualTrack-2023.pptx
ย 
BADGE.pptx
BADGE.pptxBADGE.pptx
BADGE.pptx
ย 
womenatwork.pdf
womenatwork.pdfwomenatwork.pdf
womenatwork.pdf
ย 
Transition education to employment.pdf
Transition education to employment.pdfTransition education to employment.pdf
Transition education to employment.pdf
ย 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
ย 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
ย 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
ย 

Recently uploaded (20)

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
ย 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
ย 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
ย 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
ย 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
ย 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
ย 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
ย 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
ย 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ย 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
ย 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
ย 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
ย 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
ย 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
ย 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
ย 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
ย 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
ย 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
ย 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
ย 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
ย 

Posfix

  • 1. 1 Stack.java importjava.util.*; /** * A LIFOstack of generic<CODE>Object</CODE>s. * * @author JimGlenn * @version0.1 12/05/2005 */ publicclassStack { /** * The listthatholdsthe elementsonthisstack. The endof the * lististhe topof the stack. */ private Listelements; /** * Createsan emptystack. */ publicStack() { elements=newArrayList(); } /** * Addsan itemto the top of thisstack. * * @param toAddthe itemto add */ publicvoidpush(ObjecttoAdd) { elements.add(toAdd); } /** * Removesthe itematthe top of thisstack. * * @return the itemat the top of thisstack */ publicObjectpop() { returnelements.remove(elements.size() - 1); }
  • 2. 2 /** * Returnsthe itemat the top of thisstack. That itemisnot * removed. * * @return the itemat the top of thisstack */ publicObjectpeek() { returnelements.get(elements.size()- 1); } /** * Returnsthe numberof elementsonthisstack. * * @return the size of thisstack */ publicintsize() { returnelements.size(); } }
  • 3. 3 Queue.java importjava.util.*; /** * A FIFOqueue of generic<CODE>Object</CODE>s. * * @author JimGlenn * @version0.1 12/05/2005 */ publicclassQueue { /** * The listthatholdsthe elementsonthisqueue. The frontof the * lististhe frontof the queue. */ private Listelements; /** * Createsan emptyqueue. */ publicQueue() { elements=newLinkedList(); } /** * Addsan itemto the endof thisqueue. * * @param toAddthe itemto add */ publicvoidenqueue(ObjecttoAdd) { elements.add(toAdd); } /** * Removesthe itematthe front of thisqueue. * * @return the itemat the frontof thisqueue */ publicObjectdequeue() { returnelements.remove(0); }
  • 4. 4 /** * Returnsthe itemat the front of thisqueue. Thatitemisnot * removed. * * @return the itemat the frontof thisqueue */ publicObjectfirst() { returnelements.get(0); } /** * Returnsthe numberof elementsonthisqueue. * * @return the size of thisqueue */ publicintsize() { returnelements.size(); } }
  • 5. 5 Postfix.java importjava.util.*; importjava.io.*; /** * Anarithmeticexpressioninpostfix form. * * @author JimGlenn * @version0.2 12/05/2005 * @version0.1 11/22/2004 */ publicclassPostfix { private Stringexp; /** * Createsa postfix expressionfromthe givenprefix expression. * Thisversiondoesnothandle errorsanddoesnot allow * integerliterals. * * @param infix alegal infix expression */ publicPostfix(Stringinfix) { exp= ""; // create a queue containingthe tokensinthe infix expression; // we do thisbecause StringTokenizerhasnoequivalentof peek Queue queue =newQueue(); StringTokenizertok=newStringTokenizer(infix); while (tok.hasMoreTokens()) queue.enqueue(tok.nextToken()); // create the operatorstack Stack stack = newStack(); while (stack.size() +queue.size() >0) { if (queue.size()==0) exp= exp+ stack.pop() +" "; else if (isIdentifier((String)(queue.first()))) exp= exp+ queue.dequeue()+" "; else if (queue.first().equals("(")) stack.push(queue.dequeue()); else if (stack.size()==0)
  • 6. 6 stack.push(queue.dequeue()); else if (getPrecedence((String)(queue.first())) > getPrecedence((String)(stack.peek()))) stack.push(queue.dequeue()); else if (stack.peek().equals("(") && queue.first().equals(")")) { stack.pop(); queue.dequeue(); } else exp= exp+ stack.pop() +" "; } } /** * Precedence levelsforarithmeticoperatorsandparentheses. * Levelsare spacedoutto make it easiertoadd operatorswith * in betweenprecedences. */ private staticfinal intADDITIVE= 0; private staticfinal intMULTIPLICATIVE= 10; private staticfinal intPARENTHESES= -10; /** * Returnsthe precedence of the givenoperator. * * @param op an operatoror parenthesis * @return the precedence of the givenoperator. */ private staticintgetPrecedence(Stringop) { if (op.equals("+") ||op.equals("-")) returnADDITIVE; else if (op.equals("*") ||op.equals("/")) returnMULTIPLICATIVE; else if (op.equals("(") ||op.equals(")")) returnPARENTHESES; else throw newIllegalArgumentException(); } /** * Determinesif the givenstringisanidentifier. * * @param id a string * @return true iff the givenstringisan identifier */
  • 7. 7 private staticbooleanisIdentifier(Stringid) { returnCharacter.isJavaIdentifierStart(id.charAt(0)); } /** * Returnsa stringrepresentationof thisexpression. * * @return a stringrepresentationof thisexpression. */ publicStringtoString() { returnexp; } publicstaticvoidmain(String[] args) throwsIOException { BufferedReaderin = newBufferedReader(newInputStreamReader(System.in)); System.out.println("Enteralegal infix expression:"); System.out.println(newPostfix(in.readLine())); } }