Course Material‎ > ‎

Stacks using structures

Stacks using structures

In stack implementation, instead of passing two parameters top and S, we can pass only one parameter if we use a structure. So, a stack can be declared as a structure containing two objects viz., an array to store the elements of the stack and an integer indicating the position of the topmost element in the array. The declaration can take the following form:

#define STACK_SIZE 5

struct stack

{

int items[STACK_SIZE];

int top;

};

typedef struct stack STACK;

Once this definition is done, we can use a variable s to access the contents of the stack and to obtain the position of the top most element. The declaration for this can take the form

STACK s;

The position of the top most element and the element itself can be accessed (using the ‘.’ operator) by specifying?

s. top

and

s. items[s. top];

If the declaration is of the form STACK *s, the position of the top most element and top most element can be accessed by specifying

s->top

and

s->items[s->top];

Let us implement stacks using structures also. We know that the stack is empty, if the position of the top most element is -1. The function is_empty( ) which returns true whenever the stack is empty and returns false whenever the stack is not empty is shown in below code 1.

code 1. : Function to check whether the stack is empty or not

int is_empty(STACK *s)

{

if (s->top = = -1) return -1; /* Stack empty */

return 0; /* Stack is not empty */

}

The function is_full( ) returns true if stack is full and returns false if the stack is not full. This function is shown in below code 2.

code 2: Function to check whether the stack is full or not

int is_full(STACK *s)

{

if ( s->top == STACK _ SlZE -1) return 1; /* Stack is full */

return 0; /* Stack is not full */

}

The function to insert an integer item into the stack is shown in below code 3.

code 3: Function to insert an integer item into the stack

void push(int item, STACK *s)

{

if ( is_full(s) )

{

printf(”Stack Overflow\n”);

return;

}

s->top+ +; /* Update top to point to next item */

s->items[s->top] = item; /* Insert the item into the stack*/

}

The function to insert a character item into the stack is shown in below code 4.

code 4 : Function to insert a character item into the stack

void push(char item. ST ACK *s)

{

if ( is_full(s) )

{

printf(”Stack Overflow\n”);

return;

}

s->top++; /* Update top to point to next item */

s->items[s->top] = item; /* Insert the item into the stack*/

}

The function to delete an integer item from the stack is shown in below code 5

code 5 : Function to delete an integer item from the stack

int pop(ST ACK *s)

{

int item;

if ( is_ empty(s) )

{

printf(”Stack Underflow\n”);

return 0;

}

item = s->items[s->top]; /* Access the top element */

s->top–; /* Update the pointer to point to previous item

return item; /* Return the top item to the calling function */

}

The function to delete a character item from the stack is shown in below code 6

code 6 : Function to delete a character item from the stack.

char pop(STACK *s)

{

char item;

if ( is_empty(s) )

{

printf(”Stack Underflow\n”);

return 0;

}

item = s->items[s->top]; /* Access the top element */

s->top–; /* Update the pointer to point to previous item */

return item; /* Return the top item to the calling function */

}

The program to display the contents of stack is shown in below code 7.

code 7 : Function to display the contents of the stack

void display(STACK s)

{

int i;

if ( is_empty(&s) )

{

printf(”Stack is empty\n”);

return 0;

}

printf(”The contents of the stack\n”);

for (i = 0; i<= s.top; i + +)

{

printf(”%d\n “,s.items[i];

}

}

The C program to simulate the working of a stack using structures is shown in below code.

code 8 : C program to simulate the stack operations using structures

#include<stdio.h>

#include <process.h>

#define ST ACK_SIZE 5

struct stack

{

int items[STACK_SIZE];

int top;

} ;

typedef struct stack STACK;

/* Include code 1: Check for empty stack */

/* Include code 2: Check for stack full or not */

/* Include code 3: To insert an item on the stack */

/* Include code 5: To delete an item from the stack */

/* Include code 7: To display the contents of the stack */

void main()

{

int item; /* Item to be inserted */

int choice; /* Push, pop, display or quit */

STACK s; /* To store items */

s.top = -1; /* Stack is empty initially */

for (;;)

{

printf(”1: Push 2: Pop\n”);

printf(”3: Disply 4: Exit\n”);

printf(”Enter the choice\n”);

scanf(”%d”,&choice);

switch(choice)

{

case 1:

printf(”Enter the item to be inserted\n”);

scanf(”%d”,& item);

push(item, & s);

break;

case 2:

item = pop(&s);

if (item != 0)

{

printf(”Item deleted = %d\n”, item);

}

break;

case 3:

display(s);

break;

default: exit(0);

}

}

}
Comments