SlideShare une entreprise Scribd logo
1  sur  26
STACKS- INTRODUCTION
Data structures are organized grouping of data
items, treated as a unit regarded as data
aggregates e.g. arrays, trees, stacks, queues.
 Data structures allow a programmer to create
data types by grouping together data of more
primitive types.
DEFINITIONS
• A stack is an ordered list of elements in which
insertions and deletions are made at one end
called the top.
• The point of access of a stack is called the top.
• A stack is linear list whose elements may be
created & deleted only in a last in first out
order.
DEFINITIONS
• A stack is a linear list for which all insertions
& deletions and usually all accesses are
made at one end of the list.
• A stack is an ADT that is a storage
mechanism which is defined by the
operations performed on it & not at all by the
specific manner in which it is implemented.
DEFINITIONS
A stack allows access to only one data item:
the last item inserted. If you remove this item,
then you can access the next-to-last item
inserted, and so on.
A stack is also known as a pushdown list or a
LIFO (Last In First Out) list.
DEFINITIONS
• A Push Operation
It appends one new item to the top of the stack
or to the top of a non-full stack.
• A Pop Operation
It removes or deletes the top item from the
stack.
DEFINITIONS
• Stack Pointer
• It contains the address of the top of the stack.
• If an item is appended to or deleted from the
stack, the pointer is incremented or
decremented respectively to contain the
address of the new top of the stack.
DEFINITIONS
• Stack base
• It contains the address of the bottom location in
the received block. If an attempt is made to pop
when the stack is empty an error is reported.
• Stack limit
• Contains the address of the other end of the
received block. If an attempt is made to push
when the block is fully utilized for the stack an
error is reported.
Characteristics of Stacks
It Mainly Uses The Principle Of Last In
First Out
Can add and delete without disturbing the
arrangement
EXAMPLE
1995 1996
1995
1982
1998
1995
1982
a) Initial state
(Empty)
d) After two
pops
c) After three
pushes
b) After one
push
 Use of Links (or Pointers)
A Stack can be represented by using links or
pointers.
Two nodes with two fields, possibly called
DATA and LINK can represent a stack.
 (A node is a collection of data and link
information).
The data field of each node contains an item
in the stack and the corresponding link field
points to the node containing the next item in
the stack
Use of Links (or Pointers)
The linked field of the last node is zero for
we assume that all nodes have an address
greater than zero.
For example, a stack with the items A, B, C,
D, E inserted in that order, would look as in
the figure below:
A 5 element, linked stack
E D C B A 0
Cont’d
The variable STACK points to the topmost node (the
last item inserted) in the list.
The empty stack is represented by setting STACK =
0.
The use of links to represent a stack requires more
storage than the sequential array.
Array Implementation of Stacks
A stack can be implemented with an array and
an integer.
The integer tos (Top of stack) provides the
array index of the top element of the stack.
Thus if tos is –1, the stack is empty.
 The array implementation keeps the bottom of
the stack at the beginning of the array.
It grows toward the end of the array.
Cont’d
The array implementation has one serious
drawback: you must know the max. no. of items
in your collection when you create it.
This presents problems in programs in which
this max. no. cannot be predicted accurately
when the program starts up.
A linked list can be used to overcome this
limitation.
Linked List Implementation of Stacks
The linked list is a very flexible dynamic data
structure: items may be added to it or deleted
from it at will.
The stack interface can be implemented by
using a linked list in which the top of the stack is
represented by the first item in the list
Used in linear data structure simply because they
figure so prominently in systems programming
especially in compiler design.
 Storage management in recursive programming & to
implement inherently recursive routines.
 Used for processing nested program structures for
