Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists. COMP104 Lecture 33 / Slide 2 Linked Lists: Basic Idea * Data may be stored consecutively in a linked list. * Each element of the linked.

Similar presentations


Presentation on theme: "Linked Lists. COMP104 Lecture 33 / Slide 2 Linked Lists: Basic Idea * Data may be stored consecutively in a linked list. * Each element of the linked."— Presentation transcript:

1 Linked Lists

2 COMP104 Lecture 33 / Slide 2 Linked Lists: Basic Idea * Data may be stored consecutively in a linked list. * Each element of the linked list n Stores one piece of data n Points to the next element or NULL. * Linked lists provide natural implementation. * A linked list of integers: 20457585

3 COMP104 Lecture 33 / Slide 3 * Original linked list of integers: * Insertion by pointers: * Deletion by pointers: Linked Lists: Operations 20457585 20457585 20457585 60 old value deleted item

4 COMP104 Lecture 33 / Slide 4 Pointer Based Linked Lists * Linked lists store data in increasing order: * Definitions: 1. Defining a node (recursive datatype - use itself to define itself!) class node { public: int data; node* next; }; // end class typedef node* ptrType; // ptrType is a pointer to a node 20457585

5 COMP104 Lecture 33 / Slide 5 typedef  typedef allows you to make new shortcuts to existing types typedef int FunnyInt; FunnyInt k;// same as:int k; typedef int* IntPtr; IntPtr p;// same as: int *p; p = new node; *p = 100; 100 p

6 COMP104 Lecture 33 / Slide 6 Pointer Based Linked Lists * Definitions (cont’d): 2. To create/delete node P = new node; delete P; 3. The fields in P can be accessed using: P->data = 100;and P->next = NULL; Alternatively, we could also write: (*P).data = 100;and (*P).next = NULL; 100 p

7 COMP104 Lecture 33 / Slide 7 Accessing Linked Lists * We define a pointer ptrType Head; that points to the first node of the linked list. When the linked list is empty (empty list) then Head is NULL. 20457585 Head

