hello
i want to build a function which receives a pointer to a head of linked list
reverses the order of the list and returns a pointer to the new head.

my list is defined like this :

typedef struct item {
	
	int key ;
	
	struct item *next ;
	
} item ;

my fucction is :

void swapitem ( item *a ) {
	
 (a -> next) -> next = a  ;
 
	a -> next = NULL ;
	
} // end swapitem


  item** revList ( item *head ) {
 	
 	item **p ;
 	
 	if ( !head -> next ) {
 		
 		**p  = *head ;
 		
 		return p ;
 		
 	}
 	
 	else {
 		
 	revList (head->next) ;
 		
 	swapitem (head) ;
 	
 	return p ;
 	
}	

 }  // end revList

at first when i print the list it is ok
when i try to print the list after the function it shows nothing
please advice.

Recommended Answers

All 5 Replies

When working with linked data structures, the best thing you can do is take out a piece of paper and draw the logic. Don't draw what you think is happening, draw exactly what your code does, and you'll find the bugs.

I always take it a step farther and just tell people to forget the code. Get out the paper and crayons first, draw what must happen, then write the code to do exactly that.

Disentangling new programmers from bad code is more difficult and confusing than just going through the steps of writing the code correctly to begin with...

>Disentangling new programmers from bad code is more
>difficult and confusing than just going through the steps
>of writing the code correctly to begin with...
Writing code correctly is all well and good, but learning to debug broken code is just as important (if not more so).

Agreed, but when dealing with new programmers I have learned by experience (teaching and tutoring) that debugging works best once a firm concept of what should be happing is established. Anyway...

commented: Well said +13

ok i did it all i had to do is turn my pointer static

item *revList ( item *head ) {
 	
 	static item* p ;
 	
 	if ( !head -> next ) {
 		
 		p = head ;
 		
 		return head ;
 		
 	}
 		
 	revList (head->next) ;
 		
 	swapitem (head) ;
 	
 	return p ;	

 }  // end revList

thanks everyone for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.