Data Structures and Algorithms

Slides:



Advertisements
งานนำเสนอที่คล้ายกัน
Strength of Materials I EGCE201 กำลังวัสดุ 1
Advertisements

E-COMMERCE WEBSITE Smartzap Co., Ltd.. Company Profile บริษัท สมาร์ทแซป จำกัด ก่อตั้งเมื่อปี 2543 (13 ปี ) ในช่วงยุค Internet เพิ่ง เริ่มต้น เป็นบริษัทที่ดำเนินงานทางด้าน.
John Rawls  John Rawls is the most famous American social contract theorist argued that “Justice is fairness” He Thought human natural have a appropriate.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Java Programming Language.
คำสั่ง DISPLAY รูปแบบที่ 1 DISPLAY identifier-1, identifier-2 … literal-1 literal-2 [ UPON mnemonic-name ]  ตัวอย่าง DISPLAY STUDENT-NAME. DISPLAY.
Set is a basic term in Mathematics. There is no precise definition for term “set”, But roughly speaking, a set is a collection of objects, Things or symbols,
Finite and Infinite Sets, Null set
ไวยกรณ์ไม่พึงบริบท CONTEXT-FREE GRAMMARS
จำนวน สถานะ NUMBER OF STATES. ประเด็นที่ สนใจ The number of distinct states the finite state machine needs in order to recognize a language is related.
Graphical User Interface charturong.ee.engr.tu.ac.th/CN208
รู้จักกับเทคโนโลยี RFID เบื้องต้น
VARIABLES, EXPRESSION and STATEMENTS. Values and Data Types Value เป็นสิ่งพื้นฐาน มีลักษณะเป็น ตัวอักษร หรือ ตัวเลข อาทิ 2+2 หรือ “Hello world” Value.
Chapter 5: Functions of Random Variables. สมมติว่าเรารู้ joint pdf ของ X 1, X 2, …, X n --> ให้หา pdf ของ Y = u (X 1, X 2, …, X n ) 3 วิธี 1. Distribution.
โครงสร้างข้อมูลแบบรายการโยง (Linked Lists) Data Structures and Algorithms อ. ธารารัตน์ พวงสุวรรณ คณะวิทยาศาสตร์และศิลปศาสตร์ มหาวิทยาลัยบูรพา วิทยาเขตสารสนเทศจันทบุรี
ระบบการจัดเก็บในคลังสินค้า
Stack.
: Chapter 1: Introduction 1 Montri Karnjanadecha ac.th/~montri Image Processing.
Color Standards A pixel color is represented as a point in 3-D space. Axis may be labeled as independent colors such as R, G, B or may use other independent.
1 ภาษาLANGUAGE. ภาษาอังกฤษ หน่วยของภาษา อักขระ letters อักขระ letters คำ words คำ words ประโยค sentences ประโยค sentences ย่อหน้า paragraphs ย่อหน้า paragraphs.
ออโตมาตาจำกัด FINITE AUTOMATA
REGULAR EXPRESSION การบรรยายแบบสม่ำเสมอ
ภาษาสม่ำเสมอ REGULAR LANGUAGES
Helping you make better treatment decisions for your patients.
Linked-List (รายการโยง)
Chapter 19 Network Layer: Logical Addressing
Inductive, Deductive Reasoning ผศ.( พิเศษ ) น. พ. นภดล สุชาติ พ. บ. M.P.H.
INC 637 Artificial Intelligence Lecture 13 Reinforcement Learning (RL) (continue)
บทที่ 2 Queue Queue.
Yv xv zv.
ผศ.ดร.สุพจน์ นิตย์สุวัฒน์
การออกแบบและพัฒนาซอฟต์แวร์ บทที่ 7 การทดสอบโปรแกรม
Asst.Prof. Dr.Surasak Mungsing
Merchant Marine Training Centre วิชาการเป็นเลิศ เชิดชู คุณธรรม ผู้นำ.
Dynamic Link Library (DLL)
Menu and Interactive with Powerpoint ให้นำเรื่อง Input /Output Technology มา จัดทำ การนำเสนอ โดยใช้หลักการ Menu and Interactive with powerpoint มาประยุกต์
Enhanced Entity-Relationship Model © Pearson Education Limited 1995, 2005.
Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of computer program.
ตัวแปรในภาษา GML ง การสร้างเกม คอมพิวเตอร์.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Java Programming Language.
Thread Thread ส่วนของ process ที่ให้ CPU ประมวลผล.
STACK ADT By Pantharee S.. Stack Model  A list with the restriction that insertions deletions can be performed in only one position (LIFO)  Push – insert.
List ADTs By Pantharee S..
การสร้าง WebPage ด้วย Java Script Wachirawut Thamviset.
Chapter 3 Simple Supervised learning
บทที่ 5 Link List Link List.
Chapter 1/1 Arrays. Introduction Data structures are classified as either linear or nonlinear Linear structures: elements form a sequence or a linear.
Linked List List is group of nodes that consists of data and link.
21 August ดรุณี ศมาวรรตกุล 1 2. ADT List - Unsorted list ADT - list implementation - Sorted List - Circular list - Doubly linked list.
Object-Oriented Programming with Java Burapha University, 2001 Java Array and String Week #3 Jarungjit Parnjai.
วิชา COSC2202 โครงสร้างข้อมูล (Data Structure)
 Mr.Nitirat Tanthavech.  HTML forms are used to pass data to a server.  A form can contain input elements like text fields, checkboxes, radio-buttons,
