SlideShare a Scribd company logo
1 of 27
Data Structures
06/26/15 CS 201
Data Structure
• A construct that can be defined within a
programming language to store a
collection of data
– one may store some data in an array of
integers, an array of objects, or an array of
arrays
06/26/15 CS 201
Abstract Data Type (ADT)
• Definition: a collection of data together
with a set of operations on that data
– specifications indicate what ADT
operations do, but not how to implement
them
– data structures are part of an ADT’s
implementation
• Programmer can use an ADT without
knowing its implementation.
06/26/15 CS 201
Typical Operations on Data
• Add data to a data collection
• Remove data from a data collection
• Ask questions about the data in a data
collection. E.g., what is the value at a
particular location, and is x in the
collection?
06/26/15 CS 201
Why ADT
• Hide the unnecessary details
• Help manage software complexity
• Easier software maintenance
• Functionalities are less likely to change
• Localised rather than global changes
06/26/15 CS 201
Illustration
Stacks
06/26/15 CS 201
What is a Stack?
• A stack is a list with the restriction that
insertions and deletions can be performed in
only one position, namely, the end of the list,
called the top.
• The operations: push (insert) and pop (delete)
pop push(o)
6
7
2
3Top
06/26/15 CS 201
Stack ADT Interface
• The main functions in the Stack ADT are (S is the stack)
boolean isEmpty(); // return true if empty
boolean isFull(S); // return true if full
void push(S, item); // insert item into stack
void pop(S); // remove most recent item
void clear(S); // remove all items from stack
Item top(S); // retrieve most recent item
Item topAndPop(S); // return & remove most recent item
06/26/15 CS 201
Sample Operation
Stack S = malloc(sizeof(stack));
push(S, “a”);
push(S, “b”);
push(S, “c”);
d=top(S);
pop(S);
push(S, “e”);
pop(S);
s
abc
top
e
d
06/26/15 CS 201
Implementation by Array
• use Array with a top index pointer as an implementation of stack
E F
0 1 7 8 92 3 4 5 6
A B C D
top
StackAr
arr
A
06/26/15 CS 201
Code
06/26/15 CS 201
More code
06/26/15 CS 201
More code
Effects
06/26/15 CS 201
06/26/15 CS 201
Applications
• Many application areas use stacks:
– line editing
– bracket matching
– postfix calculation
– function call stack
Queues
06/26/15 CS 201
What is a Queue?
• Like stacks, queues are lists. With a queue,
however, insertion is done at one end whereas
deletion is done at the other end.
• Queues implement the FIFO (first-in first-out)
policy. E.g., a printer/job queue!
• Two basic operations of queues:
– dequeue: remove an item/element from front
– enqueue: add an item/element at the back
dequeue enqueue
06/26/15 CS 201
Queue ADT
• Queues implement the FIFO (first-in first-out)
policy
– An example is the printer/job queue!
enqueue(o)
dequeue()
isEmpty()
getFront() createQueue()
06/26/15 CS 201
Sample Operation
Queue *Q;
enqueue(Q, “a”);
enqueue(Q, “b”);
enqueue(Q, “c”);
d=getFront(Q);
dequeue(Q);
enqueue(Q, “e”);
dequeue(Q);
q
front back
a b c e
d
06/26/15 CS 201
Queue ADT interface
• The main functions in the Queue ADT are (Q is
the queue)
void enqueue(o, Q) // insert o to back of Q
void dequeue(Q); // remove oldest item
Item getFront(Q); // retrieve oldest item
boolean isEmpty(Q); // checks if Q is empty
boolean isFull(Q); // checks if Q is full
void clear(Q); // make Q empty
}
06/26/15 CS 201
Implementation of Queue
(Array)
• use Array with front and back pointers as implementation of
queue Queue
arr 0 1 7 8 92 3 4 5 6
A B C D E F G
front
back
06/26/15 CS 201
Circular Array
• To implement queue, it is best to view arrays as circular structure
0 1 7 8 92 3 4 5 6
A B C D E F G
front
back
front
back
A
B
C
D
EF
G
0
1
7
8
9
2
3
45
6
Circular view of arrays.
06/26/15 CS 201
How to Advance
• Both front & back pointers should make advancement until they
reach end of the array. Then, they should re-point to beginning
of the array
front = adv(front);
back = adv(back);
int adv(int p)
{ return ((p+1) % maxsize);
}
Alternatively, use modular arithmetic:
mod operator
int adv(int p)
{ int r = p+1;
if (r<maxsize) return r;
else return 0;
}
upper bound of the array
06/26/15 CS 201
Sample
Queue *Q;
enqueue(Q, “a”);
enqueue(Q, “b”);
enqueue(Q, “c”);
dequeue(Q);
dequeue(Q);
enqueue(Q, “d”);
enqueue(Q, “e”);
dequeue(Q);
a
Q
F=front
B=back
F
B
b c d
F
B B B
F F
B B
e
06/26/15 CS 201
Checking for Full/Empty State
What does (F==B) denote?
F
B
Queue
Empty
State
c de
B
F
f Queue
Full
State
size 0 size 4
c de
B F
Alternative - Leave a Deliberate Gap!
No need for size field.
Full Case : (adv(B)==F)
06/26/15 CS 201
Summary
• The definition of the queue operations
gives the ADT queue first-in, first-out
(FIFO) behavior
• The queue can be implemented by
linked lists or by arrays
• There are many applications
– Printer queues,
– Telecommunication queues,
– Simulations,
– Etc.