example bracketed arithmetic/logical expressions;
programs with block structures; statements with
matching delimiters as; If, Then, Else, End.
USES OF STACKS
APPLICATIONS OF STACKS
1. Interrupts
The management of I/O data transfer goes
through three stages i.e.
Initiate the interrupt
Service the interrupt
Return from the interrupt
2) Stack as a Temporary Storage
Some computer use stack instead of general
purpose registers to store temporary values
during computation
These computations are called stack
machines or zero address machines.
The h/w recalls the source operands on the
stack which would be popped & then supplied
to the ALU & the results of the addition would
be pushed onto the stack
Cont’d
To perform an addition, the h/w would execute
two pops & a push
Two pops would remove the two sources
operands from the stack, the add would
compute the sum & push would replace the
results back on the stack.
Most calculators use stacks, Example:
Evaluate: (A+B)(C+D) and store the results in
E: where A is 25; B is 17; C is 3; D is 2
Cont’d
Push 25
Push 17
ADD
Push 3
Push 2
ADD
Multiply
Pop E
3) Data Type Conversion
Every instruction should be provided with
source operands of the data type that the
instruction requires i.e. ADD requires operands
that are two complement integers.
Example 2+3=
A stack is used in keeping track of data type of
each of the values that one is working with
LIMITATION
The concept of the stack being a LIFO structure
removing & adding items from the stack
requires a strict order, which is serially.
Insertions & deletions can be performed only
from the one position, which is at the top.
This implies that the sequence of Pushes of A,
B, C, D. has to be eventually followed by the
Pops of D,C,B and A.
The stack limit or the default values may not be
enough & this will result in overflows.
LIMITATION
Overflows are conditions that occur when one
tries to push a value onto a stack and there is
no available space.
The user should always check therefore that
the stack is not full before inserting an item.
Often a stack full condition will signal that more
storage needs to be allocated and the program
reruns.
LIMITATION
A programmer requires prior knowledge or
proper presentation & a good understanding
of the program when deciding or the number
of stack for optimum performance.
CONCLUSION
In a nutshell stacks are form of an ADT that is
a storage mechanism which is defined by the
operations performed on it & not at all by the
specific manner in which it is implemented &
are composed of pointer, top, base & peek
button.
They simply aid in writing an algorithm.

Contenu connexe

Tendances

Abstract data types (adt) intro to data structure part 2
Abstract data types (adt)   intro to data structure part 2Abstract data types (adt)   intro to data structure part 2
Abstract data types (adt) intro to data structure part 2Self-Employed
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queueSrajan Shukla
 
Slide 1.-datastructure
Slide 1.-datastructureSlide 1.-datastructure
Slide 1.-datastructureMinhaz Leo
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to VectorsRsquared Academy
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queueRai University
 
List Data Structure
List Data StructureList Data Structure
List Data StructureZidny Nafan
 
Insertion in singly linked list
Insertion in singly linked listInsertion in singly linked list
Insertion in singly linked listKeval Bhogayata
 
4 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart14 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart1SSE_AndyLi
 
data structure
data structuredata structure
data structurehashim102
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Balwant Gorad
 
Double Linked List (Algorithm)
Double Linked List (Algorithm)Double Linked List (Algorithm)
Double Linked List (Algorithm)Huba Akhtar
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler DesignKuppusamy P
 

Tendances (19)

Abstract data types (adt) intro to data structure part 2
Abstract data types (adt)   intro to data structure part 2Abstract data types (adt)   intro to data structure part 2
Abstract data types (adt) intro to data structure part 2
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
 
Slide 1.-datastructure
Slide 1.-datastructureSlide 1.-datastructure
Slide 1.-datastructure
 
Data structures
Data structuresData structures
Data structures
 
single linked list
single linked listsingle linked list
single linked list
 
Linked List
Linked ListLinked List
Linked List
 
Linked lists
Linked listsLinked lists
Linked lists
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to Vectors
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
List Data Structure
List Data StructureList Data Structure
List Data Structure
 
Insertion in singly linked list
Insertion in singly linked listInsertion in singly linked list
Insertion in singly linked list
 
4 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart14 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart1
 
data structure
data structuredata structure
data structure
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
 
Double Linked List (Algorithm)
Double Linked List (Algorithm)Double Linked List (Algorithm)
Double Linked List (Algorithm)
 
Linked list
Linked listLinked list
Linked list
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
Data structures project
Data structures projectData structures project
Data structures project
 

En vedette

Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...Dharmendra Prasad
 
Lecture 2 data structures & algorithms - sorting techniques
Lecture 2  data structures & algorithms - sorting techniquesLecture 2  data structures & algorithms - sorting techniques
Lecture 2 data structures & algorithms - sorting techniquesDharmendra Prasad
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppteShikshak
 
Array in c language
Array in c languageArray in c language
Array in c languagehome
 

En vedette (11)

Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
 
Lecture 2 data structures & algorithms - sorting techniques
Lecture 2  data structures & algorithms - sorting techniquesLecture 2  data structures & algorithms - sorting techniques
Lecture 2 data structures & algorithms - sorting techniques
 
Arrays
ArraysArrays
Arrays
 
C# Arrays
C# ArraysC# Arrays
C# Arrays
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
 
Lecture 2a arrays
Lecture 2a arraysLecture 2a arrays
Lecture 2a arrays
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Sorting
SortingSorting
Sorting
 
Arrays
ArraysArrays
Arrays
 
Array in c language
Array in c languageArray in c language
Array in c language
 

Similaire à Lecture 2c stacks

chapter three ppt.pptx
chapter three ppt.pptxchapter three ppt.pptx
chapter three ppt.pptxselemonGamo
 
Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Getachew Ganfur
 
CS8391-DATA-STRUCTURES.pdf
CS8391-DATA-STRUCTURES.pdfCS8391-DATA-STRUCTURES.pdf
CS8391-DATA-STRUCTURES.pdfraji175286
 
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4sumitbardhan
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddumaneesh boddu
 
Data Structure.pdf
Data Structure.pdfData Structure.pdf
Data Structure.pdfMemeMiner
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.pptSeethaDinesh
 
Data structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pdData structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pdNimmi Weeraddana
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arorakulachihansraj
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queuestech4us
 

Similaire à Lecture 2c stacks (20)

chapter three ppt.pptx
chapter three ppt.pptxchapter three ppt.pptx
chapter three ppt.pptx
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9
 
Rana Junaid Rasheed
Rana Junaid RasheedRana Junaid Rasheed
Rana Junaid Rasheed
 
CS8391-DATA-STRUCTURES.pdf
CS8391-DATA-STRUCTURES.pdfCS8391-DATA-STRUCTURES.pdf
CS8391-DATA-STRUCTURES.pdf
 
Linked List
Linked ListLinked List
Linked List
 
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
 
Data Structure
Data StructureData Structure
Data Structure
 
2 a stacks
2 a stacks2 a stacks
2 a stacks
 
Unit i(dsc++)
Unit i(dsc++)Unit i(dsc++)
Unit i(dsc++)
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
Data Structure.pdf
Data Structure.pdfData Structure.pdf
Data Structure.pdf
 
12888239 (2).ppt
12888239 (2).ppt12888239 (2).ppt
12888239 (2).ppt
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.ppt
 
Data structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pdData structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pd
 
Data structures1
Data structures1Data structures1
Data structures1
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queues
 
UNITIII LDS.pdf
UNITIII LDS.pdfUNITIII LDS.pdf
UNITIII LDS.pdf
 

