-
pop function
prototype:
Code:
struct stack pop (struct stack ** tos);
In main:
Code:
if (tos == NULL)
{
printf ("stack empty!\n");
}else
{
struct stack temp = pop(&tos);
printf("%d popped.\n", temp.sdata);
}
function definition:
Code:
struct stack pop (struct stack ** tos)
{
//holds the return value (data)
struct stack temp;
//use to free the head node
struct stack **freep = tos;
//reposition the head of the stack for post pop
if((*tos)->next != NULL) *tos = (*tos)->next;
//get data value
temp = **freep;
//free head node
free(*freep);
return temp;
}
Where did I go wrong with this? I can't quite figure it.
-
What I've done is quite a bit different to what you've done, but this is a stack program which does contain push and pop that I did ages ago.
-
I would like to know how to do this function with a pointer to a pointer so that the stack is updated in main automatically. I already know how to do other stack function. I'm not asking how to write a stack, but instead how to use this method of updating the stack in main through a pointer to a pointer to the stack object. Thanks anyway.
-
Oic...well gimme a little bit and I'll see what I can come up with.
-
BTW here is the push function if needed:
Code:
struct stack * push(struct stack * tos, int data)
{
struct stack *newp = NULL;
if(tos == NULL)
{
newp = (struct stack *) malloc (sizeof (struct stack));
newp->next = NULL;
newp->sdata = data;
tos = newp;
return tos;
}else
{
newp = (struct stack *) malloc (sizeof (struct stack));
newp->sdata = data;
newp->next = tos;
return newp;
}
}
The structure is:
Code:
struct stack
{
struct stack *next;
int sdata;
};
But my problem is the pop function:
And in main the definition:
Code:
int main()
{
struct stack *tos = NULL;
...
return 0;
}
-
I don't think I'm missing much, something small, but this moment I'm forced to study systems analysis because I have a test in 6 hrs. It would be great if someone can spot the error. I tried but could not figure it out.
-
I have posted my code before,u should read it carefully.
[1]->[2]->[3]->NULL
what will happen after free([3])?
[2]->next still point to [3] !!!!!
-
if((*tos)->next != NULL) *tos = (*tos)->next;
I don't go there if it is NULL.
Yet it's true that tos is than freed. Not sure what to do at that point. Maybe allocate a new node. hmm
-
The only way your pop would make sense to me is if your stack was circular.
-
hmm, but it make the same mistake.
if the linked list has only one item.
Like
[1]->NULL
>if((*tos)->next != NULL) *tos = (*tos)->next;
>I don't go there if it is NULL.
so after free(*freep), *tos point to a unavail item.
BTW: temp=**freep; can't work
replace it with memcpy(&temp,*freep,sizeof(struct stack));
-
temp=**freep;
This did work. But I also have your code now. Yes, I am slightly stuck. I'll keep working on it. I might have to consult a text book or something. I want to use the pointer to pointer. That is my goal. To pass the structure like so:
struct stack *tos;
struck stack temp = pop(&tos);
I can kind of see how your code works if the stack is circular but I don't quite get it.
-
FINALLY!!!!!!!
Code:
struct stack pop (struct stack ** tos)
{
//holds the return value (data)
struct stack temp;
//use to free the head node
struct stack *freep = *tos;
//reposition the head of the stack for post pop
if((*tos)->next == NULL)
{
*tos = NULL;
temp = *freep;
free(freep);
}else
{
*tos = (*tos)->next;
//get data value
temp = *freep;
//memcpy(&temp,*freep,sizeof(struct stack));
//free head node
free(freep);
}
return temp;
}
I got it!
-
Sorry I didn't get back sooner :) well it looks like you've worked it out anyway. I did do something pretty similar (albeit with some minor differences in structure), but I won't bother posting the code now. Good job Troll_King.