SlideShare a Scribd company logo
1 of 35
COME

DEVENDRA
KUSHWAH
LINKED LISTS
 Definition of Linked Lists
 Examples of Linked Lists
 Operations on Linked Lists
 Linked List as a Class
 Linked Lists as Implementations of
Stacks, Sets, etc.
CS 103
4
DEFINITION OF LINKED LISTS
 A linked list is a sequence of items (objects)
where every item is linked to the next.
 Graphically:
CS 103
5
data data data data
head_ptr tail_ptr
EXAMPLES OF LINKED LISTS
(A WAITING LINE)
 A waiting line of customers: John, Mary, Dan,
Sue (from the head to the tail of the line)
 A linked list of strings can represent this line:
CS 103
6
John Mary Dan Sue
head_ptr
tail_ptr
EXAMPLES OF LINKED LISTS
(A STACK OF NUMBERS)
 A stack of numbers (from top to bottom): 10,
8, 6, 8, 2
 A linked list of ints can represent this stack:
CS 103
7
10 8 6 2
head_ptr tail_ptr
8
OPERATIONS ON LINKED LISTS
 Insert a new item
 At the head of the list, or
 At the tail of the list, or
 Inside the list, in some designated position
 Search for an item in the list
 The item can be specified by position, or by some
value
 Delete an item from the list
 Search for and locate the item, then remove the
item, and finally adjust the surrounding pointers
 size( );
 isEmpty( )
CS 103
8
INSERT– AT THE HEAD
 Insert a new data A. Call new: newPtr
List before insertion:
 After insertion to head:
CS 103
9
data data data data
head_ptr tail_ptr
A
data data data data
head_ptr
tail_ptr
A
•The link value in the new item = old head_ptr
•The new value of head_ptr = newPtr
INSERT – AT THE TAIL
 Insert a new data A. Call new: newPtr
List before insertion
 After insertion to tail:
CS 103
10
data data data data
head_ptr tail_ptr
A
data data data data
head_ptr tail_ptr
A
•The link value in the new item = NULL
•The link value of the old last item = newPtr
INSERT – INSIDE THE LIST
 Insert a new data A. Call new: newPtr
List before insertion:
 After insertion in 3rd position:
CS 103
11
data data data data
head_ptr tail_ptr
data
data A data data
head_ptr
tail_ptr
data
•The link-value in the new item = link-value of 2nd item
•The new link-value of 2nd item = newPtr
DELETE – THE HEAD ITEM
 List before deletion:
 List after deletion of the head item:
CS 103
12
data data data data
head_ptr tail_ptr
data data data data
head_ptr tail_ptr
data
•The new value of head_ptr = link-value of the old head item
•The old head item is deleted and its memory returned
data
DELETE – THE TAIL ITEM
 List before deletion:
 List after deletion of the tail item:
CS 103
13
data data data data
head_ptr tail_ptr
data data data
head_ptr
tail_ptr
•New value of tail_ptr = link-value of the 3rd from last item
•New link-value of new last item = NULL.
data
datadata
DELETE – AN INSIDE ITEM
 List before deletion:
 List after deletion of the 2nd item:
CS 103
14
data data data data
head_ptr tail_ptr
data data
head_ptr tail_ptr
•New link-value of the item located before the deleted one =
the link-value of the deleted item
data
data datadata
SEARCHING FOR AN ITEM
 Suppose you want to find the item whose data
value is A
 You have to search sequentially starting from the
head item rightward until the first item whose data
member is equal to A is found.
 At each item searched, a comparison between the
data member and A is performed.
CS 103
15
STACKS
STACK SPECIFICATION
 Definitions: (provided by the user)
 MAX_ITEMS: Max number of items that might be
on the stack
 ItemType: Data type of the items on the stack
 Operations
 MakeEmpty
 Boolean IsEmpty
 Boolean IsFull
 Push (ItemType newItem)
 Pop (ItemType& item) (or pop and top)
POP (ITEMTYPE& ITEM)
 Function: Removes topItem from stack and
returns it in item.
 Preconditions: Stack has been initialized and
is not empty.
 Postconditions: Top element has been
removed from stack and item is a copy of
the removed element.
Stack overflow
 The condition resulting from trying to
push an element onto a full stack.
if(!stack.IsFull())
stack.Push(item);
Stack underflow
 The condition resulting from trying to
pop an empty stack.
if(!stack.IsEmpty())
stack.Pop(item);
IMPLEMENTING STACKS USING TEMPLATES
 Templates allow the compiler to
generate multiple versions of a class
type or a function by allowing
parameterized types.
TREES AND
BINARY TREES
Become Rich
Force Others
to be Poor
Rob
Banks
Stock
Fraud
The class notes are a compilation and edition from many sources. The
instructor does not claim intellectual property or ownership of the lecture
notes.
WHAT IS A TREE
 A tree is a finite nonempty
set of elements.
 It is an abstract model of a
hierarchical structure.
 consists of nodes with a
parent-child relation.
 Applications:
 Organization charts
 File systems
 Programming
environments
Computers”R”Us
Sales R&DManufacturing
Laptops DesktopsUS International
Europe Asia Canada
subtree
TREE TERMINOLOGY
 Root: node without parent (A)
 Siblings: nodes share the same parent
 Internal node: node with at least one
child (A, B, C, F)
 External node (leaf ): node without
children (E, I, J, K, G, H, D)
 Ancestors of a node: parent,
grandparent, grand-grandparent, etc.
 Descendant of a node: child,
grandchild, grand-grandchild, etc.
 Depth of a node: number of ancestors
 Height of a tree: maximum depth of any
node (3)
 Degree of a node: the number of its
children
 Degree of a tree: the maximum number
of its node.
A
B DC
G HE F
I J K
Subtree: tree consisting of a
node and its descendants
LEFT CHILD, RIGHT SIBLING REPRESENTATION
Data
Left
Child
Right
Sibling A
B C D
IHGFE
J K L
TREE TRAVERSAL
 Two main methods:
 Preorder
 Postorder
 Recursive definition
 Preorder:
 visit the root
 traverse in preorder the children (subtrees)
 Postorder
 traverse in postorder the children (subtrees)
 visit the root
PREORDER TRAVERSAL
 A traversal visits the nodes of a tree
in a systematic manner
 In a preorder traversal, a node is
visited before its descendants
 Application: print a structured
document
Become Rich
1. Motivations 3. Success Stories2. Methods
2.1 Get a
CS PhD
2.2 Start a
Web Site
1.1 Enjoy
Life
1.2 Help
Poor Friends
2.3 Acquired
by Google
1
2
3
5
4 6 7 8
9
Algorithm preOrder(v)
visit(v)
for each child w of v
preorder (w)
POSTORDER TRAVERSAL
 In a postorder traversal, a node is
visited after its descendants
 Application: compute space used
by files in a directory and its
subdirectories
Algorithm postOrder(v)
for each child w of v
postOrder (w)
visit(v)
cs16/
homeworks/
todo.txt
1K
programs/
DDR.java
10K
Stocks.java
25K
h1c.doc
3K
h1nc.doc
2K
Robot.java
20K
9
3
1
7
2 4 5 6
8
BINARY TREE
 A binary tree is a tree with the following
properties:
 Each internal node has at most two
children (degree of two)
 The children of a node are an ordered
pair
 We call the children of an internal node
left child and right child
 Alternative recursive definition: a
binary tree is either
 a tree consisting of a single node, OR
 a tree whose root has an ordered pair
of children, each of which is a binary
tree
Applications:
arithmetic expressions
decision processes
searching
A
B C
F GD E
H I
Examples of the Binary Tree
A
B C
GE
I
D
H
F
Complete Binary Tree
1
2
3
4
A
B
A
B
Skewed Binary Tree
E
C
D
5
Maximum Number of Nodes in a
Binary Tree
The maximum number of nodes on depth i of a binary tree is 2i, i>=0.
The maximum nubmer of nodes in a binary tree of height k is 2k+1-1,
k>=0.
Prove by induction.
122 1
0
 

 k
k
i
i
FULL BINARY TREE
 A full binary tree of a given height k has 2k+1–1 nodes.
Height 3 full binary tree.
Complete Binary Trees
A labeled binary tree containing the labels 1 to n with root 1, branches
leading to nodes labeled 2 and 3, branches from these leading to 4, 5 and
6, 7, respectively, and so on.
A binary tree with n nodes and level k is complete iff its nodes
correspond to the nodes numbered from 1 to n in the full binary tree of
level k.
1
2 3
75
11
4
10
6
98 15141312
Full binary tree of depth 3
1
2 3
75
9
4
8
6
Complete binary tree
Binary Tree Traversals
Let l, R, and r stand for moving left, visiting
the node, and moving right.
There are six possible combinations of traversal
lRr, lrR, Rlr, Rrl, rRl, rlR
Adopt convention that we traverse left before
right, only 3 traversals remain
lRr, lrR, Rlr
inorder, postorder, preorder
INORDER TRAVERSAL
 In an inorder traversal a node is
