SlideShare a Scribd company logo
1 of 19
STACK FUNDAMENTAL
WHAT ARE STACKS?
● A stack is an Abstract Data Type (ADT), commonly used
in most programming languages. It is named stack as it
it behaves like a real-world stack, for example – a deck of
cards or a pile of plates, etc.
ABSTRACT DATA TYPE
An abstract data type is defined as a mathematical model of
the data objects that make up a data type as well as the
functions that operate on these objects. There are no standard
conventions for defining them. A broad division may be drawn
between "imperative" and "functional" definition styles.
STACK
REPRESENTATION
A stack can be implemented
by means of Array,
Structure, Pointer, and
Linked List. Stack can
either be a fixed size one or
it may have a sense of
dynamic resizing. Here, we
are going to implement
stack using arrays, which
makes it a fixed size stack
implementation.
BASIC OPERATIONS
● Stack operations may involve initializing the stack, using
it and then de-initializing it. Apart from these basic stuffs,
a stack is used for the following two primary operations : -
push() − Pushing (storing) an element on the stack.
pop() − Removing (accessing) an element from the stack.
● Data is either PUSHed onto stack or POPed out of the
stack.
● To use a stack efficiently, we need to check the
status of stack as well. For the same purpose, the
following functionality is added to stacks : -
peek() − get the top data element of the stack, without
removing it.
isFull() − check if stack is full.
isEmpty() − check if stack is empty.
● At all times, we maintain a pointer to the last PUSHed
data on the stack. As this pointer always represents the
top of the stack, hence named top. The top pointer provides
top value of the stack without actually removing it.
BASIC FEATURES OF
STACK
● Stack is an ordered list of similar data type.
● Stack is a LIFO structure. (Last in First out).
● push() function is used to insert new elements into the Stack
● pop() function is used to delete an element from the stack.
● Both insertion and deletion are allowed at only one end
of Stack called Top.
● Stack is said to be in Overflow state when it is completely
full and is said to be in Underflow state if it is completely
empty.
HARDWARE STACK
A common use of stacks at the architecture level is as a means
of allocating and accessing memory.
● STACK IN MAIN MEMORY
1. Many CISC - type CPU designs, including the x86, Z80
and 6502, have a dedicated register for use as the call
stack, stack pointer with dedicated call, return, push,
and pop instructions that implicitly update the dedicated
register, thus increasing code density.
2. Some CISC processors, like the PDP-11 and the 68000,
also have special addressing modes for implementation of
stacks, typically with a semi-dedicated stack pointer as well.
STACK IN REGISTERS/
DEDICATED MEMORY
● The x87 floating point architecture is an example of a set
of registers organised as a stack where direct access
to individual registers is also possible. As with stack-based
machines in general, having the top-of-stack as an implicit
argument allows for a small machine code footprint with
a good usage of bus bandwidth and code caches, but it also
prevents some types of optimizations possible on processors
permitting random access to the register file for all operand.
● A stack structure also makes superscalar implementations
with register renaming somewhat more complex to
implement.
IMPLEMEMTATION OF
STACK
Stack can be easily implemented using an Array or a Linked
List. Arrays are quick, but are limited in size and Linked List
requires overhead to allocate, link, unlink, and deallocate, but
is not limited in size. Here we will implement Stack using
array.
PUSH OPERATION
The process of putting a new data element onto stack is known
as a Push Operation. Push operation involves a series of steps :
Step 1 − Checks if the stack is full.
Step 2 − If the stack is full, produces an error and exit.
Step 3 − If the stack is not full, increments top to point next
empty space.
Step 4 − Adds data element to the stack location, where top
is pointing.
Step 5 − Returns success.
If the linked list is used to implement the stack, then in step 3,
we need to allocate space dynamically.
POP OPERATION
Accessing the content while removing it from the stack, is
known as a Pop Operation. In an array implementation of
pop() operation, the data element is not actually removed,
instead top is decremented to a lower position in the stack to
point to the next value. But in linked-list, pop() actually
removes data element and deallocates memory space.
A Pop operation may involve the following steps −
Step 1 − Checks if the stack is empty.
Step 2 − If the stack is empty, produces an error and exit.
Step 3 − If the stack is not empty, accesses the data
element at which top is pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − Returns success.
APPLICATIONS OF
STACK
● EXPRESSION EVALUATION AND SYNATX
PARSING
Calculators employing reverse Polish notation use a stack
structure to hold values. Expressions can be represented in
prefix, postfix or infix notations and conversion from one
form to another may be accomplished using a stack. Many
compilers use a stack for parsing the syntax of expressions,
program blocks etc. before translating into low level code.
Most programming languages are context-free languages,
allowing them to be parsed with stack based machines.
● BACKTRACKING
Another important application of stacks is backtracking.
Consider a simple example of finding the correct path
in a maze. There are a series of points, from the
starting point to the destination. We start from one point. To
reach the final destination, there are several paths. Suppose
we choose a random path. After following a certain path, we
realise that the path we have chosen is wrong. So we need
to find a way by which we can return to the beginning of
path. This can be done by stacks. With the help of stacks,
we remember the point where we have reached. This is
done by pushing that point into the stack. In case we end
up on the wrong path, we can pop the last point from the
stack and thus return to the last point and continue our
quest to find the right path. This is called backtracking.
● The simplest application of a stack is to reverse a word.
You push a given word to stack - letter by letter - and then
pop letters from the stack.
● COMPILE TIME MEMORY MANAGEMENT
A number of programming languages are stack - oriented
meaning they define most basic operations as taking
their arguments from the stack, and placing any return
values back on the stack. For example, PostScript has a
return stack and an operand stack, and also has a graphics
state stack and a dictionary stack. Many virtual machines
are also stack- oriented including the p-code machine and
the Java Virtual Machine.
● TOWERS OF HANOI
One of the most interesting applications of stacks can be found
in solving a puzzle called Tower of Hanoi. According to an old
Brahmin story, the existence of the universe is calculated in
terms of the time taken by a number of monks, who are
working all the time, to move 64 disks from one pole to
another. But there are some rules about how this should be
done, which are:
- You can move only one disk at a time.
- For temporary storage, a third pole may be used.
- You cannot place a disk of larger diameter on a disk of
smaller diameter.
- Here we assume that A is first tower, B is second tower &
C is third tower.
Data structure
Data structure

More Related Content

What's hot

What's hot (20)

Co&al lecture-08
Co&al lecture-08Co&al lecture-08
Co&al lecture-08
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
 
main
mainmain
main
 
Unit iv(simple code generator)
Unit iv(simple code generator)Unit iv(simple code generator)
Unit iv(simple code generator)
 
04 pig data operations
04 pig data operations04 pig data operations
04 pig data operations
 
Abap Questions
Abap QuestionsAbap Questions
Abap Questions
 
8051 addressing
8051 addressing8051 addressing
8051 addressing
 
Unit 2
Unit 2Unit 2
Unit 2
 
Addressing mode
Addressing modeAddressing mode
Addressing mode
 
PROCESSOR AND CONTROL UNIT
PROCESSOR AND CONTROL UNITPROCESSOR AND CONTROL UNIT
PROCESSOR AND CONTROL UNIT
 
Stacks
StacksStacks
Stacks
 
CS 542 -- Query Execution
CS 542 -- Query ExecutionCS 542 -- Query Execution
CS 542 -- Query Execution
 
Buffer overflow attack
Buffer overflow attackBuffer overflow attack
Buffer overflow attack
 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocation
 
Os Reindersfinal
Os ReindersfinalOs Reindersfinal
Os Reindersfinal
 
Instruction Set and Assembly Language Programming
Instruction Set and Assembly Language ProgrammingInstruction Set and Assembly Language Programming
Instruction Set and Assembly Language Programming
 
8051 addressing modes
8051 addressing modes8051 addressing modes
8051 addressing modes
 
Addressing Modes
Addressing ModesAddressing Modes
Addressing Modes
 
Hadoop job chaining
Hadoop job chainingHadoop job chaining
Hadoop job chaining
 
15 Jo P Mar 08
15 Jo P Mar 0815 Jo P Mar 08
15 Jo P Mar 08
 

Similar to Data structure

Similar to Data structure (20)

STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
stack.pptx
stack.pptxstack.pptx
stack.pptx
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
Stack
StackStack
Stack
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory Management
 
Stack and its operations
Stack and its operationsStack and its operations
Stack and its operations
 
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
 
Stacks-and-Queues.pdf
Stacks-and-Queues.pdfStacks-and-Queues.pdf
Stacks-and-Queues.pdf
 
104332 sri vidhya eng notes
104332 sri vidhya eng notes104332 sri vidhya eng notes
104332 sri vidhya eng notes
 
Smashing the stack for fun and profit
Smashing the stack for fun and profitSmashing the stack for fun and profit
Smashing the stack for fun and profit
 
Stacks overview with its applications
Stacks overview with its applicationsStacks overview with its applications
Stacks overview with its applications
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptx
 
220 runtime environments
220 runtime environments220 runtime environments
220 runtime environments
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
 
Functions with heap and stack
Functions with heap and stackFunctions with heap and stack
Functions with heap and stack
 
5.-Stacks.pptx
5.-Stacks.pptx5.-Stacks.pptx
5.-Stacks.pptx
 
class-Stacks.pptx
class-Stacks.pptxclass-Stacks.pptx
class-Stacks.pptx
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2
 

More from krishna partiwala

More from krishna partiwala (8)

inheritance
   inheritance   inheritance
inheritance
 
data file handling
data file handlingdata file handling
data file handling
 
c++
c++c++
c++
 
C++ files and streams
C++ files and streamsC++ files and streams
C++ files and streams
 
Information system
Information systemInformation system
Information system
 
impact of information technology on society
impact of information technology on societyimpact of information technology on society
impact of information technology on society
 
Operating System
Operating SystemOperating System
Operating System
 
Database management system
Database management systemDatabase management system
Database management system
 

Recently uploaded

EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxFurkanTasci3
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一fhwihughh
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...ThinkInnovation
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...limedy534
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一F sss
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Cantervoginip
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhijennyeacort
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理e4aez8ss
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home ServiceSapana Sha
 

Recently uploaded (20)

EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptx
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Canter
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service
 

Data structure

  • 2. WHAT ARE STACKS? ● A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it it behaves like a real-world stack, for example – a deck of cards or a pile of plates, etc. ABSTRACT DATA TYPE An abstract data type is defined as a mathematical model of the data objects that make up a data type as well as the functions that operate on these objects. There are no standard conventions for defining them. A broad division may be drawn between "imperative" and "functional" definition styles.
  • 3. STACK REPRESENTATION A stack can be implemented by means of Array, Structure, Pointer, and Linked List. Stack can either be a fixed size one or it may have a sense of dynamic resizing. Here, we are going to implement stack using arrays, which makes it a fixed size stack implementation.
  • 4. BASIC OPERATIONS ● Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from these basic stuffs, a stack is used for the following two primary operations : - push() − Pushing (storing) an element on the stack. pop() − Removing (accessing) an element from the stack. ● Data is either PUSHed onto stack or POPed out of the stack.
  • 5. ● To use a stack efficiently, we need to check the status of stack as well. For the same purpose, the following functionality is added to stacks : - peek() − get the top data element of the stack, without removing it. isFull() − check if stack is full. isEmpty() − check if stack is empty. ● At all times, we maintain a pointer to the last PUSHed data on the stack. As this pointer always represents the top of the stack, hence named top. The top pointer provides top value of the stack without actually removing it.
  • 6. BASIC FEATURES OF STACK ● Stack is an ordered list of similar data type. ● Stack is a LIFO structure. (Last in First out). ● push() function is used to insert new elements into the Stack ● pop() function is used to delete an element from the stack. ● Both insertion and deletion are allowed at only one end of Stack called Top. ● Stack is said to be in Overflow state when it is completely full and is said to be in Underflow state if it is completely empty.
  • 7. HARDWARE STACK A common use of stacks at the architecture level is as a means of allocating and accessing memory. ● STACK IN MAIN MEMORY 1. Many CISC - type CPU designs, including the x86, Z80 and 6502, have a dedicated register for use as the call stack, stack pointer with dedicated call, return, push, and pop instructions that implicitly update the dedicated register, thus increasing code density. 2. Some CISC processors, like the PDP-11 and the 68000, also have special addressing modes for implementation of stacks, typically with a semi-dedicated stack pointer as well.
  • 8. STACK IN REGISTERS/ DEDICATED MEMORY ● The x87 floating point architecture is an example of a set of registers organised as a stack where direct access to individual registers is also possible. As with stack-based machines in general, having the top-of-stack as an implicit argument allows for a small machine code footprint with a good usage of bus bandwidth and code caches, but it also prevents some types of optimizations possible on processors permitting random access to the register file for all operand. ● A stack structure also makes superscalar implementations with register renaming somewhat more complex to implement.
  • 9. IMPLEMEMTATION OF STACK Stack can be easily implemented using an Array or a Linked List. Arrays are quick, but are limited in size and Linked List requires overhead to allocate, link, unlink, and deallocate, but is not limited in size. Here we will implement Stack using array.
  • 10. PUSH OPERATION The process of putting a new data element onto stack is known as a Push Operation. Push operation involves a series of steps : Step 1 − Checks if the stack is full. Step 2 − If the stack is full, produces an error and exit. Step 3 − If the stack is not full, increments top to point next empty space. Step 4 − Adds data element to the stack location, where top is pointing. Step 5 − Returns success.
  • 11. If the linked list is used to implement the stack, then in step 3, we need to allocate space dynamically.
  • 12. POP OPERATION Accessing the content while removing it from the stack, is known as a Pop Operation. In an array implementation of pop() operation, the data element is not actually removed, instead top is decremented to a lower position in the stack to point to the next value. But in linked-list, pop() actually removes data element and deallocates memory space. A Pop operation may involve the following steps − Step 1 − Checks if the stack is empty. Step 2 − If the stack is empty, produces an error and exit.
  • 13. Step 3 − If the stack is not empty, accesses the data element at which top is pointing. Step 4 − Decreases the value of top by 1. Step 5 − Returns success.
  • 14. APPLICATIONS OF STACK ● EXPRESSION EVALUATION AND SYNATX PARSING Calculators employing reverse Polish notation use a stack structure to hold values. Expressions can be represented in prefix, postfix or infix notations and conversion from one form to another may be accomplished using a stack. Many compilers use a stack for parsing the syntax of expressions, program blocks etc. before translating into low level code. Most programming languages are context-free languages, allowing them to be parsed with stack based machines.
  • 15. ● BACKTRACKING Another important application of stacks is backtracking. Consider a simple example of finding the correct path in a maze. There are a series of points, from the starting point to the destination. We start from one point. To reach the final destination, there are several paths. Suppose we choose a random path. After following a certain path, we realise that the path we have chosen is wrong. So we need to find a way by which we can return to the beginning of path. This can be done by stacks. With the help of stacks, we remember the point where we have reached. This is done by pushing that point into the stack. In case we end up on the wrong path, we can pop the last point from the stack and thus return to the last point and continue our quest to find the right path. This is called backtracking.
  • 16. ● The simplest application of a stack is to reverse a word. You push a given word to stack - letter by letter - and then pop letters from the stack. ● COMPILE TIME MEMORY MANAGEMENT A number of programming languages are stack - oriented meaning they define most basic operations as taking their arguments from the stack, and placing any return values back on the stack. For example, PostScript has a return stack and an operand stack, and also has a graphics state stack and a dictionary stack. Many virtual machines are also stack- oriented including the p-code machine and the Java Virtual Machine.
  • 17. ● TOWERS OF HANOI One of the most interesting applications of stacks can be found in solving a puzzle called Tower of Hanoi. According to an old Brahmin story, the existence of the universe is calculated in terms of the time taken by a number of monks, who are working all the time, to move 64 disks from one pole to another. But there are some rules about how this should be done, which are: - You can move only one disk at a time. - For temporary storage, a third pole may be used. - You cannot place a disk of larger diameter on a disk of smaller diameter. - Here we assume that A is first tower, B is second tower & C is third tower.