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

Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

Embed Size (px)

Citation preview

Page 1: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

CS1020 Lab 3(Linked List)Problem 1: BallsProblem 2: JosephineProblem 3: Alternating List

Page 2: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

Problem 1: Balls

Page 3: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

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.

#1: Balls

Sample Input

10 5 A 2 1 A 10 1 A 5 6 B 6 9 R 3

Page 4: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

Operations:1. A x y: move the ball labeled with number x to

the left of the ball labeled with number y.2. B x y: move the ball labeled with number x to

the right of the ball labeled with number y.3. 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.

Page 5: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

1. What kind of data structure can we use to solve this problem?

Discussion (1)

Page 6: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

1. Hint: use List- Q: What kind of List (Singly-Linked List/

Doubly-Linked List/ Circular-Linked List)?- A: Use Doubly-Linked List

Why?

Discussion (2)

Page 7: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

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)

Discussion (3)

Page 8: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

ListNode stores:1. Number/Id2. Previous Ball3. Next Ball

Discussion (4)

ID

int ID

ListNode next

ListNode prev

Page 9: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

Remove:1. Find position of x2. Update the affected ListNode.

Discussion (5)

Page 10: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

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

Removing

Page 11: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

Remove node X from position

Adjusting pointers for removing

X

next

prev

Next of X

next

prev

Previous of X

next

prev

Page 12: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

Remove node X from position

Adjusting pointers for removing

X

next

prev

Next of X

next

prev

Previous of X

next

prev

X.next.prev = X.prevX.prev.next = X.next

Must handle special case of 1st and last nodeswhen X.prev == null or X.next == null

Page 13: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

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)

Moving

Page 14: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

Insert X on the left of Y

Insert node

Y

next

prev

Previous of Y

next

prev

Page 15: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

Insert X on the left of Y

Insert node

X

next

prev

Y

next

prev

Previous of Y

next

prev

X.prev = Y.prevX.next = YY.prev.next = XY.prev = X

Must handle special case of inserting X before the 1st node when Y.prev == null

Do it in the right order – don’t “lose” the previous of Y node

Page 16: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

Insert X on the left of Y

Insert node

X

next

prev

Y

next

prev

Previous of Y

next

prev

X.prev = Y.prevX.next = YY.prev.next = XY.prev = X

Must handle special case of inserting X before the 1st node when Y.prev == null

Do it in the right order – don’t “lose” the previous of Y node

Page 17: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

Insert X to the right of Y

Insert node

Next of Y

next

prev

Y

next

prev

Page 18: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

Insert X to the right of Y

Insert node

X

next

prev

Next of Y

next

prev

Y

next

prev

X.next = Y.nextX.prev = YY.next.prev = XY.next = X

Must handle special case of inserting X after the last node when Y.next == null

Do it in the right order – don’t “lose” the previous of Y node

Page 19: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

Insert X to the right of Y

Insert node

X

next

prev

Next of Y

next

prev

Y

next

prev

X.next = Y.nextX.prev = YY.next.prev = XY.next = X

Must handle special case of inserting X after the last node when Y.next == null

Do it in the right order – don’t “lose” the previous of Y node

Page 20: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

An idea to improve performance:1. 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.

Discussion (7)

1 2 4 3 5head

1 2 3 4 5

Page 21: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

Problem 2: Josephine

Page 22: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

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.

#2: Josephine

Page 23: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

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.

#2: Josephine

Sample Input23 14 2

Sample Output1 2 32 4 3 1

Page 24: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

1. What kind of data structure can we use to solve this problem?

Discussion (1)

Page 25: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

1. Hint:- Use Circular Linked List

Why?Simulates the problem!

Discussion (2)

Page 26: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

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.

Discussion (3)

Page 27: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating 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!

Circular linked list

1 2 4 3 5head

Page 28: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

Problem 3: Alternating List

Page 29: Problem 1: Balls Problem 2: Josephine Problem 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

#3: ALTERNATING LIST

Page 30: Problem 1: Balls Problem 2: Josephine Problem 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”.

#3: ALTERNATING LISTSample Input4 4 1 -2 3 -4 M 1 3 A 1 1 14 R 2 2 A 2 1 -11 Sample OutputYES NONOYES

Page 31: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

1. What kind of data structure can we use to solve this problem?

Discussion (1)

Page 32: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

1. Hint: use List- Q: What kind of List (Singly-Linked List/

Doubly-Linked List/ Circular-Linked List)?- A: Use (Singly) Linked List

Why?

Discussion (2)

Page 33: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

M [index] [size]Move “size” number of elements starting from index to the end of list

Discussion (2) - Operations

Index 1 2 3 4 5

-4 3 -2 1 2

M 2 3

Index 1 5 2 3 4

-4 2 3 -2 1

Page 34: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

Pseudo-code:function Move (int index, int size) { for i=1 to size { temp remove element at index insert temp to the end of list }}

Why removing at the index only works?

Discussion (2) - Operations

Page 35: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

R [index] [size] Remove “size” number of elements starting

from index.

Discussion (2) - Operations

Index 1 2 3 4 5

-4 3 -2 1 2

R 2 3

Index 1 5

-4 2

Page 36: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

Pseudo-code:fun Remove (int index, int size) { for i=1 to size { remove element at index }}

Can you use this to implement Move?

Discussion (2) - Operations

Page 37: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

A [index] [size] [value] add the elements between index [index]

and [index + size - 1] (inclusive) with [value].

Discussion (2) - Operations

Index 1 2 3 4 5

-4 3 -2 1 2

A 2 3 5

Index 1 2 3 4 5

-4 8 3 6 2

+5 +5 +5

Page 38: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

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…)

Discussion (2) - Operations

Page 39: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

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

Discussion (3) - Check Alternating

Page 40: Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

The EndAny Questions?