Solved

puzzling linked lists

Posted on 1997-04-04
3
216 Views
Last Modified: 2010-04-15
here is a problem given to me by one of my friends.i have racked my brains but am unable to solve it.
  Given a pointer to the head of a singly linked list, reverse the order of the list and return a pointer to the new head?
 please provide me the solution ASAP i am very inquisitive
0
Comment
Question by:latha
[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
  • 2
3 Comments
 

Accepted Solution

by:
aleroy earned 100 total points
ID: 1249811
Here is a little program that does the thing
It use C++ syntax but should work on a C compiler without big change.

#include <stdio.h>
 
struct list {
  int one;
  list *next;
};
 
void print(list *l)
{
list *ll=l;
while (ll!=NULL)
{
  printf("%i ",ll->one);
  ll=ll->next;
}
printf("\n");
}

main()
{
list *l=NULL;
list *ll, *lll;;
int x;
scanf("%i ",&x);
while (x!=0)
{
  ll=l;
  l=new list;
  l->one=x;
  l->next=ll;
  scanf("%i",&x);
}
print(l);
 
//Here is the stuff
ll=NULL;
if (l!=NULL) lll=l->next;
while (l!=NULL)
{
  l->next=ll;
  ll=l;
  l=lll;
  if (lll!=NULL) lll=l->next;
}
l=ll;
//End of the stuff
 
print(l);
}

The whole thing stand on the use of 3 pointers :
the main pointer that follow the list
a pointer to the previous element
a pointer to the next element;

Given that, the problem is very simple.


0
 

Expert Comment

by:sanjiv040897
ID: 1249813
To latha : My code could in fact be more clear with some comment.

To sanjiv : Very funny.
You have missed the case of a empty list.
And also something worst : out of your code :
while (pCurrentNode != NULL)
{
...//The content doesn't matter
}
return pCurrentNode ;
}
I THINK pCurrentNode COULD BE EQUAL to NULL WHEN RETURNED.


In my code, change :
l   to pCurrentNode
ll  to pPrevNode
lll to pNextNode
My code has been compiled and tested.

0
 

Expert Comment

by:aleroy
ID: 1249814
Dear aleroy,

I completely agree with u about the problems u have pointed out.  I did not compile and test the program,  I don't have time to do that.  My intention was to give a quick start to latha and not to spoon-feed the solution.  I hope latha knows enough C programming to fix such problems.  


0

Featured Post

Independent Software Vendors: 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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

696 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