SlideShare a Scribd company logo
Class No.05  Data Structures http://ecomputernotes.com
Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) {   for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; } http://ecomputernotes.com 
Josephus Problem Using a circularly-linked list made the solution trivial. http://ecomputernotes.com
Josephus Problem Using a circularly-linked list made the solution trivial. The solution would have been more difficult if an array had been used. http://ecomputernotes.com
Josephus Problem Using a circularly-linked list made the solution trivial. The solution would have been more difficult if an array had been used. This illustrates the fact that the choice of the appropriate data structures can significantly simplify an algorithm. It can make the algorithm much faster and efficient. http://ecomputernotes.com
Josephus Problem Using a circularly-linked list made the solution trivial. The solution would have been more difficult if an array had been used. This illustrates the fact that the choice of the appropriate data structures can significantly simplify an algorithm. It can make the algorithm much faster and efficient. Later we will see how some elegant data structures lie at the heart of major algorithms. http://ecomputernotes.com
Josephus Problem Using a circularly-linked list made the solution trivial. The solution would have been more difficult if an array had been used. This illustrates the fact that the choice of the appropriate data structures can significantly simplify an algorithm. It can make the algorithm much faster and efficient. Later we will see how some elegant data structures lie at the heart of major algorithms. An entire CS course “Design and Analysis of Algorithms” is devoted to this topic. http://ecomputernotes.com
Abstract Data Type We have looked at four different implementations of the List data structures: Using arrays Singly linked list Doubly linked list Circularly linked list. http://ecomputernotes.com
Abstract Data Type We have looked at four different implementations of the List data structures: Using arrays Singly linked list Doubly linked list Circularly linked list. The interface to the List stayed the same, i.e., add(), get(), next(), start(), remove() etc. http://ecomputernotes.com
Abstract Data Type We have looked at four different implementations of the List data structures: Using arrays Singly linked list Doubly linked list Circularly linked list. The interface to the List stayed the same, i.e., add(), get(), next(), start(), remove() etc. The list is thus an abstract data type; we use it without being concerned with how it is implemented. http://ecomputernotes.com
Abstract Data Type What we care about is the methods that are available for use with the List ADT. http://ecomputernotes.com
Abstract Data Type What we care about is the methods that are available for use with the List ADT. We will follow this theme when we develop other ADT.  http://ecomputernotes.com
Abstract Data Type What we care about is the methods that are available for use with the List ADT. We will follow this theme when we develop other ADT.  We will publish the interface and keep the freedom to change the implementation of ADT without effecting users of the ADT. http://ecomputernotes.com
Abstract Data Type What we care about is the methods that are available for use with the List ADT. We will follow this theme when we develop other ADT.  We will publish the interface and keep the freedom to change the implementation of ADT without effecting users of the ADT. The C++ classes provide us the ability to create such ADTs. http://ecomputernotes.com
Stacks Stacks in real life: stack of books, stack of plates Add new items at the top Remove an item at the top Stack data structure similar to real life: collection of elements arranged in a linear order. Can only access element at the top  http://ecomputernotes.com
Stack Operations Push(X) – insert X as the top element of the stack Pop() – remove the top element of the stack and return it. Top() – return the top element without removing it from the stack.  http://ecomputernotes.com
Stack Operations push(2) top 2 push(5) top 2 5 push(7) top 2 5 7 push(1) top 2 5 7 1 1  pop() top 2 5 7 push(21) top 2 5 7 21 21  pop() top 2 5 7 7  pop() 2 5 top 5  pop() 2 top http://ecomputernotes.com
Stack Operation The last element to go into the stack is the first to come out:  LIFO  – Last In First Out. What happens if we call pop() and there is no element? Have IsEmpty() boolean function that returns true if stack is empty, false otherwise. Throw StackEmpty exception: advanced C++ concept. http://ecomputernotes.com
Stack Implementation: Array  Worst case for insertion and deletion from an array when insert and delete from the beginning: shift elements to the left. Best case for insert and delete is at the end of the array – no need to shift any elements. Implement push() and pop() by inserting and deleting at the end of an array. http://ecomputernotes.com
Stack using an Array top 2 5 7 1 2 5 7 1 0 1 3 2 4 top = 3 http://ecomputernotes.com
Stack using an Array In case of an array, it is possible that the array may “fill-up” if we push enough elements. Have a boolean function  IsFull()  which returns true is stack (array) is full, false otherwise. We would call this function before calling push(x). http://ecomputernotes.com
Stack Operations with Array int pop() { return A[current--]; } void push(int x) { A[++current] = x; } http://ecomputernotes.com
Stack Operations with Array int top() { return A[current]; }  int IsEmpty() { return ( current == -1 ); } int IsFull() { return ( current == size-1); } A quick examination shows that all five operations take constant time. http://ecomputernotes.com
Stack Using Linked List We can avoid the size limitation of a stack implemented with an array by using a linked list to hold the stack elements. As with array, however, we need to decide where to insert elements in the list and where to delete them so that push and pop will run the fastest. http://ecomputernotes.com
Ad

