1 / 22

ليست هاي پيوندي Linked Lists

ليست هاي پيوندي Linked Lists. ساختمان داده ها و الگوريتم ها. آرايه اي با طول متغير. طول آرايه بعد از تعريف، ثابت مي ماند براي تعريف آرايه بزرگتر، ابتدا آرايه جديدي با طول کافي تعريف کرده و سپس آرايه قبلي را در آن کپي مي کنيد: روش مورد استفاده در : java.util.Vector و java.util.arrayList

consuelo
Télécharger la présentation

ليست هاي پيوندي Linked Lists

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. ليست هاي پيونديLinked Lists ساختمان داده ها و الگوريتم ها

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

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

  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. addToFront Initial list first next data A Add A to front newNode first first Add B to front A newNode B • Create a new node using A first.setNext( ) 1 Create a new node using B 2 Set newNode’s next to first.next 3 Set first to newNode

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

  11. removeFromFront data next first B A A Initial list Remove front (A) B Remove front (B) Remove front (???) Should throw an exception A

  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. removeFromBack data next first A B C secondLast node • 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

  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 first last A B C • مزايا و معايب • addToBackسريع : O(1) • حلقه موجود removeFromBackقابل حذف نيست • last needs to be updated by addToBack and removeFromBack, and by addToFront and removeFromFront in special cases • البته چند خط بايد بيشتر کد بنويسيد !

  17. Circular linked lists first A B C • 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 last How do we refer to the first node? A B C • The last node points to the first node (instead of null)

  18. Doubly-linked lists first last A B C • 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)

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

More Related