More Related Content

What's hot

computer notes - Data Structures - 6
computer notes - Data Structures - 6computer notes - Data Structures - 6
computer notes - Data Structures - 6ecomputernotes
 
High Fidelity Wind Model Software for Real-Time Simulation Platforms
High Fidelity Wind Model Software for Real-Time Simulation PlatformsHigh Fidelity Wind Model Software for Real-Time Simulation Platforms
High Fidelity Wind Model Software for Real-Time Simulation PlatformsSimspace Ingeniería SL
 
Using FME to Transfer Park Asset Data From an Oracle Database to Trimble GPS ...
Using FME to Transfer Park Asset Data From an Oracle Database to Trimble GPS ...Using FME to Transfer Park Asset Data From an Oracle Database to Trimble GPS ...
Using FME to Transfer Park Asset Data From an Oracle Database to Trimble GPS ...Safe Software
 
WOBC AutoDISE Brief
WOBC AutoDISE BriefWOBC AutoDISE Brief
WOBC AutoDISE Briefphase3-120A
 
Design Of 64-Bit Parallel Prefix VLSI Adder For High Speed Arithmetic Circuits
Design Of 64-Bit Parallel Prefix VLSI Adder For High Speed Arithmetic CircuitsDesign Of 64-Bit Parallel Prefix VLSI Adder For High Speed Arithmetic Circuits
Design Of 64-Bit Parallel Prefix VLSI Adder For High Speed Arithmetic CircuitsIJRES Journal
 
Auto dise paper
Auto dise paper  Auto dise paper
Auto dise paper phase3-120A
 
2. 8085 introduction to instruction
2. 8085 introduction to instruction2. 8085 introduction to instruction
2. 8085 introduction to instructionsandip das
 

What's hot (15)

Master Thesis
Master ThesisMaster Thesis
Master Thesis
 
Krishankant Sharma Portfolio
Krishankant Sharma PortfolioKrishankant Sharma Portfolio
Krishankant Sharma Portfolio
 
computer notes - Data Structures - 6
computer notes - Data Structures - 6computer notes - Data Structures - 6
computer notes - Data Structures - 6
 
High Fidelity Wind Model Software for Real-Time Simulation Platforms
High Fidelity Wind Model Software for Real-Time Simulation PlatformsHigh Fidelity Wind Model Software for Real-Time Simulation Platforms
High Fidelity Wind Model Software for Real-Time Simulation Platforms
 
Using FME to Transfer Park Asset Data From an Oracle Database to Trimble GPS ...
Using FME to Transfer Park Asset Data From an Oracle Database to Trimble GPS ...Using FME to Transfer Park Asset Data From an Oracle Database to Trimble GPS ...
Using FME to Transfer Park Asset Data From an Oracle Database to Trimble GPS ...
 
WOBC AutoDISE Brief
WOBC AutoDISE BriefWOBC AutoDISE Brief
WOBC AutoDISE Brief
 
Presentation
PresentationPresentation
Presentation
 
Design Of 64-Bit Parallel Prefix VLSI Adder For High Speed Arithmetic Circuits
Design Of 64-Bit Parallel Prefix VLSI Adder For High Speed Arithmetic CircuitsDesign Of 64-Bit Parallel Prefix VLSI Adder For High Speed Arithmetic Circuits
Design Of 64-Bit Parallel Prefix VLSI Adder For High Speed Arithmetic Circuits
 
CFG to CNF
CFG to CNFCFG to CNF
CFG to CNF
 
