?
Solved

return problem...

Posted on 2003-03-09
5
Medium Priority
?
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

764 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