Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

STACKS & QUEUES

LECTURE-1
Queues

Stacks Arrays

Linear
Data
Structures
STACK
• A linear data structure following a particular order in which the operations
are performed.

• The order may be LIFO(Last In First Out) or FILO(First In Last Out).


Examples
Real Life Example
• There are many real-life examples of a stack.

• Consider an example of plates stacked over one another. The plate that is
at the top is the first one to be removed, i.e. the plate that has been
placed at the bottommost position remains in the stack for the longest
period of time.

• So, it can be simply seen to follow LIFO(Last In First Out)/FILO(First In


Last Out) order.
Stack Specification

Operations
Definitions: (provided by
MakeEmpty
the user)
MAX_ITEMS: Max number of items Boolean IsEmpty
that might be on the stack
Boolean IsFull
ItemType: Data type of the items on
the stack Push
Pop
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.
PUSH:
if(!stack.IsFull())
stack.Push(item);

Stack overflow
• The condition resulting from trying to push an element onto a full stack.

POP:
if(!stack.IsEmpty())
stack.Pop(item);

Stack underflow
• The condition resulting from trying to pop an empty stack.
Stack representation
Implementation of Stack
• A stack can be implemented by means of Arrays and Linked List, in java.

• Stack can either be a fixed size one or it may have a sense of dynamic
resizing.

• Implementing stack using arrays, makes it a fixed size stack implementation.


Stack
implementation
as linked list
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.


Push Operation
The process of putting
a new data element
Step 2 − If the stack is
onto stack is known as Step 1 − Check if the
full, produce an error
a Push Operation. Push stack is full.
and exit.
operation involves a
series of steps −

Step 3 − If the stack is Step 4 − Add data


not full, element to the stack Step 5 − Return
increment top to point location, where top is success.
next empty space. pointing.

If the linked list is used to implement the stack, then in step 3, we need
to allocate space dynamically.
Algorithm for PUSH Operation
• A simple algorithm for Push operation can be derived as follows −

begin procedure push: stack, data

if stack is full

return null

endif

Else

begin

top ← top + 1

stack[top] ← data

End else

end procedure
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 implementation, 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.


Additional Operations
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.


isempty()
Algorithm isempty()

if top less than 1

return true

else

return false

endif

end procedure
Algorithm of
isfull()
function −
begin
end
procedure
procedure
isfull

isfull() endif
if top equals
to MAXSIZE

return false return true

else
Algorithm of peek() function −

begin procedure peek

peek()
return stack[top]

end procedure
PROGRAMS
1. Write a program to implement stack using arrays.
* Definition of Done:
a) The program should display a menu (1) Push (2) Pop (3) Peep (4) Exit.
b) The program should define functions for the menu item listed above.
2. Write a program to implement stack using Linked Lists.

* Defining of done:

a) The program should display a menu (1) Push (2) Pop (3) Peep (4) Exit.
b) The program should define functions for the menu item listed above.

You might also like