ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.

Slides:



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

Linked Lists Geletaw S..
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.
Data Structures ADT List
Linked Lists Linear collections.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
CSE Lecture 12 – Linked Lists …
Lecture Stacks. A stack is a Last-In-First-Out (LIFO) or a First-In-Last-Out (FILO) abstract data type E.g. a deck of cards in which cards may be added.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
CHAPTER 7 Queues.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Priority Queues and Heaps. Overview Our last ADT: PriorityQueueADT A new data structure: heaps One more sorting algorithm: heapsort Priority Queues and.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
C++ Classes and Data Structures Jeffrey S. Childs
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
COSC 1030 Lecture 9 Binary Trees. Topics Basic Concept and Terminology Applications of Binary Tree Complete Tree Representation Traversing Binary Trees.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Topic 24 Heaps "You think you know when you can learn, are more sure when you can write even more when you can teach, but certain when you can program."
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Chapter 5 Linked Lists II
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
CS 367 Introduction to Data Structures Lecture 5.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Data Structures: Linked Lists
The List ADT.
Linked Lists CSM Linked Lists.
Doubly Linked List Review - We are writing this code
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Stack and Queue APURBO DATTA.
Chapter 20: Binary Trees.
8-1.
8-1.
Chapter 21: Binary Trees.
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Introduction to Algorithms and Data Structures
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Data Structures & Algorithms
Queues: Implemented using Linked Lists
Data Structures & Programming
Presentation transcript:

ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها

2 آرايه اي با طول متغير طول آرايه بعد از تعريف، ثابت مي ماند براي تعريف آرايه بزرگتر، ابتدا آرايه جديدي با طول کافي تعريف کرده و سپس آرايه قبلي را در آن کپي مي کنيد: – روش مورد استفاده در :java.util.Vector و java.util.arrayList – زمانبر است روش بهتر: به هر عضو آرايه، اشاره گري اضافه شود که نشانگر عضو بعدي باشد: – آرايه اي که به اين شکل ساخته شود، يک ليست پيوندي (Linked List) ناميده مي شود – يک کلاس + يک اشاره گر به کلاس هم نوع ديگر = Node – Node = Data + pointer

3 Linked List [0][1][2] array ABC Array linked ABC Linked list Linked lists are unbounded (maximum number of items limited only by memory) node

4 The Node data structure هر Node دو بخش دارد – يک بخش داده ها يا اطلاعات – يک اشاره گر به Node هاي ديگر Methods – getData, setData, getNext, setNext (access data and pointer) – toString (converts the data to a string)

5 Node ADT class Node{ private int data ; // some piece of data private Node next ; //Next item, Cpp:Node* next private Node back ; //Back item,Cpp:Node* back //Methods : public int getData() ; public void setData(int d) ; public void setNext(Node n) ; public Node getNext(); }

6 Linked List ساختاري خطي با تعدادي Node اشاره گر به Node اول متدهايي براي مديريت Node ها – addToFront, addToBack: add an object to the front/back of the list – removeFromFront/Back: remove an object from the front/back – getFront/Back: examine the object at the front/back (no removal) – isEmpty: determines whether or not the list is empty – length: returns the number of objects in the list – equals: tests two lists for equality – toString: converts a list to a String

7 Linked List ADT class LinkedList{ private Node first ; //Methods public void addNode(Node n) ; public void removeNode(Node n) ; //Additional useful methods: public int length () ; public bool isEmpty() ; …. }

8 The method isEmpty //Returns true if the linked list is empty, // or false if non-empty public boolean isEmpty( ) { return first.getNext() == null ; }

9 B newNode addToFront first Initial list Create a new node using A 1 Create a new node using B 2 Set newNode’s next to first.next 3 Set first to newNode Add B to front A first Add A to front first A data next newNode first.setNext( )

10 addToFront …? B newNode A floating node! 1 Create a new node using B 3 Set first to newNode 2 Set newNode’s next to first Add B to front A first Swap steps 2 and 3

11 A removeFromFront Remove front (A) first BA datanext Initial list BA Remove front (B) Remove front (???) Should throw an exception

12 length public int length( ){ int n = 0 ; for (C = first.Next() ; c != null ; c = c.getNext() ) n++ ; return n ; } تعداد عناصر ليست را بر مي گرداند پياده سازي در قالب يک Attribute – مثل آرايه ها: تعريف يک مشخصه length و نگهداري، بروز رساني آن بروز رساني هنگام حذف و اضافه نمودن يک Node به ليست

13 The method addToBack public void addToBack(Node n)} //Create a new node containing ‘element’ // as the data // Locate the back of the list (i.e. the final element in the list) //Link the final element to the new node, so that the new node becomes the back of the list //Special case: Empty list }

14 first AC datanext B removeFromBack removeFromBack removes the last node in the linked list This is done by setting the second-last node’s ‘next node’ pointer to null Two possible special cases: empty list, and list of one node secondLast node

15 جستجو و پيدا کردن يک گرهfind public Node find(Object element){ //Searches for an element in the list, looping //though it like toString() did //If found, it returns the Node which contains //that element //Could also return the location of the Node // (the value of the loop counter) //If not found, it returns null }

16 Using a last variable مزايا و معايب – addToBackسريع : O(1) – حلقه موجود removeFromBackقابل حذف نيست – last needs to be updated by addToBack and removeFromBack, and by addToFront and removeFromFront in special cases – البته چند خط بايد بيشتر کد بنويسيد ! first ABC last

17 Circular linked lists The last node points to the first node (instead of null) ABC first ABC last How do we refer to the first node? If the linked list points to the last node rather than the first node, it is very similar to the diagram in the previous slide

18 Doubly-linked lists The nodes in doubly-linked lists point to the previous node, as well as to the next node Removes the need for a loop in removeFromBack Complicates other methods (more updating to do) first ABC last

19 درخت با چندين فرزند

20 ساختار گره و درخت class TreeNode{ private int data; TreeNode parent, child, next ; //Methods …. } class Tree{ private TreeNode root ; //methods: addNode(TreeNode parent, TreeNode theNode) }

21 مقايسه آرايه ها و ليستهاي پيوندي Space (storage) – آرايه به تعداد حداکثر اعضاي ممکن فضا مي گيرد. اين تعداد بايد از قبل معين باشد – ليست پيوندي به تعداد اعضاي آن در حال حاضر ، جا مي گيرد. – ليست، فضاي اضافي براي نگهداري اشاره گرها نياز دارد Time – دسترسي به اعضاي ليست، نياز به توابع خاصي دارد – دسترسي به اعضاي آرايه به صورت تصادفي است – حذف و اضافه کردن به ليست سريعتر از آرايه است

22 تمرين Write Stack ADT and Queue ADT using Linked Lists( instead of arrays).