Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

STACKS

SCHOOL OF COMPUTING MITCH M. ANDAYA


STACKS

• A stack is an ordered list in which insertions


and deletions occur at the same end.

• The end of the stack where deletions and


insertions take place is the top of the stack.

• A push is a stack insertion while a pop is a


stack deletion.

• A stack obeys the last-in first-out (LIFO) rule.


Stacks
STACKS
• Operations on Stacks

– Inserting an item or element in a stack (push).

– Removing or deleting an item or element from a stack


(pop).

– Examining the item or element at the top of the stack.

– Determining if the stack is empty.

– Determining if the stack is full assuming that there is a


limitation on the number of items a stack can have.

Stacks
ARRAY IMPLEMENTATION OF STACKS

• The simplest way to represent a stack is by using a one-


dimensional array.

• To declare a stack for integers:

int stack[n];

where n is the maximum number of allowable entries.

This implies that the array implementation of stacks puts a


limit on the number of items the stack may have.

Stacks
ARRAY IMPLEMENTATION OF STACKS

stack[9] • To keep track of which cell holds


the topmost element of the
stack[8] stack, an integer variable (top)
stack[7] will hold the index of the topmost
stack[6] element.
stack[5]
stack[4]
• If top < 0 (top is less than the
smallest index of the array), then
stack[3] the stack is empty.
stack[2] 3 top = 2
stack[1] 7 • If top = n - 1 (the index of the last
stack[0] 9 element of the array), then the
stack is full.

Stacks
ARRAY IMPLEMENTATION OF STACKS

Pushing or inserting an item


stack[9] into the stack is possible only if stack[9]
the stack is not full.
stack[8] stack[8]
The following is the algorithm
stack[7] for a stack push operation stack[7]
stack[6] assuming the variable item stack[6]
contains the data to be
stack[5] pushed: stack[5]
stack[4] if (top == n - 1) stack[4]
stack[3] cout << "Stack is full!"; stack[3] 8 top = 3
else
stack[2] 3 top = 2 { stack[2] 3
stack[1] 7 ++top; stack[1] 7
stack [top] = item;
stack[0] 9 } stack[0] 9

Stacks
ARRAY IMPLEMENTATION OF STACKS

stack[9] Popping or removing an item stack[9]


from the stack is possible only
stack[8] if the stack is not empty. stack[8]
stack[7] This is the algorithm for a stack
stack[7]
stack[6] pop operation assuming that stack[6]
the data to be removed will be
stack[5] stored in the variable item: stack[5]
stack[4] if (top < 0) stack[4]
stack[3] cout << "Stack is empty!"; stack[3]
else
stack[2] 3 top = 2 { stack[2] 3
stack[1] 7 item = stack[top]; stack[1] 7 top = 2
--top;
stack[0] 9 }
stack[0] 9

Stacks
OPERATIONS ON STACKS

• Assume the following global declaration:

const int MAX_SIZE = 100;

and the following local declarations in


main():

int stack[MAX_SIZE];
int top;

Stacks
OPERATIONS ON STACKS

• Function to Check if Stack is Full (returns 1 if full


and 0 if not full)

int stack_full (int top)


{
if (top == (MAX_SIZE-1))
return (1);
else
return (0);
}

Stacks
OPERATIONS ON STACKS

• Function to Check if Stack is Empty (returns 1 if


empty and 0 if not empty)

int stack_empty(int top)


{
if (top < 0)
return (1);
else
return (0);
}

Stacks
OPERATIONS ON STACKS

• Function to Push an Item into the Stack

void push (int stack[], int *ptr_top, int item)


{
if (stack_full(*ptr_top) == 1)
cout << "\nThe Stack is Full!";
else
{
++ (*ptr_top);
stack[*ptr_top] = item;
cout << "\nItem Inserted in the Stack.";
}
}

Stacks
OPERATIONS ON STACKS

• Function to Pop an Item from the Stack (and returns the item popped)

int pop (int stack[], int *ptr_top)


{
int item;

if (stack_empty(*ptr_top) == 1)
cout << "\nThe Stack is Empty!";
else
{
item = stack[*ptr_top];
-- (*ptr_top);
cout << "\nItem Removed from the Stack.";
return (item);
}
}

Stacks
OPERATIONS ON STACKS

• Function to Determine the Topmost Item in the


Stack (and returns the value of topmost item)

int top_item(int stack[], int top)


{
if (stack_empty(top))
cout << "\nThe Stack is Empty.";
else
return (stack[top]);
}

Stacks
LINKED LIST IMPLEMENTATION OF STACKS

• An alternative representation of stacks is by using linked lists.

• A pointer variable top always points to the topmost element of


the stack.

top 7 3 9

• If top = NULL, then the stack is empty. In using linked lists, there is
no such condition as a full stack. Theoretically, using linked lists
does not put any limit on the number of items the stack can have.
The only limitation is the size of the physical memory of the
machine.

Stacks
LINKED LIST IMPLEMENTATION OF STACKS

top 7 3 9

• To declare the pointer top:

struct NODE
{
int value;
NODE *next;
};

NODE *top;

Stacks
PUSH OPERATION

top 7 3 9

• The following is the algorithm for a stack push operation


(assume the pointer ptr will point to the new node and the
variable item contains the value of this node):

ptr = (NODE*) malloc (sizeof(NODE));


ptr -> value = item;

At this point, a new node has been created that is being


pointed to by ptr.

Stacks
PUSH OPERATION

top 7 3 9

ptr 8

• To push the new node into the stack:

ptr -> next = top;


top = ptr;

Stacks
POP OPERATION

top 7 3 9

• Like in the array implementation of stacks, popping or removing an item from a


linked list stack is possible only if the stack is not empty. The following is the
algorithm for a stack pop operation:

if (top == NULL)
cout << "Stack is empty!";
else
{
item = top -> value;
ptr = top;
top = top -> next;
free(ptr);
}

Stacks
OPERATIONS ON STACKS

• Assume the following global declaration:

struct NODE
{
int value;
NODE *next;
};

And the following local declaration in main():

NODE *top;

Stacks
OPERATIONS ON STACKS

• Function to Check if Stack is Empty (returns 1 if


empty and 0 if not empty)

int stack_empty (NODE *top)


{
if (top == NULL)
return (1);
else
return (0);
}

Stacks
OPERATIONS ON STACKS

• Function to Push an Item into the Stack

void push (NODE **ptr_top, int item)


{
NODE *ptr;

ptr = (NODE *) malloc(sizeof(NODE));


ptr->value = item;
ptr->next = *ptr_top;
*ptr_top = ptr;
cout << "\nItem inserted in the Stack.";
}

Stacks
OPERATIONS ON STACKS

• Function to Pop an Item from the Stack

int pop (NODE **ptr_top)


{
NODE *ptr;
int item;

if (stack_empty(*ptr_top)==1)
cout << "\nStack is Empty!";
else
{
item = (*ptr_top)-> value;
ptr = *ptr_top;
*ptr_top = (*ptr_top)->next;
free(ptr);
cout << "\n\nItem Removed from the Stack.";
return (item);
}
}

Stacks
OPERATIONS ON STACKS

• Function to Determine the Topmost Item in the


Stack

int top_item (NODE *top)


{
if (stack_empty(top)==1)
cout << "\nStack is Empty!";
else
return (top->value);
}

Stacks

You might also like