Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها."— Presentation transcript:

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

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

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

5 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 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 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 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 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 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 11 A removeFromFront Remove front (A) first BA datanext Initial list BA Remove front (B) Remove front (???) Should throw an exception

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

13 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 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 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 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 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 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 19 درخت با چندين فرزند

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

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

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


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

Similar presentations


Ads by Google