More Related Content

What's hot (19)

My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
Senthil Kumar
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
gomathi chlm
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
vaibhav2910
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
Abbott
 
Lecture2
Lecture2Lecture2
Lecture2
Muhammad Zubair
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
anooppjoseph
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
ForwardBlog Enewzletter
 
Stack and queue
Stack and queueStack and queue
Stack and queue
CHANDAN KUMAR
 
Lecture3
Lecture3Lecture3
Lecture3
Muhammad Zubair
 
Lecture4
Lecture4Lecture4
Lecture4
Muhammad Zubair
 
Stack Data structure
Stack Data structureStack Data structure
Stack Data structure
B Liyanage Asanka
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
Muhazzab Chouhadry
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
Buxoo Abdullah
 
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queuesFallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
SnehilKeshari
 
Stack data structure
Stack data structureStack data structure
Stack data structure
rogineojerio020496
 
Chapter 10: hashing data structure
Chapter 10:  hashing data structureChapter 10:  hashing data structure
Chapter 10: hashing data structure
Mahmoud Alfarra
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Lovely Professional University
 
computer notes - Priority queue
computer notes -  Priority queuecomputer notes -  Priority queue
computer notes - Priority queue
ecomputernotes
 
Chapter 6: stack data structure
Chapter 6:  stack data structureChapter 6:  stack data structure
Chapter 6: stack data structure
Mahmoud Alfarra
 

Viewers also liked (7)

General Solution for Josephus Problem
General Solution for Josephus ProblemGeneral Solution for Josephus Problem
General Solution for Josephus Problem
Yoav Francis
 
Stack & queue
Stack & queueStack & queue
Stack & queue
Siddique Ibrahim
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
swajahatr
 
Queue and stacks
Queue and stacksQueue and stacks
Queue and stacks
grahamwell
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structure
faran nawaz
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
greatqadirgee4u
 
Stack data structure
Stack data structureStack data structure
Stack data structure
Tech_MX
 
General Solution for Josephus Problem
General Solution for Josephus ProblemGeneral Solution for Josephus Problem
General Solution for Josephus Problem
Yoav Francis
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
swajahatr
 
Queue and stacks
Queue and stacksQueue and stacks
Queue and stacks
grahamwell
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structure
faran nawaz
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
greatqadirgee4u
 
Stack data structure
Stack data structureStack data structure
Stack data structure
Tech_MX
 
Ad

Similar to Computer notes - Josephus Problem (20)

Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
pinakspatel
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8
ecomputernotes
 
Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9
Getachew Ganfur
 
Rana Junaid Rasheed
Rana Junaid RasheedRana Junaid Rasheed
Rana Junaid Rasheed
Rana junaid Rasheed
 
Java Generics for Dummies
Java Generics for DummiesJava Generics for Dummies
Java Generics for Dummies
knutmork
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
Rajkiran Nadar
 
stack.ppt
stack.pptstack.ppt
stack.ppt
ssuserec1395
 
Computer notes data structures - 9
Computer notes   data structures - 9Computer notes   data structures - 9
Computer notes data structures - 9
ecomputernotes
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
ADT STACK and Queues
ADT STACK and QueuesADT STACK and Queues
ADT STACK and Queues
BHARATH KUMAR
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Balwant Gorad
 
computer notes - Data Structures - 9
computer notes - Data Structures - 9computer notes - Data Structures - 9
computer notes - Data Structures - 9
ecomputernotes
 
2 a stacks
2 a stacks2 a stacks
2 a stacks
Nguync91368
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queues
tech4us
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
kulachihansraj
 
Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018
Codemotion
 
Lazy Java
Lazy JavaLazy Java
Lazy Java
J On The Beach
 
Lazy Java
Lazy JavaLazy Java
Lazy Java
Nicola Pedot
 
