Download presentation

Presentation is loading. Please wait.

Published byClark Lugg Modified over 3 years ago

1
Pointers and Data Structures 1 Yinzhi Cao Modified from slides made by Prof Goce Trajcevski

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 )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. 2

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? 3

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

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 5

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

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

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

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

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

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

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

13
Option One Deal with it differently. 13 a Head bc

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

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

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

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

18
Comparison with array 18 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 19

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 cells 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 20

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 A datapointer node A Head BC a1 a2 a3 a4 a1 800 a2 712 a3 992a4 0 Memory Content Memory Address 1000 800 712992 21

22
Linked Lists So, where is the self-referentiality struct Node { doubledata;// 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; 22

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 void DisplayList(Node* ptrToHead) { int num=0; Node* currNode=ptrToHead; while (currNode != NULL){ cout data << endl; currNode=currNode->next; num++; } cout << "Number of nodes in the list: " << num << endl; } Accessing a component of a structure, when that structure is assigned to a pointer-variable 23

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 24

25
Inserting a new node Node* InsertNode(int index, double x) Insert a node with data equal to x after the index th elements. (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 length of the list, the insertion will fail.) Steps 1. Locate index th element 2. Allocate memory for the new node 3. Point the new node to its successor 4. Point the new nodes predecessor to the new node newNode indexth element 25

26
Inserting a new node Possible cases of InsertNode 1. Insert into an empty list 2. Insert in front 3. Insert at back 4. 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) 26

27
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=newNode; newNode->data=x; if (index == 0) { newNode->next=head; head=newNode; } else { newNode->next=currNode->next; currNode->next=newNode; } return newNode; } Try to locate index th node. If it doesnt exist, return NULL. 27

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=newNode; 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 28

29
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=newNode; 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 head newNode 29

30
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=newNode; newNode->data=x; if (index == 0) { newNode->next=head; head=newNode; } else { newNode->next=currNode->next; currNode->next=newNode; } return newNode; } Insert after currNode newNode currNode 30

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 } 31

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 32

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 33

34
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; } currNodeprevNode Since the space for the node was allocated by new 34

35
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; } currNodehead 35

Similar presentations

Presentation is loading. Please wait....

OK

Data Structure & Algorithms

Data Structure & Algorithms

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google