文档详情

cc2203数据结构算法tutorial 8教程.pdf

发布:2025-05-10约1万字共5页下载文档
文本预览下载声明

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

显示全部
相似文档