Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1020 L AB 3 (L INKED L IST ) Problem 1: Balls Problem 2: Josephine Problem 3: Keyboard.

Similar presentations


Presentation on theme: "CS1020 L AB 3 (L INKED L IST ) Problem 1: Balls Problem 2: Josephine Problem 3: Keyboard."— Presentation transcript:

1 CS1020 L AB 3 (L INKED L IST ) Problem 1: Balls Problem 2: Josephine Problem 3: Keyboard

2 P ROBLEM 3: K EYBOARD

3 P ROBLEM Implement some basic tasks of a text editor. 1. Move the cursor to LEFT K times. 2. Move the cursor to RIGHT K times. 3. Insert a new character. 4. Remove the character preceding the cursor (and return the cursor to the beginning).

4 I NPUT /O UTPUT First line- integer N representing number of user operations. Following N lines of operations Output final text after all of the operations

5 O PERATIONS left K (K is an integer) Move the cursor to the left for K times right K Move the cursor to the right for K times insert X (X is a character) Insert character X at cursor position Cursor moves to the right of new character delete Remove the character on the left of the cursor Cursor moves to the beginning of the line

6 I MPLEMENTATION Use a doubly linked list (DLL) to implement the structure. Why? What can we use as our special head node?

7 C LASS L IST N ODE [P RELIMINARY ] Private attributes 1. char id 2. ListNode prev 3. ListNode next Public methods getNext() getPrevious() setNext(ListNode next) setPrevious(ListNode previous) ID prev next

8 C LASS R ESULT [ PRELIMINARY ] Stores the head of the list Does the manipulation of the list of characters Private Attributes ListNode cursorPosition The char that the cursor is currently at ListNode head The beginning of the line Constructor Result() Public Methods moveLeft(intk) moveRight(intk) insertChar(char x) deleteChar() toString() How to store reference to head?

9 I MPLEMENT H EAD (1) 1 HEAD Empty list HEAD Has element ListNode head = null ListNode head = ListNode(1) 2 prev next Store head as a pointer Head will point to the first node of the list If the list is empty, then head will be null

10 U SE A SPECIAL HEAD NODE Special head node: node that contains no legitimate data Simply to anchor the start of the list for easy reference Head will always point to a special head node

11 I MPLEMENT H EAD (2) Empty list Has element ListNode head = ListNode(‘$’) ($ is special, since char is a lower-case alphabet) Otherwise, can have extra boolean value isHead attribute in ListNode True if it is a head ListNode head = ListNode($) a prev next $ HEAD b $ prev next

12 W HICH IS BETTER ? a HEAD As a pointer b next Special list node a prev next b $ HEAD prev next prev

13 W HICH IS BETTER ? Special Head node Avoid special code for empty list Store extra information (e.g. length of list) Simplify pre-pending element to list

14 C LASS L IST N ODE [F INAL ] Private attributes 1. char id 2. ListNode prev 3. ListNode next 4. bool isHead (if a special char cannot be found) Public methods getNext() getPrevious() setNext(ListNode next) setPrevious(ListNode previous) checkIsHead() returns true if the node is a special head node (not part of actual list) id == ‘$’ head == true ID prev next

15 C LASS R ESULT [F INAL ] Stores the head of the list Does the manipulation of the list of characters Private Attributes ListNode current The char that the cursor is currently at ListNode head A special head node, which next points to the first node of the list Constructor Result() Public Methods moveLeft(intk) moveRight(intk) insertChar(char x) deleteChar() toString()

16 C LASS R ESULT MA | D D A M $ current HEAD

17 R ESULT C ONSTRUCTOR Result() Create special head node Set head to special head node Set cursorPosition to special head node

18 O PERATION 1: LEFT K void left(int K) Move the cursor to the left for K times Only special head node does not have a previous node for (int i = 0; i < K; i++) if (current node has previous) Set current node to the previous node of the current node; else break

19 O PERATION 2: RIGHT K void right(int K) Move the cursor to the right for K times for int i = 0 -> k if (current node has next element) Set current node to the next node of the current node; else break

20 O PERATION 3: INSERT K void insert(char X) Insert character X at cursor position Cursor moves to the right of new character

21 Insert MAX|D D XA M $ MA | D D A M $

22 O PERATION 4: DELETE void delete() If cursor is at special head node nothing to delete Else link up the previous and next nodes appropriately Move cursor back by setting it to special head node

23 Delete() MAX|D |MAD D XA M $ D A M $

24 O UTPUT void toString Create an empty result string From special head node Iterate through the ListNodes via getNext of each node Grow the result string with each node’s char value Return the result string Note: Do not add the head node’s char to the result string Write @Override one line above the toString method

25 Q UESTIONS ?

26 P ROBLEM 1: B ALLS

27 #1: B ALLS Problem: - Given N balls labeled from 1 to N and M operations, determine the last state after doing M operations. Input The first line contains two integers, N (1<= N <= 1,000) and M (1<= M <= 1,000). The next M lines contain the operations.

28 Operations: 1. A x y: move ball x to the left of ball y. 2. B x y: move ball x to the right of ball y. 3. R x: Remove the x from our list. Output: Output the final arrangement of the N balls from left to right. Each number is followed by a whitespace.

29 D ISCUSSION (1) 1. What kind of data structure can we use to solve this problem?

30 D ISCUSSION (2) 1. Hint: use List - Q: What kind of List (Singly-Linked List/ Doubly- Linked List/ Circular-Linked List)? - A: Use Doubly-Linked List Why?

31 W HY DOUBLY LINKED LIST ? Need to shift ball to the left of the current ball If single linked list is used Need to keep track the original left of the current ball, so as to update the next pointer of the original left to the new ball May need to iterate through again to find the original left of current ball What can we use for special head? 0, since numbers are from 1 to n

32 D ISCUSSION (3) 2. Complexity for algorithm 1 <= N <= 1,000 and 1 <= M <= 1,000 Simplest implementation: -Traversing all elements for removing and searching Better implementation: - Without traversing through elements Any Idea? Hint: balls can only take number from 1 to N

33 D ISCUSSION (7) Better Idea in terms of performance: 1. We store the position of all balls, so that we don’t need to trace the position of a ball if we want to move that ball or remove that ball (Array of Balls, with each Balls an element in the LinkedList)

34 D ISCUSSION (4) Class Ball stores: 1. int id 2. ListNode prev 3. ListNode next ID

35 C LASS B ALL Private Attributes int id Ball next Ball previous Constructor Ball(int id) Public Methods isHead() getId() getNext() getPrevious() setNext() setPrevious()

36 C LASS B ALL L IST Stores all the nodes Private Attributes Ball head int numBalls Constructor BallList(int n) Public Methods moveLeft(int x, int y) moveRight(int x, int y) remove(int x)

37 D ISCUSSION (6) Move(Simplest Implementation): 1. Find position of X and Y 2. Update the affected ListNode Example: A x y move the ball labelled x to the left of the ball labelled y

38 B EFORE : Y PYNY prev next prev next PX NX X prev next prev next

39 A FTER X Y PY PX NX prev next prev next prev next Remove X Insert X

40 B X Y move the ball x to the right of the ball y Y PYNY prev next prev next PX NX X prev next prev next

41 R X Remove ball x : 1. Find position of x 2. Update the neighbours of the affected Ball.

42 R EMOVE Y PYNY prev next prev next PYNY prev next

43 Q UESTIONS ?

44 P ROBLEM 2: J OSEPHINE

45 #2: J OSEPHINE Problem: - Given N candidates in circle - we want to keep removing the K-th candidate until we find the number of people in the circle equals to 1. - Output the removed candidate for each remove operation.

46 #2: J OSEPHINE Input T, the number of test cases, T <= 100. The following T lines describe T test cases, each line containing two integers, N and K. Output Output the final arrangement of the N balls from left to right. Each number is followed by a whitespace.

47 D ISCUSSION (1) 1. What kind of data structure can we use to solve this problem? 2. Hint: - Use Circular Linked List

48 1 2 3 4 5

49 D ISCUSSION (3) Keep removing K-th person by iterating K times from current position. Update the State of currentNode. link up the currentNode’s previous with currentNode’s next Move to currentNode’s next before iterating again More or less similar to Previous Problem but using different type of List.

50 K = 3 1 2 3 4 5 Current

51 K = 3 1 2 3 4 5 current

52 K = 3 1 2 3 4 5 Current: Remove!

53 K = 2 1 2 3 4 5 Current

54 W HEN TO STOP When the next of the last person is himself There can be no other people, otherwise he would be standing before someone else in the circle, which contradicts the fact that he is the only one left 12 1

55 Q UESTIONS ?


Download ppt "CS1020 L AB 3 (L INKED L IST ) Problem 1: Balls Problem 2: Josephine Problem 3: Keyboard."

Similar presentations


Ads by Google