Stacks 
Author Message
 Stacks

Hello,

 I am a student and have just started learning C. I want to creat two
stacks and be able to push and pop a list of numbers from one stack to
another.

eg.   push_1(6)
         push_1(7)
         X = top_1
         pop_1
         push_2(X) etc

However I am unable to generate a function that reads the top number of
a stack and assign it to a variable. The push and pop functions seem to
work correctly. Can any one help.

regards

craig

#include <stdio.h>
#include <stdlib.h>

typedef struct stackTag
{
 int entry;
 struct stackTag *next;

Quote:
} stackNode, *stack;

void initStack (stack *S)
{
 *S = NULL;

Quote:
} /* initStack */

int emptyStack(const stack *S)
{
 return *S == NULL;

Quote:
} /* emptyStack */

int push(stack *S, const int item)
{
 stack newNode = (stack)malloc(sizeof(stackNode));

 if (newNode == NULL)
 {
  printf("\aOut of Memory\n");
  return 0;
 }
 newNode ->entry = item;
 newNode -> next = *S;
 *S = newNode;
 return 1;

Quote:
} /* push */

int pop(stack *S)
{
 stack oldNode;

 if (emptyStack(S))
 {
  printf("\aStack Empty\n");
  return 0;
 }
 oldNode = *S;                /* remember current top pointer */
 *S = oldNode -> next;        /* adjust top pointer */
 free(oldNode);               /* dispose of old node */
 return 1;

Quote:
} /* pop */

 int top(stack *S, int *item)
 {

  if (emptyStack(S))
  {
   return -1;
  }
  *Item = S -> entry;
  return item;

Quote:
}

--



Wed, 19 Mar 2003 03:00:00 GMT  
 Stacks
Your problem is that your "pop" function is returning 1 instead of returning
the value
that was on the top of the stack.

You need to save "S->entry" somewhere before deleting the top node, and
then return it when your function exits.

-Will


Quote:
> Hello,

>  I am a student and have just started learning C. I want to creat two
> stacks and be able to push and pop a list of numbers from one stack to
> another.

> eg.   push_1(6)
>          push_1(7)
>          X = top_1
>          pop_1
>          push_2(X) etc

> However I am unable to generate a function that reads the top number of
> a stack and assign it to a variable. The push and pop functions seem to
> work correctly. Can any one help.

> regards

> craig

> #include <stdio.h>
> #include <stdlib.h>

> typedef struct stackTag
> {
>  int entry;
>  struct stackTag *next;
> } stackNode, *stack;

> void initStack (stack *S)
> {
>  *S = NULL;
> } /* initStack */

> int emptyStack(const stack *S)
> {
>  return *S == NULL;
> } /* emptyStack */

> int push(stack *S, const int item)
> {
>  stack newNode = (stack)malloc(sizeof(stackNode));

>  if (newNode == NULL)
>  {
>   printf("\aOut of Memory\n");
>   return 0;
>  }
>  newNode ->entry = item;
>  newNode -> next = *S;
>  *S = newNode;
>  return 1;
> } /* push */

> int pop(stack *S)
> {
>  stack oldNode;

>  if (emptyStack(S))
>  {
>   printf("\aStack Empty\n");
>   return 0;
>  }
>  oldNode = *S;                /* remember current top pointer */
>  *S = oldNode -> next;        /* adjust top pointer */
>  free(oldNode);               /* dispose of old node */
>  return 1;
> } /* pop */

>  int top(stack *S, int *item)
>  {

>   if (emptyStack(S))
>   {
>    return -1;
>   }
>   *Item = S -> entry;
>   return item;
> }
> --


--



Fri, 28 Mar 2003 03:00:00 GMT  
 Stacks

Quote:

> Hello,

>  I am a student and have just started learning C. I want to creat two
> stacks and be able to push and pop a list of numbers from one stack to
> another.

> eg.   push_1(6)
>          push_1(7)
>          X = top_1
>          pop_1
>          push_2(X) etc

> However I am unable to generate a function that reads the top number of
> a stack and assign it to a variable. The push and pop functions seem to
> work correctly. Can any one help.

Since you have declared push and pop thusly,

int push(stack *, const int);
int pop(stack *);

Then you can say "push(pStack_to, pop(pStack_from));". There is no need
for a special function.
I hope I wasn't too late...

Thanks, Ed
--



Sat, 05 Apr 2003 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. /STACK increase the stack, means decrease the heap ?

2. STACKS and STACKS

3. Stack Structure and Stack Overflow for CE

4. The C stack (or: implementing a stack trace)

5. /STACK increase the stack, means decrease the heap ?

6. Stack overflow exception

7. Creating objects on stack

8. CertFreeCertificateChain corrupts my stack...

9. Stack Overflow

10. Exception Stack Problem?

11. Showing Call Stack and Determine which object is not valid LARGE CHUNK OF CODE

12. Call Stack not showing up?

 

 
Powered by phpBB® Forum Software