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

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Linked Lists Geletaw S..
Singly linked lists Doubly linked lists
1 - Recursion on linked lists Lecture 7 ADS2 Lecture 7.
Lecture 15 Linked Lists part 2
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Stacks, Queues, and Linked Lists
Linked Lists.
Linked Lists Chapter 4.
Data Structures ADT List
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Section 5 Lists again. Double linked lists – insertion, deletion. Trees.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Linked Lists CENG 213 Data Structures.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
LISTS & TREES Lecture 8 CS2110 – Fall List Overview 2  Purpose  Maintain an ordered set of elements (with possible duplication)  Common operations.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Chapter 17 Linked List.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
CS2006- Data Structures I Chapter 5 Linked Lists III.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1.
CS 367 Introduction to Data Structures Lecture 5.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
2/21/20161 List Operations Advanced Programming Ananda Gunawardena.
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
Lab-12 Keerthi Nelaturu. BitList Stores bit values in Linked List Has to be stored in right to left order Example :
Java linked list.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CSCS-200 Data Structure and Algorithms Lecture
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Given a node v of a doubly linked list, we can easily insert a new node z immediately after v. Specifically, let w the be node following v. We execute.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Chapter 16: Linked Lists.
Unit – I Lists.
C++ Programming:. Program Design Including
Pointers and Linked Lists
Pointers and Linked Lists
Big-O notation Linked lists
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Linked Lists.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Data Structures & Algorithms
Presentation transcript:

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

P ROBLEM 3: K EYBOARD

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

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

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

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

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

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?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 one line above the toString method

Q UESTIONS ?

P ROBLEM 1: B ALLS

#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.

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.

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

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?

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

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

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)

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

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

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)

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

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

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

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

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

R EMOVE Y PYNY prev next prev next PYNY prev next

Q UESTIONS ?

P ROBLEM 2: J OSEPHINE

#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.

#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.

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

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.

K = Current

K = current

K = Current: Remove!

K = Current

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

Q UESTIONS ?