You are on page 1of 3

2008 International Conference on MultiMedia and Information Technology

The Ring Data Structure Implemented With Java and Its Application
Jianbing Lin
Department of Electronic Information Putian University Putian,Chian liniss@126.com
AbstractThis paper first introduces the ring data structure and its main characteristics such as capacity,head and tail, then anylizes how to implement the new data structure and the key operations of isFull and isEmpty with java programming.The paper lists out further the most important codes of the main algorithms of the ring data structure,finally imploys the ring data structure to a appliction of words counting and shows the result. Keywords- Ring;Data Structure;Word Counting; JAVA

Jinan Zou
Department of Electronic Information Putian University Putian,Chian zoujinan@163.com next character will move along to the head position,the next next character will move along to the next position and so on,thus the character to be taken out will always locate on the head position,the character to be store will always locate on the tail position.In the beginning,the ring data structures capacity is be defined,the head and tail index is set to zero at first,so the ring data structure has nothing,it can be used to store characters now. a b head tail c d
Figure 1. The Sketch Map Of The Ring Data Structure

I.

INTRODUCTION

Java is one of the most advance foreland and quickly developing technology in computer science at present,many applications based on java are found in many fields such as office,entertainment,communicatin and education.As a core concept of computer software,data structure mainly studies the logic relation of all kinds of data,the way of data storage and the operation of data.Data structure is one of most important of computer programming design,constructing a suitable data structure has a urgent need for many applications.Data structure implemented with java language has many advantages includes simple implementation,easily portable in many hardware plotforms and operation systems.Thus more and more people start to study data structure with java. II. A THE RING DATA STRUCTURE The Characteries of Ring Data Structure There are many popular data structures such as stack,link list,tree and graph in classic data structure textbook.This paper bases on the advantages of javas full object-oriented characteries,constructs the ring data structure.The ring data structure is a kind of data structure some likes stack which has the First in,First outcharacteristic,the ring data structure is a closed loop,it can be used to store characters or strings.Figure 1 is the ring data structure sketch map,it has a index named as head which demonstrates the start position,it also has a index named as tail which shows the first free place that character can be placed.Character can be taken out from the start position that head shows and stored on the position that tail shows.When character taken out,the ring data structures length decrease one byte,on the other hand,when character stored into the ring data structure,its length increase one byte.The ring data structure has a max capacity,when its capacity is not full,the free place can be used to store the new character;when the capacity is full,the new storing character will overlay in turn the existing characters,this is difference obvious with stack.Once the character in head position taken out,the
978-0-7695-3556-2/08 $25.00 2008 IEEE DOI 10.1109/MMIT.2008.98

The Characteries of Ring Data Structure When beginning to construct the ring data structure,it is important to define some variables that have directly relating to the structure such as the capacity,the head position and the tail position.In the construction method of the ring data structure,the head,tail,length and size have been initialized,at the same time the character array used to store characters in memory has been initialized also.The following programming codes show the process. public CircleBuf(int s) { head=tail=length=0; size=s; cb=new char[s]; } public CircleBuf() { this(SIZE);} In the contruction method,the head,tail and length variables have all been set to zero,this will make the ring have nothing in it.Then when to store data into the ring,the data will be store in the tail position;when take out data from the ring,the data will be taken out from the head position.

761

After finishing the initialization,the most frequent operation of the ring data structure will be implemented.The two methods of isEmpty and isFull will be implemented at first,the method isEmpty is used to dicide if the ring is empty or have no any character in it;the method isFull is used to decide if the ring is full or have no space left,any character to be store well overwrite the existing one in turn.The next two methods are get and put,the main function of them are store and fetch characters to and from the ring.It is important to decide the capacity of the ring before get or put character.If the storing or fetching character positon exceed the capacity of the ring,the character will be put or gotten according to the exceeding position,thus can lead to a close structure. The following codes illuminates the process of the main function of the ring data structure. Public boolean isEmpty() { return length==0; } public boolean isFull() { return length==size; } public boolean put(char c) {if ( ! isFull() ) { cb[tail++] = c; length++; tail = mod(tail); return true; } return false; } public boolean get(char[] c) { if(!isEmpty() ) { c[0] = cb[head++]; length--; head = mod(head); return true; } return false; } private int mod(int x) { return(x >= size ? x - size : x); }

III.

THE APPLICATION OF THE RING DATA STRUCTURE

An appropriating data structure has much importance in computer programming.Just the same as the other data structure,the ring data structure has its given application field..The paper will show a program that counting the number of the words deploying the ring data structure in the next codes.The program has a method named readInput,which will store the inputing words into the ring,the program has also a method named wordCount,which takes the word out of the ring and counting the word number at the same time.In the readInput,the ring data structures method isFull is called;In the wordCount,the ring data structures method isEmpty is called,this will lead to the counting words program implement simply. The following codes shows the progress.

private static boolean readInput(Cirbuf b) throws IOException { int c; while ( ! b.isFull() ) // while b not full if ( (c = System.in.read()) != -1) b.put((char)c); // deposit into buffer else return false; // input closed return true; // buffer full } private static void wordCount(Cirbuf b) // count number of words { while ( ! b.isEmpty() ) // while buffer not empty { b.get(inc); switch( inc[0] ) // remove one character { case ' ' : case '\r': case '\t': case '\n': // word delimiters if ( word ) wcnt++; // word complete word = false; // word indicator false break; default: word = true; // word indicator true } } }

762

REFERENCES
[1] Paul S.Wang , Java with object-orienter programming, Beijing: Higher Education Press,2004.8. [2] Gen Xiangyi, Zhang Yueping, Java2 utility tutorial, Beijing:Tsinghua University Press, 2004.2 [3] Yan Fei. A Pyramid-like Data Structure Implemented With C++. Computer Knowledge and Technology, 2005.17 [4] Lin Jie.The Multimedia Data Structure.Journal of Shanghai Institute of Technology,2005.2 [5] Pan Mingyong,Lu Zhangping.The Organization of Data Structure During Grid Data Processing].Computer Engineering and Application,2004.6

public static void main(String[] args) throws IOException { Circlebuf bf = new Circlebuf(12); wcnt = 0; word = false; for (;;) if ( readInput(bf) ) // producer wordCount(bf); // consumer else { wordCount(bf); if ( word ) wcnt++; break; } System.out.println ("total " + wcnt + " words"); }

When saving the above program as the WordCounting.java, in the command line after combiling the program,then run the program,when input You are a student ,we can get the following result:total 4 words,it means you have input four words.Figure 2 show the result.

Figure 2. The Result Of The Program

IV.

CONCLUSION

With more and more program language appearing,the form of data structure to be implemented are increasingly various.As one of the main computer programming language,JAVA has the unique advantages of platformindepentent and fully object-oriented,it has the peculiar superiority comparing to other programming language.Of course,the ring data structure can used not only in word counting but also in many other situation sucn as producer and consumer model.In practice,we often modify the ring data structure in order to let it more fit to the specific need according to the appropriating problem.

763

You might also like