Auto dise paper
Auto dise paper  Auto dise paper
Auto dise paper
 
Vldb14
Vldb14Vldb14
Vldb14
 
Management Network Lec 3
Management Network Lec 3Management Network Lec 3
Management Network Lec 3
 
IMPLEMENTATION OF 128-BIT SPARSE KOGGE-STONE ADDER USING VERILOG
IMPLEMENTATION OF 128-BIT SPARSE KOGGE-STONE ADDER USING VERILOGIMPLEMENTATION OF 128-BIT SPARSE KOGGE-STONE ADDER USING VERILOG
IMPLEMENTATION OF 128-BIT SPARSE KOGGE-STONE ADDER USING VERILOG
 
Proceso desarrollo con uml
Proceso desarrollo con umlProceso desarrollo con uml
Proceso desarrollo con uml
 
2. 8085 introduction to instruction
2. 8085 introduction to instruction2. 8085 introduction to instruction
2. 8085 introduction to instruction
 

Similar to Array stack-queue1

lect- 3&4.ppt
lect- 3&4.pptlect- 3&4.ppt
lect- 3&4.pptmrizwan38
 
cs201-list-stack-queue.ppt
cs201-list-stack-queue.pptcs201-list-stack-queue.ppt
cs201-list-stack-queue.pptRahulYadav738822
 
Queue implementation
Queue implementationQueue implementation
Queue implementationRajendran
 
Data Structures - Lecture 2 - Unit 2.pptx
Data Structures - Lecture 2 - Unit 2.pptxData Structures - Lecture 2 - Unit 2.pptx
Data Structures - Lecture 2 - Unit 2.pptxDanielNesaKumarC
 
C++ Program It is only 1 rotation. Up-rotation of a stack. Write a.pdf
C++ Program It is only 1 rotation. Up-rotation of a stack.  Write a.pdfC++ Program It is only 1 rotation. Up-rotation of a stack.  Write a.pdf
C++ Program It is only 1 rotation. Up-rotation of a stack. Write a.pdfpallavi953613
 
Hands-On with U-SQL and Azure Data Lake Analytics (ADLA)
Hands-On with U-SQL and Azure Data Lake Analytics (ADLA)Hands-On with U-SQL and Azure Data Lake Analytics (ADLA)
Hands-On with U-SQL and Azure Data Lake Analytics (ADLA)Jason L Brugger
 
Stack Data Structure with Static Implementation (2).pptx
Stack Data Structure with Static Implementation (2).pptxStack Data Structure with Static Implementation (2).pptx
Stack Data Structure with Static Implementation (2).pptxEmroz Sardar
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbRAtna29
 
13 Stacks and Queues.pptx
13 Stacks and Queues.pptx13 Stacks and Queues.pptx
13 Stacks and Queues.pptxssuserb7c8b8
 
Complete the implementation of the Weighted Graph that we began in t.pdf
Complete the implementation of the Weighted Graph that we began in t.pdfComplete the implementation of the Weighted Graph that we began in t.pdf
Complete the implementation of the Weighted Graph that we began in t.pdfmarketing413921
 

Similar to Array stack-queue1 (20)

lect- 3&4.ppt
lect- 3&4.pptlect- 3&4.ppt
lect- 3&4.ppt
 
cs201-list-stack-queue.ppt
cs201-list-stack-queue.pptcs201-list-stack-queue.ppt
cs201-list-stack-queue.ppt
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 
Data Structures - Lecture 2 - Unit 2.pptx
Data Structures - Lecture 2 - Unit 2.pptxData Structures - Lecture 2 - Unit 2.pptx
Data Structures - Lecture 2 - Unit 2.pptx
 
Lecture 07
Lecture 07Lecture 07
Lecture 07
 
C++ Program It is only 1 rotation. Up-rotation of a stack. Write a.pdf
C++ Program It is only 1 rotation. Up-rotation of a stack.  Write a.pdfC++ Program It is only 1 rotation. Up-rotation of a stack.  Write a.pdf
C++ Program It is only 1 rotation. Up-rotation of a stack. Write a.pdf
 
Hands-On with U-SQL and Azure Data Lake Analytics (ADLA)
Hands-On with U-SQL and Azure Data Lake Analytics (ADLA)Hands-On with U-SQL and Azure Data Lake Analytics (ADLA)
Hands-On with U-SQL and Azure Data Lake Analytics (ADLA)
 
2 a stacks
2 a stacks2 a stacks
2 a stacks
 
Unit 5 dsa QUEUE
Unit 5 dsa QUEUEUnit 5 dsa QUEUE
Unit 5 dsa QUEUE
 