visited after its left subtree and
before its right subtree
Algorithm inOrder(v)
if isInternal (v)
inOrder (leftChild (v))
visit(v)
if isInternal (v)
inOrder (rightChild (v))
3
1
2
5
6
7 9
8
4

More Related Content

What's hot

What's hot (20)

Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap tree
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Binary tree and Binary search tree
Binary tree and Binary search treeBinary tree and Binary search tree
Binary tree and Binary search tree
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Trees
TreesTrees
Trees
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Pig statements
Pig statementsPig statements
Pig statements
 
Bst(Binary Search Tree)
Bst(Binary Search Tree)Bst(Binary Search Tree)
Bst(Binary Search Tree)
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Binary tree and operations
Binary tree and operations Binary tree and operations
Binary tree and operations
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
7.tree
7.tree7.tree
7.tree
 

Viewers also liked

Viewers also liked (20)

TOKENS Saloni rajput
TOKENS Saloni rajputTOKENS Saloni rajput
TOKENS Saloni rajput
 
BARNOLI THEROY Swati manjhi
BARNOLI THEROY Swati manjhiBARNOLI THEROY Swati manjhi
BARNOLI THEROY Swati manjhi
 
Sanjana yadav
Sanjana yadavSanjana yadav
Sanjana yadav
 
vector atom model and quantum numbers associated with an electronVinita pa...
vector atom model  and quantum numbers  associated  with an electronVinita pa...vector atom model  and quantum numbers  associated  with an electronVinita pa...
vector atom model and quantum numbers associated with an electronVinita pa...
 
GREETING Shireen khan
GREETING Shireen khanGREETING Shireen khan
GREETING Shireen khan
 
KEPALER LAWSonam yadav
KEPALER LAWSonam yadavKEPALER LAWSonam yadav
KEPALER LAWSonam yadav
 
FRANHOPER Sudha mukati
FRANHOPER  Sudha mukatiFRANHOPER  Sudha mukati
FRANHOPER Sudha mukati
 
Vishal meena 1
Vishal meena 1Vishal meena 1
Vishal meena 1
 
COMPILER Meenu khan
COMPILER Meenu khanCOMPILER Meenu khan
COMPILER Meenu khan
 
OPARETER R Vandhana meena
OPARETER R Vandhana meenaOPARETER R Vandhana meena
OPARETER R Vandhana meena
 
Mathematical derivation of Uncertainty Principal For One-dimensional Wave Pac...
Mathematical derivation of Uncertainty Principal For One-dimensional Wave Pac...Mathematical derivation of Uncertainty Principal For One-dimensional Wave Pac...
Mathematical derivation of Uncertainty Principal For One-dimensional Wave Pac...
 
frinaz Shif a khan
frinaz Shif a khanfrinaz Shif a khan
frinaz Shif a khan
 
Neetesh sahu
Neetesh sahuNeetesh sahu
Neetesh sahu
 
Neetesh sahu
Neetesh sahuNeetesh sahu
Neetesh sahu
 
Seema
SeemaSeema
Seema
 
FUNCTION Sudha mukati 1
FUNCTION Sudha mukati 1FUNCTION Sudha mukati 1
FUNCTION Sudha mukati 1
 
SCHOTER WAVE EUQUTION Swati jaiswal
SCHOTER WAVE EUQUTION Swati jaiswalSCHOTER WAVE EUQUTION Swati jaiswal
SCHOTER WAVE EUQUTION Swati jaiswal
 
Nikita panwar
Nikita panwarNikita panwar
Nikita panwar
 
Galilean Transformation Sudeep pethari
Galilean Transformation Sudeep pethariGalilean Transformation Sudeep pethari
Galilean Transformation Sudeep pethari
 
DTP Shada khan
DTP Shada khanDTP Shada khan
DTP Shada khan
 

Similar to Linked Lists Saloni

Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures treemaamir farooq
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structuresNiraj Agarwal
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data StructureOm Prakash
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptxskilljiolms
 
Can somebody solve the TODO parts of the following problem- Thanks D.pdf
Can somebody solve the TODO parts of the following problem- Thanks   D.pdfCan somebody solve the TODO parts of the following problem- Thanks   D.pdf
Can somebody solve the TODO parts of the following problem- Thanks D.pdfvinaythemodel
 
Data structure
Data structureData structure
Data structureNida Ahmed
 
Admissions in india 2015
Admissions in india 2015Admissions in india 2015
Admissions in india 2015Edhole.com
 
Bc0038– data structure using c
Bc0038– data structure using cBc0038– data structure using c
Bc0038– data structure using chayerpa
 
Chap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.pptChap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.pptshashankbhadouria4
 
Data Structure Question Bank(2 marks)
Data Structure Question Bank(2 marks)Data Structure Question Bank(2 marks)
Data Structure Question Bank(2 marks)pushpalathakrishnan
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)Durga Devi
 
Trees - Non Linear Data Structure
Trees - Non Linear Data StructureTrees - Non Linear Data Structure
Trees - Non Linear Data StructurePriyanka Rana
 
bca data structure
bca data structurebca data structure
bca data structureshini
 

Similar to Linked Lists Saloni (20)

Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 
Bc0041
Bc0041Bc0041
Bc0041
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
Cs341
Cs341Cs341
Cs341
 
Can somebody solve the TODO parts of the following problem- Thanks D.pdf
Can somebody solve the TODO parts of the following problem- Thanks   D.pdfCan somebody solve the TODO parts of the following problem- Thanks   D.pdf
Can somebody solve the TODO parts of the following problem- Thanks D.pdf
 
Data structure
Data structureData structure
Data structure
 
Admissions in india 2015
Admissions in india 2015Admissions in india 2015
Admissions in india 2015
 
Unit8 C
Unit8 CUnit8 C
Unit8 C
 
Bc0038– data structure using c
Bc0038– data structure using cBc0038– data structure using c
Bc0038– data structure using c
 
Chap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.pptChap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.ppt
 
Data Structure Question Bank(2 marks)
Data Structure Question Bank(2 marks)Data Structure Question Bank(2 marks)
Data Structure Question Bank(2 marks)
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
CSE240 Doubly Linked Lists
CSE240 Doubly Linked ListsCSE240 Doubly Linked Lists
CSE240 Doubly Linked Lists
 
L3
L3L3
L3
 
Trees - Non Linear Data Structure
Trees - Non Linear Data StructureTrees - Non Linear Data Structure
Trees - Non Linear Data Structure
 
Lec6 mod linked list
Lec6 mod linked listLec6 mod linked list
Lec6 mod linked list
 
bca data structure
bca data structurebca data structure
bca data structure
 

More from Rai Saheb Bhanwar Singh College Nasrullaganj (20)

lec34.ppt
lec34.pptlec34.ppt
lec34.ppt
 
lec33.ppt
lec33.pptlec33.ppt
lec33.ppt
 
lec31.ppt
lec31.pptlec31.ppt
lec31.ppt
 
lec32.ppt
lec32.pptlec32.ppt
lec32.ppt
 
lec42.ppt
lec42.pptlec42.ppt
lec42.ppt
 
lec41.ppt
lec41.pptlec41.ppt
lec41.ppt
 
lec39.ppt
lec39.pptlec39.ppt
lec39.ppt
 
lec38.ppt
lec38.pptlec38.ppt
lec38.ppt
 
lec37.ppt
lec37.pptlec37.ppt
lec37.ppt
 
lec23.ppt
lec23.pptlec23.ppt
lec23.ppt
 
lec21.ppt
lec21.pptlec21.ppt
lec21.ppt
 
lec20.ppt
lec20.pptlec20.ppt
lec20.ppt
 
lec19.ppt
lec19.pptlec19.ppt
lec19.ppt
 
lec18.ppt
lec18.pptlec18.ppt
lec18.ppt
 
lec17.ppt
lec17.pptlec17.ppt
lec17.ppt
 
lec16.ppt
lec16.pptlec16.ppt
lec16.ppt
 
lec30.ppt
lec30.pptlec30.ppt
lec30.ppt
 
lec28.ppt
lec28.pptlec28.ppt
lec28.ppt
 
lec27.ppt
lec27.pptlec27.ppt
lec27.ppt
 
lec26.ppt
lec26.pptlec26.ppt
lec26.ppt
 

Recently uploaded

HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 

Recently uploaded (20)

HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 

Linked Lists Saloni

  • 3.
  • 4. LINKED LISTS  Definition of Linked Lists  Examples of Linked Lists  Operations on Linked Lists  Linked List as a Class  Linked Lists as Implementations of Stacks, Sets, etc. CS 103 4
  • 5. DEFINITION OF LINKED LISTS  A linked list is a sequence of items (objects) where every item is linked to the next.  Graphically: CS 103 5 data data data data head_ptr tail_ptr
  • 6. EXAMPLES OF LINKED LISTS (A WAITING LINE)  A waiting line of customers: John, Mary, Dan, Sue (from the head to the tail of the line)  A linked list of strings can represent this line: CS 103 6 John Mary Dan Sue head_ptr tail_ptr
  • 7. EXAMPLES OF LINKED LISTS (A STACK OF NUMBERS)  A stack of numbers (from top to bottom): 10, 8, 6, 8, 2  A linked list of ints can represent this stack: CS 103 7 10 8 6 2 head_ptr tail_ptr 8
  • 8. OPERATIONS ON LINKED LISTS  Insert a new item  At the head of the list, or  At the tail of the list, or  Inside the list, in some designated position  Search for an item in the list  The item can be specified by position, or by some value  Delete an item from the list  Search for and locate the item, then remove the item, and finally adjust the surrounding pointers  size( );  isEmpty( ) CS 103 8
  • 9. INSERT– AT THE HEAD  Insert a new data A. Call new: newPtr List before insertion:  After insertion to head: CS 103 9 data data data data head_ptr tail_ptr A data data data data head_ptr tail_ptr A •The link value in the new item = old head_ptr •The new value of head_ptr = newPtr
  • 10. INSERT – AT THE TAIL  Insert a new data A. Call new: newPtr List before insertion  After insertion to tail: CS 103 10 data data data data head_ptr tail_ptr A data data data data head_ptr tail_ptr A •The link value in the new item = NULL •The link value of the old last item = newPtr
  • 11. INSERT – INSIDE THE LIST  Insert a new data A. Call new: newPtr List before insertion:  After insertion in 3rd position: CS 103 11 data data data data head_ptr tail_ptr data data A data data head_ptr tail_ptr data •The link-value in the new item = link-value of 2nd item •The new link-value of 2nd item = newPtr
  • 12. DELETE – THE HEAD ITEM  List before deletion:  List after deletion of the head item: CS 103 12 data data data data head_ptr tail_ptr data data data data head_ptr tail_ptr data •The new value of head_ptr = link-value of the old head item •The old head item is deleted and its memory returned data
  • 13. DELETE – THE TAIL ITEM  List before deletion:  List after deletion of the tail item: CS 103 13 data data data data head_ptr tail_ptr data data data head_ptr tail_ptr •New value of tail_ptr = link-value of the 3rd from last item •New link-value of new last item = NULL. data datadata
  • 14. DELETE – AN INSIDE ITEM  List before deletion:  List after deletion of the 2nd item: CS 103 14 data data data data head_ptr tail_ptr data data head_ptr tail_ptr •New link-value of the item located before the deleted one = the link-value of the deleted item data data datadata
  • 15. SEARCHING FOR AN ITEM  Suppose you want to find the item whose data value is A  You have to search sequentially starting from the head item rightward until the first item whose data member is equal to A is found.  At each item searched, a comparison between the data member and A is performed. CS 103 15
  • 17. STACK SPECIFICATION  Definitions: (provided by the user)  MAX_ITEMS: Max number of items that might be on the stack  ItemType: Data type of the items on the stack  Operations  MakeEmpty  Boolean IsEmpty  Boolean IsFull  Push (ItemType newItem)  Pop (ItemType& item) (or pop and top)
  • 18. POP (ITEMTYPE& ITEM)  Function: Removes topItem from stack and returns it in item.  Preconditions: Stack has been initialized and is not empty.  Postconditions: Top element has been removed from stack and item is a copy of the removed element.
  • 19.
  • 20. Stack overflow  The condition resulting from trying to push an element onto a full stack. if(!stack.IsFull()) stack.Push(item); Stack underflow  The condition resulting from trying to pop an empty stack. if(!stack.IsEmpty()) stack.Pop(item);
  • 21. IMPLEMENTING STACKS USING TEMPLATES  Templates allow the compiler to generate multiple versions of a class type or a function by allowing parameterized types.
  • 22. TREES AND BINARY TREES Become Rich Force Others to be Poor Rob Banks Stock Fraud The class notes are a compilation and edition from many sources. The instructor does not claim intellectual property or ownership of the lecture notes.
  • 23. WHAT IS A TREE  A tree is a finite nonempty set of elements.  It is an abstract model of a hierarchical structure.  consists of nodes with a parent-child relation.  Applications:  Organization charts  File systems  Programming environments Computers”R”Us Sales R&DManufacturing Laptops DesktopsUS International Europe Asia Canada
  • 24. subtree TREE TERMINOLOGY  Root: node without parent (A)  Siblings: nodes share the same parent  Internal node: node with at least one child (A, B, C, F)  External node (leaf ): node without children (E, I, J, K, G, H, D)  Ancestors of a node: parent, grandparent, grand-grandparent, etc.  Descendant of a node: child, grandchild, grand-grandchild, etc.  Depth of a node: number of ancestors  Height of a tree: maximum depth of any node (3)  Degree of a node: the number of its children  Degree of a tree: the maximum number of its node. A B DC G HE F I J K Subtree: tree consisting of a node and its descendants
  • 25. LEFT CHILD, RIGHT SIBLING REPRESENTATION Data Left Child Right Sibling A B C D IHGFE J K L
  • 26. TREE TRAVERSAL  Two main methods:  Preorder  Postorder  Recursive definition  Preorder:  visit the root  traverse in preorder the children (subtrees)  Postorder  traverse in postorder the children (subtrees)  visit the root
  • 27. PREORDER TRAVERSAL  A traversal visits the nodes of a tree in a systematic manner  In a preorder traversal, a node is visited before its descendants  Application: print a structured document Become Rich 1. Motivations 3. Success Stories2. Methods 2.1 Get a CS PhD 2.2 Start a Web Site 1.1 Enjoy Life 1.2 Help Poor Friends 2.3 Acquired by Google 1 2 3 5 4 6 7 8 9 Algorithm preOrder(v) visit(v) for each child w of v preorder (w)
  • 28. POSTORDER TRAVERSAL  In a postorder traversal, a node is visited after its descendants  Application: compute space used by files in a directory and its subdirectories Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) cs16/ homeworks/ todo.txt 1K programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8
  • 29. BINARY TREE  A binary tree is a tree with the following properties:  Each internal node has at most two children (degree of two)  The children of a node are an ordered pair  We call the children of an internal node left child and right child  Alternative recursive definition: a binary tree is either  a tree consisting of a single node, OR  a tree whose root has an ordered pair of children, each of which is a binary tree Applications: arithmetic expressions decision processes searching A B C F GD E H I
  • 30. Examples of the Binary Tree A B C GE I D H F Complete Binary Tree 1 2 3 4 A B A B Skewed Binary Tree E C D 5
  • 31. Maximum Number of Nodes in a Binary Tree The maximum number of nodes on depth i of a binary tree is 2i, i>=0. The maximum nubmer of nodes in a binary tree of height k is 2k+1-1, k>=0. Prove by induction. 122 1 0     k k i i
  • 32. FULL BINARY TREE  A full binary tree of a given height k has 2k+1–1 nodes. Height 3 full binary tree.
  • 33. Complete Binary Trees A labeled binary tree containing the labels 1 to n with root 1, branches leading to nodes labeled 2 and 3, branches from these leading to 4, 5 and 6, 7, respectively, and so on. A binary tree with n nodes and level k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of level k. 1 2 3 75 11 4 10 6 98 15141312 Full binary tree of depth 3 1 2 3 75 9 4 8 6 Complete binary tree
  • 34. Binary Tree Traversals Let l, R, and r stand for moving left, visiting the node, and moving right. There are six possible combinations of traversal lRr, lrR, Rlr, Rrl, rRl, rlR Adopt convention that we traverse left before right, only 3 traversals remain lRr, lrR, Rlr inorder, postorder, preorder
  • 35. INORDER TRAVERSAL  In an inorder traversal a node is visited after its left subtree and before its right subtree Algorithm inOrder(v) if isInternal (v) inOrder (leftChild (v)) visit(v) if isInternal (v) inOrder (rightChild (v)) 3 1 2 5 6 7 9 8 4