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]);
c
add a comment |
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]);
c
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
add a comment |
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]);
c
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
c
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
add a comment |
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
add a comment |
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
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 toint top
. Then you would use it just liketop
in yourpush
function and get rid ofint 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 makei
astatic
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
|
show 5 more comments
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
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 toint top
. Then you would use it just liketop
in yourpush
function and get rid ofint 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 makei
astatic
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
|
show 5 more comments
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
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 toint top
. Then you would use it just liketop
in yourpush
function and get rid ofint 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 makei
astatic
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
|
show 5 more comments
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
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
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 toint top
. Then you would use it just liketop
in yourpush
function and get rid ofint 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 makei
astatic
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
|
show 5 more comments
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 toint top
. Then you would use it just liketop
in yourpush
function and get rid ofint 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 makei
astatic
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
|
show 5 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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