Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

return problem...

Posted on 2003-03-09
5
Medium Priority
?
190 Views
Last Modified: 2010-04-15
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...
0
Comment
Question by:ant01ne
  • 3
5 Comments
 
LVL 8

Accepted Solution

by:
akshayxx earned 200 total points
ID: 8100208
here is how ur stack delete would go
// well it actually shouldnt be called delete .. it is POPping out the topmost element

NODE *delete(NODE *head){

NODE * top;
top=head;
head=head->next;
top->next=NULL;

return top;
}
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8100212
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;  
}
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8100325
//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;
}
0
 

Author Comment

by:ant01ne
ID: 8101044
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...
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8101449
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question