Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is wrong with my program implementing a stack in C
    text
    copied!<p>Hi i have a problem with my stack data structure program. It seems that when i define the size of my array/the imaginary size of array just to call it through a loop the size i defined or specified by the user is being depleted or somewhat edited when i enter a data or push.</p> <p>For ex. i entered 5 for the size and choose push and then add 2. It is working properly. But if i choose to push data again, it is now passing to the size variable. I do not know understand what is happening...</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;ctype.h&gt; #include &lt;conio.h&gt; #include &lt;string.h&gt; #include &lt;stdlib.h&gt; #define p printf #define s scanf int top; int ar[1]; int size; main() { void push(); int opt, num; char cont[] = { 'y' }; clrscr(); p("Stacking Program"); p("\n\nData Size: "); s("%d", &amp;size); p("\n"); while((cont[0] == 'y') || (cont[0] == 'Y')) { clrscr(); p("Stacking Program"); p("\n\nData Size: %d\n\n", size); p("MAIN MENU\n1. Pop\n2. Push\n3. Pick\n4. View\nChoose: "); s("%d", &amp;opt); p("\n"); switch(opt) { case 1: pop(); break; case 2: if(top &gt; size) { p("You can't push more data"); } else { p("Enter data for Data[%d]: ", top); s("%d", &amp;num); push(num); } break; case 3: pick(); break; case 4: view(); break; default: p("Your choice is not in the list."); break; } p("\n\nDo you want continue\(Y\/N\)?"); s("%s", &amp;cont[0]); } } pop() { int a; if(top &lt; 0) { p("Stack empty."); return 0; } else { a = ar[top]; p("\(Data[%d] = %d\) removed.", top, a); top--; } } void push(int b) { top++; ar[top] = b; } pick() { if(top &lt; 0) { p("Nothing to display."); return 0; } else { p("\(Data[%d] = %d\) is the last data.", top, ar[top]); } } view() { int i; if(top &lt; 0) { p("Nothing to display."); return 0; } else { for(i = 1; i &lt; (top + 1); i++) { p("Data[%d] = %d\n", i, ar[i]); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload