0% found this document useful (0 votes)
882 views37 pages

Linked List 01 - Class Notes - (DECODE DSA With C++ 2.0)

The document discusses linked lists and their implementation in C++. It explains the concept of linking nodes to store data non-contiguously in memory. It covers creating and linking nodes to form a linked list, traversing the list using iterators, and implementing methods like display, length, insertAtEnd etc. on the Linked List class.

Uploaded by

kxzsxz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
882 views37 pages

Linked List 01 - Class Notes - (DECODE DSA With C++ 2.0)

The document discusses linked lists and their implementation in C++. It explains the concept of linking nodes to store data non-contiguously in memory. It covers creating and linking nodes to form a linked list, traversing the list using iterators, and implementing methods like display, length, insertAtEnd etc. on the Linked List class.

Uploaded by

kxzsxz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

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

b er 92.6
->
percen float =

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)

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;

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;


=

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

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


SO

y (40) head no-

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

(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. delete At(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.
Size O ll. insertAtEnd (YO);
-1:

⑩ Nodet temp new


=
Node (40);
head temp
tail tail temp
next:

Case-2:Size
-
0
=

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


Node* temp new Node (10); sizett;

=

NULL
head
tail:
head: 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
=

head; Node* temp Node (S0);


temp-next- new
=

NULL

head: temp; head tail


head: 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

0] ll. Insert AtHead


If(idx = = -

(idx==size) all insert At Tail


el if
II idx>size) e invalid
dif(idxco
elser
%8 ere
had
run
el. Insert At (2,70);

%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

Ilidx>=size) Invalid dee


if (idx<0
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
(size==1) NULL,
head:tail:
if NOT RESD.
Size --

if (Sizec=1)
head,
head: next;

size --;

⑩ 1ee
head tail
delete Attail: Y

mee

defini -> NULL ⑳ - Nu

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

(idx 0) delete AtHead));


if
=
=
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
~
At
insert Tail
Display
At
insert IdX

get At 9dX 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