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]);
 }
 printf("size = %d\n", s->size[0]);

 for (i = STACK_SIZE; i < STACK_SIZE * 2; i++) {
  printf("%d ", s->array[i]);
 }
 printf("size = %d\n", s->size[1]);
 
 for (i = STACK_SIZE * 2; i < STACK_SIZE * 3; i++) {
  printf("%d ", s->array[i]);
 }
 printf("size = %d\n", s->size[2]);
}

/*
 * pop()
 * pops an element from the stack.
 */
int pop(struct stack *s, int stack_num)
{
 int index;
 int data;

 /* Calculate the top index */
 index = s->size[stack_num - 1] + ((stack_num - 1) * STACK_SIZE) - 1;
 
 data = s->array[index];
 s->array[index] = 0;
 s->size[stack_num - 1]--;
 
 return data;
}

/*
 * push()
 * pushes an element to the stack.
 */
void push(struct stack *s, int data, int stack_num)
{
 int index;

 index = s->size[stack_num - 1] + ((stack_num - 1) * STACK_SIZE);
 s->array[index] = data;
 s->size[stack_num - 1]++;
}

/*
 * main()
 * Test code.
 */
int main(void)
{
 struct stack *my_stack;

 my_stack = (struct stack *)malloc(sizeof(struct stack));
 if (!my_stack) {
  return 1;
 }

 memset(my_stack, 0, sizeof(struct stack));

 push(my_stack, 1, 1); 
 push(my_stack, 2, 2); 
 push(my_stack, 3, 3); 
 push(my_stack, 4, 1); 
 push(my_stack, 5, 2); 
 push(my_stack, 6, 3); 
 push(my_stack, 7, 1); 
 push(my_stack, 8, 2); 
 push(my_stack, 9, 3); 
 push(my_stack, 10, 1); 
 push(my_stack, 11, 2); 
 push(my_stack, 12, 3); 

 printf("After pushing the elements\n"); 
 print_stack(my_stack);

 printf("\n");

 pop(my_stack, 1);
 pop(my_stack, 2);
 pop(my_stack, 3);
 pop(my_stack, 1);
 pop(my_stack, 1);
 pop(my_stack, 3);
 pop(my_stack, 1);
 
 printf("After poping the elements\n"); 
 print_stack(my_stack);

 return 0;
}

Output:

After pushing the elements 1 4 7 10 0 0 0 0 0 0 size = 4 2 5 8 11 0 0 0 0 0 0 size = 4 3 6 9 12 0 0 0 0 0 0 size = 4
After poping the elements 0 0 0 0 0 0 0 0 0 0 size = 0 2 5 8 0 0 0 0 0 0 0 size = 3 3 6 0 0 0 0 0 0 0 0 size = 2
Source Download:

Comments

Popular posts from this blog

How to Use container_of in Linux Kernel Development

Notification Chains in Linux Kernel

Build and Flash OpenWrt for Raspberry Pi