Professional Documents
Culture Documents
Stacks 1
Stacks 1
LECTURE-1
Queues
Stacks Arrays
Linear
Data
Structures
STACK
• A linear data structure following a particular order in which the operations
are performed.
• 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.
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.
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.
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 −
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.
• peek() − get the top data element of the stack, without removing it.
return true
else
return false
endif
end procedure
Algorithm of
isfull()
function −
begin
end
procedure
procedure
isfull
isfull() endif
if top equals
to MAXSIZE
else
Algorithm of peek() function −
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.