Lazy java
Lazy javaLazy java
Lazy java
Mario Fusco
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
freddysarabia1
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
pinakspatel
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8
ecomputernotes
 
Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9
Getachew Ganfur
 
Java Generics for Dummies
Java Generics for DummiesJava Generics for Dummies
Java Generics for Dummies
knutmork
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
Rajkiran Nadar
 
Computer notes data structures - 9
Computer notes   data structures - 9Computer notes   data structures - 9
Computer notes data structures - 9
ecomputernotes
 
ADT STACK and Queues
ADT STACK and QueuesADT STACK and Queues
ADT STACK and Queues
BHARATH KUMAR
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Balwant Gorad
 
computer notes - Data Structures - 9
computer notes - Data Structures - 9computer notes - Data Structures - 9
computer notes - Data Structures - 9
ecomputernotes
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queues
tech4us
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
kulachihansraj
 
Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018
Codemotion
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
freddysarabia1
 
Ad

More from ecomputernotes (20)

computer notes - Data Structures - 30
computer notes - Data Structures - 30computer notes - Data Structures - 30
computer notes - Data Structures - 30
ecomputernotes
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39
ecomputernotes
 
computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11
ecomputernotes
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20
ecomputernotes
 
computer notes - Data Structures - 15
computer notes - Data Structures - 15computer notes - Data Structures - 15
computer notes - Data Structures - 15
ecomputernotes
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraints
ecomputernotes
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functions
ecomputernotes
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueries
ecomputernotes
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objects
ecomputernotes
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28
ecomputernotes
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19
ecomputernotes
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31
ecomputernotes
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4
ecomputernotes
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13
ecomputernotes
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueries
ecomputernotes
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functions
ecomputernotes
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16
ecomputernotes
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22
ecomputernotes
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35
ecomputernotes
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36
ecomputernotes
 
computer notes - Data Structures - 30
computer notes - Data Structures - 30computer notes - Data Structures - 30
computer notes - Data Structures - 30
ecomputernotes
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39
ecomputernotes
 
computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11
ecomputernotes
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20
ecomputernotes
 
computer notes - Data Structures - 15
computer notes - Data Structures - 15computer notes - Data Structures - 15
computer notes - Data Structures - 15
ecomputernotes
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraints
ecomputernotes
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functions
ecomputernotes
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueries
ecomputernotes
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objects
ecomputernotes
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28
ecomputernotes
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19
ecomputernotes
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31
ecomputernotes
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4
ecomputernotes
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13
ecomputernotes
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueries
ecomputernotes
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functions
ecomputernotes
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16
ecomputernotes
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22
ecomputernotes
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35
ecomputernotes
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36
ecomputernotes
 

Recently uploaded (20)

Engage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdfEngage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdf
TechSoup
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
"Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules""Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules"
rupalinirmalbpharm
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
Nguyen Thanh Tu Collection
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Contact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: OptometryContact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: Optometry
MushahidRaza8
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
Engage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdfEngage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdf
TechSoup
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
"Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules""Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules"
rupalinirmalbpharm
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
Nguyen Thanh Tu Collection
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Contact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: OptometryContact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: Optometry
MushahidRaza8
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 

