1 / 31

Programming

Programming. Linked Lists. Motivation. A “List” is a useful structure to hold a collection of data. Currently, we use arrays for lists Examples: List of ten students marks int studentMarks[10]; List of temperatures for the last two weeks double temperature[14];.

josephmills
Download Presentation

Programming

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. Programming Linked Lists

  2. Motivation • A “List” is a useful structure to hold a collection of data. • Currently, we use arrays for lists • Examples: List of ten students marks int studentMarks[10]; List of temperatures for the last two weeks double temperature[14];

  3. Motivation • list using static array int myArray[10]; We have to decide in advance the size of the array (list) • list using dynamic array int n, *myArray; cin >> n; myArray = new int[n]; We allocate an array (list) of any specified size while the program is running • linked-list (dynamic size) size = ?? The list is dynamic. It can grow and shrink to any size.

  4. Link Data 20 45 75 85 Linked Lists: Basic Idea • A linked list is an ordered collection of data • Each element of the linked list has • Some data • A link to the next element • The link is used to chain the data • Example: A linked list of integers:

  5. 20 45 add(75), add(85) 20 45 75 85 delete(85), delete(45), delete(20) Linked Lists: Basic Ideas • The list can grow and shrink 75

  6. Linked Lists: Operations • Original linked list of integers: • Insertion: • Deletion 20 45 75 85 old value 20 45 75 85 60 20 45 75 85 deleted item

  7. #include <iostream> using namespace std; struct Node{ int data; Node *next; }; typedef Node* NodePtr;

  8. typedef • typedef allows you to make new shortcuts to existing types typedef int WAH; WAH k; // same as: int k; typedef int* WAHPTR; WAHPTR p; // same as: int *p; typedef Node* NodePtr; NodePtr Head; // same as: Node* Head;

  9. Linked List Structure • Node : Data + Link • Definition struct Node { int data; //contains useful information Node* next; //points to next element or NULL }; • Create a Node Node* p; p = new Node; //points to newly allocated memory • Delete a Node delete p;

  10. Linked List Structures • Access fields in a node (*p).data; //access the data field (*p).next; //access the pointer field Or it can be accessed this way p->data //access the data field p->next //access the pointer field

  11. Head 20 45 75 85 Representing and accessing Linked Lists • We define a pointer NodePtr Head; that points to the first node of the linked list. When the linked list is empty then Head is NULL.

  12. Head 20 45 75 85 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 (or using a function to return a new pointer value)

  13. Manipulation of a Unsorted Linked List

  14. Head 20 newPtr Start the first node from scratch NodePtr newPtr; newPtr = new Node; newPtr->data = 20; newPtr->next = NULL; Head = newPtr; Head = NULL; Head

  15. Inserting a Node at the Beginning newPtr = new Node; newPtr->data = 13; newPtr->next = Head; Head = newPtr; 20 Head 13 newPtr

  16. Head 50 40 13 20 newPtr Keep going …

  17. Adding an element to the head: void addHead(NodePtr& Head, int newdata){ NodePtr newPtr = new Node; newPtr->data = newdata; newPtr->next = Head; Head = newPtr; }

  18. Deleting the Head Node NodePtr cur; cur = Head; Head = Head->next; delete cur; Head (to delete) 50 40 13 20 cur

  19. void delHead(NodePtr& Head){ if(Head != NULL){ NodePtr cur = Head; Head = Head->next; delete cur; } }

  20. Displaying a Linked List cur = Head; cur = cur->next; Head 20 45 cur Head 20 45 cur

  21. A linked list is displayed by walking through its nodes one by one, and displaying their data fields. void DisplayList(NodePtr Head){ NodePtr cur; cur = Head; while(cur != NULL){ cout << cur->data << endl; cur = cur->next; } }

  22. Searching for a node //return the pointer of the node has data=item //return NULL if item does not exist NodePtr searchNode(NodePtr Head, int item){ NodePtr Cur = Head; NodePtr Result = NULL; while(Cur != NULL){ if(Cur->data == item) Result = Cur; Cur = Cur->next; } return Result; }

  23. 50 40 13 20 More operation: adding to the end • Original linked list of integers: • Add to the end (insert at the end): 60 50 40 13 20 Last element The key is how to locate the last element or node of the list!

  24. Add to the end: void addEnd(NodePtr& Head, int newdata){ NodePtr last = Head; NodePtr newPtr = new Node; newPtr->data = newdata; newPtr->next = NULL; if(Head != NULL){ // non-empty list case while(last->next != NULL) last = last->next; last->next = newPtr; } else // deal with the case of empty list Head = newPtr; } Link new object to last->next Link a new object to empty list

  25. Manipulation of a Sorted Linked List

  26. Inserting a Node Head ... 20 45 75 prev cur 33 newPtr

  27. To insert a new node into the list 1. (a) Create a new node using: NodePtr 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 and *cur. 3. Connect the new node to the list by using: (a) newPtr->next = cur; (b) prev->next = newPtr;

  28. Finding prev and cur • 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; }

  29. //insert item into linked list in ascending order void insertNode(NodePtr& Head, int item){ NodePtr New, Cur, Pre; New = new Node; New->data = item; Pre = NULL; Cur = Head; while(Cur != NULL && item > Cur->data){ Pre = Cur; Cur = Cur->next; } if(Pre == NULL){ //insert to head of linked list New->next = Head; Head = New; } else { Pre->next = New; New->next = Cur; } }

  30. Deleting a Node • To delete a node from the list 1. Locate the node to be deleted (a) cur points to the node. (b) prev points to its predecessor 2. Disconnect node from list using: prev->next = cur->next; 3. Return deleted node to system: delete cur; (to delete) Head ... 20 45 75 85 prev cur

  31. Delete an element in a sorted linked list: void deleteNode(NodePtr& Head, int item){ NodePtr prev, cur = Head; while(cur!=NULL && item > cur->data){ prev = cur; cur = cur->next; } if(cur==NULL || cur->data!=item){ cout << "Delete error: " << item << " not in list!" << endl; return; } if(cur==Head) Head = Head->next; else prev->next = cur->next; delete cur; }

More Related