C++ Linked List-1 PPT.pdf (1) (1)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

Sunday Live-S - Practical OOPs

PW skills

Linked List
Part - 1
-
User defined data
type

Raghav Garg
Revision of OOPS
Defining a class

Declaring and Initialising an object


class student d

public:
int zno; Studentordere
percent;
float ~no

name;
string
3;
name string name: "Raghav"
raghar
->

-> 2n0 int rno=76

92.6
->
percen floatber =

per
wno,
a
(
=0/azo
-

name who marks

"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

↓ ass 100 200 400 300 ⑧

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;

With Parameterised constructor class Nodel

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

Node* temb head;


=

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;

⑱-- 8ewu next:t ;


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)[
T.c. 0(n)
if (head==NULL) return;
I
=

W
cout head-val; di/NS =
NULL
S.c. 0(n)
=

I display (heads next); -ag(so) head=


SO

y(40) head no-

- (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

(a) Print all the elements of the linked list.


(b) Print all the elements except last one.
(c) Print alternate nodes of linked list
v
(d) Print all the nodes in reverse order
Implementation 8 82888
-

t
n

Linked List class


class linked list ( linked list el;

Node* head; b. add (60);

I

Node*tail; e. add At Head (90);


int size 0;
=

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)
=

Implement a method to insert a node at the end of a


linked list.
O ll. insertAtEnd (YO);
-1:Size

⑩ Nodet temp new


=
Node (40);

head temp
tail tail next:temp

Case-2:Size 0
=

ll. Insert At end (10) tail=temb;


-

Node* temp new Node (10); sizett;



=

NULL
head
head:tail: temp
tail size+ +;
function
InsertAtBeginning method =

Implement a method to insert a node at the start of


a linked list.
Ged: size so
ge -- Ne tail
head
ll. Insert At Head (S0);

Node (SO);Ca2:size=
O
Node* temp new
=

Node* temp Node (S0);


temp-next- head; new
=

NULL

head: temp; head head:tail tampi


=

tail
size++i sizett;
Insert method insert AAlint idx, int n)
vall;

Implement a method to insert a node at any given


index.
% Dewfewus tail
size S
=

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

el. Insert At (2,70);

%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

return tail eval


if (idx==size-1)
An evident limitation of Linked List


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

defini -> NULL ⑳ - Nu

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

11idx>=size) Invalid Index


if (idxc0
-

0) delete AtHead));
if (idx
=
=
e

if (idx== size-1) delete At Fail));


e

else[
-

3

head

delete At Idx (3);

Steps:
idx-I
1) traverse temp at

next
2) temp temps next
next:
-
Next Lecture

More interesting things about Linked List

deal
ass

~
insert At Head size
~
insertAt Tail Display
At IdX
insert

get At9dX iV
delete - 3

work:Implement I class but, with only head & size


Next Lecture

More interesting things about Linked List

Dhew saara
eode
I

will
L -pattern

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy