1 / 52

Data Structures

Data Structures. 4 Lists and Linked List Data Structures Prof A Alkhorabi. Overview. List concepts. List applications. A list ADT: requirements, contract. Implementations of lists: using arrays, linked lists. Linked lists: singly-linked Insertion. Deletion. List Traversal

callie
Download Presentation

Data Structures

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. Data Structures 4Lists and Linked List Data Structures Prof A Alkhorabi

  2. Overview • List concepts. • List applications. • A list ADT: requirements, contract. • Implementations of lists: using arrays, linked lists. • Linked lists: singly-linked • Insertion. • Deletion. • List Traversal • Doubly-linked Lists

  3. List concepts (1) • A list is a sequence of elements, whose order is significant. Elements can be added and removed anywhere in the list. • The length of a list is the number of elements it contains. • An empty list has length zero. • The concatenation of lists l1 and l2 is a list containing all the elements of l1 followed by all the elements of l2. • Traversal of a list (or iterating over a list) means visiting each of the list’s elements in turn, in some desired order.

  4. List concepts (2) • Notation for lists: «a, b, …, z». The empty list is « ». • List notation is used here, but not supported by C++. • Examples of lists: fibonacci = «1, 1, 2, 3, 5, 8, 13, 21, 44, 65»birthdays = «2001-02-23, 2001-05-05, 2001-11-05»tour = «GLA, LHR, CDG, GLA»hamlet1 = «‘to’, ‘be’, ‘or’, ‘not’, ‘to’, ‘be’»hamlet2 = «‘that’, ‘is’, ‘the’, ‘question’» • Concatenation of hamlet1 and hamlet2: «‘to’, ‘be’, ‘or’, ‘not’, ‘to’, ‘be’, ‘that’, ‘is’, ‘the’, ‘question’»

  5. Lists vs. linked lists جواب 1 • Do not confuse the list ADT with a linked-list data structure. • A list ADT can be implemented using different data structures (arrays, linked lists). • Linked-list data structures can be used to implement many ADTs (e.g., stacks, queues, sets).

  6. List applications • A sentence is a list of words. • The order of words is significant. • The same word can occur more than once in a sentence. • An itinerary is a list of places visited on a tour. • The order of places is significant. • The same place can occur more than once in an itinerary. • A log is a list of event records (e.g., equipment faults). • The event records are in time order.

  7. Example: simple text editor (1) • Consider a simple text editor that supports insertion and deletion of complete lines only. • The user can load text from a file, or save the text to a file. • The user can select any line of the text: • directly (e.g., by a mouse-click) • by searching for the next line matching a given search string. • The user can delete the selected line. • The user can insert a new line, either before or after the selected line.

  8. Example: simple text editor (2) • We can represent the text being edited by: • a list of lines, text • the number of the selected line, sel(where 0 sel < length of text lines, or sel = –1 if text is empty) • We can implement the user commands straightforwardly in terms of list operations, e.g.: • Delete: remove line sel of text. • Insert before: add the new line as line sel of text, then increment sel. • Insert after: increment sel, then add the new line as line sel of text. • Save: traverse text, writing each line to the output file.

  9. List ADT: requirements جواب 2 Requirements: • It must be possible to make a list empty. • It must be possible to test whether a list is empty. • It must be possible to obtain the length of a list. • It must be possible to add an element anywhere in a list. • It must be possible to remove an element anywhere in a list. • It must be possible to inspect or update an element anywhere in a list. • It must be possible to concatenate lists. • It must be possible to test lists for equality. • It must be possible to traverse a list.

  10. List ADT: contract (1) • Possible contract: class List { // Each List object is an indexed list whose // elements are objects. ////////// Accessors ////////// bool IsEmpty (); // Return true if and only if this list is empty. int Size (); // Return this list’s length.

  11. List ADT: contract (2) Possible contract (continued): void Get (int i); // Return the element with index i in this list. bool Equals (List that);// Return true if and only if this list and that list have the // same length, and each element of this list equals the // corresponding element of that.

  12. List ADT: contract (3) • Possible contract (continued): //////////// Transformers //////////// void Clear (); // Make this list empty. void Set (int i, Elem elem); // Replace the element at index i in this list by elem. // The element could be a structure or a single piece of Info void Add (int i, Elem elem); // Add elem as the element with index i in this list. void Add (Elem elem); // Add elem after the last element of this list.

  13. List ADT: contract (4) • Possible contract (continued): void AddAll (List that); // Add all the elements of that after the last element of // this list. void Del (int i); // Remove the element with index i in this list. }

  14. List traversal جواب 4 • Traverse a data structure is to visit its components (elements, nodes), almost serially • To traverse array a: for (int i = 0; i < a.Length; i++)… a[i] … • We could mimic this to traverse list: for (int i = 0; i < list.Size(); i++)… list.Get(i) …… list.Set(i, x) … This traversal has time complexity O(n2),if Get and Set is O(n) each.

  15. Linked Data Structures • Linked data structures (or simply data structures), are structures represent objects linked in one of the following types: • Linear data structures: • Linked lists: single, double, circular. • Stack. • Queue. • Non-Linear data structures : • Tree: binary, multi-branch. • Graph.

  16. Implementation • Any of the above mentioned data structures can also be implemented in one of the following ways: • Arrays • Pointers (known as linked list method)

  17. first element last element element element element Invariant: Empty list: Illustration: GLA LHR CDG GLA Implementation of lists using SLLs • Represent an (unbounded) list by: • a variable length • a SLL, with links first and last to both ends:

  18. ant bat cat ant bat cat Linked lists (1) • A linked list consists of a sequence of nodes connected by links, plus a header. • Each node (except the last) has a successor, and each node (except the first) has a predecessor. • Each node contains a single element (object or value), plus links to its successor and/or predecessor. header node element link null link

  19. Linked lists (2) • The length of a linked list is the number of nodes. • An empty linked list has no nodes. • In a linked list: • We can manipulate the individual elements. • We can manipulate the links, thus changing the linked list’s very structure! (This is impossible in an array.)

  20. pig dog cat rat dog Singly-linked lists (1) • A singly-linked list (SLL) consists of a sequence of nodes, connected by links in one direction only. • Each SLL node contains a single element, plus a link to the node’s successor (or a null link if the node has no successor). • A SLL header contains a link to the SLL’s first node (or a null link if the SLL is empty).

  21. Singly-linked lists (2) • A C++ structure is used to implement a SLL node: // SLL Node declaration struct SLLNode{ char Data[10]; // Node Information SLLNode* Next; // Node Communication (SLL-Link) };

  22. Singly-linked lists (3) • A C++ class is used to implement the Single Linked List: // SLL declaration class SingleLinkedList { private: SLLNode *First, *Last; int Length; public: //////////// Constructor //////////// SingleLinkedList () { First = Last = NULL; Length = 0; } ; … }; SLL methods (to follow)

  23. first first ant ant bat bat cat cat curr first ant bat cat curr first ant bat cat curr First ant bat cat curr Example: SLL traversal • A method (in class SLL) to traverse an SLL: // Traverse the SLL and print its Nodes void SingleLinkedList::PrintList(){ SLLNode* curr = First; // Start from first node while(curr){ cout << curr -> Data << " "; curr = curr -> Next; } cout << endl; } • Animation:

  24. first ant bat cat first ant bat cat second first ant bat cat second first ant bat cat second First ant bat cat Second Example: SLL manipulation (Swaping) • A method (in class SLL) to swap an SLL’s first and second nodes: void swapFirstTwo () {// Swap this SLL’s 1st and 2nd nodes (assuming length > 1). SLLNode* Second; Second = First -> Next; First -> Next = Second -> Next; Second -> Next = First; First = Second;} • Animation:

  25. Insertion • Problem: Insert a new element at a given point in a linked list. • Four cases to consider: • insertion in an empty linked list; • insertion before the first node of a nonempty linked list; • insertion after the last node of a nonempty linked list; • insertion between nodes of a nonempty linked list. • The insertion algorithm needs links to the new node’s successor and predecessor. • We will consider case 2 and 4 in our SLL.

  26. SLL insertion (1) • SLL insertion algorithm: To insert elem at a given point in the SLL headed by First: 1. Make ins a link (pointer) to a newly-created node with element elem and successor null.2. If the insertion point is before the first node: 2.1. Set node ins’s successor to first. 2.2. Set first to ins.3. If the insertion point is before the node pred: 3.1. Set node ins’s successor to node pred’s successor. 3.2. Set node pred’s successor to ins.4. Terminate.

  27. To insert elem at a given point in the SLL headed by first:1. Make ins a link to a newly-created node with elementelem and successor null.2. If the insertion point is before the first node: 2.1. Set node ins’s successor to first. 2.2. Set first to ins.3. If the insertion point is after the node pred: 3.1. Set node ins’s successor to node pred’s successor. 3.2. Set node pred’s successor to ins.4. Terminate. To insert elem at a given point in the SLL headed by first:1. Make ins a link to a newly-created node with element elem and successor null.2. If the insertion point is before the first node: 2.1. Set node ins’s successor to first. 2.2. Set first to ins.3. If the insertion point is after the node pred: 3.1. Set node ins’s successor to node pred’s successor. 3.2. Set node pred’s successor to ins.4. Terminate. To insert elem at a given point in the SLL headed by first:1. Make ins a link to a newly-created node with element elem and successor null.2. If the insertion point is before the first node: 2.1. Set node ins’s successor to first. 2.2. Set first to ins.3. If the insertion point is after the node pred: 3.1. Set node ins’s successor to node pred’s successor. 3.2. Set node pred’s successor to ins.4. Terminate. To insert elem at a given point in the SLL headed by first:1. Make ins a link to a newly-created node with element elem and successor null.2. If the insertion point is before the first node: 2.1. Set node ins’s successor to first. 2.2. Set first to ins.3. If the insertion point is after the node pred: 3.1. Set node ins’s successor to node pred’s successor. 3.2. Set node pred’s successor to ins.4. Terminate. To insert elem at a given point in the SLL headed by first:1. Make ins a link to a newly-created node with elementelem and successor null.2. If the insertion point is after the first node: 2.1. Set node ins’s successor to first. 2.2. Set first to ins.3. If the insertion point is after the node pred: 3.1. Set node ins’s successor to node pred’s successor. 3.2. Set node pred’s successor to ins.4. Terminate. first first first first bat bat bat bat cat cat cat cat ant ant ant ins ins ins first bat cat ant SLL insertion (2) • Animation (insertion beforefirst node):

  28. To insert elem at a given point in the SLL headed by first:1. Make ins a link to a newly-created node with element elem and successor null.2. If the insertion point is before the first node: 2.1. Set node ins’s successor to first. 2.2. Set first to ins.3. If the insertion point is after the node pred: 3.1. Set node ins’s successor to node pred’s successor. 3.2. Set node pred’s successor to ins.4. Terminate. To insert elem at a given point in the SLL headed by first:1. Make ins a link to a newly-created node with element elem and successor null.2. If the insertion point is before the first node: 2.1. Set node ins’s successor to first. 2.2. Set first to ins.3. If the insertion point is after the node pred: 3.1. Set node ins’s successor to node pred’s successor. 3.2. Set node pred’s successor to ins.4. Terminate. To insert elem at a given point in the SLL headed by first:1. Make ins a link to a newly-created node with element elem and successor null.2. If the insertion point is before the first node: 2.1. Set node ins’s successor to first. 2.2. Set first to ins.3. If the insertion point is after the node pred: 3.1. Set node ins’s successor to node pred’s successor. 3.2. Set node pred’s successor to ins.4. Terminate. To insert elem at a given point in the SLL headed by first:1. Make ins a link to a newly-created node with element elem and successor null.2. If the insertion point is before the first node: 2.1. Set node ins’s successor to first. 2.2. Set first to ins.3. If the insertion point is after the node pred: 3.1. Set node ins’s successor to node pred’s successor. 3.2. Set node pred’s successor to ins.4. Terminate. To insert elem at a given point in the SLL headed by first:1. Make ins a link to a newly-created node with element elem and successor null.2. If the insertion point is after the first node: 2.1. Set node ins’s successor to first. 2.2. Set first to ins.3. If the insertion point is after the node pred: 3.1. Set node ins’s successor to node pred’s successor. 3.2. Set node pred’s successor to ins.4. Terminate. first first first first dog dog dog dog fox fox fox fox eel eel eel pred pred pred pred ins ins ins first dog fox eel SLL insertion (3) • Animation (insertion after intermediate node):

  29. SLL insertion (4) The following C++ inserting method adds a node at the end of SLL: void Add(char* c) { SLLNode* p; // 1. Declare a pointer to the node type p = new SLLNode; // 2. Allocate memory to the new node // 3. Fill Node strcpy( p -> Data, c); p -> Next = NULL; // 4. Link the Filled Node to the Linked List if(Length == 0) First = p; // If SLL is empty p will be the first else Last->Next = p; // Otherwise put p after Last Last=p; Length++; }

  30. Deletion • Problem: Delete a given node from a linked list. • Three cases to consider for deletion of a singleton node; • deletion of the first node; • deletion of the last node; • deletion of an intermediate node. • The deletion algorithm needs links to the deleted node’s successor and predecessor. • We will consider case 1 and 3 in our SLL.

  31. SLL deletion (1) • SLL deletion algorithm: To delete node del from the SLL headed by first: 1. Let succ be node del’s successor.2. If del = first: 2.1. Set first to succ.3. Otherwise (if delfirst): 3.1. Let pred be node del’s predecessor. 3.2. Set node pred’s successor to succ.4. Terminate. • But there is no link from node del to its predecessor, so step 3.1 can access del’s predecessor only by following links from first!

  32. To delete node del from the SLL headed by first:1. Let succ be node del’s successor.2. If del = first: 2.1. Set first to succ.3. Otherwise (if delfirst): 3.1. Let pred be node del’s predecessor. 3.2. Set node pred’s successor to succ.4. Terminate. To delete node del from the SLL headed by first:1. Let succ be node del’s successor.2. If del = first: 2.1. Set first to succ.3. Otherwise (if delfirst): 3.1. Let pred be node del’s predecessor. 3.2. Set node pred’s successor to succ.4. Terminate. To delete node del from the SLL headed by first:1. Let succ be node del’s successor.2. If del = first: 2.1. Set first to succ.3. Otherwise (if delfirst): 3.1. Let pred be node del’s predecessor. 3.2. Set node pred’s successor to succ.4. Terminate. To delete node del from the SLL headed by first:1. Let succ be node del’s successor.2. If del = first: 2.1. Set first to succ.3. Otherwise (if delfirst): 3.1. Let pred be node del’s predecessor. 3.2. Set node pred’s successor to succ.4. Terminate. first first first first ant ant ant ant bat bat bat bat cat cat cat cat del del del succ succ garbage SLL deletion (2) • Animation (deleting the first node):

  33. first ant bat cat First ant bat cat Example: SLL manipulation (1) • Instance method (in class SLL) to delete an SLL’s first node: void DeleteFirst () {// Delete this SLL’s first node (assuming length > 0). First = First -> Next;} • Animation:

  34. To delete node del from the SLL headed by first:1. Let succ be node del’s successor.2. If del = first: 2.1. Set first to succ.3. Otherwise (if delfirst): 3.1. Let pred be node del’s predecessor. 3.2. Set node pred’s successor to succ.4. Terminate. To delete node del from the SLL headed by first:1. Let succ be node del’s successor.2. If del = first: 2.1. Set first to succ.3. Otherwise (if delfirst): 3.1. Let pred be node del’s predecessor. 3.2. Set node pred’s successor to succ.4. Terminate. To delete node del from the SLL headed by first:1. Let succ be node del’s successor.2. If del = first: 2.1. Set first to succ.3. Otherwise (if delfirst): 3.1. Let pred be node del’s predecessor. 3.2. Set node pred’s successor to succ.4. Terminate. To delete node del from the SLL headed by first:1. Let succ be node del’s successor.2. If del = first: 2.1. Set first to succ.3. Otherwise (if delfirst): 3.1. Let pred be node del’s predecessor. 3.2. Set node pred’s successor to succ.4. Terminate. To delete node del from the SLL headed by first:1. Let succ be node del’s successor.2. If del = first: 2.1. Set first to succ.3. Otherwise (if delfirst): 3.1. Let pred be node del’s predecessor. 3.2. Set node pred’s successor to succ.4. Terminate. first first first first first dog dog dog dog dog eel eel eel eel eel fox fox fox fox fox pred pred del del del del succ succ succ garbage SLL deletion (3) • Animation (deleting an intermediate (or last) node):

  35. first ant bat cat first ant bat cat second First ant bat cat Second Example: SLL manipulation (2) • Instance method (in class SLL) to delete an SLL’s second node: void deleteSecond () {// Delete this SLL’s second node (assuming length > 1). SLLNode* Second; Second = First -> Next; First -> Next = Second -> Next;} • Animation:

  36. SLL deletion (4) • Analysis: Let n be the SLL’s length. Step 3.1 must visit all nodes from the first node to the deleted node’s predecessor. There are between 0 and n–1 such nodes. Average no. of nodes visited = (n – 1)/2 Time complexity is O(n).

  37. SLL deletion (5) • The following method deletes the node of an SLL which carry the string similar to what is pointed to by c pointer: void SingleLinkedList::Del(char* c) { SLLNode* p=First, *q; if( !strcmp (p->Data, c)) { First=First->Next; delete p; Length--; } else{ while (p){ q = p; p = p->Next; if( !strcmp (p->Data, c)) { q->Next=p->Next; delete p; p=NULL; Length--; } } } } Example: SLL_CPP.CPP

  38. The SLL class can be tested through the following main() function: void main() { SingleLinkedList SLL1; //1 SLL1.Add("Salem"); SLL1.PrintList(); //2 cout<< "No of nodes in SLL = " << SLL1.size() << endl; SLL1.Add("Ahmed"); SLL1.PrintList(); //3 SLL1.Add("Ali"); SLL1.PrintList(); //4 SLL1.Add("Omar"); SLL1.PrintList(); //5 cout<< "No of nodes in SLL = " << SLL1.size() << endl << endl; cout<< "Person in First node of SLL is " << SLL1.get(0) << endl; cout<< "Person in Second node of SLL is " << SLL1.get(1) << endl; cout<< "Person in Third node of SLL is ”<< SLL1.get(2) << endl; cout<< "Person in Fourth node of SLL is " << SLL1.get(3) << endl << endl; SLL1.Del("Salem"); SLL1.PrintList(); //6 SLL1.Del("Ali"); SLL1.PrintList(); //7 cout<< "No of nodes in SLL = " << SLL1.size() << endl; }

  39. The Output is: Salem No of nodes in SLL = 1 Salem Ahmed Salem Ahmed Ali Salem Ahmed Ali Omar No of nodes in SLL = 4 Person in First node of SLL is Salem Person in Second node of SLL is Ahmed Person in Third node of SLL is Ali Person in Fourth node of SLL is Omar Ahmed Ali Omar Ahmed Omar No of nodes in SLL = 2

  40. pig dog cat rat dog Doubly-linked lists (1) • A doubly-linked list (DLL) consists of a sequence of nodes, connected by links in both directions. • Each DLL node contains a single element, plus links to the node’s successor and predecessor (or null link(s)). • The DLL header contains links to the DLL’s first node (or null link if the DLL is empty).

  41. Doubly-linked lists (2) • C++ structure implementing DLL node: // DLL Node declaration struct DLLNode{ char Data[10]; // Node Information DLLNode * Next, *Pred; // Node Communication (DLL-Link) };

  42. Doubly-linked lists (3) • C++ class implementing a DLL: // DLL declaration class DoubleLinkedList { private: DLLNode *First, *Last; int Length; public: //////////// Constructor //////////// DoubleLinkedList () { First = Last = NULL; Length = 0; } ; … }; DLL methods (to follow)

  43. first ant bat cat last first ant bat cat last curr first ant bat cat last curr first ant bat cat last curr First ant bat cat Last curr Example: DLL traversal • Instance method (in class DLL) to traverse a DLL, from last node to first: publicvoid printLastToFirst () {// Print all elements in this DLL, in last-to-first order.for (DLLNode curr = Last; curr != Null; curr = curr -> pred) cout << curr-> Data;} • Animation:

  44. first ant bat cat last first ant bat cat last second first ant bat cat last second First ant bat cat Last Second Example: DLL manipulation (1) • Instance method (in class DLL) to delete a DLL’s first node: publicvoid deleteFirst () {// Delete the DLL’s first node (assuming length > 0). DLLNode *Second; Second = First -> Next; Second -> Pred = Null; First = Second;} • Animation:

  45. first ant bat cat last first ant bat cat last Last2 first ant bat cat last Last2 First ant bat cat Last Last2 Example: DLL manipulation (2) • Instance method (in class DLL) to delete a DLL’s last node: publicvoid deleteLast () {// Delete this DLL’s last node (assuming length > 0). DLLNode* Last2; Last2 = Last -> Pred; Last2 -> Next = Null; Last = Last2;} • Animation:

  46. To insert elem at a given point in the DLL headed by (first, last):1. Make ins a link to a newly-created node with element elem, predecessor null, and successor null.2. Insert ins at the insertion point in the forward SLL headed by first.3. Let succ be ins’s successor (or null if ins has no successor).4. Insert ins after node succ in the backward SLL headed by last.5. Terminate. To insert elem at a given point in the DLL headed by (first, last):1. Make ins a link to a newly-created node with element elem, predecessor null, and successor null.2. Insert ins at the insertion point in the forward SLL headed by first.3. Let succ be ins’s successor (or null if ins has no successor).4. Insert ins after node succ in the backward SLL headed by last.5. Terminate. To insert elem at a given point in the DLL headed by (first, last):1. Make ins a link to a newly-created node with element elem, predecessor null, and successor null.2. Insert ins at the insertion point in the forward SLL headed by first.3. Let succ be ins’s successor (or null if ins has no successor).4. Insert ins after node succ in the backward SLL headed by last.5. Terminate. To insert elem at a given point in the DLL headed by (first, last):1. Make ins a link to a newly-created node with element elem, predecessor null, and successor null.2. Insert ins at the insertion point in the forward SLL headed by first.3. Let succ be ins’s successor (or null if ins has no successor).4. Insert ins after node succ in the backward SLL headed by last.5. Terminate. To insert elem at a given point in the DLL headed by (first, last):1. Make ins a link to a newly-created node with element elem, predecessor null, and successor null.2. Insert ins at the insertion point in the forward SLL headed by first.3. Let succ be ins’s successor (or null if ins has no successor).4. Insert ins after node succ in the backward SLL headed by last.5. Terminate. To insert elem at a given point in the DLL headed by (first, last):1. Make ins a link to a newly-created node with element elem, predecessor null, and successor null.2. Insert ins at the insertion point in the forward SLL headed by first.3. Let succ be ins’s successor (or null if ins has no successor).4. Insert ins after node succ in the backward SLL headed by last.5. Terminate. ant ant ant ant ant ins ins ins ins first first first first first first bat bat bat bat bat bat cat cat cat cat cat cat last last last last last last succ succ DLL insertion (4) • Animation (insertion before the first node):

  47. To insert elem at a given point in the DLL headed by (first, last):1. Make ins a link to a newly-created node with element elem, predecessor null, and successor null.2. Insert ins at the insertion point in the forward SLL headed by first.3. Let succ be ins’s successor (or null if ins has no successor).4. Insert ins after node succ in the backward SLL headed by last.5. Terminate. To insert elem at a given point in the DLL headed by (first, last):1. Make ins a link to a newly-created node with element elem, predecessor null, and successor null.2. Insert ins at the insertion point in the forward SLL headed by first.3. Let succ be ins’s successor (or null if ins has no successor).4. Insert ins after node succ in the backward SLL headed by last.5. Terminate. To insert elem at a given point in the DLL headed by (first, last):1. Make ins a link to a newly-created node with element elem, predecessor null, and successor null.2. Insert ins at the insertion point in the forward SLL headed by first.3. Let succ be ins’s successor (or null if ins has no successor).4. Insert ins after node succ in the backward SLL headed by last.5. Terminate. To insert elem at a given point in the DLL headed by (first, last):1. Make ins a link to a newly-created node with element elem, predecessor null, and successor null.2. Insert ins at the insertion point in the forward SLL headed by first.3. Let succ be ins’s successor (or null if ins has no successor).4. Insert ins after node succ in the backward SLL headed by last.5. Terminate. To insert elem at a given point in the DLL headed by (first, last):1. Make ins a link to a newly-created node with element elem, predecessor null, and successor null.2. Insert ins at the insertion point in the forward SLL headed by first.3. Let succ be ins’s successor (or null if ins has no successor).4. Insert ins after node succ in the backward SLL headed by last.5. Terminate. To insert elem at a given point in the DLL headed by (first, last):1. Make ins a link to a newly-created node with element elem, predecessor null, and successor null.2. Insert ins at the insertion point in the forward SLL headed by first.3. Let succ be ins’s successor (or null if ins has no successor).4. Insert ins after node succ in the backward SLL headed by last.5. Terminate. dog dog dog dog dog ins ins ins ins first first first first first first bat bat bat bat bat bat cat cat cat cat cat cat last last last last last last succ succ DLL insertion (5) • Animation (insertion after the last node):

  48. To insert elem at a given point in the DLL headed by (first, last):1. Make ins a link to a newly-created node with element elem, predecessor null, and successor null.2. Insert ins at the insertion point in the forward SLL headed by first.3. Let succ be ins’s successor (or null if ins has no successor).4. Insert ins after node succ in the backward SLL headed by last.5. Terminate. To insert elem at a given point in the DLL headed by (first, last):1. Make ins a link to a newly-created node with element elem, predecessor null, and successor null.2. Insert ins at the insertion point in the forward SLL headed by first.3. Let succ be ins’s successor (or null if ins has no successor).4. Insert ins after node succ in the backward SLL headed by last.5. Terminate. To insert elem at a given point in the DLL headed by (first, last):1. Make ins a link to a newly-created node with element elem, predecessor null, and successor null.2. Insert ins at the insertion point in the forward SLL headed by first.3. Let succ be ins’s successor (or null if ins has no successor).4. Insert ins after node succ in the backward SLL headed by last.5. Terminate. To insert elem at a given point in the DLL headed by (first, last):1. Make ins a link to a newly-created node with element elem, predecessor null, and successor null.2. Insert ins at the insertion point in the forward SLL headed by first.3. Let succ be ins’s successor (or null if ins has no successor).4. Insert ins after node succ in the backward SLL headed by last.5. Terminate. To insert elem at a given point in the DLL headed by (first, last):1. Make ins a link to a newly-created node with element elem, predecessor null, and successor null.2. Insert ins at the insertion point in the forward SLL headed by first.3. Let succ be ins’s successor (or null if ins has no successor).4. Insert ins after node succ in the backward SLL headed by last.5. Terminate. To insert elem at a given point in the DLL headed by (first, last):1. Make ins a link to a newly-created node with element elem, predecessor null, and successor null.2. Insert ins at the insertion point in the forward SLL headed by first.3. Let succ be ins’s successor (or null if ins has no successor).4. Insert ins after node succ in the backward SLL headed by last.5. Terminate. eel eel eel eel eel ins ins ins ins fox fox fox fox fox fox first first first first first first dog dog dog dog dog dog last last last last last last succ succ DLL insertion (6) • Animation (insertion between nodes):

  49. DLL deletion (1) • DLL deletion algorithm: To delete node del from the DLL headed by (first, last): 1. Let pred and succ be node del’s predecessor and successor.2. Delete node del, whose predecessor is pred, from the forward SLL headed by first.3. Delete node del, whose successor is succ, from the backward SLL headed by last.4. Terminate.

  50. To delete node del from the DLL headed by (first, last):1. Let pred and succ be node del’s predecessor and successor.2. Delete node del, whose predecessor is pred, from the forward SLL headed by first.3. Delete node del, whose successor is succ, from the backward SLL headed by last.4. Terminate. To delete node del from the DLL headed by (first, last):1. Let pred and succ be node del’s predecessor and successor.2. Delete node del, whose predecessor is pred, from the forward SLL headed by first.3. Delete node del, whose successor is succ, from the backward SLL headed by last.4. Terminate. To delete node del from the DLL headed by (first, last):1. Let pred and succ be node del’s predecessor and successor.2. Delete node del, whose predecessor is pred, from the forward SLL headed by first.3. Delete node del, whose successor is succ, from the backward SLL headed by last.4. Terminate. To delete node del from the DLL headed by (first, last):1. Let pred and succ be node del’s predecessor and successor.2. Delete node del, whose predecessor is pred, from the forward SLL headed by first.3. Delete node del, whose successor is succ, from the backward SLL headed by last.4. Terminate. To delete node del from the DLL headed by (first, last):1. Let pred and succ be node del’s predecessor and successor.2. Delete node del, whose predecessor is pred, from the forward SLL headed by first.3. Delete node del, whose successor is succ, from the backward SLL headed by last.4. Terminate. del del del del first first first first first ant ant ant ant ant bat bat bat bat bat cat cat cat cat cat last last last last last pred pred pred succ succ succ DLL deletion (2) • Animation (deleting the first (but not last) node):

More Related