Question:

multiple stacks in an array

by Guest3359  |  12 years, 8 month(s) ago

0 LIKES UnLike

multiple stacks in an array

 Tags: array, multiple, stacks

   Report

1 ANSWERS

  1. amomipais82
    Hi,
    #include<stdio.h>
    #include<malloc.h>


    int top1 = -1,top2 = 50;
    int bottom1, bottom2;

    int push(int *base,int data,int stack_no);
    int pop(int *base,int stack_no);

    int main()
    {
      int arr[50]; ///array to be used for stack implementation


      push(arr,10,1);
      push(arr,20,2);

      printf("\t %d",pop(arr,2));
      printf("\t %d",pop(arr,1));
      printf("\t %d",pop(arr,1));


      return 0;
    }



    int push(int *base,int data,int stack_no)
    {

      if( top1+1 == top2)
      {
         return -1;
      }

      switch(stack_no)
      {
      case 1:
         base[++top1] = data;
         break;
      case 2:
         base[--top2] = data;
         break;
      default:
         return -1;
      }

      return 0;
    }


    int pop(int *base,int stack_no)
    {
      switch(stack_no)
      {
      case 1:
         if(-1 == top1)
            break;
         else
            return base[top1--];

         break;

      case 2:
         if(50 == top2)
            break;
         else
            return base[top2++];

         break;

      default:
         return -1;
         break;
      }

      return -1;
    }

Sign In or Sign Up now to answser this question!

Question Stats

Latest activity: 14 years, 6 month(s) ago.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions