An example on Stack implementation I tried to solve









up vote
-1
down vote

favorite












I am trying to practice an example on stack, and I am finding a bit difficulty in printing the answer of this example.



/* A letter means push and an asterisk means pop in the
following sequence. Give the sequence of values returned by the pop operations
when this sequence of operations is performed on an initially empty LIFO stack.

E A S * Y * Q U E * * * S T * * * I O * N * * *
*/
#include<stdio.h>

char a[40] = "EAS*Y*QUE***ST***IO*N***", s[20], b[30];
int top = -1;


This one is the PUSH operation.



void push(char a)

int i=0;
if (top >= 24)
printf("Overflow.");
else if (a == '*')
b[i++] = pop();
else
s[++top]= a;



And this is the POP operation.



 void pop()

if (top < 0)
printf("Underflow");
else
top--;

return s[top+1];



And the main function.



void main()

int i;
printf("%sn", a);

for (i=0; i<24; i++)
push(a[i]);

for (i=0; i<24; i++)
printf("%c", b[i]);










share|improve this question



















  • 1




    What's the issue?
    – Ajay Gaur
    Nov 8 at 18:13










  • I can't get to print the array b , i ran it on codeblocks and it just showed me a blank screen
    – Deep Mevada
    Nov 8 at 18:16










  • Did you try to debug your code?
    – Jeroen Heier
    Nov 8 at 18:40










  • It isn't letting me to debug the code
    – Deep Mevada
    Nov 8 at 18:46










  • I'm sorry but i'm a bit new at all this
    – Deep Mevada
    Nov 8 at 18:46














up vote
-1
down vote

favorite












I am trying to practice an example on stack, and I am finding a bit difficulty in printing the answer of this example.



/* A letter means push and an asterisk means pop in the
following sequence. Give the sequence of values returned by the pop operations
when this sequence of operations is performed on an initially empty LIFO stack.

E A S * Y * Q U E * * * S T * * * I O * N * * *
*/
#include<stdio.h>

char a[40] = "EAS*Y*QUE***ST***IO*N***", s[20], b[30];
int top = -1;


This one is the PUSH operation.



void push(char a)

int i=0;
if (top >= 24)
printf("Overflow.");
else if (a == '*')
b[i++] = pop();
else
s[++top]= a;



And this is the POP operation.



 void pop()

if (top < 0)
printf("Underflow");
else
top--;

return s[top+1];



And the main function.



void main()

int i;
printf("%sn", a);

for (i=0; i<24; i++)
push(a[i]);

for (i=0; i<24; i++)
printf("%c", b[i]);










share|improve this question



















  • 1




    What's the issue?
    – Ajay Gaur
    Nov 8 at 18:13










  • I can't get to print the array b , i ran it on codeblocks and it just showed me a blank screen
    – Deep Mevada
    Nov 8 at 18:16










  • Did you try to debug your code?
    – Jeroen Heier
    Nov 8 at 18:40










  • It isn't letting me to debug the code
    – Deep Mevada
    Nov 8 at 18:46










  • I'm sorry but i'm a bit new at all this
    – Deep Mevada
    Nov 8 at 18:46












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I am trying to practice an example on stack, and I am finding a bit difficulty in printing the answer of this example.



/* A letter means push and an asterisk means pop in the
following sequence. Give the sequence of values returned by the pop operations
when this sequence of operations is performed on an initially empty LIFO stack.

E A S * Y * Q U E * * * S T * * * I O * N * * *
*/
#include<stdio.h>

char a[40] = "EAS*Y*QUE***ST***IO*N***", s[20], b[30];
int top = -1;


This one is the PUSH operation.



void push(char a)

int i=0;
if (top >= 24)
printf("Overflow.");
else if (a == '*')
b[i++] = pop();
else
s[++top]= a;



And this is the POP operation.



 void pop()

if (top < 0)
printf("Underflow");
else
top--;

return s[top+1];



And the main function.



void main()

int i;
printf("%sn", a);

for (i=0; i<24; i++)
push(a[i]);

for (i=0; i<24; i++)
printf("%c", b[i]);










share|improve this question















I am trying to practice an example on stack, and I am finding a bit difficulty in printing the answer of this example.



/* A letter means push and an asterisk means pop in the
following sequence. Give the sequence of values returned by the pop operations
when this sequence of operations is performed on an initially empty LIFO stack.

E A S * Y * Q U E * * * S T * * * I O * N * * *
*/
#include<stdio.h>

char a[40] = "EAS*Y*QUE***ST***IO*N***", s[20], b[30];
int top = -1;


This one is the PUSH operation.



void push(char a)

int i=0;
if (top >= 24)
printf("Overflow.");
else if (a == '*')
b[i++] = pop();
else
s[++top]= a;



And this is the POP operation.



 void pop()

if (top < 0)
printf("Underflow");
else
top--;

return s[top+1];



And the main function.



void main()

int i;
printf("%sn", a);

for (i=0; i<24; i++)
push(a[i]);

for (i=0; i<24; i++)
printf("%c", b[i]);







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 18:52









Jeroen Heier

2,02131626




2,02131626










asked Nov 8 at 18:10









Deep Mevada

33




33







  • 1




    What's the issue?
    – Ajay Gaur
    Nov 8 at 18:13










  • I can't get to print the array b , i ran it on codeblocks and it just showed me a blank screen
    – Deep Mevada
    Nov 8 at 18:16










  • Did you try to debug your code?
    – Jeroen Heier
    Nov 8 at 18:40










  • It isn't letting me to debug the code
    – Deep Mevada
    Nov 8 at 18:46










  • I'm sorry but i'm a bit new at all this
    – Deep Mevada
    Nov 8 at 18:46












  • 1




    What's the issue?
    – Ajay Gaur
    Nov 8 at 18:13










  • I can't get to print the array b , i ran it on codeblocks and it just showed me a blank screen
    – Deep Mevada
    Nov 8 at 18:16










  • Did you try to debug your code?
    – Jeroen Heier
    Nov 8 at 18:40










  • It isn't letting me to debug the code
    – Deep Mevada
    Nov 8 at 18:46










  • I'm sorry but i'm a bit new at all this
    – Deep Mevada
    Nov 8 at 18:46







1




1




What's the issue?
– Ajay Gaur
Nov 8 at 18:13




What's the issue?
– Ajay Gaur
Nov 8 at 18:13












I can't get to print the array b , i ran it on codeblocks and it just showed me a blank screen
– Deep Mevada
Nov 8 at 18:16




I can't get to print the array b , i ran it on codeblocks and it just showed me a blank screen
– Deep Mevada
Nov 8 at 18:16












Did you try to debug your code?
– Jeroen Heier
Nov 8 at 18:40




Did you try to debug your code?
– Jeroen Heier
Nov 8 at 18:40












It isn't letting me to debug the code
– Deep Mevada
Nov 8 at 18:46




It isn't letting me to debug the code
– Deep Mevada
Nov 8 at 18:46












I'm sorry but i'm a bit new at all this
– Deep Mevada
Nov 8 at 18:46




I'm sorry but i'm a bit new at all this
– Deep Mevada
Nov 8 at 18:46












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










In your push function, you are declaring int i as a local variable. With this in mind, can you see how your line b[i++]=pop(); will always evaluate to b[0]=pop();?



Edit:
Here are the suggested changes. Per Tim's suggestion, you should make int i a static variable.



void push(char a)

static int i=0;
if(top>=24)
printf("Overflow.");
else if(a=='*')
b[i++]=pop();
else
s[++top]=a;



You also need to make pop return a char instead of void



char pop()

if(top<0)
printf("Underflow");
else
top--;

return s[top+1];



jdoodle






share|improve this answer






















  • Ok Then what should I be using instead the above problem
    – Deep Mevada
    Nov 8 at 18:18










  • They're a few ways you could do it. The most obvious to me would be to use a new global variable similar to int top. Then you would use it just like top in your push function and get rid of int i. Let me know if you can get this working.
    – Tyler Zeller
    Nov 8 at 18:25










  • The most obvious to me would be to make i a static variable. It would keep its value from one function call to the next, but be hidden (protected) from the rest of the code.
    – Tim Randall
    Nov 8 at 18:28










  • This is what I get. UnderflowUnderflow ? ?
    – Deep Mevada
    Nov 8 at 18:29











  • @DeepMevada have you tried Tim's suggestion? His is better than mine.
    – Tyler Zeller
    Nov 8 at 18:35










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53213743%2fan-example-on-stack-implementation-i-tried-to-solve%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










In your push function, you are declaring int i as a local variable. With this in mind, can you see how your line b[i++]=pop(); will always evaluate to b[0]=pop();?



Edit:
Here are the suggested changes. Per Tim's suggestion, you should make int i a static variable.



void push(char a)

static int i=0;
if(top>=24)
printf("Overflow.");
else if(a=='*')
b[i++]=pop();
else
s[++top]=a;



You also need to make pop return a char instead of void



char pop()

if(top<0)
printf("Underflow");
else
top--;

return s[top+1];



jdoodle






share|improve this answer






















  • Ok Then what should I be using instead the above problem
    – Deep Mevada
    Nov 8 at 18:18










  • They're a few ways you could do it. The most obvious to me would be to use a new global variable similar to int top. Then you would use it just like top in your push function and get rid of int i. Let me know if you can get this working.
    – Tyler Zeller
    Nov 8 at 18:25










  • The most obvious to me would be to make i a static variable. It would keep its value from one function call to the next, but be hidden (protected) from the rest of the code.
    – Tim Randall
    Nov 8 at 18:28










  • This is what I get. UnderflowUnderflow ? ?
    – Deep Mevada
    Nov 8 at 18:29











  • @DeepMevada have you tried Tim's suggestion? His is better than mine.
    – Tyler Zeller
    Nov 8 at 18:35














up vote
1
down vote



accepted










In your push function, you are declaring int i as a local variable. With this in mind, can you see how your line b[i++]=pop(); will always evaluate to b[0]=pop();?



Edit:
Here are the suggested changes. Per Tim's suggestion, you should make int i a static variable.



void push(char a)

static int i=0;
if(top>=24)
printf("Overflow.");
else if(a=='*')
b[i++]=pop();
else
s[++top]=a;



You also need to make pop return a char instead of void



char pop()

if(top<0)
printf("Underflow");
else
top--;

return s[top+1];



jdoodle






share|improve this answer






















  • Ok Then what should I be using instead the above problem
    – Deep Mevada
    Nov 8 at 18:18










  • They're a few ways you could do it. The most obvious to me would be to use a new global variable similar to int top. Then you would use it just like top in your push function and get rid of int i. Let me know if you can get this working.
    – Tyler Zeller
    Nov 8 at 18:25










  • The most obvious to me would be to make i a static variable. It would keep its value from one function call to the next, but be hidden (protected) from the rest of the code.
    – Tim Randall
    Nov 8 at 18:28










  • This is what I get. UnderflowUnderflow ? ?
    – Deep Mevada
    Nov 8 at 18:29











  • @DeepMevada have you tried Tim's suggestion? His is better than mine.
    – Tyler Zeller
    Nov 8 at 18:35












up vote
1
down vote



accepted







up vote
1
down vote



accepted






In your push function, you are declaring int i as a local variable. With this in mind, can you see how your line b[i++]=pop(); will always evaluate to b[0]=pop();?



Edit:
Here are the suggested changes. Per Tim's suggestion, you should make int i a static variable.



void push(char a)

static int i=0;
if(top>=24)
printf("Overflow.");
else if(a=='*')
b[i++]=pop();
else
s[++top]=a;