How do scientists think and find( พบ ) answers?.
ปริมาณสัมพันธ์ ผู้สอน อ. ศราวุทธ แสงอุไร Composition Stoichiometry ว ปริมาณสัมพันธ์ สถานะของ สาร และเคมีไฟฟ้า นายศราวุทธ แสงอุไร ครูวิชาการสาขาเคมี
รศ. ดร. บุญธีร์ เครือตราชู รศ. กฤตวัน ศิริบูรณ์ KMITL Data Structures & Algorithms : Stack & Queue 1 Stack & Queue Lecturers : Boontee Kruatrachue.
Linked List ( ต่อ ) Lecturer : Kritawan Siriboon, Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss, Addison Wesley.
List, Stack, Queue 2018/9/21.
Linked List.
พื้นฐานการเขียนแบบทางวิศวกรรม
Linked List.
Dr.Surasak Mungsing CSE 221/ICT221 การวิเคราะห์และออกแบบขั้นตอนวิธี Lecture 13: การคำนวณได้และการตัดสินใจของปัญหา ที่ยากต่อการแก้ไข.
Linked List Lecturer : Kritawan Siriboon, Room no. 913
บทที่ 1 ความรู้เบื้องต้น เกี่ยวกับระบบสารสนเทศ
Principles of Accounting II
Linked List (ต่อ) Lecturer : Kritawan Siriboon, Room no. 913
Dr.Surasak Mungsing CSE 221/ICT221 การวิเคราะห์และออกแบบขั้นตอนวิธี Lecture 05: การวิเคราะห์ความซับซ้อนของ ขั้นตอนวิธีการเรียงลำดับข้อมูล.
สถิติเพื่อการวิเคราะห์ข้อมูล
Lesson 7-6: Function Operations
การวิเคราะห์และออกแบบขั้นตอนวิธี
สารสนเทศศาสตร์เบื้องต้น
Year 9 Term 1 Foundation (Unit 1) INTEGERS, ROUNDING AND PLACE VALUE
ใบสำเนางานนำเสนอ:

291101 Data Structures and Algorithms โครงสร้างข้อมูลแบบรายการโยง (Linked Lists) อ.ธารารัตน์ พวงสุวรรณ คณะวิทยาศาสตร์และศิลปศาสตร์ มหาวิทยาลัยบูรพา วิทยาเขตสารสนเทศจันทบุรี thararat@buu.ac.th

เนื้อหา โครงสร้างข้อมูลแบบ Linked Lists Single Linked List การดำเนินการกับ Single Linked Lists Double Linked Lists การดำเนินการกับ Double Linked Lists

