GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Jul-2004, 20:03
ozzytx ozzytx is offline
New Member
 
Join Date: Jul 2004
Posts: 12
ozzytx is on a distinguished road

logic problem?


i think my logic is screwy in this function. i'm not sure if this is enough info but if more is needed then i'll give it or the files for it... the program has a stack with the items pushed in as listed below:
11
22
33
44
55

when i pop it should be taking the 11 out then the 22 then the 33, but instead it's taking the 55 out then the 44 (backwards...) and i'm pretty sure my logic is messed up in the pop function:

CPP / C++ / C Code:
void CStack::Pop()
{
	if(IsEmpty())
	{
		pTop = pStack + size - 1;
	}
	else
	{
		pTop++;
		if(pTop > pStack + size - 1)
		{
			pTop = '\0';
		}
	}
}

this is probably easy to fix, or not, but i've been staring at it for almost 2 hours and this is the only thing i have to fix and i'm done. so if anybody could possibly help...
Last edited by dsmith : 09-Jul-2004 at 07:04. Reason: Please use [c] & [/c] for syntax highlighting
  #2  
Old 08-Jul-2004, 21:32
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,242
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by ozzytx
i think my logic is screwy in this function. i'm not sure if this is enough info but if more is needed then i'll give it or the files for it... the program has a stack with the items pushed in as listed below:
11
22
33
44
55

when i pop it should be taking the 11 out then the 22 then the 33, but instead it's taking the 55 out then the 44 (backwards...) and i'm pretty sure my logic is messed up in the pop function:

void CStack:op()
{
if(IsEmpty())
{
pTop = pStack + size - 1;
}
else
{
pTop++;
if(pTop > pStack + size - 1)
{
pTop = '\0';
}
}
}

this is probably easy to fix, or not, but i've been staring at it for almost 2 hours and this is the only thing i have to fix and i'm done. so if anybody could possibly help...

Doesn't anyone read the rules? CODE Tags dang it! They are explained, defined, and requested in the stickies and the rules

Now, onto the problem.
All you list is what is on the stack, but not the order they were pushed. Which was last? The 55? If so, that's what's popped first. and it works. Other than that, I would need more information.
__________________

Age is unimportant -- except in cheese
  #3  
Old 09-Jul-2004, 00:41
ozzytx ozzytx is offline
New Member
 
Join Date: Jul 2004
Posts: 12
ozzytx is on a distinguished road
the 11 is pushed first. i guess what's confusing me is which number would be considered the top number? the 11 since it was pushed first or the 55?

my teacher has me totally confused because he gave us a stack program and a queue program to do and i'm supposed to pop the top on both. both have the exact code except the pop functions are different. the stack program pops the 55 first (last number pushed) and the queue program pops the 11 first (first number pushed)

would it help if i sent the entire code?
  #4  
Old 09-Jul-2004, 02:23
sho sho is offline
Junior Member
 
Join Date: Jun 2004
Posts: 49
sho will become famous soon enough
Quote:
Originally Posted by ozzytx
the stack program pops the 55 first (last number pushed) and the queue program pops the 11 first (first number pushed)
yeah thats right... stacks are called LIFO (Last In First Out) structures because the last element pushed is the first being pob...
on the other hand queues are FIFO (First In First Out) structures, since the first pushed element is also the first to pop...

about your program... you could use a pointer to point to the position of the element to pop (which depends on your implementation), so that it would be the only to change between the stack and queue structures (in one case it would point to the last element pushed and in the other to the first one pushed)...
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't find logic error crystalattice C++ Forum 2 24-May-2004 19:31
C I/O problem kelly80 C Programming Language 4 27-Apr-2004 20:15
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 07:53
problem with php5 cgi installation fab13 Apache Web Server Forum 3 19-Nov-2003 09:11
unwanted scrollbar problem kelly001 Web Design Forum 3 24-Oct-2003 10:44

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:49.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.