8 COMP104 Lecture 33 / Slide 8 Traversing a Linked List * A linked list is displayed by visiting its nodes one by one, and displaying their data fields. void DisplayList(ptrType Head) { ptrType current = Head; while(current != NULL){ cout data << endl; current = current->next; }

9 COMP104 Lecture 33 / Slide 9 Traversing a Linked List * Alternatively, we can use for loop to traverse a linked list: void DisplayList(ptrType Head){ ptrType cur; for (cur = Head; cur != NULL; cur = cur->next) { cout data << endl; }

10 COMP104 Lecture 33 / Slide 10 Displaying a Linked List current = Head; current = current->next; 2045 Head current 2045 Head current

11 COMP104 Lecture 33 / Slide 11 (to delete) Deleting a Node * To delete a node from the list 1. Locate the node to be deleted (a) cur points to the node to be deleted. (b) prev points to cur ’s predecessor 2. Disconnect node from list using: prev->next = cur->next; 3. Return deleted node to system: delete cur; Head cur 20457585 prev...

12 COMP104 Lecture 33 / Slide 12 (to delete) Deleting the Head Node * When the node to be deleted is the first node, there is no predecessor node. Use: Head = Head->next; instead of: prev->next = cur->next; Head cur 20457585...

13 COMP104 Lecture 33 / Slide 13 Inserting a Node * To insert a new node into the list 1. (a) Create a new node using: newPtr = new node; (b) Fill in the data field correctly. 2. Find “prev” and “cur” such that the new node should be inserted between *prev (the node being pointed at by prev ) and *cur. 3. Connect the new node to the list by using: (a) newPtr->next = cur; (b) prev->next = newPtr;

14 COMP104 Lecture 33 / Slide 14 Inserting a Node Head 204575... 3(b) prev->next = newPtr; curprev 2. find prev and cur 33 newPtr 1. newPtr = new node 3(a) newPtr->next = cur ;

15 COMP104 Lecture 33 / Slide 15 Inserting a Node at the Beginning * When the new node is to be inserted at the beginning of the list there is no predecessor node. Use: newPtr->next = Head; Head = newPtr; instead of: newPtr->next = cur; prev->next = newPtr; Head newPtr 13 204575... cur newPtr->next = Head; Head = newPtr;

16 COMP104 Lecture 33 / Slide 16 Finding prev and cur * In previous slides we assumed that prev and cur were known. How did we find them?  Suppose that we want to insert or delete a node with data value newValue. Then the following code successfully finds prev and cur. prev = NULL; cur = Head; while(cur!=NULL && newValue > cur->data){ prev = cur; cur = cur->next; }

17 COMP104 Lecture 33 / Slide 17 Finding prev and cur * Insertion Example with newValue=60:... Head 204575 prev = NULL; cur = Head; while(cur!=NULL && newValue > cur->data){ prev = cur; cur = cur->next; } cur prev curprevcurprev

18 COMP104 Lecture 33 / Slide 18 Passing a Linked List to a Function  When passing a linked list to a function it should suffice to pass the value of Head. Using the value of Head the function can access the entire list.  Problem: If a function changes the beginning of a list by inserting or deleting a node, then Head will no longer point to the beginning of the list.  Solution: When passing Head always pass it by reference. 20457585 Head

19 COMP104 Lecture 33 / Slide 19 Pointer Based Linked Lists * We now describe how to implement a Linked-List ADT (Abstract Data Type) using pointers. * Main Points: Contains a “Size” field and a “Head” pointing to the list. n Implemented as a Class. n Uses “Deep Copy” in the copy-constructor to copy the entire data structure. n Destructor deallocates dynamic memory of object. 20457585 Head 4 Size

20 #include using namespace std; class listNode{ // a node on the list public: int data;// a data item on the list listNode *next; // pointer to next node }; typedef listNode* ptrType; // pointer to node

21 class listClass { public: // constructors and destructor: listClass(); listClass(const listClass& L); ~listClass(); // list operations: bool empty(); int length(); void add(int newdata); void del(int deldata); void print(); private: int Size; ptrType Head; ptrType new_and_check(); };

22 ptrType listClass::new_and_check() { // allocate a new node; returns only upon successful // memory allocation; else, exit ptrType temp; temp = new listNode; if (temp == NULL) { // not successful cerr << "Memory allocation error!" << endl; exit(1); } return temp; }

23 listClass::listClass(){ Size = 0; Head = NULL; } listClass::~listClass(){ while(Head!=NULL){ ptrType cur = Head; Head = Head->next; delete cur; } bool listClass::empty(){ if(Size==0) return true; else return false; } int listClass::length(){ return Size; }

24 listClass::listClass(const listClass& L){ Size = L.Size; if(L.Head==NULL){ Head = NULL; // empty list return; } Head = new_and_check (); // copy first node Head->data = L.Head->data; ptrType cur = Head;// new list pointer ptrType orig = L.Head->next; while(orig != NULL) { cur->next = new_and_check(); cur = cur->next; cur->data = orig->data; orig = orig->next; } cur->next = NULL; }

25 void listClass::add(int newdata){ Size++; ptrType newPtr = new listNode; if(newPtr==NULL){ cerr << "Memory error" << endl; exit(1); } newPtr->data = newdata; newPtr->next = NULL; ptrType prev, cur = Head; while(cur!=NULL && newdata > cur->data){ prev = cur; cur = cur->next; } if(cur==Head){ newPtr->next = Head; Head = newPtr; } else{ newPtr->next = cur; prev->next = newPtr; }

26 void listClass::del(int deldata){ ptrType prev, cur = Head; while(cur!=NULL && deldata > cur->data){ prev = cur; cur = cur->next; } if(cur==NULL || cur->data!=deldata){ cout << "Delete error: " << deldata << " not in list!" << endl; return; } if(cur==Head) Head = Head->next; else prev->next = cur->next; delete cur; Size--; }

27 void listClass::print(){ cout << "[ "; ptrType cur = Head; while(cur != NULL){ cout data << " "; cur = cur->next; } cout << "]" << endl; }

28 void main(){ listClass L; L.add(30); L.print(); listClass N(L); N.print(); L.add(20); L.print(); L.add(40); L.print(); L.add(10); L.print(); L.del(50); L.print(); L.del(40); L.print(); L.del(30); L.print(); L.del(20); L.print(); L.del(10); L.print(); }

29 COMP104 Lecture 33 / Slide 29 Output [ 30 ] [ 20 30 ] [ 20 30 40 ] [ 10 20 30 40 ] Delete error: 50 not in list! [ 10 20 30 40 ] [ 10 20 30 ] [ 10 20 ] [ 10 ] [ ]


Download ppt "Linked Lists. COMP104 Lecture 33 / Slide 2 Linked Lists: Basic Idea * Data may be stored consecutively in a linked list. * Each element of the linked."

Similar presentations


Ads by Google