230 likes | 409 Views
ليست هاي پيوندي Linked Lists. ساختمان داده ها و الگوريتم ها. آرايه اي با طول متغير. طول آرايه بعد از تعريف، ثابت مي ماند براي تعريف آرايه بزرگتر، ابتدا آرايه جديدي با طول کافي تعريف کرده و سپس آرايه قبلي را در آن کپي مي کنيد: روش مورد استفاده در : java.util.Vector و java.util.arrayList
E N D
ليست هاي پيونديLinked Lists ساختمان داده ها و الگوريتم ها
آرايه اي با طول متغير • طول آرايه بعد از تعريف، ثابت مي ماند • براي تعريف آرايه بزرگتر، ابتدا آرايه جديدي با طول کافي تعريف کرده و سپس آرايه قبلي را در آن کپي مي کنيد: • روش مورد استفاده در :java.util.Vector و java.util.arrayList • زمانبر است • روش بهتر: به هر عضو آرايه، اشاره گري اضافه شود که نشانگر عضو بعدي باشد: • آرايه اي که به اين شکل ساخته شود، يک ليست پيوندي (Linked List) ناميده مي شود • يک کلاس + يک اشاره گر به کلاس هم نوع ديگر = Node • Node = Data + pointer
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)
The Node data structure • هر Node دو بخش دارد • يک بخش داده ها يا اطلاعات • يک اشاره گر به Node هاي ديگر • Methods • getData, setData, getNext, setNext (access data and pointer) • toString (converts the data to a string)
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(); }
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
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() ; …. }
The method isEmpty //Returns true if the linked list is empty, // or false if non-empty public boolean isEmpty( ) { return first.getNext() == null ; }
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
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
removeFromFront data next first B A A Initial list Remove front (A) B Remove front (B) Remove front (???) Should throw an exception A
length public int length( ){ int n = 0 ; for (C = first.Next() ; c != null ; c = c.getNext() ) n++ ; return n ; } • تعداد عناصر ليست را بر مي گرداند • پياده سازي در قالب يک Attribute • مثل آرايه ها: تعريف يک مشخصهlength و نگهداري، بروز رساني آن • بروز رساني هنگام حذف و اضافه نمودن يک Node به ليست
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 }
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
جستجو و پيدا کردن يک گره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 }
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 • البته چند خط بايد بيشتر کد بنويسيد !
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)
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)
ساختار گره و درخت class TreeNode{ private int data; TreeNode parent, child , next ; //Methods …. } class Tree{ private TreeNode root ; //methods: addNode(TreeNode parent, TreeNode theNode) }
مقايسه آرايه ها و ليستهاي پيوندي • Space (storage) • آرايه به تعداد حداکثر اعضاي ممکن فضا مي گيرد. اين تعداد بايد از قبل معين باشد • ليست پيوندي به تعداد اعضاي آن در حال حاضر ، جا مي گيرد. • ليست، فضاي اضافي براي نگهداري اشاره گرها نياز دارد • Time • دسترسي به اعضاي ليست، نياز به توابع خاصي دارد • دسترسي به اعضاي آرايه به صورت تصادفي است • حذف و اضافه کردن به ليست سريعتر از آرايه است
تمرين • Write Stack ADT and Queue ADT using Linked Lists( instead of arrays).