Posts

Showing posts with the label print stack

A simple Stack implementation: push, pop, print_stack

In this source code, a simple stack is implemented.  A single array is used to implement 3 stacks.  Let's say we have an array with a size of 300. It consists of 3 stacks concatenated to each other. The user can add/remove elements from each stack by passing the stack number as a parameter to the push/pop functions.  Usage: push(s, 12, 1); // Pushes element 12 to the stack number 1. push(s, 2, 3); // Pushes element 42 to the stack number 3. pop(s, 3); // Pops an element from the stack number 3. Source Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #define NUM_OF_STACK 3 #define STACK_SIZE 10 /* * stack data structure */ struct stack { int array[STACK_SIZE * NUM_OF_STACK]; int size[NUM_OF_STACK]; }; /* * print_stack() * Prints the stack */ void print_stack ( struct stack * s) { int i; for (i = 0 ; i < STACK_SIZE; i ++ ) { printf( "%d " , s -> array[i]...