C++ Linked List-1 PPT.pdf (1) (1)
C++ Linked List-1 PPT.pdf (1) (1)
C++ Linked List-1 PPT.pdf (1) (1)
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
->
92.6
->
percen floatber =
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)
lost ill
I
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 - t
400
x
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;
=
Node* temp-head
while (temp!- mill) E
I temp temp-next;
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
while (temp!=NULL) [
cont temps val;
temp:temp-snext;
3
T.c. 0(n)
=
5.2. 0(1)
=
Displaying a Linked List
(temp-next! NrL2)
while =
I temp-temp next;
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)[
T.c. 0(n)
if (head==NULL) return;
I
=
W
cout head-val; di/NS =
NULL
S.c. 0(n)
=
- (30)
day
ab/20)
30
head=
head 20
-
10 20 30 40 SO -
aay
(10) head=10
What will this function do?
void display(Node head) {
* void
display (Nodet head) [
if (head==NULL) return;
if(head == null) return;
display(head.next);
I
System.out.print(head.val + “ “);
I
I cout head-val;
}
I display (heads next);
3
t
n
I
↑
el. deleteAt(2);
el insert At/3,80);
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)
=
head temp
tail tail next:temp
Case-2:Size 0
=
NULL
head
head:tail: temp
tail size+ +;
function
InsertAtBeginning method =
Node (SO);Ca2:size=
O
Node* temp new
=
NULL
tail
size++i sizett;
Insert method insert AAlint idx, int n)
vall;
head
If(idx 0]
= = - ll. Insert AtHead
insert At Tail
el if (idx==size) all
II idx>size) e invalid
dif(idxco
elser
%8ere run
had
%8 rie gran *
had temp
⑳
1) Traverse temp t
idx-1
to
next
2) tenext:temps
3) temp -next:t
getElement method [0(n)
Implement a method to return the element at any
given index of the linked list.
. design tail
ear
head
Invalid dee
if (idx<0 Ilidx>=size)
head eval
if (idx==8) return
⑧
coutarr[4];
awr[Y)=8;
AtHead:
if (size =
0)
=
- List is
empty
NULL,
head:tail:
if (size==1) NOT RESD.
Size --
if (Sizec=1)
head, next;
head:
size --;
⑩ 1ee
head tail
delete Attail: Y
mee
head temp
tail
Steps:
traverse temb such that
1)
tail
temp snext:
NULL
enext=
2) temp
tail
3) temp
=
deleteAtIndex method
Implement a function to delete a node at a given
index
8
% ofe Dew wor
tail
head
0) delete AtHead));
if (idx
=
=
e
else[
-
3
⑱
head
Steps:
idx-I
1) traverse temp at
next
2) temp temps next
next:
-
Next Lecture
deal
ass
↓
~
insert At Head size
~
insertAt Tail Display
At IdX
insert
get At9dX iV
delete - 3
Dhew saara
eode
I
will
L -pattern