Please Critique my Code
Ron
  Posted: Feb 9 2006, 07:07 AM


Administrator


Group: Admin
Posts: 54
Member No.: 1
Joined: 2-February 06



This was part of the last assignment for Data Structures. The due date has passed, and Dr. Hao doesn't accept late homework, so it should be safe to discuss now. wink.gif

I can usually get my programs to work, but often find that after I'm done that I've gone the long way around. I'm wondering if I've done that again this time. Please check out my code and tell me what you think.

The assignment involved a stack created by the following:
CODE
typedef struct
{
int top;
int items[SIZE];
}stack;


Then among other things, we were to pick out the bottom element on the stack and assign it to a variable, leaving the stack unchanged. My solution was to pop the stack elements to an array, copy the variable, then push them all back.
CODE
// Set i to the bottom element of the stack, leaving the stack unchanged.
int bottom (stack* p)
{
int j;      // Counter
int i;      // To be assigned the bottom element
int temp_arr[SIZE];   // Array to temporarily hold the stack elements

// Place the stack elements in the array
for (j = 0; !empty(p); ++j)
 temp_arr[j] = pop(p);

i = temp_arr[j-1];

// Copy the elements back to the stack
for (; j >0; --j)  
 push (p, temp_arr[j-1]);

return i;
}


SIZE is a constant. empty checks to see if the stack is empty. pop pops the top element off the stack. push puts one item back on the stack.

So what do you think? Any suggestions or comments?

Thanks. smile.gif

PS. I'm sorry to say the [code][/code] blocks are quite what I'd hoped they be. They don't keep the color of the text, but hopefully still allow some degree of readability.
Top
The Tony
Posted: Feb 10 2006, 08:55 PM


Newbie


Group: Members
Posts: 6
Member No.: 7
Joined: 9-February 06



First off thank you, Ron for the readible code second this may be a little late because he went over it class but i did it about the same; but instead of an array i used a secondary stack they use the same memory but im pretty sure the overhead of using a stack is a bit more than an array with that said the use of an alternative stack is a more dynamic (memory saving) way of solving this problem because stack are very verstile, but only if they are beign used multiple times. In this case it really dosnt matter. The final consideration is a counter varible vs a temp varible to push back on the second stack. i think your soltion is best for the reason that you dont pick up a
CODE
i = pop(s2); push (s1,i)
your code goes straight to the push.
QUOTE
round and round we go
Top
Ron
Posted: Feb 11 2006, 01:27 AM


Administrator


Group: Admin
Posts: 54
Member No.: 1
Joined: 2-February 06



Thanks for the feedback. smile.gif
Top
JuanSauceda
Posted: Sep 9 2007, 11:40 PM


Advanced Member


Group: Admin
Posts: 48
Member No.: 3
Joined: 4-February 06



Welcome back CSA!
Top
« Next Oldest | Programming Questions | Next Newest »


Topic Options



Hosted for free by InvisionFree (Terms of Use: Updated 7/7/05) | Powered by Invision Power Board v1.3 Final © 2003 IPS, Inc.
Page creation time: 0.0995 seconds | Archive