Stack as the name suggests, it stores elements one above another like in the picture. It is also known as LIFO or Last In First Out. A new element gets added on the top of lest element and also when comes for removal, it is also the last added element which gets deleted first. Addition of a new elemnt in the queue is also known as Push and removal of any element is called Pop.

Let us take an example of how we keep our books one above another. We can add a new book on top of the last book. This collection grows from bottom to top or in the reverse order. Also removal of any book is not possible. If we do so it will collapse. Thus the only option remains is getting back the last book which is on the top position.

Stack of books

Let us now visualize a stack with the help of a linked list. We can say that elements will be added at tail as well as will be deleted from the tail or will be added at head and deleted from head.

Stack

/* stack.cpp : stack demo program
*/
#include<stdlib.h>
#include<malloc.h>
#include<process.h>
#include<conio.h>
#include<string.h>

struct node
{
  int value;
  struct node * next;
};
typedef struct node * stack_t;

struct node * stack_push(stack_t *stack, int value)
{
  struct node * new_node = NULL;
  new_node = (struct node *)malloc(sizeof(struct node));
  if(!stack)
  {
    return NULL;
  }
  if(new_node == NULL)
  {
    printf("Terminating: no more memory left!\n");
    exit(-1);
    return NULL;
  }
  new_node->value = value;
  new_node->next = *stack;
  if(*stack == NULL)
  {
    *stack = new_node;
  }

  return new_node;

}
struct node * stack_pop(stack_t *stack)
{
  struct node * temp;
  if(!stack)
  {
     return NULL;
  }
  if(*stack == NULL)
  {
    printf("Stack is empty!\n");
    return NULL;
  }
  else
  {
    printf("One element poped\n");
    temp = (*stack)->next;
    free(*stack);
    return temp;
  }


}
void stack_printall(struct node *queue)
{
  struct node * l_node = queue;
  while(l_node != NULL)
  {
    printf("node(0x%X) value = %d\n", l_node, l_node->value);
    l_node = l_node->next;
  }

}
void stack_popall(struct node *queue)
{
  struct node * l_temp = NULL;
  struct node * l_node = queue;
  while(l_node != NULL)
  {
    l_temp = l_node->next;
    printf("poping node(0x%X) value = %d\n", l_node, l_node->value);
    free(l_node);
    l_node = l_temp;
  }

}

int main(int argc, char* argv[])
{

  stack_t stack  = NULL;
  struct node *temp  = NULL;
  char input;
  int node_val;

  while(1)
  {
    printf("Stack Demo\nPush a new element [p]\n"
           "Pop top element [o]\n"
           "Exit [ESC]\nEnter :");
    input = getch();
    if(input == 'p')
    {
        printf("\nEnter a number :");
        scanf("%d", &node_val);
        temp = stack_push(&stack, node_val);
        if(temp)
        {
            printf("One element has been pushed\n");
        }
    }
    else if(input == 'o')
    {
        stack_pop(&stack);
    }
    else
    {
         break;
    }
      
    printf("Current stack elements\n");    
    stack_printall(stack);

  }
  stack_popall(stack);
  return 0;
}

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

Circular Linked list, Doubly Circular Linked list, Queue, reverse single linked list, delete single linked list, Stack, Tree and Binary tree, Binary tree, traverse Binary tree,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan