cc2203数据结构算法tutorial 8教程.pdf
CC2203DataStructuresandAlgorithms
Sem2,2011-2012
Tutorial8
Instruction
Inthistutorial,youarerequiredtosubmityourtutorialworkanswerforexercisetome.
Tutorialworkwillnotbegradedunlessyourcourseworkfails(courseworkbelowD
grade).Ifone’scourseworkfails,thattutorialworkwillbegradedtoseeifcondonepass
shouldbeissuedornot.
Exercise1
GiventhefollowingJavacode:
classNode{
Objectelement;//elementinthenode
Nodenext;//nextisthepointerpointingatthenextnode
Node(Objectelement){
this.element=element;
this.next=null;
}
Node(Objectelement,Nodenext){
this.element=element;
this.next=next;
}
}
classLinkedQueue{
Nodefront;
publicbooleanisEmpty(){
returnfrontnull;//returntrueiffqueueisempty
}
publicObjectgetFrontElement(){
if(isEmpty())
returnnull;//returnnullifthequeueisempty
else
returnfront.element;//returntheelementatthequeuefront
}
publicObjectgetRearElement(){
//codetobecompleted.
}
publicvoidput(ObjecttheElement){//inserttheElementatthequeuerear
Noden=newNode(theElement);//createanodefortheElement
Nodep;
if(frontnull)
front=n;//inserttheElementintoemptyqueue
else{
p=front;
while(p.next!=null)
p=p.next;
p.next=n;//inserttheElementatthequeuerea