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

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

Chapter 25 Lists, Stacks, Queues, and Priority Queues
Linked Lists Geletaw S..
Copyright © 2003 Pearson Education, Inc. Slide 1.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
Chapter 3: Linked List Tutor: Angie Hui
Lecture 15 Linked Lists part 2
Lists CS 3358.
Chapter 17 Linked Lists.
COMP171 Fall 2005 Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Singly Linked Lists What is a singly-linked list? Why linked lists?
Lists Chapter 6 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linked Lists.
Linked Lists Chapter 4.
1/27 COP 3540 Data Structures with OOP Chapter 5 Linked Lists.
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Chapter 10: Applications of Arrays and the class vector
Linear Lists – Linked List Representation
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Chapter 24 Lists, Stacks, and Queues
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
11 Data Structures Foundations of Computer Science ã Cengage Learning.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 CSE1301 Computer Programming: Lecture 27 List Manipulation.
Abstract Data Types and Algorithms
Data Structures Using C++
LIST PROCESSING.
Double-Linked Lists and Circular Lists
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.
CSCI2100B Linked List Jeffrey
CSE Lecture 12 – Linked Lists …
Hash Tables.
2 |SharePoint Saturday New York City
Green Eggs and Ham.
DATA STRUCTURE “Linked Lists” SHINTA P STMIK MDP April 2011.
Review Pseudo Code Basic elements of Pseudo code
Linked Lists.
© 2012 National Heart Foundation of Australia. Slide 2.
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
10 -1 Chapter 10 Amortized Analysis A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Pointers and Arrays Chapter 12
CSE Lecture 17 – Balanced trees
PSSA Preparation.
Foundations of Data Structures Practical Session #7 AVL Trees 2.
Insertion Sort Introduction to Algorithms Insertion Sort CSE 680 Prof. Roger Crawfis.
Profile. 1.Open an Internet web browser and type into the web browser address bar. 2.You will see a web page similar to the one on.
Data Structures Using C++ 2E
Linked List
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Linked Lists Spring Linked Lists / Slide 2 List Overview * Linked lists n Abstract data type (ADT) * Basic operations of linked lists n Insert,
Chapter 5 – Dynamic Data Structure Part 2: Linked List DATA STRUCTURES & ALGORITHMS Teacher: Nguyen Do Thai Nguyen
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
Unit – I Lists.
Lists CS 3358.
Linked Lists.
Linked List.
Lecture No.02 Data Structures Dr. Sohail Aslam
Linked Lists.
Presentation transcript:

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

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 ( ) 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

A true story When? 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

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

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

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

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

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

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

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

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

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

Option One Deal with it differently. 13 a Head bc

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

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

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

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

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

Backup 19

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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