Cp unit 3
Cp unit 3Cp unit 3
Cp unit 3
 
Stack Data Structure with Static Implementation (2).pptx
Stack Data Structure with Static Implementation (2).pptxStack Data Structure with Static Implementation (2).pptx
Stack Data Structure with Static Implementation (2).pptx
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
 
Stack in C.pptx
Stack in C.pptxStack in C.pptx
Stack in C.pptx
 
13 Stacks and Queues.pptx
13 Stacks and Queues.pptx13 Stacks and Queues.pptx
13 Stacks and Queues.pptx
 
L7 pointers
L7 pointersL7 pointers
L7 pointers
 
Complete the implementation of the Weighted Graph that we began in t.pdf
Complete the implementation of the Weighted Graph that we began in t.pdfComplete the implementation of the Weighted Graph that we began in t.pdf
Complete the implementation of the Weighted Graph that we began in t.pdf
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
Ch06 Stack
Ch06  StackCh06  Stack
Ch06 Stack
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Array and functions
Array and functionsArray and functions
Array and functions
 

More from Rajendran

Element distinctness lower bounds
Element distinctness lower boundsElement distinctness lower bounds
Element distinctness lower boundsRajendran
 
Scheduling with Startup and Holding Costs
Scheduling with Startup and Holding CostsScheduling with Startup and Holding Costs
Scheduling with Startup and Holding CostsRajendran
 
Divide and conquer surfing lower bounds
Divide and conquer  surfing lower boundsDivide and conquer  surfing lower bounds
Divide and conquer surfing lower boundsRajendran
 
Red black tree
Red black treeRed black tree
Red black treeRajendran
 
Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statisticsRajendran
 
Proof master theorem
Proof master theoremProof master theorem
Proof master theoremRajendran
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree methodRajendran
 
Recurrence theorem
Recurrence theoremRecurrence theorem
Recurrence theoremRajendran
 
Master method
Master method Master method
Master method Rajendran
 
Master method theorem
Master method theoremMaster method theorem
Master method theoremRajendran
 
Master method theorem
Master method theoremMaster method theorem
Master method theoremRajendran
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithmsRajendran
 
Longest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisLongest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisRajendran
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisRajendran
 
Average case Analysis of Quicksort
Average case Analysis of QuicksortAverage case Analysis of Quicksort
Average case Analysis of QuicksortRajendran
 
Np completeness
Np completenessNp completeness
Np completenessRajendran
 
computer languages
computer languagescomputer languages
computer languagesRajendran
 

More from Rajendran (20)

Element distinctness lower bounds
Element distinctness lower boundsElement distinctness lower bounds
Element distinctness lower bounds
 
Scheduling with Startup and Holding Costs
Scheduling with Startup and Holding CostsScheduling with Startup and Holding Costs
Scheduling with Startup and Holding Costs
 
Divide and conquer surfing lower bounds
Divide and conquer  surfing lower boundsDivide and conquer  surfing lower bounds
Divide and conquer surfing lower bounds
 
Red black tree
Red black treeRed black tree
Red black tree
 
Hash table
Hash tableHash table
Hash table
 
Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statistics
 
Proof master theorem
Proof master theoremProof master theorem
Proof master theorem
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Recurrence theorem
Recurrence theoremRecurrence theorem
Recurrence theorem
 
Master method
Master method Master method
Master method
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
 
Hash tables
Hash tablesHash tables
Hash tables
 
Lower bound
Lower boundLower bound
Lower bound
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Longest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisLongest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm Analysis
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm Analysis
 
Average case Analysis of Quicksort
Average case Analysis of QuicksortAverage case Analysis of Quicksort
Average case Analysis of Quicksort
 
Np completeness
Np completenessNp completeness
Np completeness
 
computer languages
computer languagescomputer languages
computer languages
 

Recently uploaded

Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterMateoGardella
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 

Recently uploaded (20)

Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 

