1 / 35

Pointers and Data Structures

Yinzhi Cao. Modified from slides made by Prof Goce Trajcevski. Pointers and Data Structures. Q & A?. Q: Who am I? A: Yinzhi Cao One of your TAs … I mostly do research in web security. I have a web page ( http://www.cs.northwestern.edu/~yca179 )

ziven
Download Presentation

Pointers and Data Structures

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. Yinzhi Cao Modified from slides made by Prof Goce Trajcevski Pointers and Data Structures

  2. Q & A? • Q: Who am I? A: Yinzhi Cao • One of your TAs … • I mostly do research in web security. • I have a web page ( http://www.cs.northwestern.edu/~yca179 ) • Q: Why am I here? A: Prof Henschen is away… • Q: What do we learn today? • Linked list. • Before that, let me tell you a true story.

  3. A true story • When? • 2009. • Where? • My office. • What happens? • My officemate was interviewing Facebook  • He was asked to program on Facebook wall. • 1. Creating a linked list. • 2. Deleting a node from a linked list. • 3. Deciding whether the linked list contains loop or not?

  4. That is what we are going to learn in this class 

  5. Recall: Array Representation of Sequences Requires an estimate of the maximum size of the list waste space + always need to track/update “size” insert and delete: have prohibitive overheads when the sequences are sorted (or, if we insist on inserting at a given location…) e.g. insert at position 0 (making a new element) requires first pushing the entire array down one spot to make room e.g. delete at position 0 requires shifting all the elements in the list up one On average, half of the lists needs to be moved for either operation

  6. Pointer Implementation (Linked List) struct Node { double data; // data Node* next; // pointer to next }; a “self-referentiality” First, define a Node.

  7. Node * A; Node * B; Node * C;  a b c (*B).next = C; (*A).next = B; (*C).next = NULL; We want a new symbol  A->next = B; B->next = C; C->next = NULL; Second, linked them together.

  8. a4 a1 a3 a2 a2 712 Memory Content Memory Address a3 992 a4 0 a1 800 712 992 800 1000 What does the memory look like?

  9. Then we ask: • How to change the list? • Inserting a new node. • Deleting a node. • Finding a node / displaying nodes.

  10. Inserting a node a c B->next = C; A->next = B; b B->next = p->next; p->next = B; newNode p What if I only want to use a pointer that points to A?

  11. Inserting a node a c B->next = p->next; b p->next = B; newNode p • What if we switch those two operation? • No

  12. However, what if we want to insert in the front?

  13. b a c Option One  Head Deal with it differently.

  14. b a c Option Two  Head Add a faked node.  It is always there (even for an empty linked list)

  15. Deleting a Node delete B; b a c A->next = C;

  16. Finding a node. b a c d g … … p p = p->next; if (p->data == G) …

  17. More Practice delete B; B B A C D p->next = p->next->next; p->data = p->next->data; p How do I delete A?

  18. Comparison with array • Compared to the array implementation, • the pointer implementation uses only as much space as is needed for the elements currently on the list • but requires extra-space for the pointers in each cell

  19. Backup

  20. Pointer Implementation (Linked List) Ensure that the sequence is stored “on demand” use a linked list a series of “compounds” that are not necessarily adjacent in memory, borrowed when needed from the Heap… • Each node contains the element and a pointer to a structure containing its successor • the last cell’s next link points to NULL • Compared to the array implementation, • the pointer implementation uses only as much space as is needed for the elements currently on the list • but requires extra-space for the pointers in each cell

  21. Linked Lists A linked list is 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  a4 a1 a3 a2 Head a2 712 Memory Content Memory Address a3 992 a4 0 a1 800 712 992 800 1000 A C B A node data pointer

  22. Linked Lists So, where is the “self-referentiality” struct Node { double data; // data Node* next; // pointer to next }; Now, to have an access to the very first node in the list, we need a variable whose type will exactly be “pointer to a Node”… Node* headPtr;

  23. Printing the elements in a Linked List void PrintList(Node* ptrToHead) Print the data of all the elements Print the number of the nodes in the list Accessing a component of a structure, when that structure is assigned to a pointer-variable void DisplayList(Node* ptrToHead) { int num = 0; Node* currNode = ptrToHead; while (currNode != NULL){ cout << currNode->data << endl; currNode = currNode->next; num++; } cout << "Number of nodes in the list: " << num << endl; }

  24. Typical Algorithms for Lists • Operations of List • IsEmpty: determine whether or not the list is empty • InsertNode: insert a new node at a particular position • FindNode: find a node with a given value • DeleteNode: delete a node with a given value • DisplayList: print all the nodes in the list

  25. Inserting a new node • Node* InsertNode(int index, double x) • Insert a node with data equal to x after the index’thelements. (i.e., when index = 0, insert the node as the first element; when index = 1, insert the node after the first element, and so on) • If the insertion is successful, return the inserted node. Otherwise, return NULL. (If index is < 0 or > length of the list, the insertion will fail.) • Steps • Locate index’th element • Allocate memory for the new node • Point the new node to its successor • Point the new node’s predecessor to the new node index’th element newNode

  26. Inserting a new node • Possible cases of InsertNode • Insert into an empty list • Insert in front • Insert at back • Insert in middle • But, in fact, only need to handle two cases • Insert as the first node (Case 1 and Case 2) • Insert in the middle or at the end of the list (Case 3 and Case 4)

  27. Inserting a new node Try to locate index’th node. If it doesn’t exist, return NULL. Node* InsertNode(int index, double x) { if (index < 0) return NULL; int currIndex = 1; Node* currNode = head; while (currNode && index > currIndex) { currNode = currNode->next; currIndex++; } if (index > 0 && currNode == NULL) return NULL; Node* newNode = new Node; newNode->data = x; if (index == 0) { newNode->next = head; head = newNode; } else { newNode->next = currNode->next; currNode->next = newNode; } return newNode; }

  28. Inserting a new node Node* InsertNode(int index, double x) { if (index < 0) return NULL; int currIndex = 1; Node* currNode = head; while (currNode && index > currIndex) { currNode = currNode->next; currIndex++; } if (index > 0 && currNode == NULL) return NULL; Node* newNode = new Node; newNode->data = x; if (index == 0) { newNode->next = head; head = newNode; } else { newNode->next = currNode->next; currNode->next = newNode; } return newNode; } Create a new node

  29. head newNode Inserting a new node Node* InsertNode(int index, double x) { if (index < 0) return NULL; int currIndex = 1; Node* currNode = head; while (currNode && index > currIndex) { currNode = currNode->next; currIndex++; } if (index > 0 && currNode == NULL) return NULL; Node* newNode = new Node; newNode->data = x; if (index == 0) { newNode->next = head; head = newNode; } else { newNode->next = currNode->next; currNode->next = newNode; } return newNode; } Insert as first element

  30. newNode Inserting a new node Node* List::InsertNode(int index, double x) { if (index < 0) return NULL; int currIndex = 1; Node* currNode = head; while (currNode && index > currIndex) { currNode = currNode->next; currIndex++; } if (index > 0 && currNode == NULL) return NULL; Node* newNode = new Node; newNode->data = x; if (index == 0) { newNode->next = head; head = newNode; } else { newNode->next = currNode->next; currNode->next = newNode; } return newNode; } Insert after currNode currNode

  31. Finding a node • int FindNode(double x) • Search for a node with the value equal to x in the list. • If such a node is found, return its position. Otherwise, return 0. int FindNode(double x) { Node* currNode = head; int currIndex = 1; while (currNode && currNode->data != x) { currNode = currNode->next; currIndex++; } if (currNode) return currIndex; return 0; // NOTE: Also common to return // a pointer of a type Node to // the node holding the element }

  32. Deleting a node • int DeleteNode(double x) • Delete a node with the value equal to x from the list. • If such a node is found, return its position. Otherwise, return 0. • Steps • Find the desirable node (similar to FindNode) • Release the memory occupied by the found node • Set the pointer of the predecessor of the found node to the successor of the found node • Like InsertNode, there are two special cases • Delete first node • Delete the node in middle or at the end of the list

  33. Deleting a node int DeleteNode(double x) { Node* prevNode = NULL; Node* currNode = head; int currIndex = 1; while (currNode && currNode->data != x) { prevNode = currNode; currNode = currNode->next; currIndex++; } if (currNode) { if (prevNode) { prevNode->next = currNode->next; delete currNode; } else { head = currNode->next; delete currNode; } return currIndex; } return 0; } Try to find the node with its value equal to x

  34. prevNode currNode Deleting a node int DeleteNode(double x) { Node* prevNode = NULL; Node* currNode = head; int currIndex = 1; while (currNode && currNode->data != x) { prevNode = currNode; currNode = currNode->next; currIndex++; } if (currNode) { if (prevNode) { prevNode->next = currNode->next; delete currNode; } else { head = currNode->next; delete currNode; } return currIndex; } return 0; } Since the space for the node was allocated by “new”

  35. head currNode Deleting a node int DeleteNode(double x) { Node* prevNode = NULL; Node* currNode = head; int currIndex = 1; while (currNode && currNode->data != x) { prevNode = currNode; currNode = currNode->next; currIndex++; } if (currNode) { if (prevNode) { prevNode->next = currNode->next; delete currNode; } else { head = currNode->next; delete currNode; } return currIndex; } return 0; }

More Related