1 / 40

CS1020 Lab 3 (Linked List)

CS1020 Lab 3 (Linked List). Problem 1 : Balls Problem 2 : Josephine Problem 3 : Alternating List . Problem 1: Balls. #1: Balls. 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,

galya
Download Presentation

CS1020 Lab 3 (Linked List)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1020 Lab 3(Linked List) Problem 1 : Balls Problem 2 : Josephine Problem 3 : Alternating List

  2. Problem 1: Balls

  3. #1: Balls • 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. Sample Input 10 5 A 2 1 A 10 1 A 5 6 B 6 9 R 3

  4. Operations: • A x y: move the ball labeled with number x to the left of the ball labeled with number y. • B x y: move the ball labeled with number x to the right of the ball labeled with number y. • R x: Remove the ball labeled with number x from our list. Output: • Output the final arrangement of the N balls from left to right. Each number is followed by a whitespace.

  5. Discussion (1) 1. What kind of data structure can we use to solve this problem?

  6. Discussion (2) • Hint: use List • Q: What kind of List (Singly-Linked List/ Doubly-Linked List/ Circular-Linked List)? • A: Use Doubly-Linked List Why?

  7. Discussion (3) 2. Simplest implementation: -Traversing all the nodes (to find the correct node) for removing and moving Better implementation: - Without traversing through the nodes(will be discussed later)

  8. Discussion (4) ListNode stores: 1. Number/Id 2. Previous Ball 3. Next Ball int ID ID ListNodeprev ListNode next

  9. Discussion (5) Remove: 1. Find position of x 2. Update the affected ListNode.

  10. Removing • Rough steps: • Iterate though the list to find the node with label x, and remember which node it is (point to it from a variable) • Adjust the pointers

  11. Adjusting pointers for removing • Remove node X from position Previous of X X Next of X prev prev prev next next next

  12. Adjusting pointers for removing • Remove node X from position Previous of X X Next of X prev prev prev next next next X.next.prev = X.prev X.prev.next = X.next Must handle special case of 1stand last nodeswhen X.prev == null or X.next == null

  13. Moving • Rough steps: • Iterate though the list to find the node with labels x and y, and store them in two variables. • Remove node X • Insert node X left/right of node Y • (practice using back the same node removed and insert it back, rather than creating a new node)

  14. Insert node • Insert X on the left of Y Previous of Y Y prev prev next next

  15. Insert node • Insert X on the left of Y Previous of Y X Y prev prev prev next next next Do it in the right order – don’t “lose” the previous of Y node X.prev = Y.prev X.next = Y Y.prev.next = X Y.prev = X Must handle special case of inserting X before the 1st node when Y.prev == null

  16. Insert node • Insert X on the left of Y Previous of Y X Y prev prev prev next next next Do it in the right order – don’t “lose” the previous of Y node X.prev = Y.prev X.next = Y Y.prev.next = X Y.prev = X Must handle special case of inserting X before the 1st node when Y.prev == null

  17. Insert node • Insert X to the right of Y Y Next of Y prev prev next next

  18. Insert node • Insert X to the right of Y Y X Next of Y prev prev prev next next next Do it in the right order – don’t “lose” the previous of Y node X.next = Y.next X.prev = Y Y.next.prev = X Y.next = X Must handle special case of inserting X after the last node when Y.next == null

  19. Insert node • Insert X to the right of Y Y X Next of Y prev prev prev next next next Do it in the right order – don’t “lose” the previous of Y node X.next = Y.next X.prev = Y Y.next.prev = X Y.next = X Must handle special case of inserting X after the last node when Y.next == null

  20. Discussion (7) An idea to improve performance: • We store a “reference” of all balls (ListNodes) in an array, so that we don’t need to traverse the Linked List to find the balls every time. head 1 2 4 3 5

  21. Problem 2: Josephine

  22. #2: Josephine • 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.

  23. #2: Josephine • Input • The first line consists of 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. Sample Input 2 3 1 4 2 Sample Output 1 2 3 2 4 3 1

  24. Discussion (1) 1. What kind of data structure can we use to solve this problem?

  25. Discussion (2) • Hint:- Use Circular Linked List Why? Simulates the problem!

  26. Discussion (3) • Keep removing K-th person by iterating K times from current position. • Update the State of ListNode. • More or less similar to Previous Problem but using different type of LinkedList.

  27. Circular linked list • Removing a node: • Since you don’t have the previous pointer in a singly-linked circular linked list, you need to keep track of the previous node when you traverse the list! head 1 2 4 3 5

  28. Problem 3: Alternating List

  29. #3: Alternating list Problem: • Given a list of integers, and a list of operations, determine whether it is alternating after each operation • Definition of Alternating: -Adjacent integers have different signs • List of one or zero elements is alternating

  30. #3: Alternating list Input • 1 <= N <= 100 1 <= Q <= 100 • N is the size of the original linked list • Q the number of operations • Output • For each operation print “YES” if the updated linked list is alternating, otherwise print “NO”. Sample Input 4 4 1 -2 3 -4 M 1 3 A 1 1 14 R 2 2 A 2 1 -11 Sample Output YES NO NO YES

  31. Discussion (1) 1. What kind of data structure can we use to solve this problem?

  32. Discussion (2) • Hint: use List • Q: What kind of List (Singly-Linked List/ Doubly-Linked List/ Circular-Linked List)? • A: Use (Singly) Linked List Why?

  33. Discussion (2) - Operations • M [index] [size]Move “size” number of elements starting from index to the end of list M 2 3

  34. Discussion (2) - Operations • Pseudo-code:function Move (int index, int size) { for i=1 to size { temp  remove element at indexinsert temp to the end of list}} • Why removing at the index only works?

  35. Discussion (2) - Operations • R [index] [size] Remove “size” number of elements starting from index. R 2 3

  36. Discussion (2) - Operations • Pseudo-code:fun Remove (int index, int size) { for i=1 to size {remove element at index }} • Can you use this to implement Move?

  37. Discussion (2) - Operations • A [index] [size] [value] add the elements between index [index] and [index + size - 1] (inclusive) with [value]. +5 +5 +5 A 2 3 5

  38. Discussion (2) - Operations • Pseudo-code:fun AddValue (int index, int size, int value) { for i=1 to size { temp  remove value at index+i-1 insert (temp+value) to index+i-1 }} • How do we get the (index+i-1)? • (or we can cheat and just modify the number in listNode…)

  39. Discussion (3) - Check Alternating • List of 1 element : YES • Loop through list: once two adjacent elements with same sign found, return “NO” i.e. • Keep track of “previousSign”, and then check every iteration if “previousSign == currentSign” • Return “YES” if loop completes without returning

  40. The End Any Questions?

More Related