Link to home
Start Free TrialLog in
Avatar of ant01ne
ant01ne

asked on

return problem...

Hi,

I have a little problem with a stack program.
My program is divided into different functions, one of them is a delete function which delete one element from the beginnning of the stack. This is still simple to do. However, I would like to return the first element i have just deleted. Moreover, the element i have to return must be a node type.
Here is some parts of the program to make you understand it, but it is not working at all:

typedef struct node{
   int val;
   struct node *next;
} NODE;

void main(){
   NODE *head = NULL, *temp = NULL;
   
   *temp = Delete( &head );
   return;  
}


NODE Delete( NODE **h ){
   NODE *deletenode = NULL;

//here are the statements which delete the element at the beginning of the stack

   return( *deletenode );
}

I am not sure if I have to return it like this, but this wy, it does not work.
I just want to know if someone could tell me how to return that kind of structure from a function.
Plus, make sure it works when returning to the temp NODE.
Thanks...
ASKER CERTIFIED SOLUTION
Avatar of akshayxx
akshayxx
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
my version of delete should be used like this..

void main(){
  NODE *head = NULL, *temp = NULL;
 
  temp = delete( head );
//access the data like this ... temp->val;
  return;  
}
Avatar of Kocil
Kocil

//NODE Delete( NODE **h ){
NODE* Delete(NODE **h) {
  NODE *deletenode = NULL;

//here are the statements which delete the element at the beginning of the stack

//  return( *deletenode );
return deletenode;
}
Avatar of ant01ne

ASKER

Yeah i know it is pop, i just put delete i do not even know why :P
Anyway, thanx for the answer, it solved my problem...
Just another kestion, do i need to free the Node i deleted??? And if so, can i still return it? because i tried and it does not work...
thanks again...
u need to free it .. if u dont need it any more .. after processing ..
show ur code and we will tell why and what is not working