You also need to make pop return a char instead of void



char pop()

if(top<0)
printf("Underflow");
else
top--;

return s[top+1];



jdoodle






share|improve this answer














In your push function, you are declaring int i as a local variable. With this in mind, can you see how your line b[i++]=pop(); will always evaluate to b[0]=pop();?



Edit:
Here are the suggested changes. Per Tim's suggestion, you should make int i a static variable.



void push(char a)

static int i=0;
if(top>=24)
printf("Overflow.");
else if(a=='*')
b[i++]=pop();
else
s[++top]=a;



You also need to make pop return a char instead of void



char pop()

if(top<0)
printf("Underflow");
else
top--;

return s[top+1];



jdoodle







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 18:49

























answered Nov 8 at 18:16









Tyler Zeller

895




895











  • Ok Then what should I be using instead the above problem
    – Deep Mevada
    Nov 8 at 18:18










  • They're a few ways you could do it. The most obvious to me would be to use a new global variable similar to int top. Then you would use it just like top in your push function and get rid of int i. Let me know if you can get this working.
    – Tyler Zeller
    Nov 8 at 18:25










  • The most obvious to me would be to make i a static variable. It would keep its value from one function call to the next, but be hidden (protected) from the rest of the code.
    – Tim Randall
    Nov 8 at 18:28










  • This is what I get. UnderflowUnderflow ? ?
    – Deep Mevada
    Nov 8 at 18:29











  • @DeepMevada have you tried Tim's suggestion? His is better than mine.
    – Tyler Zeller
    Nov 8 at 18:35
















  • Ok Then what should I be using instead the above problem
    – Deep Mevada
    Nov 8 at 18:18










  • They're a few ways you could do it. The most obvious to me would be to use a new global variable similar to int top. Then you would use it just like top in your push function and get rid of int i. Let me know if you can get this working.
    – Tyler Zeller
    Nov 8 at 18:25










  • The most obvious to me would be to make i a static variable. It would keep its value from one function call to the next, but be hidden (protected) from the rest of the code.
    – Tim Randall
    Nov 8 at 18:28










  • This is what I get. UnderflowUnderflow ? ?
    – Deep Mevada
    Nov 8 at 18:29











  • @DeepMevada have you tried Tim's suggestion? His is better than mine.
    – Tyler Zeller
    Nov 8 at 18:35















Ok Then what should I be using instead the above problem
– Deep Mevada
Nov 8 at 18:18




Ok Then what should I be using instead the above problem
– Deep Mevada
Nov 8 at 18:18












They're a few ways you could do it. The most obvious to me would be to use a new global variable similar to int top. Then you would use it just like top in your push function and get rid of int i. Let me know if you can get this working.
– Tyler Zeller
Nov 8 at 18:25




They're a few ways you could do it. The most obvious to me would be to use a new global variable similar to int top. Then you would use it just like top in your push function and get rid of int i. Let me know if you can get this working.
– Tyler Zeller
Nov 8 at 18:25












The most obvious to me would be to make i a static variable. It would keep its value from one function call to the next, but be hidden (protected) from the rest of the code.
– Tim Randall
Nov 8 at 18:28




The most obvious to me would be to make i a static variable. It would keep its value from one function call to the next, but be hidden (protected) from the rest of the code.
– Tim Randall
Nov 8 at 18:28












This is what I get. UnderflowUnderflow ? ?
– Deep Mevada
Nov 8 at 18:29





This is what I get. UnderflowUnderflow ? ?
– Deep Mevada
Nov 8 at 18:29













@DeepMevada have you tried Tim's suggestion? His is better than mine.
– Tyler Zeller
Nov 8 at 18:35




@DeepMevada have you tried Tim's suggestion? His is better than mine.
– Tyler Zeller
Nov 8 at 18:35

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53213743%2fan-example-on-stack-implementation-i-tried-to-solve%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế