User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jun 2006
Posts: 1
Reputation: laki is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
laki laki is offline Offline
Newbie Poster

Help reverse the LL using recurtion in C#

  #1  
Jun 28th, 2006
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 :
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2006
Posts: 219
Reputation: Lord Soth is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 3
Lord Soth's Avatar
Lord Soth Lord Soth is offline Offline
Posting Whiz in Training

Re: reverse the LL using recurtion in C#

  #2  
Jul 8th, 2006
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
if (NewL.NextItem != null)
{
/* assign the return value to something here =*/ RevLL(NewL.NextItem);
}
To what the assignement should be made is left as an exercise to the reader.

Loren Soth
Crimson K. Software _________________________________________________________________ Crimson K. Blog
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 3:35 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC