580 likes | 658 Views
Learn about the functionality and benefits of linked lists in data structures, including insertion, deletion, and search operations. Dive into examples and code snippets to understand implementation. Enhance your programming skills with this comprehensive guide.
E N D
CS214 – Data StructuresLecture 8&9: Linked Lists Slides by Mohamed El-Ramly, PhD
Cairo University Faculty of Computers and Information Data Structures CS 214 2nd Term 2012-2013 Linked Lists
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
Agenda Single Linked Lists Double Linked Lists Circular Linked Lists STL support for linked lists
Sites Learning a new language http://www.drdobbs.com/architecture-and-design/learning-new-languages/240150070 DrDobb’s Journal for Developers http://drdobbs.com/
1- Linear Data Structures • Linear Data Structures (1 to 1) • Stacks • Queues • Linked Lists
2- one-to-many Data Structures • Trees • Binary Search Tree Heap • B Tree and B+ Tree
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.
4- Non ordered Data Structures • Sets • Maps • Dictionaries • Hash Tables • (Items doe not have a particular order)
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
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.
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;} };
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"); }
Code to build the Previous SLL friend ostream& operator<< (ostream& stream, IntNode node) { stream << node.getInfo(); stream << (node.getNext() == 0 ? "":"-> "); return stream; }
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); };
SLL Operations • Insertion • at head • at tail • in the middle • Deletion • from head • from tail • from the middle • Search Chapter 10: Linked Lists
Head Insertion in SLL • Create newNode • newNode->setNext (head) • head = newNode • If (tail == null) • tail = head; • O(1) Chapter 10: Linked Lists
Head Insertion in SLL Chapter 10: Linked Lists
Tail Insertion in SLL • Create newNode • If (tail == null) • head = tail = newNode • Else • tail->setNext (newNode) • tail = newNode • O(1)
Tail Insertion in SLL Chapter 3: Linked Lists
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)
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)
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
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
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
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
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)
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
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
Double Linked Lists • Insert Head, remove head • Insert middle, remove middle • Insert Tail, remove tail • Write these algorithms yourself Chapter 10: Linked Lists
addToTail in Double Linked Lists Chapter 10: Linked Lists
removeFromTail in Double Linked Lists Chapter 10: Linked Lists
Circular Linked Lists • Read them from Adam Drozdek’s book, Chapter 3. Chapter 10: Linked Lists
Circular Linked Lists Chapter 10: Linked Lists
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
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()