•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 422,993 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,928 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 1714 | Replies: 1
![]() |
•
•
Join Date: Jun 2006
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
hi
i want to reverse the LL useing recurtion in C#, i am able to print it in reverse order but unable to store it as LL in reverse order,
below is the code for it :
i want to reverse the LL useing recurtion in C#, i am able to print it in reverse order but unable to store it as LL in reverse order,
below is the code for it :
using System; using System.Collections.Generic; using System.Text; namespace linearLL { class LinkList { public string Val; public object NextItem; [STAThread] static void Main(string[] args) { LinkList obList = new LinkList(); obList.Val = "Head"; LinkList obHead = obList; LinkList tmphead = obList; // Add 10 items to the list. for (int i = 0; i < 5; i++) { obList.NextItem = new LinkList(); obList = (LinkList)obList.NextItem; obList.Val = i.ToString(); } //obList.Val = null; //obList.NextItem = null; // Print the added items. while (obHead != null) { Console.WriteLine("Item: {0}", obHead.Val); obHead = (LinkList)obHead.NextItem; } Console.WriteLine("Reversing Link List"); Console.ReadLine(); Reverse r = new Reverse(); LinkList objrev = obList; objrev= r.RevLL(tmphead.NextItem); // Print the reversed items. while (objrev != null) { Console.WriteLine("Item: {0}", objrev.Val); objrev = (LinkList) objrev.NextItem; } Console.ReadLine(); } } class Reverse { LinkList obrev = new LinkList(); public LinkList RevLL(object NewLL) { LinkList NewL = (LinkList)NewLL; if (NewL.NextItem != null) { RevLL(NewL.NextItem); } Console.WriteLine(NewL.Val.ToString()); obrev.NextItem = new LinkList(); obrev = (LinkList) obrev.NextItem; obrev.Val = NewL.Val.ToString(); return obrev; } } }
Last edited by alc6379 : Jul 3rd, 2006 at 9:33 pm.
Hi,
Your code is confusing and require some cleaning up. I suggest that you put the ststic void main in a separate class than your base classes. The class name LinkList is misleading because as far as I understand that class is implemented as a single element of a LL (you may name it linkListElement). You don't need to type parameters as Object and cast them back to LinkList class, use directly LinkListElement as NextItem and the parameter of RevLL (thus no need for NevLL to NewL conversion).
Your main problem is that you discard the value you return with "return obrev;" on
To what the assignement should be made is left as an exercise to the reader. 
Loren Soth
Your code is confusing and require some cleaning up. I suggest that you put the ststic void main in a separate class than your base classes. The class name LinkList is misleading because as far as I understand that class is implemented as a single element of a LL (you may name it linkListElement). You don't need to type parameters as Object and cast them back to LinkList class, use directly LinkListElement as NextItem and the parameter of RevLL (thus no need for NevLL to NewL conversion).
Your main problem is that you discard the value you return with "return obrev;" on
if (NewL.NextItem != null)
{
/* assign the return value to something here =*/ RevLL(NewL.NextItem);
}
Loren Soth
Crimson K. Software _________________________________________________________________ Crimson K. Blog
![]() |
•
•
•
•
•
•
•
•
DaniWeb C# Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- I plugged in my laptop with reverse polarity! any help please please please... (Cases, Fans and Power Supplies)
- how to reverse link list using recurtion (C)
- Reverse Output (Stack) (C++)
Other Threads in the C# Forum
- Previous Thread: Linking Java and C#
- Next Thread: Resolve my problem


Linear Mode