Objectives of this lecture
Implementation of ADTs
Ø
The
implementation of the operations can be changed (if need be) without changing
the applications that use them.
Ø
It also
provides a protection to the structure since it can only be manipulated using
the prescribed set of operations.
What is Stack?
Create_stack
– make stack logically accessible
Destroy_stack
– make stack logically inaccessible
Empty_stack
– checks if a stack is empty
Full_stack
– checks if a stack is full
Push - add item ot the top of the stack
Pop
– remove item from the top of the stack.
Implementation of Stack using Array
stack |
|
|
|
|
|
|
|
|
|
|
top |
|
|
|
|
|
|
|
|
|
|
The user_inteface:
#define
MAX_STACK 100
typedef
char ITEM_TYPE
typedef
struct { ITEM_TYPE item[MAX_STACK];
int top;
} STACK_TYPE;
typedef
enum {FALSE, TRUE} BOOLEAN;
void
create_stack(STACK_TYPE *stack);
void
destroy_stack(STACK_TYPE *stack);
BOOLEAN
empy_stack(STACK_TYPE *stack);
BOOLEAN
full_stack(STACK_TYPE *stack);
void
push(STACK_TYPE *stack, ITEM_TYPE newitem);
void
pop(STACK_TYPE *stack, ITEM_TYPE *old_item);
Implementation details
#include
<stdio.h>
#include
"stack.h"
/*
initializes stack by setting top to 0 */
void
create_stack(STACK_TYPE *stack)
{
stack->top=0;
}
/*
resets stack by resetting top to 0 */
void
destroy_stack(STACK_TYPE *stack)
{ stack->top=0;
}
/*
checks if stack is empty (if top is 0) */
BOOLEAN
empty_stack(STACK_TYPE *stack)
{
if (stack->top==0)
return TRUE;
else
return FALSE;
}
/*
checks if stack is full (if top is MAX_STACK) */
BOOLEAN
full_stack(STACK_TYPE *stack)
{
if (stack->top == MAX_STACK)
return TRUE;
else
return FALSE;
}
/*
put item at position top and then increment top */
void
push(STACK_TYPE *stack, ITEM_TYPE newitem)
{ stack->item[stack->top]=newitem;
stack->top++;
}
/* decrement top by 1 and then copy the item
at top */
void
pop(STACK_TYPE *stack, ITEM_TYPE *old_item)
{ stack->top--;
*old_item=stack->item[stack->top];
}
Example:
#include
<stdio.h>
#include
"stack.h"
void
input_stack(STACK_TYPE *stack);
void
output_stack (STACK_TYPE *stack);
main()
{ STACK_TYPE stack;
create_stack(&stack);
input_stack(&stack);
output_stack(&stack);
destroy_stack(&stack);
return 0;
}
void
input_stack(STACK_TYPE *stack)
{ ITEM_TYPE in_value;
printf("\nEnter a string of
caharacters: ");
while (((in_value=getchar()) != '\n')
&&
(full_stack(stack)==FALSE))
push(stack,in_value);
}
void
output_stack (STACK_TYPE *stack)
{ ITEM_TYPE out_value;
printf("\nThe string reversed is:
");
while ( empty_stack(stack)== FALSE)
{
pop(stack,&out_value);
putchar(out_value);
}
}
Application of Stack –Infix to Postfix conversion
Prefix |
Infix |
Postfix |
* 3 4 |
3 * 4 |
3 4 * |
+ 3 * 4 5 |
3 + 4 * 5 |
3 4 5 * + |
* + 3 4 5 |
(3 + 4) * 5 |
3 4 + 5 * |
#include
<stdio.h>
#include
"stack.h"
main()
{
char in_char;
ITEM_TYPE op1, op2, result;
STACK_TYPE op_stack;
create_stack(&op_stack);
while ((in_char=getchar()) != '\n' &&
full_stack(&op_stack)==FALSE)
{ if (in_char != ' ')
if (in_char >= '0' && in_char
<= '9')
push(&op_stack, in_char - '0');
else
{
pop(&op_stack, &op2);
pop(&op_stack, &op1);
switch (in_char)
{
case '+' : push(&op_stack, op1+op2); break;
case '-' : push(&op_stack, op1-op2); break;
case '*' : push(&op_stack,
op1*op2); break;
case '/' : push(&op_stack,
op1/op2); break;
default : printf("nError in input");
}
}
}
pop(&op_stack, &result);
printf("\nResult = %d\n\n",
result);
destroy_stack(&op_stack);
return 0;
}