This is my program of stacks. The problem is this that it does not show output. whenever I run the program, the output screen flashes and nothing else happens. (using Dev-C++)

Don't know what's the problem with it.

//****************************STACKS********************************
//===============Stacks using linked list(Arrays)=================

#include<iostream>
using namespace std;

int avail=-1, start=-1, i;

void ins();
void del();
void trav();

struct node
{
       int data;
       int link;
};


node arr[10];

int main()
{
    int choice;
    
    for (i=10; i>=0; i++)
    {
        arr[i].link=avail;
        avail=i;
    }

top:
    cout<<"\n\tMain Menu";
    cout<<"\n\t----------";
    cout<<"\n1. Insertion";
    cout<<"\n2. Deletion";
    cout<<"\n3. Traversing";
    cout<<"\n4. Exit";
    cout<<"\n\nEnter your choice ";
    cin>>choice;
    
    switch(choice)
    {
                  case 1:
                       ins();
                       break;
                  case 2:
                       del();
                       break;
                  case 3:
                       trav();
                       break;
                  case 4:
                       exit(0);
                  default:
                          cout<<"\nInvalid choice entered!";
                          system("PAUSE");
                          break;
    }
    
    goto top;
         
    system("PAUSE");
    
    return 0;
}

//=========================Insertion=========================
void ins()
{
     int newnode;
     int item;
     cout<<"\nEnter any item to insert: ";
     cin>>item;
     
     if (avail==-1)
     {
     cout<<"\nOverflow";
     return;
     }
     
     else 
     {
          newnode=avail;
          avail=arr[avail].link;
          arr[newnode].data=item;
          arr[newnode].link=start;
          start=newnode;
     }
}

//====================Deletion=====================
void del()
{
     int loc;
     if(start==-1)
     {
                cout<<"Underflow";
                return;
     }
     
     else
     {
         loc=start;
         start=arr[start].link;
         arr[loc].link=avail;
         avail=loc;                                      
     }
}

//===========================Traversing=========================================
void trav()
{
     for (i=start; i!=-1; i=arr[i].link)
         cout<<arr[i].data<<endl;
}

Recommended Answers

All 4 Replies

Perhaps if you used a while loop, instead of a goto, you could get out of that loop in a sensible manner rather than calling exit(). Which by the way will skip your attempt to PAUSE the display.

U have defne an array of 10 nodes i.e. node arr[10]. But while looping in main() u are using for in wrong way i.e.

for (i=10; i>=0; i++)

here i should be initialized to 9 as the length of array is 10 (0-9). and also u have to decrement i not increment.

for(i=9; i>=0;i--)

I tried that too, but it didn't work...

Thanks Luckychap, it's running now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.