17
REVIEW POINTERS, DYNAMIC MEMORY LINKED LISTS Problem Solving with Computers-II

REVIEW POINTERS, DYNAMIC MEMORY LINKED LISTS · Questions you must ask about any data structure: 14 • What operations does the data structure support? A linked list supports the

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: REVIEW POINTERS, DYNAMIC MEMORY LINKED LISTS · Questions you must ask about any data structure: 14 • What operations does the data structure support? A linked list supports the

REVIEW POINTERS, DYNAMIC MEMORYLINKED LISTS

Problem Solving with Computers-II

Page 2: REVIEW POINTERS, DYNAMIC MEMORY LINKED LISTS · Questions you must ask about any data structure: 14 • What operations does the data structure support? A linked list supports the

Have you implemented a linked-list before?A. Yes B. No

Page 3: REVIEW POINTERS, DYNAMIC MEMORY LINKED LISTS · Questions you must ask about any data structure: 14 • What operations does the data structure support? A linked list supports the

Representing a node in code

Page 4: REVIEW POINTERS, DYNAMIC MEMORY LINKED LISTS · Questions you must ask about any data structure: 14 • What operations does the data structure support? A linked list supports the

Linked Lists3

Linked List

Array List 1 2 3

What is the key difference between these?

Page 5: REVIEW POINTERS, DYNAMIC MEMORY LINKED LISTS · Questions you must ask about any data structure: 14 • What operations does the data structure support? A linked list supports the

Review: pointers

Q: Which of the following pointer diagrams best represents the outcome of the above code?

4

int *p, x = 10; p = &x; *p = *p + 1;

A. 10x

B.x

C. Neither, the code is incorrect

11

p p

Page 6: REVIEW POINTERS, DYNAMIC MEMORY LINKED LISTS · Questions you must ask about any data structure: 14 • What operations does the data structure support? A linked list supports the

• Pointer: A variable that contains the address of another variable

• Declaration: type * pointer_name;

5

int *p; // p stores the address of an int

How do we initialize a pointer?

Pointers

What is outcome of the following code? cout<<*p; A. Random numberB. Undefined behavior C. Null value

Page 7: REVIEW POINTERS, DYNAMIC MEMORY LINKED LISTS · Questions you must ask about any data structure: 14 • What operations does the data structure support? A linked list supports the

Review: Pointer assignment

Q: Which of the following pointer diagrams best represents the outcome of the above code?

6

int *p1, *p2, x; p1 = &x; p2 = p1;

A.

xB.

x

C. Neither, the code is incorrect

p1 p2 p2 p1

Page 8: REVIEW POINTERS, DYNAMIC MEMORY LINKED LISTS · Questions you must ask about any data structure: 14 • What operations does the data structure support? A linked list supports the

Review: Pointers to structs

Q: Which of the following pointer diagrams best represents the outcome of the above code?

7

Node x = {10, nullptr};Node *p = &x;p->data = p->data +1;P = p->next;

A. B.x

C. Neither, the code is incorrect

0

p p

struct Node { int data; Node *next; };

11 0 11 0

Page 9: REVIEW POINTERS, DYNAMIC MEMORY LINKED LISTS · Questions you must ask about any data structure: 14 • What operations does the data structure support? A linked list supports the

Dynamic memory allocation• To allocate memory on the heap use the ‘new’

operator • To free the memory use delete

8

int* createInt(){int x = 10; return &x;}

int* createIntOnHeap(){

}

int *p= new int;delete p;

Page 10: REVIEW POINTERS, DYNAMIC MEMORY LINKED LISTS · Questions you must ask about any data structure: 14 • What operations does the data structure support? A linked list supports the

Dynamic memory allocation• To allocate memory on the heap use the ‘new’ operator • To free the memory use delete

9

Node* createNode(){Node x = {10, nullptr}; return &x;}

Node* createNodeOnHeap(){

}

int *p= new int;delete p;

Page 11: REVIEW POINTERS, DYNAMIC MEMORY LINKED LISTS · Questions you must ask about any data structure: 14 • What operations does the data structure support? A linked list supports the

Create a two node list10

• Define an empty list • Add a node to the list with data = 10

struct Node { int data; Node *next; };

Page 12: REVIEW POINTERS, DYNAMIC MEMORY LINKED LISTS · Questions you must ask about any data structure: 14 • What operations does the data structure support? A linked list supports the

Accessing elements of a list

Assume the linked list has already been created, what do the following expressions evaluate to? 1. head->data 2. head->next->data 3. head->next->next->data 4. head->next->next->next->data

A. 1 B. 2 C. 3 D. NULL E. Run time error

head

struct Node { int data; Node *next; };

Page 13: REVIEW POINTERS, DYNAMIC MEMORY LINKED LISTS · Questions you must ask about any data structure: 14 • What operations does the data structure support? A linked list supports the

Iterating through the list

void printElements(Node* head) { /* Print the values in the list */

}

head

Page 14: REVIEW POINTERS, DYNAMIC MEMORY LINKED LISTS · Questions you must ask about any data structure: 14 • What operations does the data structure support? A linked list supports the

Clear the list

Node* clearList(Node* head) { /* Free all the memory that was created on the heap*/

}

list

Page 15: REVIEW POINTERS, DYNAMIC MEMORY LINKED LISTS · Questions you must ask about any data structure: 14 • What operations does the data structure support? A linked list supports the

Questions you must ask about any data structure:14

• What operations does the data structure support? A linked list supports the following operations:

1. Insert (a value) 2. Delete (a value) 3. Search (for a value) 4. Min 5. Max 6. Print all values

• How do you implement the data structure? • How fast is each operation?

Page 16: REVIEW POINTERS, DYNAMIC MEMORY LINKED LISTS · Questions you must ask about any data structure: 14 • What operations does the data structure support? A linked list supports the

Linked-list as an Abstract Data Type (ADT)class IntList {public: IntList(); // constructor ~IntList(); // destructor // other methodsprivate: // definition of Node structure struct Node { int info; Node *next; }; Node *head; // pointer to first node};

Page 17: REVIEW POINTERS, DYNAMIC MEMORY LINKED LISTS · Questions you must ask about any data structure: 14 • What operations does the data structure support? A linked list supports the

Next time• More linked list with classes