CS1020 TAKE HOME LAB 3 Linked List

advertisement
CS1020
TAKE HOME LAB 3
Linked List
2
 Tasks:
 Passing
 Classroom
 Browser
3
Task: Passing
Task: Passing
Creating linked list
class LinkedList<E> {
protected ListNode<E> head = null;
protected ListNode<E> tail = null;
}
4
Task: Passing
Initialising the linked list
class ListNode<E> {
protected E element;
protected ListNode<E> next;
protected int counter;
}
class LinkedList<E> {
public void add(E item) {
if (head == null) {
head = new ListNode<E> (item, head);
tail = head;
} else { // insert after the last element
tail.setNext(new ListNode<E>(item, head));
tail = tail.getNext();
}
}
}
5
Task: Passing
6
For each turn,
At any turn, there will be an integer A. The player who gets the ball from the
last turn (or the first player if this is the first turn) will pass the ball to the
person on his right. Then, this person will also pass the ball to the person on
his right. This passing will repeat until there are A passings.
Suppose after A passings, player X receives a ball.
If player X has received K balls up to this turn, then he is out of the game and
he will give the ball to the person on his right.
If player X has received less than K balls up to this turn, player X will swap
position with the player who has the ball on the start of this turn.
If player X is the same as the player who starts the turn, then there will be no
swapping.
For each turn, you must output the name of the person who receives the ball
after A passings.
Task: Passing
7
For each turn,
int A = sc.nextInt();
ListNode<String> nextPlayer = curPlayer;
// passing the ball
for (int j = 0; j < A; j++) {
nextPlayer = nextPlayer.getNext();
}
System.out.println(nextPlayer.getElement());
nextPlayer.increaseCouter();
if (nextPlayer.getCounter() >= k) {
players.remove(nextPlayer);
// remove this player
curPlayer = nextPlayer.getNext(); // update curPlayer
} else {
// swap old player and new player
players.swap(curPlayer, nextPlayer);
}
Task: Passing
8
Swap
Easier to swap the content of the person,
no pointer change required
class LinkedList<E> {
public void swap(ListNode<E> player1, ListNode<E> player2) {
player1.swap(player2);
}
}
class ListNode<E> {
public void swap(ListNode<E> other) {
E curElement = element;
int curCounter = counter;
this.setElement(other.getElement());
this.setCounter(other.getCounter());
other.setElement(curElement);
other.setCounter(curCounter);
}
}
Task: Passing
9
Delete
Need the person on the left for pointer changing
class LinkedList<E> {
public void remove(ListNode<E> current)
throws NoSuchElementException {
if (current == null)
throw new NoSuchElementException("does not exist");
// find the player on the left of this player
ListNode<E> pre = current.getNext();
while (pre.getNext() != current)
pre = pre.getNext();
pre.setNext(current.getNext());
}
}
10
Task: Classroom
Task: Classroom
11
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
Task: Classroom
12
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
Task: Classroom
13
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
Task: Classroom
14
enter Peter Sharon 0
– Insert Peter next to the student 0 place away
from Sharon
Sharon
Sharon will always be at the table
Task: Classroom
15
enter Peter Sharon 0
– Insert Peter next to the student 0 place away
from Sharon
Sharon
Peter
Insertion is done on the left of the student (clockwise)
Task: Classroom
16
enter Danny Peter 1
– Insert Danny next to the student 1 place away
from Peter
Sharon
Peter
Task: Classroom
17
enter Danny Peter 1
– Insert Danny next to the student 1 place away
from Peter
Sharon
Danny
Peter
Task: Classroom
18
enter David Sharon 1
– Insert David next to the student 1 place away
from Sharon
Sharon
Danny
Peter
Task: Classroom
19
enter David Sharon 1
– Insert David next to the student 1 place away
from Sharon
Sharon
Danny
David
Peter
Task: Classroom
20
leave David
– Remove David from the table
Sharon
Danny
David
Peter
Task: Classroom
21
leave David
– Remove David from the table
Sharon
Danny
Peter
Task: Classroom
22
enter Jane Peter 2
– Insert Jane next to the student 2 places away
from Peter
Sharon
Danny
Peter
Task: Classroom
23
enter Jane Peter 2
– Insert Jane next to the student 2 places away
from Peter
Sharon
Danny
Jane
Peter
Task: Classroom
24
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
Task: Classroom
25
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
Task: Classroom
26
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
Task: Classroom
27
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
Task: Classroom
28
remove(Student)
• Find the node which contains the student
• Remove the node from the list
S1
S2
S3
Task: Classroom
29
remove(S2)
• Find the node which contains the student by
iterating through the list.
S1
S2
S3
Task: Classroom
30
remove(S2)
• Remove the node from the list by setting S1.next
to be S3
S1
S3
Task: Classroom
31
printList()
• Print out all the students at the table in clockwise
order starting from Sharon.
• Last name should not contain a space after it.
Task: Classroom
32
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() );
}
Task: Classroom
33
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
Task: Classroom
34
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);
35
Task: Browser
Task: Browser
36
Browser
• Emulating the Tab Bar on the browser
• Commands are given to manipulate the tabs
• Input:
• Integer – N operations
• N lines of operations
• Output the url of the currently opened tab after
every operation
Task: Browser
37
Operations
• NEWTAB
• Create new tab after current tab with url
•
•
•
•
•
“http://www.comp.nus.edu.sg”
CLOSETAB
• Close current tab and switch to next tab if exists, else previous
tab
NEXTTAB
• Switch to next tab
PREVTAB
• Switch to previous tab
OPENHERE urlstr
• Change current tab’s url to urlstr
OPENNEW urlstr
• Create new tab after current tab with url urlstr
Task: Browser
38
Visualisation
next
previous
Head
previous
comp.nus
.edu.sg
next
previous
google.c
om.sg
null
next
previous
web.what
sapp.com
next
null
Current
Tab
Task: Browser
39
Visualisation
NEXTTAB
next
previous
Head
previous
comp.nus
.edu.sg
next
previous
google.c
om.sg
next
previous
web.what
sapp.com
null
next
null
Current
Tab
Task: Browser
40
Visualisation
PREVTAB
next
previous
Head
previous
comp.nus
.edu.sg
next
previous
google.c
om.sg
null
next
previous
web.what
sapp.com
next
null
Current
Tab
Task: Browser
41
Visualisation
OPENHERE
bing.com
next
previous
Head
previous
comp.nus
.edu.sg
next
next
bing.com
previous
previous
null
web.what
sapp.com
next
null
Current
Tab
Task: Browser
42
Visualisation
CLOSETAB
next
previous
Head
previous
comp.nus
.edu.sg
next
previous
next
web.what
sapp.com
null
null
Current
Tab
Task: Browser
43
Visualisation
NEWTAB
next
previous
Head
previous
comp.nus
.edu.sg
next
previous
web.what
sapp.com
null
next
previous
comp.nus
.edu.sg
next
null
Current
Tab
Task: Browser
44
Visualisation
Compare it with
NEWTAB above
OPENNEW facebook.com
next
previous
Head
previous
comp.nus
.edu.sg
next
previous
web.what
sapp.com
null
next
previous
facebook
.com
next
null
Current
Tab
Task: Browser
Classes
• Tab
• The “list node”
• TabBar
• The “linked list” class
• Browser
• The main class
45
Task: Browser
Browser
• Read in number of operations N
• Repeat N times:
• Read the operation string
• Use TabBar class to carry out the operations
i.e. tabBar.closeTab()
• Print the url of the current tab
46
Task: Browser
47
TabBar
• Private Attributes
• Tab headTab
• Tab currentTab
• Constructor
• TabBar()
• Public Methods
•
•
•
•
•
•
•
newTab()
closeTab()
nextTab()
prevTab()
openHere(String url)
openNew(String url)
getCurrentTabUrl()
Task: Browser
48
Tab
• Private Attributes
• String url
• boolean head
• Tab next
• Tab previous
• Public Methods
• Constructors
• Tab(String url)
• Tab(String url, boolean head)
• isHead()
• hasNext(), hasPrevious()
• getUrl(), getNext(),
getPrevious()
• setUrl(), setNext(),
setPrevious()
• linkNext(), linkPrevious()
Task: Browser
Tab – constructors
public Tab(String url, boolean head) {
this.head = head;
this.url = url;
this.next = null;
this.previous = null;
}
public Tab(String url) {
this(url, false);
}
49
Task: Browser
Tab – hasNext(), hasPrev()
hasNext()
return this.next != null
hasPrevious()
return this.previous != null
50
Task: Browser
Tab – two-way linking
linkNext(Tab next)
this.setNext(next)
if (next is not null)
next.setPrevious(this)
linkPrevious(Tab prev)
this.setPrevious(prev)
if (prev is not null)
prev.setNext(this)
51
Task: Browser
52
TabBar
• Private Attributes
• Tab headTab
• Tab currentTab
• Constants
• String DEFAULT_URL =
“http://comp.nus.edu.sg”
• Constructor
• TabBar()
• Public Methods
•
•
•
•
•
•
•
newTab()
closeTab()
nextTab()
prevTab()
openHere(String url)
openNew(String url)
getCurrentTabUrl()
Task: Browser
TabBar – constructor
public TabBar() {
this.headTab = new Tab(null, true);
Tab defaultTab = new Tab(DEFAULT_URL);
this.headTab.linkNext(defaultTab);
this.currentTab = defaultTab;
}
53
Task: Browser
TabBar – nextTab
nextTab()
If currentTab has next
Set currentTab to currentTab’s next
54
Task: Browser
55
TabBar – prevTab
prevTab()
If currentTab has prev AND if the prev is not the head
Set currentTab to currentTab’s prev
Task: Browser
TabBar – openHere
openHere(String urlstr)
If currentTab is not the head
Set currentTab’s url to urlstr
56
Task: Browser
57
TabBar – closeTab
public void closeTab() {
Tab originalPrev = currentTab.getPrevious();
Tab originalNext = currentTab.getNext();
originalPrev.linkNext(originalNext);
if (currentTab.hasNext()) {
currentTab = originalNext;
} else {
currentTab = originalPrev;
}
}
Task: Browser
58
TabBar – openNew
public void openNew(String urlstr) {
Tab newTab = new Tab(urlstr);
Tab originalNext = currentTab.getNext();
currentTab.linkNext(newTab);
newTab.linkNext(originalNext);
}
Task: Browser
TabBar – newTab
newTab()
openNew(DEFAULT_URL)
59
Task: Browser
TabBar – getCurrentTabUrl
getCurrentTabUrl()
Return currentTab.getUrl()
60
61
END OF FILE
Download