Array stack-queue1

  • 2. 06/26/15 CS 201 Data Structure • A construct that can be defined within a programming language to store a collection of data – one may store some data in an array of integers, an array of objects, or an array of arrays
  • 3. 06/26/15 CS 201 Abstract Data Type (ADT) • Definition: a collection of data together with a set of operations on that data – specifications indicate what ADT operations do, but not how to implement them – data structures are part of an ADT’s implementation • Programmer can use an ADT without knowing its implementation.
  • 4. 06/26/15 CS 201 Typical Operations on Data • Add data to a data collection • Remove data from a data collection • Ask questions about the data in a data collection. E.g., what is the value at a particular location, and is x in the collection?
  • 5. 06/26/15 CS 201 Why ADT • Hide the unnecessary details • Help manage software complexity • Easier software maintenance • Functionalities are less likely to change • Localised rather than global changes
  • 8. 06/26/15 CS 201 What is a Stack? • A stack is a list with the restriction that insertions and deletions can be performed in only one position, namely, the end of the list, called the top. • The operations: push (insert) and pop (delete) pop push(o) 6 7 2 3Top
  • 9. 06/26/15 CS 201 Stack ADT Interface • The main functions in the Stack ADT are (S is the stack) boolean isEmpty(); // return true if empty boolean isFull(S); // return true if full void push(S, item); // insert item into stack void pop(S); // remove most recent item void clear(S); // remove all items from stack Item top(S); // retrieve most recent item Item topAndPop(S); // return & remove most recent item
  • 10. 06/26/15 CS 201 Sample Operation Stack S = malloc(sizeof(stack)); push(S, “a”); push(S, “b”); push(S, “c”); d=top(S); pop(S); push(S, “e”); pop(S); s abc top e d
  • 11. 06/26/15 CS 201 Implementation by Array • use Array with a top index pointer as an implementation of stack E F 0 1 7 8 92 3 4 5 6 A B C D top StackAr arr A
  • 16. 06/26/15 CS 201 Applications • Many application areas use stacks: – line editing – bracket matching – postfix calculation – function call stack
  • 18. 06/26/15 CS 201 What is a Queue? • Like stacks, queues are lists. With a queue, however, insertion is done at one end whereas deletion is done at the other end. • Queues implement the FIFO (first-in first-out) policy. E.g., a printer/job queue! • Two basic operations of queues: – dequeue: remove an item/element from front – enqueue: add an item/element at the back dequeue enqueue
  • 19. 06/26/15 CS 201 Queue ADT • Queues implement the FIFO (first-in first-out) policy – An example is the printer/job queue! enqueue(o) dequeue() isEmpty() getFront() createQueue()
  • 20. 06/26/15 CS 201 Sample Operation Queue *Q; enqueue(Q, “a”); enqueue(Q, “b”); enqueue(Q, “c”); d=getFront(Q); dequeue(Q); enqueue(Q, “e”); dequeue(Q); q front back a b c e d
  • 21. 06/26/15 CS 201 Queue ADT interface • The main functions in the Queue ADT are (Q is the queue) void enqueue(o, Q) // insert o to back of Q void dequeue(Q); // remove oldest item Item getFront(Q); // retrieve oldest item boolean isEmpty(Q); // checks if Q is empty boolean isFull(Q); // checks if Q is full void clear(Q); // make Q empty }
  • 22. 06/26/15 CS 201 Implementation of Queue (Array) • use Array with front and back pointers as implementation of queue Queue arr 0 1 7 8 92 3 4 5 6 A B C D E F G front back
  • 23. 06/26/15 CS 201 Circular Array • To implement queue, it is best to view arrays as circular structure 0 1 7 8 92 3 4 5 6 A B C D E F G front back front back A B C D EF G 0 1 7 8 9 2 3 45 6 Circular view of arrays.
  • 24. 06/26/15 CS 201 How to Advance • Both front & back pointers should make advancement until they reach end of the array. Then, they should re-point to beginning of the array front = adv(front); back = adv(back); int adv(int p) { return ((p+1) % maxsize); } Alternatively, use modular arithmetic: mod operator int adv(int p) { int r = p+1; if (r<maxsize) return r; else return 0; } upper bound of the array
  • 25. 06/26/15 CS 201 Sample Queue *Q; enqueue(Q, “a”); enqueue(Q, “b”); enqueue(Q, “c”); dequeue(Q); dequeue(Q); enqueue(Q, “d”); enqueue(Q, “e”); dequeue(Q); a Q F=front B=back F B b c d F B B B F F B B e
  • 26. 06/26/15 CS 201 Checking for Full/Empty State What does (F==B) denote? F B Queue Empty State c de B F f Queue Full State size 0 size 4 c de B F Alternative - Leave a Deliberate Gap! No need for size field. Full Case : (adv(B)==F)
  • 27. 06/26/15 CS 201 Summary • The definition of the queue operations gives the ADT queue first-in, first-out (FIFO) behavior • The queue can be implemented by linked lists or by arrays • There are many applications – Printer queues, – Telecommunication queues, – Simulations, – Etc.