ลักษณะของลิงค์ลิสต์ ลิงค์ลิสต์เป็นการนำข้อมูลแต่ละโหนดมาจัดเรียงต่อกันเป็นลิสต์ที่มีการเชื่อมโยงกัน ภายในแต่ละโหนดแบ่งส่วนประกอบได้เป็น 2 ฟิลด์ คือ ข้อมูล และแอดเดรสของโหนดถัดไป (Link) โดยฟิลด์ข้อมูลสามารถเก็บข้อมูลได้มากกว่าหนึ่งค่า ลิงค์ลิสต์เป็นการนำข้อมุลแต่ละโหนดมาเรียงต่อกันเป็นลิสต์ สิ่งที่เป็นตัวกำหนดลำดับ คือ ฟิลด์แอดเดรสของแต่ละโหนด การเข้าถึงลิงค์ลิสต์เริ่มต้นจะต้องมีพอยน์เตอร์ชี้ไปยังโหนดแรกของลิงค์ลิสต์ (Head) ฟิลด์แอดเดรสของโหนดสุดท้ายต้องกำหนดให้เป็น null ซึ่งเป็นการกำหนดให้จบลิงค์ลิสต์ ลิงค์ลิสต์ที่ไม่มีโหนดอยู่ภายในจะเรียกว่า ลิสต์ว่าง (empty list หรือ null list)

ประเภทของลิงค์ลิสต์ ลิงค์ลิสต์เดี่ยว (Single Linked List) คือ ลิงค์ลิสต์ที่มีฟิลด์แอดเดรสเพียงฟิลด์เดียวสำหรับชี้ตำแหน่งของโหนดถัดไป ลิงค์ลิสต์คู่ (Double Linked List) คือ ลิงค์ลิสต์ที่มีฟิลด์แอดเดรสจำนวน 2 ฟิลด์ สำหรับชี้ตำแหน่งของโหนถัดไปและตำแหน่งของโหนดที่อยู่ก่อนหน้า Dog USA Cop 12 Head 13 78 Dog USA Cop Head

Single Linked List Each item in the list is called a node. It contains two field - An information field : holds the actual element on the list - A next address field : contains the address of the next node in the list The entire linked list is accessed from an external pointer that points to the first node in the list. The next address field of the last node in the list contains a special value, known as null. The list with no nodes on it is called the empty list or null list.

ลักษณะของ Single Linked List info next info next info next info next list Null node node node node “A List is a dynamic data structure. The number of nodes on the list may vary dramatically as elements are inserted and removed.” Operation for Linked List - Inserting - Removing

Notation for use in algorithm (Not in C program) If p is a pointer to a node - node(p) refers to the node pointed to by p - info(p) refers to the information portion of that node - next(p) refers to the next address portion - info(next(p)) refers to the information portion of the node that follows node(p) in the list - getnode() refers creating a new node freenode(p) refers destroying node p

Inserting an element to the front of a list สมมติว่า กำหนดให้มี List of integers และต้องการเพิ่ม integer 6 เข้าไปไว้ส่วนหน้าของ List info next info next info next list 5 3 8 Null

1. Obtain an empty node to be added onto the list. p = getnode() Steps for inserting 1. Obtain an empty node to be added onto the list. p = getnode() 2. Insert the integer 6 into info portion of the newly allocated node. info(p) = 6 info next p info next p 6

