lecture 6: linked list
DESCRIPTION
This slide is part of course designed for placement preparation for students of IIT Guwahati.TRANSCRIPT
![Page 1: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/1.jpg)
Placement PreparationLinked List
Shobhit ChaurasiaB.Tech, CSE
![Page 2: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/2.jpg)
Target Audience
People who have coding experience limited to the basic
syntax of C++, pointers and structures, but didn’t take
CS101 seriously and want to learn some basics of coding
quickly.
If you already know how to implement a linked list, then
please spare me the pain of taking a lecture on linked
lists, and volunteer to deliver this lecture.
![Page 3: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/3.jpg)
Motivation
Suppose I have an array: 1,4,10,19,6
I want to insert a 7 between the 4 and the 10
What do we need to do?
![Page 4: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/4.jpg)
Linked List
head
![Page 5: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/5.jpg)
Node
![Page 6: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/6.jpg)
How to represent a Node in C++ ?
How to represnt integers in C++?
int x;
But Node is a combination of data + pointer
(int) + pointer
(char) + pointer
(string) + pointer
![Page 7: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/7.jpg)
Structures Revisited
Structure variable
stud s;
Access members of struct
s.rollNo = 11010179
s.cpi = 8.1
s.gender = ‘M’
;
![Page 8: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/8.jpg)
How to represent Node in C++
;
![Page 9: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/9.jpg)
Head
Pointer to the first node.Holds the address of the first node in the list
head
![Page 10: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/10.jpg)
Head
Pointer to the first node.
node* head
;
![Page 11: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/11.jpg)
Struct Pointer revisited
Struct Pointer
stud *s;
Access members of struct
s->rollNo = 11010179
s->cpi = 8.1
s->gender = ‘M’rollNo cpi gender
s
;
![Page 12: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/12.jpg)
Accessing data/next node
node* head
Data stored at first node
head->data = ‘a’;
Pointer to second node
head->next
Data stored at second node
(head->next)->data = ‘b’;
head
;
![Page 13: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/13.jpg)
Demo #0(a)
node* temp;
temp = head;
cout<<temp->data;
head
![Page 14: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/14.jpg)
Demo #0(a)
How to go to the second node?
temp = temp->next;
cout<<temp->data;
head
![Page 15: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/15.jpg)
Demo #0(a)
How to go to the third node?
temp = temp->next;
cout<<temp->data;
head
![Page 16: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/16.jpg)
Where’s the list’s end?
headptr
![Page 17: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/17.jpg)
Demo #0(b)
Printing the linked list
![Page 18: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/18.jpg)
Demo #0(b)
Printing the linked list (correct version)
![Page 19: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/19.jpg)
Demo #0(b)
An observation
![Page 20: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/20.jpg)
Demo #0(b) - MORAL
Never move the HEAD while traversing a
linked list
![Page 21: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/21.jpg)
Demo #1
Function to print the 3rd element in linked list
![Page 22: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/22.jpg)
Insert a new node at the end
node* temp;
temp = start;
temp->next == NULL ??
![Page 23: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/23.jpg)
Insert a new node at the end
temp = temp->next;
temp->next == NULL ??
![Page 24: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/24.jpg)
Insert a new node at the end
temp = temp->next;
temp->next == NULL ??
![Page 25: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/25.jpg)
Insert a new node at the end
temp = temp->next;
temp->next == NULL ??
![Page 26: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/26.jpg)
Insert a new node at the end
temp = temp->next;
temp->next == NULL ?? (YES!! Finally reached the end)
![Page 27: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/27.jpg)
Insert a new node at the end
ptr
node* ptr = new node
![Page 28: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/28.jpg)
Insert a new node at the end
ptr
ptr->data = ‘f’;
ptr->next = NULL;
f
![Page 29: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/29.jpg)
Insert a new node at the end
ptr
temp->next = ptr;
![Page 30: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/30.jpg)
Demo #2
Inserting a new node at the end.Discuss 2.cpp
![Page 31: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/31.jpg)
Insert a new node at the start
node* ptr = new node;
ptr
![Page 32: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/32.jpg)
Insert a new node at the start
node* ptr = new node;
ptr->data = ‘z’;
ptrz
![Page 33: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/33.jpg)
Insert a new node at the start
ptr->next = start;
ptr
![Page 34: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/34.jpg)
Insert a new node at the start
start = ptr;
ptr
![Page 35: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/35.jpg)
Demo #3
Insert a new node at the headDiscuss 3.cpp (two methods)
![Page 36: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/36.jpg)
Demo #4
Create a linked list and print its contents.
![Page 37: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/37.jpg)
Demo #5
Same as last problem. Use functions.
![Page 38: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/38.jpg)
Insert after a particular node
![Page 39: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/39.jpg)
Demo #6
Write a function which takes as parameter a
node pointer and inserts a new node after it.
insertInBetween (node* temp)
![Page 40: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/40.jpg)
Deleting first node
![Page 41: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/41.jpg)
Deleting first node
![Page 42: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/42.jpg)
ACTUALLY Deleting first node
![Page 43: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/43.jpg)
ACTUALLY Deleting first node
![Page 44: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/44.jpg)
ACTUALLY deleting first node
![Page 45: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/45.jpg)
ACTUALLY deleting first node
delete frees the memory allocated by new
![Page 46: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/46.jpg)
Demo #7
Write a function to delete the first node of a
linked list.Add this function to 5.cpp
![Page 47: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/47.jpg)
Demo #8
Write a function to delete the last node of
linked listAdd this function to 7.cpp
![Page 48: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/48.jpg)
Delete a node inside the linked list
![Page 49: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/49.jpg)
Delete a node inside the linked list
![Page 50: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/50.jpg)
Locating the node “y”
![Page 51: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/51.jpg)
Can we delete the node “y” now?
Having just the pointer to the node containing
“y” sufficient to delete it?
![Page 52: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/52.jpg)
Break this link, and make it point to the
node “d”
What do we want to do?
![Page 53: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/53.jpg)
Expected Output
![Page 54: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/54.jpg)
What do we need?
Pointer to the previous node
![Page 55: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/55.jpg)
Step 1
prev->next = temp->next;
![Page 56: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/56.jpg)
Step 2
delete temp;
![Page 57: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/57.jpg)
Final output
![Page 58: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/58.jpg)
Demo #9
Delete the node with data = 3Add this function to 5.cpp
![Page 59: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/59.jpg)
Homework
Reverse a linked list.
![Page 60: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/60.jpg)
Linked list vs Arrays
Operation Linked List Array
Insert at start O(1) O(n)
Insert anywhere O(1) O(n)
Delete anywhere O(1) O(n)
Access ith element O(n) O(1)
![Page 61: Lecture 6: linked list](https://reader034.vdocuments.us/reader034/viewer/2022051314/55988fdc1a28ab41578b45b6/html5/thumbnails/61.jpg)
THANK YOU