Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,561 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,637 people online right now. Registration is fast and FREE... Join Now!




chek this code

 
Reply to this topicStart new topic

chek this code, Implementation of 2 stack in one array

gauthamnaggg
31 Oct, 2007 - 07:07 AM
Post #1

New D.I.C Head
*

Joined: 25 Oct, 2007
Posts: 2


My Contributions
Plz check this code i have written.
CODE

    /****Implementation of two stacks in a array***/


#include<stdio.h>
#include<stdlib.h>
#define MAX 10
#define MID 5
    
    int a[MAX],ch,count=0;

    int top1=-1,top2=MID;

int main()
{
    while(1)

    {

        printf("\t1.PUSH\n");
        printf("\t2.POP\n");
        printf("\t3.DISPLAY\n");
        printf("\t4.EXIT\n");
    
        printf("\tEnter u r choice\n");
        scanf("%d",&ch);
                
        switch(ch)
          {
    
        case 1:push();
            break;
    
        case 2:pop();
            break;
    
        case 3:display();
            break;
    
        case 4:exit(0);
                        break;          
            
        }
        
               }
}

        
           push()
    {
      
        int n,s;
        printf("\tselect a stack..\n\n");
        printf("\tType 1 to select a 1.stack\n");
        printf("\tType 2 to select a 2.stack\n");
        scanf("%d",&s);
    
    switch(s)
        {
        
        case 1:
        printf("\tEnter elements for stack 1\n");
        scanf("%d",&n);
        

               if(top1>=MID)
        {
           printf("\tStack 1 overflow\n");    
          return;
          }    
        a[++top1]=n;
    
        break;
        
               case 2:
        
        printf("\tEnter elements for stack 2\n");
        scanf("%d",&n);
    
                if(top2 >=MAX)
             {
           printf("\tStack 2 overflow\n");    
          return;
                 }
                  a[++top2]=n;
                    

              }
}

    pop()
    {
        int s;
        printf("\t...select a stack to pop up ...\n");
        printf("\tType 1 to select a 1.stack\n");
        printf("\tType 2 to select a 2.stack\n");
        scanf("%d",&s);
    
    switch(s)
              {
    
    case 1:
        if(top1==-1)
        {
            printf("\tStack 1 is underflow\n");
             return;
        }
        else    
            printf("\tdeleted item from the stack 1 is =%d\n",a[top1]);
            top1--;
                     break;
    
    case 2:
        if(top2==MID)
        
        {
            printf("\tStack 2 is underflow\n");
             return;
        }
        printf("\tDeleted item from the stack 2 is =%d\n",a[top2]);
        top2--;
        break;
        }
    }

    

    display()
{
       int i;
       if(top1==-1)
          {
       printf("\tStack 1 is empty\n");
    
    }
    else
        {
            printf("\telements in the 1 stack\n");
            for(i=top1;i>=0;i--)
            printf("\t%d\n",a[i]);
        }
    
       if(top2==MID)
        
               printf("\tStack 2 is empty\n");
    
    else
        {
            printf("\telements in the 2 stack\n");
            for(i=top2;i>MID;i--)
    
            printf("\t%d\n",a[i]);

        }
    }


*1lacca: next time please use code tags, like this: code.gif
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Chek This Code
31 Oct, 2007 - 07:18 AM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Could you specify what should we check?
Do you receive any errors, or you are not sure that your code works fine?
If the latter, please specify what is it expected to do (for what output, what input should come, etc.)
User is offlineProfile CardPM
+Quote Post

girasquid
RE: Chek This Code
31 Oct, 2007 - 07:18 AM
Post #3

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,294



Thanked: 18 times
Dream Kudos: 725
My Contributions
What do you want us to check it for? Can you give us an idea of what might be going wrong?
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 10:11PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month