Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

3. Stack - Sample Program

Uploaded by

mohanvino
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

3. Stack - Sample Program

Uploaded by

mohanvino
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Data Structures and Algorithms Design (SS ZG519) 1

Stack Program - Sample. Author: Prof. Vineet Garg

Sample main program that uses stack functions:

/* --------------------------- Link Section---------------------------------*/


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

/* ---------------------------Constants ------------------------------------*/
#define TRUE 1
#define FALSE 0
#define STACK_EMPTY_VALUE -1
#define SUCCESS 1
#define NO_SUCCESS 0
typedef int boolean;

/* -------------------------Global Variables -------------------------------*/


int *pIntStack = NULL;
int stackSize = 0;
int top = -1;

/* -------------------------Function prototypes ----------------------------*/


int *createStack (void);
boolean isStackEmpty (void);
boolean isStackFull (void);
int push (int element);
int pop (int *element);
void destroyStack (void);

/*---------------------main program ------------------------------------------*/


void main()
{
boolean result;
int element;

clrscr();

/* Get the stack size from the user into the global variable – stackSize */
printf("Enter the stack size to keep integers: \n");
scanf("%d", &stackSize);

/* Create stack of stackSize */


pIntStack = createStack ();

/* Check if stack is empty */


result = isStackEmpty();
Data Structures and Algorithms Design (SS ZG519) 2
Stack Program - Sample. Author: Prof. Vineet Garg

/* Print the stack empty or not based on the result */


if (result == TRUE)
{
printf("Stack is empty.\n");
}
else
{
printf("Stack is not empty.\n");

/* Push an integer to stack. E.g. 43 */


result = push(43);

/* Print the push result */


if (result == SUCCESS)
{
printf("Push successful.\n");
}
else
{
printf("Push failed.\n");

/* Pop out an element from stack. It will be stored in element */


result = pop (&element);

if (result == SUCCESS)
{
printf("Popped out element is %d.\n", element);
}
else
{
printf("Pop failed, because stack is empty.\n");

/* Sample program is done; destroy the stack */


printf("Program is complete, destroying the stack.\n");
destroyStack();

/* Wait for the user input before end */


getch();
}
Data Structures and Algorithms Design (SS ZG519) 3
Stack Program - Sample. Author: Prof. Vineet Garg

int *createStack()
This function creates a stack dynamically that will keep integers:

int *createStack()
{
int *x = NULL;

/* stackSize is a global variable it will be read from the user input */


x = (int *) calloc (stackSize, sizeof(int));

if (x == NULL)
{
printf("Error in stack memory allocation.\n");
exit(0);
}

/* Else, memory allocation for stack is successful, return pointer to it and


initial top value is STACK_EMPTY_VALUE (that is defined as -1).
*/
else
{
top = STACK_EMPTY_VALUE;
return x;
}
}

boolean isStackFull ()
This function checks if stack is full by checking the value of top. If it has reached to (stackSize-
1) it is full (TRUE), otherwise not (FALSE):

boolean isStackFull()
{

/* C stores array values from 0 to n-1, so used stackSize-1 */


if (top == stackSize-1)
{
return TRUE;
}
else
{
return FALSE;
}
}

boolean isStackEmpty ():


Data Structures and Algorithms Design (SS ZG519) 4
Stack Program - Sample. Author: Prof. Vineet Garg

This function checks if stack is empty by checking the value of top. If it is


STACK_EMPTY_VALUE (-1) it is empty (TRUE), otherwise not (FALSE).

boolean isStackEmpty()
{

if (top == STACK_EMPTY_VALUE)
{
return TRUE;
}
else
{
return FALSE;
}
}

int push (int element):


This function pushes an element into stack. If stack is full push has NO_SUCCESS, otherwise
increase top by 1, and stores the element on stack at this top value index and return
SUCCESS.

int push (int element)


{
if (isStackFull() == TRUE)
{
return NO_SUCCESS;
}
else
{
top++;
pIntStack[top] = element;
return SUCCESS;
}
}

int pop (int *element):


Data Structures and Algorithms Design (SS ZG519) 5
Stack Program - Sample. Author: Prof. Vineet Garg

This function pops out an element from stack. If stack is empty then pop has NO_SUCCESS,
otherwise, it returns the value stored at the index top and then decrements the top by 1.
Note that in case of SUCCESS, the popped out element is saved in element that is an
argument whose reference is passed in the function.

int pop (int *element)


{
if (isStackEmpty() == TRUE)
{
return NO_SUCCESS;
}
else
{
*element = pIntStack[top];
top --;
return SUCCESS;
}
}

void destroyStack():
This function destroys the stack (if it exists) and re-initializes the global variables.

void destroyStack()
{
if (pIntStack != NULL)
{
free(pIntStack);
pIntStack = NULL;
stackSize = 0;
top = -1;
}
}

You might also like