




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An example of how to design and implement queues and lists in java using interfaces and classes. The definition of a queue interface and its implementation classes q1, q2, and q3, as well as a list interface and its implementation class l1. The document also discusses the concept of linked lists and stacks, which are related data structures. The document can be useful for university students taking a data structures course, specifically cs 211.
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





CS 211 Data Structures
public interface Queue { // note that there aren’t even hints of method definitions below, save for the comments!!! void joinQ ( Person p ) ; // the person p gets added to the back of the queue Person leaveQ ( ) ; // to return the person at the front, and then dump them in favour of the next person in line int getLength ( ) ; // to return the number of people in the queue boolean isFull ( ) ; // to return true if there’s no room left in the queue boolean isEmpty ( ) ; // to return true if there’s no-one in the queue } // end interface Queue
CS 211 Data Structures
public class Q1 implements Queue { void joinQ ( Person p ) { return ; } Person leaveQ ( ) { return new Person ( “anonymous” ) ; } int getLength ( ) { return Integer. MAX_VALUE ; } boolean isFull ( ) { return true ; } boolean isEmpty ( ) { return true ; } public Q1 ( int n ) { System.out.println ( “This queue is so efficient it takes up almost no space!!!” ) ; } // constructor public Q1 ( ) { this ( -47 ) ; } // equally silly default constructor } // end interface Queue
public class Q2 implements Queue { private Person [ ] storage ; private int length , front , back , size ; private final int DEFAULT_SIZE = 100 ; void joinQ ( Person p ) { if (! isFull( ) ) { storage [ back++ ] = p ; length++ ; } else System.out.println ( “Sorry, full up” ) ; } // end joinQ method Person leaveQ ( ) { Person temp ; if (! isEmpty( ) ) { temp = storage [ front++ ] ; length-- ; return temp ; } else System.out.println ( “Sorry, queue is empty” ) ; } // end leaveQ method int getLength ( ) { return this.length ; } boolean isFull ( ) { return ( this.back == size ) ; } boolean isEmpty ( ) { return ( length == 0 ) ; } public Q2 ( int n ) { size = n ; storage = new Person [ n ] ; length = 0 ; front = 0 ; back = 0 ; } // hopefully n > 0! public Q2 ( ) { this ( DEFAULT_SIZE ) ; } // or some such default value } // end class Q
CS 211 Data Structures
CS 211 Data Structures
null null joining a linked list leaving a linked list
CS 211 Data Structures
This will produce the postfix version above from the infix flavour term-by-term under the algorithm: (i) output non-operands immediately (ii) rank operands by strength, so + and - are at the bottom, * and / are next, and “(” is the highest (iii) on reading an operand, pop from the stack (into the output) until seeing a weaker one, then push that operand (iv) on reading a “)”, pop everything from the stack until the “(”, which is discarded (popped but not output) (v) if there’s no more input, then pop from the stack until empty (cf 3.6 of the textbook)
CS 211 Data Structures