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.

Lecture 2c stacks

  • 1.
    STACKS- INTRODUCTION ď‚—Data structuresare 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 stackis 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 stackis 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 allowsaccess 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 PushOperation 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 ItMainly 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) Initialstate (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 STACKpoints 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 ofStacks 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 implementationhas 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 Implementationof 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 lineardata 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 asa 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 anaddition, 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 Push3 Push 2 ADD Multiply Pop E
  • 22.
    3) Data TypeConversion ď‚—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 ofthe 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 conditionsthat 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 requiresprior knowledge or proper presentation & a good understanding of the program when deciding or the number of stack for optimum performance.
  • 26.
    CONCLUSION ď‚—In a nutshellstacks 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.