By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,219 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

Can you help me with stacks?

Mhel
P: 8
I'm really having a hard time with Stack...

The problem goes this way:

Create an array ( a[10] ).
If choice == 1, push number.
If choice == 2, pop number,
If choice == 3, display stack.
If choice == 0 quit.


Please help me!
I'm new to stacks that's why I'm having some difficulties.
Thank you.
Feb 21 '07 #1
Share this Question
Share on Google+
14 Replies


P: 10
Stack is a queue wich adopt a last-in first-out strategy.

If you would like to use an array to implemet a stack, you have to keep in mind that:
1] When you are inserting an element in the stack (push), you can write the element in the first available position of the array, making sure that the position is within the array dimension.

2] When you are removing an element (pop), you are extracting the last element inserted, making sure the stack is non-empty.

This can be done by keeping in a variable the position of the last inserted element of the array, and using it to find which element to pop or in which position push a new element.

Hope this helps.
Feb 21 '07 #2

DeMan
100+
P: 1,806
Stack is a queue wich adopt a last-in first-out strategy.
Not to be horribly pedantic (just a little bit):

I would say that a stack is a list which adopts LI-FO, A queue is a FI-FO structure, and IMHO it doesn't make much sense to define a LI-FO structure which adopts a FI-FO strategy.......
Feb 21 '07 #3

Mhel
P: 8
Stack is a queue wich adopt a last-in first-out strategy.

If you would like to use an array to implemet a stack, you have to keep in mind that:
1] When you are inserting an element in the stack (push), you can write the element in the first available position of the array, making sure that the position is within the array dimension.

2] When you are removing an element (pop), you are extracting the last element inserted, making sure the stack is non-empty.

This can be done by keeping in a variable the position of the last inserted element of the array, and using it to find which element to pop or in which position push a new element.

Hope this helps.
Thanks for the help!
But can you help me make a program that would POP, PUSH and DISPLAY the stack? I'm really having a hard time.

PLEASE?

Thank you!
Feb 21 '07 #4

sicarie
Expert Mod 2.5K+
P: 4,677
Thanks for the help!
But can you help me make a program that would POP, PUSH and DISPLAY the stack? I'm really having a hard time.

PLEASE?

Thank you!
Sure we can help you.

What do you have so far?
Feb 22 '07 #5

Mhel
P: 8
Sure we can help you.

What do you have so far?
I need to make a program (array with size 10) that would ask the user if he wants to POP, PUSH, DISPLAY THE STACK OR QUIT....... I've made an initial program and i think it's wrong and it's not complete.... I'm really confused. Please help me.......

Thank you!

INITIAL PROGRAM:
#include<stdio.h>
#include<conio.h>
void main(void)
{
int num[10],input;
char A,B,C;
int i;
int Choice;
clrscr();
printf("What would you like to do?\n");
printf("1- Push to the stack\n");
printf("2- Pop from the stack\n");
printf("3- Display the stack\n");
printf("\n0 Quit\n");
scanf("%i",&Choice);
if (Choice == 1);
for(i = 0; i < 10; i++)
{
printf("Push your number in the stack: ");
scanf("%d",& input);
}


getch();
}


The program would be something like that...
Feb 22 '07 #6

sicarie
Expert Mod 2.5K+
P: 4,677
I need to make a program (array with size 10) that would ask the user if he wants to POP, PUSH, DISPLAY THE STACK OR QUIT....... I've made an initial program and i think it's wrong and it's not complete.... I'm really confused. Please help me.......

Thank you!

INITIAL PROGRAM:
#include<stdio.h>
#include<conio.h>
void main(void)
{
int num[10],input;
char A,B,C;
int i;
int Choice;
clrscr();
printf("What would you like to do?\n");
printf("1- Push to the stack\n");
printf("2- Pop from the stack\n");
printf("3- Display the stack\n");
printf("\n0 Quit\n");
scanf("%i",&Choice);
if (Choice == 1);
for(i = 0; i < 10; i++)
{
printf("Push your number in the stack: ");
scanf("%d",& input);
}


getch();
}


The program would be something like that...
That's a good start (I have issues with the portability of 'getch' but that's another issue...). What are you confused about?

I think using a 'while' loop will be better - then you can push/pop to your hearts delight, but other than that it seems like you're on the right track.
Feb 22 '07 #7

Mhel
P: 8
That's a good start (I have issues with the portability of 'getch' but that's another issue...). What are you confused about?

I think using a 'while' loop will be better - then you can push/pop to your hearts delight, but other than that it seems like you're on the right track.


Can you help me make a program?
Please?
And I'm gonna try to simulate it....
Please?

Thank you...........
Feb 22 '07 #8

sicarie
Expert Mod 2.5K+
P: 4,677
Can you help me make a program?
Please?
And I'm gonna try to simulate it....
Please?

Thank you...........
Yeah, I can help you make it - that's what I'm trying to do. I can't write it for you, but I can help.

And you are on the right track, if you explain where you're confused, we can get you going again.
Feb 22 '07 #9

Mhel
P: 8
Yeah, I can help you make it - that's what I'm trying to do. I can't write it for you, but I can help.

And you are on the right track, if you explain where you're confused, we can get you going again.
Okey,

#include<stdio.h>
#include<conio.h>
void main(void)
{
int num[10],input;
char A,B,C;
int i;
int Choice;
clrscr();
printf("What would you like to do?\n");
printf("1- Push to the stack\n");
printf("2- Pop from the stack\n");
printf("3- Display the stack\n");
printf("\n0 Quit\n");
scanf("%i",&Choice);
if (Choice == 1);
for(i = 0; i < 10; i++)
{
printf("Push your number in the stack: ");
scanf("%d",& input);



Starting from here, how can i tell the user that the Stack is full and that he can no longer push a number? And How can i POP a number?
Feb 22 '07 #10

sicarie
Expert Mod 2.5K+
P: 4,677
Okey,

#include<stdio.h>
#include<conio.h>
void main(void)
{
int num[10],input;
char A,B,C;
int i;
int Choice;
clrscr();
printf("What would you like to do?\n");
printf("1- Push to the stack\n");
printf("2- Pop from the stack\n");
printf("3- Display the stack\n");
printf("\n0 Quit\n");
scanf("%i",&Choice);
if (Choice == 1);
for(i = 0; i < 10; i++)
{
printf("Push your number in the stack: ");
scanf("%d",& input);



Starting from here, how can i tell the user that the Stack is full and that he can no longer push a number? And How can i POP a number?
Well, I have a few comments on the code you have before we get there - I want you working with something that compiles, so that you can run/test as we go (I'm re-installing and dont' have a compiler handy and won't be able to).

1) ';' after your if statement. That ends the loop, and you want it to go on to the 'for' loop. You should delete the ';' and

2) enclose the for loop in opening and closing curly brackets { and }.

3) close the for loop's curly brackets - you just need one on the end.

After that your code should compile (though won't have full functionality).
Feb 22 '07 #11

Mhel
P: 8
Well, I have a few comments on the code you have before we get there - I want you working with something that compiles, so that you can run/test as we go (I'm re-installing and dont' have a compiler handy and won't be able to).

1) ';' after your if statement. That ends the loop, and you want it to go on to the 'for' loop. You should delete the ';' and

2) enclose the for loop in opening and closing curly brackets { and }.

3) close the for loop's curly brackets - you just need one on the end.

After that your code should compile (though won't have full functionality).

The code compiled but how can I tell the user that the stack is full and you can no longer push a number? And the other problem is, I don't know how to pop a number? Can you please help me?
Feb 22 '07 #12

Ganon11
Expert 2.5K+
P: 3,652
Well, pushing and popping should be implemented using a Stack class - unless you are just simulating a stack using an integer array. In this case, you could use a variable to keep track of how many elements are in the array. When the program begins, there are 0 values, so the counter = 0. As you read in the first number, you will read it into the array's 0 index, and then add 1 to the counter - you can use counter to specify which value will be overwritten.

To 'pop' a number from the stack, you can subtract one from the counter - this means the next write will overwrite the last number in the 'stack'.

Now, since a stack is Last-In, First-Out, in which order will you have to print the 'stack'?
Feb 22 '07 #13

Mhel
P: 8
Well, pushing and popping should be implemented using a Stack class - unless you are just simulating a stack using an integer array. In this case, you could use a variable to keep track of how many elements are in the array. When the program begins, there are 0 values, so the counter = 0. As you read in the first number, you will read it into the array's 0 index, and then add 1 to the counter - you can use counter to specify which value will be overwritten.

To 'pop' a number from the stack, you can subtract one from the counter - this means the next write will overwrite the last number in the 'stack'.

Now, since a stack is Last-In, First-Out, in which order will you have to print the 'stack'?

Is this correct?
#include<stdio.h>
#include<conio.h>
void main(void)
{
int num[10], input;
int i;
int Choice;
int ctr;
ctr=0;
clrscr();
printf("What would you like to do?\n");
printf("1- Push to the stack\n");
printf("2- Pop from the stack\n");
printf("3- Display the stack\n");
printf("\n0 Quit\n");
scanf("%i", &Choice);
if (Choice==1)
{
printf("The stack is empty. You can push to the stack.\n");
for(i=0; i<10; i++)
{
printf("Push your number in the stack: ");
scanf("%d", &input);
ctr= ctr+1; /* Is this correct? */
}
if (input > 10);
printf("The Stack Is Full. You can no longer Push a number!\n");
}
else if(Choice==0)
{
exit();
}
else if(Choice == 2)
{
printf("Pop a number from the stack: ");
/* I don't know what to put here */
ctr= ctr - 1;
}
getch();
}

The popping thing isn't working......
Feb 22 '07 #14

Ganon11
Expert 2.5K+
P: 3,652
OK, I think you need to review what exactly each option will mean. You should go over what pushing and popping mean for stacks, and then re-think how to implement them using an array. For instance, pushing a value does not mean inputting into the entire loop - find out what it means.
Feb 22 '07 #15

Post your reply

Sign in to post your reply or Sign up for a free account.