You are on page 1of 4

Stack (abstract data type)

In computer science, a stack is a last in, first out (LIFO) abstract data type and data structure. A
stack can have any abstract data type as an element, but is characterized by only three
fundamental operations: push, pop and stack top. The push operation adds a new item to the top
of the stack, or initializes the stack if it is empty. If the
stack is full and does not contain enough space to
accept the given item, the stack is then considered to
be in an overflow state. The pop operation removes an
item from the top of the stack. A pop either reveals
previously concealed items, or results in an empty
stack, but if the stack is empty then it goes into
underflow state (It means no items are present in stack
to be removed). The stack top operation gets the data
from the top-most position and returns it to the user
without deleting it. The same underflow state can also
occur in stack top operation if stack is empty.
A stack is a restricted data structure, because only a small number of operations are performed
on it. The nature of the pop and push operations also means that stack elements have a natural
order. Elements are removed from the stack in the reverse order to the order of their addition:
therefore, the lower elements are those that have been on the stack the longest.[1]

Abstract definition
A stack is a fundamental computer science data structure and can be defined in an abstract,
implementation-free manner, or it can be generally defined as, Stack is a linear list of items in
which all additions and deletion are restricted to one end that is Top.
This is a VDM (Vienna Development Method) description of a stack
Function signatures:
init: -> Stack
push: N x Stack -> Stack
top: Stack -> (N U ERROR)
remove: Stack -> Stack
isempty: Stack -> Boolean

(where N indicates an element (natural numbers in this case), and U indicates set union)
Semantics
top(init()) = ERROR

top(push(i,s)) = i
remove(init()) = init()
remove(push(i, s)) = s
isempty(init()) = true
isempty(push(i, s)) = false

Reference [3]

[edit] Inessential operations


In modern computer languages, the stack is usually implemented with more operations than just
"push","pop" and "Stack Top". Some implementations have a function which returns the current
number of items on the stack. Alternatively, some implementations have a function that just
returns if the stack is empty. Another typical helper operation stack top[4] (also known as peek)
can return the current top element of the stack without removing it.

[edit] Software stacks


[edit] Implementation
In most high level languages, a stack can be easily implemented either through an array or a
linked list. What identifies the data structure as a stack in either case is not the implementation
but the interface: the user is only allowed to pop or push items onto the array or linked list, with
few other helper operations. The following will demonstrate both implementations, using C.

[edit] Array
The array implementation aims to create an array where the first element (usually at the zerooffset) is the bottom. That is, array[0] is the first element pushed onto the stack and the last
element popped off. The program must keep track of the size, or the length of the stack. The
stack itself can therefore be effectively implemented as a two-element structure in C:
typedef struct {
size_t size;
int items[STACKSIZE];
} STACK;

The push() operation is used both to initialize the stack, and to store values to it. It is
responsible for inserting (copying) the value into the ps->items[] array and for incrementing
the element counter (ps->size). In a responsible C implementation, it is also necessary to check
whether the array is already full to prevent an overrun.
void push(STACK *ps, int x)
{
if (ps->size == STACKSIZE) {

fputs("Error: stack overflow\n", stderr);


abort();
} else
ps->items[ps->size++] = x;
}

The pop() operation is responsible for removing a value from the stack, and decrementing the
value of ps->size. A responsible C implementation will also need to check that the array is not
already empty.
int pop(STACK *ps)
{
if (ps->size == 0){
fputs("Error: stack underflow\n", stderr);
abort();
} else
return ps->items[--ps->size];
}

If we use a dynamic array, then we can implement a stack that can grow or shrink as much as
needed. The size of the stack is simply the size of the dynamic array. A dynamic array is a very
efficient implementation of a stack, since adding items to or removing items from the end of a
dynamic array is amortized O(1) time.
[edit] Linked list
The linked-list implementation is equally simple and straightforward. In fact, a simple singly
linked list is sufficient to implement a stackit only requires that the head node or element can
be removed, or popped, and a node can only be inserted by becoming the new head node.
Unlike the array implementation, our structure typedef corresponds not to the entire stack
structure, but to a single node:
typedef struct stack {
int data;
struct stack *next;
} STACK;

Such a node is identical to a typical singly linked list node, at least to those that are implemented
in C.
The push() operation both initializes an empty stack, and adds a new node to a non-empty one.
It works by receiving a data value to push onto the stack, along with a target stack, creating a
new node by allocating memory for it, and then inserting it into a linked list as the new head:
void push(STACK **head, int value)
{
STACK *node = malloc(sizeof(STACK));
if (node == NULL){

/* create a new node */

fputs("Error: no space available for node\n", stderr);


abort();
} else {
/* initialize node */
node->data = value;
node->next = empty(*head) ? NULL : *head; /* insert new head if any */
*head = node;
}

A pop() operation removes the head from the linked list, and assigns the pointer to the head to
the previous second node. It checks whether the list is empty before popping from it:
int pop(STACK **head)
{
if (empty(*head)) {
/* stack is empty */
fputs("Error: stack underflow\n", stderr);
abort();
} else {
/* pop a node */
STACK *top = *head;
int value = top->data;
*head = top->next;
free(top);
return value;
}
}

Algorithm:
1. Create a stack
2. Enter a decimal number, which has to be converted into its equivalent
binary form.
3. iteration1 (while number > 0)
3.1 digit = number % 2
3.2 Push digit into the stack
3.3 If the stack is full
3.3.1 Print an error
3.3.2 Stop the algorithm
3.4 End the if condition
3.5 Divide the number by 2
4. End iteration1
5. iteration2 (while stack is not empty)
5.1 Pop digit from the stack
5.2 Print the digit
6. End iteration2
7. STOP

You might also like