1 / 33

LAB#4

LAB#4 . Linked List. Overview Before we go to our lesson we must know about : data structure . Algorithms .

tave
Télécharger la présentation

LAB#4

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. LAB#4 Linked List

  2. Overview Before we go to our lesson we must know about : data structure . Algorithms . data structure is an arrangement of data in a computer’s memory (or sometimes on a disk). Data structures include linked lists, arry , stacks, binary trees, and hash tables. Algorithms is manipulate the data in these structures in various ways, such as inserting a new data item, searching for a particular item, or sorting the items.

  3. Overview of Data Structures A data structure is an arrangement of data in a computer’s memory (or sometimes on a disk). Data structures include linked lists, stacks, binary trees, and hash tables. Overview of Algorithms Algorithms manipulate the data in these structures in various ways, such as inserting a new data item, searching for a particular item, or sorting the items.

  4. Arrays: pluses and minuses + Fast element access. -- Impossible to resize. • Many applications require resizing! • Required size not always immediately available. • All of these problem in array we can fix it we linked list Linked Lists

  5. Q: What is the meaning of this symbol -> It mean Accessing Class MembersThrough an object of the class using (.)Through a pointer to the class using (->)For example:StudentS1;S1.setstnum(1234);Student *S1ptr;Or(*S1ptr).setstnum(1234);S1ptr->setstnum(1234);

  6. A C B A Linked List Linked List : 0 Tail Head • A linked listis a series of connected nodes. • Each node contains at least: • A piece of data (any type) • Pointer to the next node in the list • Head: pointer to the first node. • The last node points to NULL. node data pointer

  7. Linked List SinglyLinked List : • We use two classes: Node and List • Declare IntSLLNode class for the nodes class IntSLLNode { public: IntSLLNode() { next = 0; } IntSLLNode(inti, IntSLLNode *ptr = 0) { info = i; next = ptr; } int info; IntSLLNode *next; };

  8. Linked List SinglyLinked List : • Declare IntSLList which contains : class IntSLList { public: IntSLList() {head = tail =0; } void AddToHead(int); void AddToTail(int); void DeleteFromHead(); void DeleteFromTail(); void DeleteNode(int); boolisInList(int) const; void DisplayList(); private: IntSLLNode *head, *tail; };

  9. Allocate a new node Insert new element Make new node point to old head Update head to point to new node Inserting at the Head Linked Lists

  10. Linked List SinglyLinked List : • Declare IntSLList Class member function: 1- void AddToHead(int); • void IntSLList::AddToHead(int el) • { • head = new IntSLLNode(el,head); • if (tail == 0) • tail = head; • }

  11. Inserting at the Tail Allocate a new node Insert new element Have new node point to null Have old last node point to new node Update tail to point to new node Linked Lists

  12. Linked List SinglyLinked List : • Declare IntLList Class member function: 2- void AddToTail(int); void IntSLList::AddToTail(int el) { if (tail != 0) // if list not empty; { tail->next = new IntSLLNode(el); tail = tail->next; } else head = tail = new IntSLLNode(el); }

  13. Removing at the Head • Update head to point to next node in the list • Allow garbage collector to reclaim the former first node Linked Lists

  14. Linked List SinglyLinked List : • Declare IntLList Class member function: 3- voidDeleteFromHead(); void IntSLList::DeleteFromHead(){ if(head !=0) { IntSLLNode *tmp =head; if (head == tail) //if only one node in the list head = tail = 0; else head = head ->next; delete tmp;} }

  15. Removing at the Tail • Removing at the tail of a singly linked list cannot be efficient! • There is no constant-time way to update the tail to point to the previous node Linked Lists

  16. Linked List SinglyLinked List : 4- voidDeleteFromTail(); void IntSLList::DeleteFromTail() {if(head != 0) {if (head == tail) //if only one node in the list {delete head; head=tail=0;} else { IntSLLNode *tmp; //find the predecessor of tail for(tmp=head; tmp->next != tail; tmp = tmp->next); delete tail; tail=tmp; tail->next=0;}} }

  17. Linked List SinglyLinked List : • Declare IntLList Class member function: 5- intDeleteNode(int el);

  18. Linked List

  19. Linked List SinglyLinked List : • Declare IntLList Class member function: 6- boolisInList(int) const; boolIntSLList::isInList(int el) const { IntSLLNode *tmp; for (tmp=head; tmp != 0 && !(tmp->info == el); tmp = tmp->next); return tmp !=0; }

  20. Linked List SinglyLinked List : • Declare IntSLList Class member function: 7- void DisplayList(); void IntSLList::DisplayList() {IntSLLNode *current; current = head; cout << "head = " << head << "\n"; while(current != 0) {cout << current->info << " " << current << "\n"; current=current->next;} cout << "tail = " << tail << "\n"; cout << "----------------------" << "\n";}

  21. Linked List SinglyLinked List : • Using List : void main() {IntSLListmyllist; myllist.AddToHead(50); myllist.AddToHead(90); myllist.AddToHead(60); myllist.AddToHead(68); myllist.DisplayList(); myllist.DeleteFromHead(); myllist.DeleteNode(60); if (myllist.isInList(60)== 0) cout<<"60 isn't in the list" << endl; cout<<"60 is in the list" << endl; myllist.DisplayList();}

  22. Doubly Linked List • A doubly linked list is often more convenient! • Nodes store: • element • link to the previous node • link to the next node • Special trailer and header nodes prev next elem node trailer nodes/positions header elements Linked Lists

  23. Linked List DoublyLinked List : • Declare IntDLLNode which contains : class IntDLLNode{ public: IntDLLNode() {next=prev=0;} IntDLLNode(intel,IntDLLNode *n=0,IntDLLNode *p=0){ info = el; next=n; prev =p; } Int info; IntDLLNode *next, *prev; };

  24. Linked List DoublyLinked List : • Declare IntDLList which contains : class IntDLList{ public: IntDLList(){ Head=tail=0;} void addToDLLTail(int el); void deleteFromDLLTail(); void IntDLList::DisplayFromHead(); protected: IntDLLNode *head ,*tail; };

  25. Insertion • We visualize operation insertAfter(p, X), which returns position q p A B C p q A B C X p q A B X C Linked Lists

  26. Insertion Algorithm Algorithm insertAfter(p,e): Create a new node v v.setElement(e) v.setPrev(p) {link v to its predecessor} v.setNext(p.getNext()) {link v to its successor} (p.getNext()).setPrev(v) {link p’s old successor to v} p.setNext(v) {link p to its new successor, v} return v {the position for the element e} Linked Lists

  27. Linked List DoublyLinked List : • Declare IntDLList Class member function: 1- void addToDLLTail(int el); void IntDLList::addToDLLTail(int el) {if (tail!=0){ tail=new IntDLLNode(el,0,tail); tail->prev->next=tail;} else head=tail=new IntDLLNode(el); }

  28. p A B C D Deletion • We visualize remove(p), where p == last() A B C p D A B C Linked Lists

  29. Deletion Algorithm Algorithm remove(p): t = p.element {a temporary variable to hold the return value} (p.getPrev()).setNext(p.getNext()) {linking out p} (p.getNext()).setPrev(p.getPrev()) p.setPrev(null) {invalidating the position p} p.setNext(null) return t Linked Lists

  30. Linked List DoublyLinked List : • Declare IntDLList Class member function: 2- void deleteFromDLLTail() void IntDLList::deleteFromDLLTail() {if(tail !=0){ if(head==tail) { //if only one node in the list delete head; head = tail =0;} else { tail = tail->prev; delete tail->next; tail->next = 0;}}}

  31. Linked List DoublyLinked List : • Declare IntDLList Class member function: 2- void IntDLList::DisplayFromHead(); void IntDLList::DisplayFromHead() { IntDLLNode *current; for( current = head ; current != 0 ; current = current->next ) cout<<current->info<<endl; cout<<"--------------------------------"<<endl; }

  32. Evolution question • Create empty linked list and then add a new node with data 50

  33. Answer of Evolution question

More Related