SlideServe
Menu
Préférences
© SlideServe · v2026

M180: Data Structures & Algorithms in Java

tahmores-nay avatar
19 diapositives · 414 Views · Nov 09, 2014

M180: Data Structures & Algorithms in Java. Linked Lists – Part 1. Arab Open University. Outline. Linked list nodes Linked list operations Insertion Append Deletion Other types of linked lists Doubly-linked Circular. Limitation of Arrays. An array has a limited number of elements

M180: Data Structures & Algorithms in Java - slide 1
01 / 19M180: Data Structures & Algorithms in Java
1 / 17

Télécharger la présentation

M180: Data Structures & Algorithms in Java
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. M180: Data Structures & Algorithms in Java Linked Lists – Part 1 Arab Open University

  2. Outline • Linked list nodes • Linked list operations • Insertion • Append • Deletion • Other types of linked lists • Doubly-linked • Circular

  3. Limitation of Arrays • An array has a limited number of elements • routines inserting a new value have to check that there is room • Can partially solve this problem by reallocating the array as needed (how much memory to add?) • adding one element at a time could be costly • one approach - double the current size of the array • A better approach: use a Linked List

  4. Dynamically Allocating Elements • Allocate elements one at a time as needed, have each element keep track of the next element • Result is referred to as linked list of elements, track next element with a pointer

  5. myList a b c d Anatomy of a linked list • A linked list consists of: • A sequence of nodes • Each node contains a value and a link(reference) to some other node The last node contains a null link The list may have a header

  6. More terminology • A node’s successor is the next node in the sequence • The last node has no successor • A node’s predecessor is the previous node in the sequence • The first node has no predecessor • A list’s length is the number of elements in it • A list may be empty (contain no elements)

  7. ListNode ListNode ListNode ListNode A0 A1 A2 A3 first Linked Lists • Stores a collection of items non-contiguously. • Each item in the list is stored with an indication of where the next item is. • Must know where first item is. • The list will be a chain of objects, called nodes, of type ListNode that contain the data and a reference to the next ListNode in the list. • Allows addition or deletion of items in the middle of collection with only a constant amount of data movement. Contrast this with array.

  8. ListNode: Definition public class ListNode <DataType> { DataType data; ListNode<DataType> next; // constructors ListNode(DataType d, ListNode<DataType> n) { data = d; next = n; } ListNode(DataType d) { this (d, null); } ListNode() { this (null); } }

  9. a a b c d b c d x current Linked List: Insertion • Insert X immediately after current position current

  10. a b current tmp Implementing Insertion: Step By Step • Insertion immediately after current position // create a new node tmp= new ListNode<DataType>();

  11. a b current tmp Implementing Insertion: Step By Step • Insertion immediately after current position // create a new node tmp= new ListNode<DataType>(); // place x in the element field tmp.data= x;

  12. a b current tmp Implementing Insertion: Step By Step • Insertion immediately after current position // create a new node tmp= new ListNode<DataType>(); // place x in the element field tmp.data= x; x

  13. a b current tmp Implementing Insertion: Step By Step • Insertion immediately after current position // create a new node tmp= new ListNode<DataType>(); // place x in the element field tmp.data= x; // x’s next node is b tmp.next= current.next; // a’s next node is x current.next= tmp; x

  14. a b current tmp Implementing Insertion: Shorter Version • A shorter version: // create a new node tmp = new ListNode<DataType>(x,current.next); // a’s next node is x current.next = tmp; x

  15. a b current tmp Implementing Insertion: Shorter Version • A shorter version: // create a new node tmp = new ListNode<DataType>(x,current.next); // a’s next node is x current.next = tmp; x

  16. a b c d a b c d X Implementing Append • Insert X immediately at the end of the list // last refers to the last node in the linked list last.next = new ListNode<DataType>(); last = last.next; // adjust last last.data = x; // place x in the node last.next = null; // adjust next • Most efficient approach last = last.next = new ListNode (x, null); last last

  17. a b x a b current current Implementing Basic Deletion • Delete an item immediately after current position • Basic deletion is a bypass in the linked list.

  18. a b x a b x current current current a b Implementing Basic Deletion • current.next = current.next.next; • Need a reference to node prior to the one to be deleted.

  19. A0 A1 A2 A3 first Iterate Through The Linked List • If items are stored in contiguous array: //step through array, outputting each item for (int index = 0; index < a.length; index++) System.out.println (a[index]); • If items are stored in a linked list: // step through list, outputting each item for(ListNode p=l.first; p!=null; p=p.next) System.out.println (p.data);