1 / 56

CS214 – Data Structures Lecture 8&9: Linked Lists

CS214 – Data Structures Lecture 8&9: Linked Lists. Slides by Mohamed El-Ramly, PhD. Cairo University. Faculty of Computers and Information. Data Structures CS 214 2 nd Term 2012-2013. Linked Lists. Readings. Read Mark Weiss, Chap 3 http://www.youtube.com/watch?v=LOHBGyK3Hbs

Télécharger la présentation

CS214 – Data Structures Lecture 8&9: 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. CS214 – Data StructuresLecture 8&9: Linked Lists Slides by Mohamed El-Ramly, PhD

  2. Cairo University Faculty of Computers and Information Data Structures CS 214 2nd Term 2012-2013 Linked Lists

  3. Readings Read Mark Weiss, Chap 3 http://www.youtube.com/watch?v=LOHBGyK3Hbs http://www.cosc.canterbury.ac.nz/mukundan/dsal/LinkListAppl.html Animated Data Structures http://www.ansatt.hig.no/frodeh/algmet/animate.html

  4. Agenda Single Linked Lists Double Linked Lists Circular Linked Lists STL support for linked lists

  5. Sites Learning a new language http://www.drdobbs.com/architecture-and-design/learning-new-languages/240150070 DrDobb’s Journal for Developers http://drdobbs.com/

  6. 1- Linear Data Structures • Linear Data Structures (1 to 1) • Stacks • Queues • Linked Lists

  7. 2- one-to-many Data Structures • Trees • Binary Search Tree Heap • B Tree and B+ Tree

  8. 3- Many-to-many Data Structures • Graphs • Can represent numerous problems • Graph theory is an independent science that you can take PhD in. • Numerous algorithms were developed. • Think of 20 problems that can be represented as graphs.

  9. 4- Non ordered Data Structures • Sets • Maps • Dictionaries • Hash Tables • (Items doe not have a particular order)

  10. What is wrong with arrays? • ++ Fast access of data with indices • -- Size has to be known at compile time • -- Resizing needs re-location • -- Insertion and removal are O(n) • Data are continuous in memory • Data need to be shifted up or down

  11. Single Linked Lists • Linked Lists avoid these problems by storing data in nodes that are linked by pointers. • Main operations: insert, delete, search • Better performance than arrays in some operations and worse perforamcne in others.

  12. SLL Node Class class IntNode { int info; IntNode* next; public: IntNode () {next = 0;} IntNode (int n, IntNode* in = 0) { next = in; info = n; } IntNode* getNext () {return next;} void setNext(IntNode* pt) {next = pt;} int getInfo () {return info;} void setInfo (int n) {info = n;} };

  13. Example SLL

  14. Code to build the Previous SLL int main () { IntNode* p = new IntNode (10); p-> setNext (new IntNode (8)); p-> getNext() -> setNext (new IntNode (50)); for (IntNode* pt = p; pt != 0; pt = pt->getNext()) cout << *pt; system ("pause"); }

  15. Code to build the Previous SLL friend ostream& operator<< (ostream& stream, IntNode node) { stream << node.getInfo(); stream << (node.getNext() == 0 ? "":"-> "); return stream; }

  16. Chapter 10: Linked Lists

  17. Chapter 10: Linked Lists

  18. SLL Class class IntSLL { private: IntNode *head, *tail; public: IntSLL() {head = tail = NULL;} ~IntSLL (); void addToHead(int item); void addToTail(int item); void removeFromHead(); void removeFromTail(); friend ostream& operator<< (ostream& out, IntSLL list); };

  19. SLL Operations • Insertion • at head • at tail • in the middle • Deletion • from head • from tail • from the middle • Search Chapter 10: Linked Lists

  20. Head Insertion in SLL • Create newNode • newNode->setNext (head) • head = newNode • If (tail == null) • tail = head; • O(1) Chapter 10: Linked Lists

  21. Head Insertion in SLL Chapter 10: Linked Lists

  22. Tail Insertion in SLL • Create newNode • If (tail == null) • head = tail = newNode • Else • tail->setNext (newNode) • tail = newNode • O(1)

  23. Tail Insertion in SLL Chapter 3: Linked Lists

  24. Middle Insertion in SLL • Get ptr of item before location to insert in • If (location == 0) • Insert at Head • Else • Create newNode(item, ptr->getNext()) • ptr->setNext(newNode) • 4. If (tail == ptr) • tail = tail->getNext(); • O(n)

  25. Middle Insertion in SLL • Create newNode • for (ptr = head; ptr != 0 && • count < index - 1; count++) • ptr = ptr->getNext(); • 4.if (ptr != 0 && !(index < 0)) • if (index == 0) • addToHead (item); • else • ptr->setNext(new IntNode • (item, ptr->getNext())); • 5. if (tail = ptr) • tail = tail->getNext(); • O(n)

  26. Chapter 10: Linked Lists

  27. Head Deletion in SLL • If head == tail// One node or nothing • head = tail = null • 2.Else • ptr = head • head = head -> getNext() • delete ptr • O(1) Chapter 10: Linked Lists

  28. Head Deletion Code in SLL • IntNode* ptr = head; • if (head == tail) • head = tail = 0; • else { • head = head->getNext(); • delete ptr; • } • O(1) Chapter 10: Linked Lists

  29. Head Deletion Code in SLL

  30. Tail Deletion in SLL • If head == tail// One node or nothing • head = tail = null • 2.Else • get ptr to location before tail • delete tail • tail = ptr • tail-> setNext(null) • O(n) Chapter 10: Linked Lists

  31. Tail Deletion Code in SLL • IntNode* ptr = head; • if (head == tail) • head = tail = 0; • else { • while (ptr->getNext() != tail) • ptr = ptr->getNext(); • delete tail; • tail = ptr; • tail->setNext(0); • } // O(n) Chapter 10: Linked Lists

  32. Tail Deletion Code in SLL

  33. Middle Deletion in SLL • Get item • If empty list • Return not found • 3.If item == head->getData() • Remove head • Retrun found • Else • Get ptr to nodewhose next • has the required item • If (ptr != null) • ptr->setNext (ptr->getNext()->getNext()) • Return found • Else Return not found • O(n)

  34. Middle Deletion in SLL

  35. Search in SLL • Get item • ptr = head • for(ptr != null && item not found) • ptr = ptr -> getNext() • 3.If ptr != null • return found • Else • return not found • O(n) Chapter 10: Linked Lists

  36. Search in SLL bool search (int value){ IntNode* ptr = head; while (ptr != 0 && !(ptr->getInfo() == value)) ptr = ptr -> getNext(); return ptr != 0; } O(n) Chapter 10: Linked Lists

  37. Chapter 10: Linked Lists

  38. Double Linked Lists • Insert Head, remove head • Insert middle, remove middle • Insert Tail, remove tail • Write these algorithms yourself Chapter 10: Linked Lists

  39. addToTail in Double Linked Lists Chapter 10: Linked Lists

  40. removeFromTail in Double Linked Lists Chapter 10: Linked Lists

  41. Circular Linked Lists • Read them from Adam Drozdek’s book, Chapter 3. Chapter 10: Linked Lists

  42. Circular Linked Lists Chapter 10: Linked Lists

  43. Chapter 10: Linked Lists

  44. Chapter 10: Linked Lists

  45. Self-Organizing Lists • Searching a Linked List is O(n) • Can we do better ? • Any ideas ? • We can, using one of these methods: • Move last accessed item to front method • Transpose method (swap towards head) • Order items by frequency of access • Keep the list sorted

  46. Self-Organizing Lists

  47. Chapter 10: Linked Lists

  48. STL Support for LLs • STL implements a generic double linked list with head and tail pointers. • #include <list> (or list.h) • Has many interface functions: • void clear() • bool empty() • insert (iterator,T) • void push_back (T) • void sort() • void size()

More Related