Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sit in Lab 3 ( Circular Linked List )

Similar presentations


Presentation on theme: "Sit in Lab 3 ( Circular Linked List )"— Presentation transcript:

1 Sit in Lab 3 ( Circular Linked List )
Classroom

2 Quick reminder Open two windows while coding: one for writing code, one for compiling How to test my code? Compile: javac Classroom.java Run code: java Classroom < input1.in > my_output.out Check difference: diff output1.out my_output.out No results given: All is good! View file: cat output1.out

3 Problem Keep track of the positions of students around a round table
Insert student_1 next to the student K places away from student_2 Remove a student from the table List out all the students at the table in clockwise order

4 Input First line- integer N representing number of user operations.
Following N lines- 1. enter STUDENT_1 STUDENT2 k 2. leave STUDENT_NAME 3. list

5 enter Peter Jonas 0 -Insert Peter next to the student 0 places away from Jonas
Jonas will always be at the table

6 enter Peter Jonas 0 -Insert Peter next to the student 0 places away from Jonas
Insertion is done on the left of the student ( clockwise )

7 enter Danny Peter 1 -Insert Danny next to the student 1 place away from Peter
Jonas Peter

8 enter Danny Peter 1 -Insert Danny next to the student 1 place away from Peter
Jonas Danny Peter

9 enter David Jonas 1 -Insert David next to the student 1 place away from Jonas
Danny Peter

10 enter David Jonas 1 -Insert David next to the student 1 place away from Jonas
Danny David Peter

11 leave David -Remove David from the table
Jonas Danny David Peter

12 leave David -Remove David from the table
Jonas Danny Peter

13 enter Jane Peter 2 -Insert Jane next to the student 2 places away from Peter
Jonas Danny Peter

14 enter Jane Peter 2 -Insert Jane next to the student 2 places away from Peter
Jonas Danny Jane Peter

15 Implementation Use a circular linked list to implement the structure.
addStudent (Student_1, Student_2, K) : Insert student_1 next to the student which is K places away from student_2 remove(Student) : Remove the student from the table printList() : Print out all students at the table in a clockwise manner

16 addStudent (Student_1, Student_2, K)
First find the node which contains Student_2 Find the node of the student K places away from Student_2 Add Student_1 after that node S0 S2 S3

17 addStudent (Student_1, Student_2, 2)
First find the node which contains Student_2 Find the node of the student 2 places away from Student_2 Add Student_1 after that node 2 places away 0 places away 1 place away S0 S2 S3

18 addStudent (Student_1, Student_2, 2)
First find the node which contains Student_2 Find the node of the student 2 places away from Student_2 Add Student_1 after that node S1 S0 S2 S3

19 remove(Student) S1 S2 S3 Find the node which contains the student
Remove the node from the list S1 S2 S3

20 remove(S2) Find the node which contains the student by iterating through the list. S1 S2 S3

21 remove(S2) Remove the node from the list by setting S1.next to be S3 S1 S3

22 printList() Print out all the students at the table in clockwise order starting from Jonas. Last name should not contain a space after it.

23 public void printList(){ current = head;
System.out.print( current.getElement() ); for ( int i = 1 ; i < numOfNodes; i++ ){ current = current.getNext(); System.out.print( “ ” + current.getElement() ); } System.out.println(); public void printList(){ current = head; do{ output += ( current.getElement() + “ ” ); current = current.getNext(); } while ( current != head ); System.out.println( output.trim() ); }

24 Faster method? No iterators
Only 1 class (only Classroom class, no ListNode and LinkedList classes) No self-defined linked list Only import LinkedList API from Java and Scanner

25 Solution Represent the circular linked list as a doubly linked list (using Java LinkedList API) LinkedList<String> students Find index of particular student students.indexOf(String name) Find index of insertion (numOfSeatsAway + studentIndex) % students.size() +1 Insert student students.add(String name, int index); Remove a particular student Students.remove(String name);


Download ppt "Sit in Lab 3 ( Circular Linked List )"

Similar presentations


Ads by Google