Linked List 01 - Class Notes - (DECODE DSA With C++ 2.0)
Linked List 01 - Class Notes - (DECODE DSA With C++ 2.0)
PW skills
Linked List
Part - 1
-
User defined data
type
Raghav Garg
Revision of OOPS
Defining a class
public:
int zno; Studentordere
percent;
float ~no
name;
string
3;
name string name: "Raghav"
raghar
->
b er 92.6
->
percen float =
per
wno,
a
(
=0/azo
-
"Raghar"
(56(92.6
ori
intx 4;
=
change(x)
·
Raghav
⑨
Revision of OOPS
Constructors & making parameterised constructors
Arrays - Linear D-S. -
multiple dabbe bana sakte hai
-list
Limitations ② I 23 4
1) Fixed Size
·er arr [2] S00; =
//0(1 T.C
problem
vector cint> aur (S);
-140 so
v. push-back (100)
I
lost ill
Arrays int ar [4];
Limitations ↓
16 bytes
allocation:
2) Contiguous memory 6x4 bytes
intarr [6]; -
24
=
11 free
zu
bytes Sbytes
bytes
Arrays
Need for a new linear data structure
Introduction to Linked List
Idea of linking two non-contiguous memory
locations (nodes)
-ens
S size ki lk list
Introduction to Linked List
Creation of a linked list
now-ditors
400
x
- t
Euro
⑱now nor are coco
I
O
so
↳
~ head tail
Ao- so-sor-e
-
-
W
-
a next
V
↓
~
(* a), next
w
⑳
-lisme l
a
d
nuc
temp
is no
Retation:
- - run
⑱ - -
temp
n 0/RBM
= S
n 0;
=
head
Node* temp-
while (temp!- mill) E
temp-next;
I temp
3
Introduction to Linked List
Does linked list overcomes the limitations of arrays?
Unlimited Size
=>
continuous allocation
memory
-
Implementation
class Node [
Node class
int val;
Nodet next;
3;
I
int val;
Nodetnext;
Node(intval)E
this sval=val;
I this - next = NULL;
3
3;
Implementation
Linking nodes to form a linked list
Displaying a Linked List -
2 size
NULL) [
while (temp!=
cont temps val;
temp:temp-snext;
3
T.c. 0(n)
=
5.2. 0(1)
=
Displaying a Linked List
(temp-
while n ext! NrL2)
=
I temp-temp next;
⑱-- 8ewu t;
next:
temp temps
Node*t new
=
Node
(60);
T.c 0
= (n)
Displaying a Linked List
a b c d e
Can we do it recursively? ⑩ -
Nur
head head head head
void
display (Nodet head) [
(head==NULL) return; T.c. 0(n)
I
=
if
W
cout head-val; di/NS =
NULL
S.c. 0(n)
=
- (30)
day
ab/20)
3
head=
head
0
20
-
10 20 30 40 SO -
aay
(10) head=10
What will this function do?
void
display (Nodet head) [
void display(Node head) {
*
(head==NULL) return;
if(head == null) return;
I
if
display(head.next);
I cout head-val;
System.out.print(head.val + “ “);
I
I (heads next);
} display
3
t
n
I
↑
3
⑭ size 0
=
h
t
Display method
Implement display method to print all the elements
Length method
Implement a method to find out the length of a
Linked List (Iterative and Recursive)
T .C.
InsertAtEnd method 0(1)
=
Case-2:Size
-
0
=
NULL
head
tail:
head: temp
tail size+ +;
function
InsertAtBeginning method =
NULL
tail
size++i sizett;
Insert method insert AAlint idx, int
n)
vall;
head
%8 rie
had temp
*
gran
⑳
1) Traverse temp t
idx-1
to
temps
tenext: next
2)
3) temp t
-next:
getElement method [0(n)
Implement a method to return the element at any
given index of the linked list.
. design tail
ear
head
⑧
coutarr[4];
awr[Y)=8;
AtHead:
if (size =
0)
=
- List is
empty
(size==1) NULL,
head:tail:
if NOT RESD.
Size --
if (Sizec=1)
head,
head: next;
size --;
⑩ 1ee
head tail
delete Attail: Y
mee
head temp
tail
Steps:
traverse temb such that
1)
snext: tail
temp
NULL
enext=
2) temp
tail
3) temp
=
deleteAtIndex method
Implement a function to delete a node at a given
index
% ofe 8
Dew wor
tail
head
if (idxc0 size)
11idx>= - Invalid Index
else[
-
3
⑱
head
Steps:
idx-I
1) traverse temp at
next
2) temp temps
next:
-
next
Next Lecture
deal
ass
↓
~
insert At Head size
~
At
insert Tail
Display
At
insert IdX
get At 9dX iV
delete - 3
Dhew saara
eode
I
will
L -pattern