3. Set the next portion of that node Steps for inserting 3. Set the next portion of that node (Since node(p) is to be inserted at the front of the list,the node that follows should be the current first node on the list. next(p) = list info next info next info next info next p 6 list 5 3 8 Null

be modified to the address of the new first node of the list. list = p Steps for inserting 4. Since list is the external pointer to the desired list, its value must be modified to the address of the new first node of the list. list = p info next info next info next info next p 6 5 3 8 Null list list 6 5 3 8 Null

Algorithm for adding the integer 6 to the front of the list p = getnode() info(p) = 6 next(p) = list list = p “The algorithm can be generalized so that it adds any object X to the front of a list by replacing the operation info(p) = 6 to info(p) = X ”

Removing the first node of a nonempty list สมมติว่า กำหนดให้มี List of integers และต้องการลบโหนดแรก ของ List และเก็บค่า info ของโหนดนั้นไว้ในตัวแปร X info next info next info next list 7 5 9 Null

Algorithm 1. p = list info next info next info next list p 7 5 9 Null p 2. list = next(p) info next info next info next p 7 5 9 Null list

Algorithm 3. x = info(p) info next info next info next X=7 p list 5 9 Null list 4. freenode(p) info next info next X=7 p 5 9 Null list

Algorithm for removing a node from the front of a list p = list list = next(p) x = info(p) freenode(p)

Linked Implementation of Stacks - The operation of adding an element to the front of a linked list is similar to that of pushing an element onto a stack. - A stack can be accessed only through its top element, and a list can be accessed only from the pointer to its first element. - The operation of removing the first element from a linked list is analogous to popping a stack. - A stack may be represented by a linear linked list. The first node of the list is the top of the stack.

If an external pointer s points to such a linked list, the operation push(s,x) may be implemented by p = getnode() info(p) = x next(p) = s s = p s 5 3 8 null 7 s 6 5 3 8 null 7

The operation x = pop(s) removes the first node from a nonempty list and signals underflow if the list is empty : If (empty(s)) print ‘stack underflow’ exit else p = s s = next(p) x = info(p) freenode(p) “The operation empty(s) is merely a test of whether s equals null”

Linked Implementation of Queues - The items are deleted from of a queue and inserted at the rear. - A pointer to the first element of a list represent the front of the queue - Another pointer to the last element of the list represents the rear of the queue. - A queue q consists of a list and two pointers, q.front and q.rear - The operations empty(q) and x = remove(q) are analogous to empty(s) and x = pop(s), with the pointer q.front replacing s. - When the last element is removed from a queue, q.rear must also be set to null, since in an empty queue both q.front and q.rear must be null.

Algorithm for x = remove(q) If(empty(q) print “queue underflow” exit() p = q.front x = info(p) q.front = next(p) if(q.front == null) q.rear = null freenode(p) return(x)

Algorithm for insert(q) p = getnode() info(p) = x next(p) = null if(q.rear == null) q.front = p else next(q.rear) = p q.rear = p

Inserting a new element after node การเพิ่มโหนดใหม่หลังโหนดใด ๆ จะเกี่ยวข้องกับขั้นตอนต่าง ๆ ดังนี้ - Allocating a node - Inserting the information - adjusting two pointer ** The amount of work is independent of the size of the list.

Let insafter(p,x) denote the operation of inserting an item x into a list after a node pointed to by p. Algorithm q = getnode() info(q) = x next(q) = next(p) next(p) = q p list null x0 x1 x2 x3 q x

Let delafter(p,x) denote the operation of deleting the node following node(p) and assigning its contents to variable x. Algorithm q = next(p) x = info(q) next(p) = next(q) freenode(q)

doubleLinked Lists Each node in such a list contains two pointers, one to its predecessor and another to its successor null null May consider the nodes on a doublelinked list to consist of 3 field : - An info field : contains the information stored in the node - Left field : contains pointer to the left node - Right field : contain pointer to the right node

Operation on doublelinked lists - Delete a given node Deletes the node pointed to by p from a double linked list and stores its contents in x - Insert a node to the right of node(p) Insert a node with information field x to the right of node(p) in a double linked list. - Insert a node to the left of node(p) Insert a node with information field x to the left of node(p) in a

Notation for use in algorithm (Not in program) If p,q,r is a pointer to a node - node(p) refers to the node pointed to by p - info(p) refers to the information portion of that node left(p) refers to the left address portion (address of prior node for node p) right(p) refers to the right address portion (address of next node for node p) left(r) refers to the left address portion (address of prior node for node r) - right(r) refers to the right address portion (address of next node for node r) left(q) refers to the left address portion (address of prior node for node q) right(q) refers to the right address portion (address of next node for node q)

Delete a given node สมมติว่า กำหนดให้ Double linked list เป็น List of integers และต้องการลบโหนดที่ p ชี้อยู่ และเก็บค่า info ของโหนดนั้นไว้ในตัวแปร X กำหนด q และ r เป็นตัวชี้ไปยังโหนดใด ๆ x = info(p) q = left(p) r = right(p) right(q) = r left(r) = q freenode(p)

Insert a node to the right of node(p) สมมติว่า กำหนดให้ Double linked list เป็น List of integers และต้องการแทรกโหนดทางขวามือจากโหนดที่ p ชี้อยู่ และกำหนด q และ r เป็นตัวชี้ไปยังโหนดใด ๆ q = getnode() info(q) = x r = right(p) left(r) = q right(q) = r left(q) = p right(p) = q

Insert a node to the left of node(p) สมมติว่า กำหนดให้ Double linked list เป็น List of integers และต้องการแทรกโหนดทางซ้ายมือจากโหนดที่ p ชี้อยู่ และกำหนด q และ r เป็นตัวชี้ไปยังโหนดใด ๆ q = getnode() info(q) = x r = left(p) right(r) = q left(q) = r right(q) = p left(p) = q