Lecture 2c stacks

  • 1. STACKS- INTRODUCTION Data structures are organized grouping of data items, treated as a unit regarded as data aggregates e.g. arrays, trees, stacks, queues.  Data structures allow a programmer to create data types by grouping together data of more primitive types.
  • 2. DEFINITIONS • A stack is an ordered list of elements in which insertions and deletions are made at one end called the top. • The point of access of a stack is called the top. • A stack is linear list whose elements may be created & deleted only in a last in first out order.
  • 3. DEFINITIONS • A stack is a linear list for which all insertions & deletions and usually all accesses are made at one end of the list. • A stack is an ADT that is a storage mechanism which is defined by the operations performed on it & not at all by the specific manner in which it is implemented.
  • 4. DEFINITIONS A stack allows access to only one data item: the last item inserted. If you remove this item, then you can access the next-to-last item inserted, and so on. A stack is also known as a pushdown list or a LIFO (Last In First Out) list.
  • 5. DEFINITIONS • A Push Operation It appends one new item to the top of the stack or to the top of a non-full stack. • A Pop Operation It removes or deletes the top item from the stack.
  • 6. DEFINITIONS • Stack Pointer • It contains the address of the top of the stack. • If an item is appended to or deleted from the stack, the pointer is incremented or decremented respectively to contain the address of the new top of the stack.
  • 7. DEFINITIONS • Stack base • It contains the address of the bottom location in the received block. If an attempt is made to pop when the stack is empty an error is reported. • Stack limit • Contains the address of the other end of the received block. If an attempt is made to push when the block is fully utilized for the stack an error is reported.
  • 8. Characteristics of Stacks It Mainly Uses The Principle Of Last In First Out Can add and delete without disturbing the arrangement
  • 9. EXAMPLE 1995 1996 1995 1982 1998 1995 1982 a) Initial state (Empty) d) After two pops c) After three pushes b) After one push
  • 10.  Use of Links (or Pointers) A Stack can be represented by using links or pointers. Two nodes with two fields, possibly called DATA and LINK can represent a stack.  (A node is a collection of data and link information). The data field of each node contains an item in the stack and the corresponding link field points to the node containing the next item in the stack
  • 11. Use of Links (or Pointers) The linked field of the last node is zero for we assume that all nodes have an address greater than zero. For example, a stack with the items A, B, C, D, E inserted in that order, would look as in the figure below:
  • 12. A 5 element, linked stack E D C B A 0
  • 13. Cont’d The variable STACK points to the topmost node (the last item inserted) in the list. The empty stack is represented by setting STACK = 0. The use of links to represent a stack requires more storage than the sequential array.
  • 14. Array Implementation of Stacks A stack can be implemented with an array and an integer. The integer tos (Top of stack) provides the array index of the top element of the stack. Thus if tos is –1, the stack is empty.  The array implementation keeps the bottom of the stack at the beginning of the array. It grows toward the end of the array.
  • 15. Cont’d The array implementation has one serious drawback: you must know the max. no. of items in your collection when you create it. This presents problems in programs in which this max. no. cannot be predicted accurately when the program starts up. A linked list can be used to overcome this limitation.
  • 16. Linked List Implementation of Stacks The linked list is a very flexible dynamic data structure: items may be added to it or deleted from it at will. The stack interface can be implemented by using a linked list in which the top of the stack is represented by the first item in the list
  • 17. Used in linear data structure simply because they figure so prominently in systems programming especially in compiler design.  Storage management in recursive programming & to implement inherently recursive routines.  Used for processing nested program structures for example bracketed arithmetic/logical expressions; programs with block structures; statements with matching delimiters as; If, Then, Else, End. USES OF STACKS
  • 18. APPLICATIONS OF STACKS 1. Interrupts The management of I/O data transfer goes through three stages i.e. Initiate the interrupt Service the interrupt Return from the interrupt
  • 19. 2) Stack as a Temporary Storage Some computer use stack instead of general purpose registers to store temporary values during computation These computations are called stack machines or zero address machines. The h/w recalls the source operands on the stack which would be popped & then supplied to the ALU & the results of the addition would be pushed onto the stack
  • 20. Cont’d To perform an addition, the h/w would execute two pops & a push Two pops would remove the two sources operands from the stack, the add would compute the sum & push would replace the results back on the stack. Most calculators use stacks, Example: Evaluate: (A+B)(C+D) and store the results in E: where A is 25; B is 17; C is 3; D is 2
  • 21. Cont’d Push 25 Push 17 ADD Push 3 Push 2 ADD Multiply Pop E
  • 22. 3) Data Type Conversion Every instruction should be provided with source operands of the data type that the instruction requires i.e. ADD requires operands that are two complement integers. Example 2+3= A stack is used in keeping track of data type of each of the values that one is working with
  • 23. LIMITATION The concept of the stack being a LIFO structure removing & adding items from the stack requires a strict order, which is serially. Insertions & deletions can be performed only from the one position, which is at the top. This implies that the sequence of Pushes of A, B, C, D. has to be eventually followed by the Pops of D,C,B and A. The stack limit or the default values may not be enough & this will result in overflows.
  • 24. LIMITATION Overflows are conditions that occur when one tries to push a value onto a stack and there is no available space. The user should always check therefore that the stack is not full before inserting an item. Often a stack full condition will signal that more storage needs to be allocated and the program reruns.
  • 25. LIMITATION A programmer requires prior knowledge or proper presentation & a good understanding of the program when deciding or the number of stack for optimum performance.
  • 26. CONCLUSION In a nutshell stacks are form of an ADT that is a storage mechanism which is defined by the operations performed on it & not at all by the specific manner in which it is implemented & are composed of pointer, top, base & peek button. They simply aid in writing an algorithm.