Having problem with STACK

Nov 11, 2011 at 4:44pm
i know this question is very simple but i dont know how to write an easy C++ programm using Stack,i wll very thank full if somebody help me
my question : write a C++ programm that print 10 int in reverse with stack
thank you
Nov 11, 2011 at 5:44pm
This is a program use stack to convert a integer to binary.
You can use it for your needs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "stdafx.h"
#include<conio.h>
#include<stdio.h>
#define n 30
struct STACK
{
	int b[n];
	int top;

};
void initStack(STACK &s)
{
	s.top=0;
}
int IsEmpty(STACK s)
{
	if(s.top==0)
		return 1;
	return 0;
}
int IsFull(STACK s)
{
	if(s.top==n)
		return 1;
	return 0;
}
void Push(STACK &s, int x)
{
	if(IsFull( s)==1)
	{
		return ;
		printf("Stack full");
	}
	else
	{
		
		s.b[s.top]=x;
		s.top++;
		
	}
}
int Pop(STACK &s)
{
	if(IsEmpty(s)==1)
		return 32767;
	else
	{
		s.top--;
		return s.b[s.top];
	}
}
void ChangeBase(STACK &s)
{
	int x;
	printf("Enter a number to convert from binary::::");
	scanf("%d",&x);
	while(x!=0)
	{
		int dv=x%2;
		Push(s, dv);
		x=x/2;
	}
}
void OutPut(STACK s)
{
	int i=s.top;
	while(i>0)
	{
		int kw=Pop(s);
		printf("%d",kw);
		i--;
	}
}
void main()
{
	STACK s;	
	initStack(s);
	ChangeBase(s);
	OutPut( s);


}
Nov 11, 2011 at 5:47pm
thank you so muchhhh, i have 1 question ,in 45th line why return 32767?
Nov 11, 2011 at 6:08pm
Last edited on Nov 11, 2011 at 6:09pm
Nov 11, 2011 at 6:11pm
ooo i find out! thank u
Nov 11, 2011 at 6:28pm
in this programm i cant undrestand where we reversed the int ! can somebody help me plz??
Nov 11, 2011 at 6:37pm
thank you so muchhhh, i have 1 question ,in 45th line why return 32767?

To check stack is empty.
1
2
3
4
5
6
7
8
9
10
void OutPut(STACK s)
{
	int i=s.top;
	while(i>0)
	{
		int kw=Pop(s);
		printf("%d",kw);
		i--;
	}
}



1
2
3
4
5
6
7
8
9
10
void OutPut(STACK s)
{
	int kw=Pop(s);
	while(kw!=32767)
	{
		printf("%d",kw);
		 kw=Pop(s);
		
	}
}
Nov 11, 2011 at 9:05pm
gimbilak
in this programm i cant undrestand where we reversed the int ! can somebody help me plz??


I'm not quite sure what you mean.

Stacks use the LIFO(Last In First Out) sequence. So whatever you put on top of the stack is the first to be removed from the stack.
Topic archived. No new replies allowed.