Computer notes - Josephus Problem

  • 1. Class No.05 Data Structures http://ecomputernotes.com
  • 2. Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; } http://ecomputernotes.com 
  • 3. Josephus Problem Using a circularly-linked list made the solution trivial. http://ecomputernotes.com
  • 4. Josephus Problem Using a circularly-linked list made the solution trivial. The solution would have been more difficult if an array had been used. http://ecomputernotes.com
  • 5. Josephus Problem Using a circularly-linked list made the solution trivial. The solution would have been more difficult if an array had been used. This illustrates the fact that the choice of the appropriate data structures can significantly simplify an algorithm. It can make the algorithm much faster and efficient. http://ecomputernotes.com
  • 6. Josephus Problem Using a circularly-linked list made the solution trivial. The solution would have been more difficult if an array had been used. This illustrates the fact that the choice of the appropriate data structures can significantly simplify an algorithm. It can make the algorithm much faster and efficient. Later we will see how some elegant data structures lie at the heart of major algorithms. http://ecomputernotes.com
  • 7. Josephus Problem Using a circularly-linked list made the solution trivial. The solution would have been more difficult if an array had been used. This illustrates the fact that the choice of the appropriate data structures can significantly simplify an algorithm. It can make the algorithm much faster and efficient. Later we will see how some elegant data structures lie at the heart of major algorithms. An entire CS course “Design and Analysis of Algorithms” is devoted to this topic. http://ecomputernotes.com
  • 8. Abstract Data Type We have looked at four different implementations of the List data structures: Using arrays Singly linked list Doubly linked list Circularly linked list. http://ecomputernotes.com
  • 9. Abstract Data Type We have looked at four different implementations of the List data structures: Using arrays Singly linked list Doubly linked list Circularly linked list. The interface to the List stayed the same, i.e., add(), get(), next(), start(), remove() etc. http://ecomputernotes.com
  • 10. Abstract Data Type We have looked at four different implementations of the List data structures: Using arrays Singly linked list Doubly linked list Circularly linked list. The interface to the List stayed the same, i.e., add(), get(), next(), start(), remove() etc. The list is thus an abstract data type; we use it without being concerned with how it is implemented. http://ecomputernotes.com
  • 11. Abstract Data Type What we care about is the methods that are available for use with the List ADT. http://ecomputernotes.com
  • 12. Abstract Data Type What we care about is the methods that are available for use with the List ADT. We will follow this theme when we develop other ADT. http://ecomputernotes.com
  • 13. Abstract Data Type What we care about is the methods that are available for use with the List ADT. We will follow this theme when we develop other ADT. We will publish the interface and keep the freedom to change the implementation of ADT without effecting users of the ADT. http://ecomputernotes.com
  • 14. Abstract Data Type What we care about is the methods that are available for use with the List ADT. We will follow this theme when we develop other ADT. We will publish the interface and keep the freedom to change the implementation of ADT without effecting users of the ADT. The C++ classes provide us the ability to create such ADTs. http://ecomputernotes.com
  • 15. Stacks Stacks in real life: stack of books, stack of plates Add new items at the top Remove an item at the top Stack data structure similar to real life: collection of elements arranged in a linear order. Can only access element at the top http://ecomputernotes.com
  • 16. Stack Operations Push(X) – insert X as the top element of the stack Pop() – remove the top element of the stack and return it. Top() – return the top element without removing it from the stack. http://ecomputernotes.com
  • 17. Stack Operations push(2) top 2 push(5) top 2 5 push(7) top 2 5 7 push(1) top 2 5 7 1 1 pop() top 2 5 7 push(21) top 2 5 7 21 21 pop() top 2 5 7 7 pop() 2 5 top 5 pop() 2 top http://ecomputernotes.com
  • 18. Stack Operation The last element to go into the stack is the first to come out: LIFO – Last In First Out. What happens if we call pop() and there is no element? Have IsEmpty() boolean function that returns true if stack is empty, false otherwise. Throw StackEmpty exception: advanced C++ concept. http://ecomputernotes.com
  • 19. Stack Implementation: Array Worst case for insertion and deletion from an array when insert and delete from the beginning: shift elements to the left. Best case for insert and delete is at the end of the array – no need to shift any elements. Implement push() and pop() by inserting and deleting at the end of an array. http://ecomputernotes.com
  • 20. Stack using an Array top 2 5 7 1 2 5 7 1 0 1 3 2 4 top = 3 http://ecomputernotes.com
  • 21. Stack using an Array In case of an array, it is possible that the array may “fill-up” if we push enough elements. Have a boolean function IsFull() which returns true is stack (array) is full, false otherwise. We would call this function before calling push(x). http://ecomputernotes.com
  • 22. Stack Operations with Array int pop() { return A[current--]; } void push(int x) { A[++current] = x; } http://ecomputernotes.com
  • 23. Stack Operations with Array int top() { return A[current]; } int IsEmpty() { return ( current == -1 ); } int IsFull() { return ( current == size-1); } A quick examination shows that all five operations take constant time. http://ecomputernotes.com
  • 24. Stack Using Linked List We can avoid the size limitation of a stack implemented with an array by using a linked list to hold the stack elements. As with array, however, we need to decide where to insert elements in the list and where to delete them so that push and pop will run the fastest. http://ecomputernotes.com

Editor's Notes

  • #3: End of lecture 4. The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #4: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #5: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #6: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #7: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #8: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #9: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #10: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #11: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #12: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #13: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #14: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #15: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #16: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #17: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #18: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #19: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #20: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #21: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #22: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #23: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #24: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #25: End of lecture 5 You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.