![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](https://reader036.vdocument.in/reader036/viewer/2022062918/5ede3a64ad6a402d66698af6/html5/thumbnails/1.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062918/5ede3a64ad6a402d66698af6/html5/thumbnails/2.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062918/5ede3a64ad6a402d66698af6/html5/thumbnails/3.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062918/5ede3a64ad6a402d66698af6/html5/thumbnails/4.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062918/5ede3a64ad6a402d66698af6/html5/thumbnails/5.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062918/5ede3a64ad6a402d66698af6/html5/thumbnails/6.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022062918/5ede3a64ad6a402d66698af6/html5/thumbnails/7.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062918/5ede3a64ad6a402d66698af6/html5/thumbnails/8.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062918/5ede3a64ad6a402d66698af6/html5/thumbnails/9.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062918/5ede3a64ad6a402d66698af6/html5/thumbnails/10.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062918/5ede3a64ad6a402d66698af6/html5/thumbnails/11.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062918/5ede3a64ad6a402d66698af6/html5/thumbnails/12.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062918/5ede3a64ad6a402d66698af6/html5/thumbnails/13.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062918/5ede3a64ad6a402d66698af6/html5/thumbnails/14.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062918/5ede3a64ad6a402d66698af6/html5/thumbnails/15.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062918/5ede3a64ad6a402d66698af6/html5/thumbnails/16.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062918/5ede3a64ad6a402d66698af6/html5/thumbnails/17.jpg)
Next time• More linked list with classes