Psst.. new poll here.
[email protected] webmail now available. Want one? Go here.
Cannot use outlook/hotmail/live here to register as they blocking our mail servers. #microsoftdeez
Obey the Epel!
Paste
Pasted as C# by Alex Dunn ( 13 years ago )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace LinkedStoryList
{
class StoryList : IList
{
// Attributes
private LinkedNode headNode;
private LinkedNode tailNode;
private int count;
#region IList Members
public void Add(string data)
{
// Make the new node
LinkedNode newNode = new LinkedNode(data);
// Check the head node
if (headNode == null)
{
headNode = newNode;
tailNode = newNode;
}
else
{
// Link to the previous node
newNode.SetPrevious(tailNode);
// Add a new thing to the end of the list
tailNode.SetNext(newNode);
tailNode = newNode;
}
// Increment the count
count++;
}
public void Add(string data, int index)
{
// Make the new node
LinkedNode newNode = new LinkedNode(data);
// Check for a valid index
if (index < 0 || index > count - 1)
{
// Tell the user about faulty input
Console.WriteLine("You entered a faulty spot in the list! Added to the end.");
// Add data to the end
Add(data);
}
// Create a temporary search node to get to the right spot in the list
LinkedNode searchNode = headNode;
// Loop through to get to the right spot
for (int i = 0; i < index; i++)
{
searchNode = searchNode.GetNext();
}
// Link everything
searchNode.SetPrevious(newNode);
newNode.SetNext(searchNode);
searchNode = searchNode.GetPrevious();
newNode.SetPrevious(searchNode);
searchNode.SetNext(newNode);
// Update the count
count++;
}
public string Remove(int index)
{
if (index < 0 || index > count)
{
return null;
}
else if (index == 0)
{
String oldData = headNode.GetData();
headNode = headNode.GetNext();
headNode.SetPrevious(null);
count--;
return oldData;
}
else if (index == count)
{
String oldData = tailNode.GetData();
tailNode = tailNode.GetPrevious();
tailNode.SetNext(null);
count--;
return oldData;
}
else
{
LinkedNode searchNode = headNode;
for (int i = 0; i < index; i++)
{
searchNode = searchNode.GetNext();
}
LinkedNode previousNode = searchNode.GetPrevious();
LinkedNode nextNode = searchNode.GetNext();
previousNode.SetNext(nextNode);
nextNode.SetPrevious(previousNode);
count--;
return searchNode.GetData();
}
}
public string GetElement(int index)
{
if (index < 0 || index > count)
{
return null;
}
else
{
LinkedNode searchNode = headNode;
for (int i = 0; i < index; i++)
{
searchNode = searchNode.GetNext();
}
return searchNode.GetData();
}
}
public int Count
{
get { return count; }
}
public bool IsEmpty()
{
if(headNode.GetData() == null)
{
return true;
}
else
{
return false;
}
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinkedStoryList
{
/// <summary>
/// Represents one node in a Linked List
/// </summary>
public class LinkedNode
{
// Attributes
private String data; // The data in this linked node
private LinkedNode next; // The next node in the list;
private LinkedNode previous; // The previous node in the list;
/// <summary>
/// Default constructor for a Linked Node
/// </summary>
public LinkedNode()
: this(null, null, null)
{
}
/// <summary>
/// Creates a new Linked Node with data
/// </summary>
/// <param name="data">The data to hold in this node</param>
public LinkedNode(String data)
: this(data, null, null)
{
}
/// <summary>
/// Creates a new Linked Node with data that also
/// knows about other nodes in the chain
/// </summary>
/// <param name="data">The data to store in this node</param>
/// <param name="next">The next node in the chain</param>
/// <param name="previous">The previous node in the chain</param>
public LinkedNode(String data, LinkedNode next, LinkedNode previous)
{
this.data = data;
this.next = next;
this.previous = previous;
}
/// <summary>
/// Gets the next node in the chain
/// </summary>
/// <returns>The next node in the chain</returns>
public LinkedNode GetNext()
{
return next;
}
/// <summary>
/// Gets the previous node in the chain
/// </summary>
/// <returns>The previous node in the chain</returns>
public LinkedNode GetPrevious()
{
return previous;
}
/// <summary>
/// Gets the data stored in this node
/// </summary>
/// <returns>The data stored in this node</returns>
public String GetData()
{
return data;
}
/// <summary>
/// Sets the link to the next node
/// </summary>
/// <param name="next">The new next node</param>
public void SetNext(LinkedNode next)
{
this.next = next;
}
/// <summary>
/// Sets the link to the previous node
/// </summary>
/// <param name="previous">The new previous node</param>
public void SetPrevious(LinkedNode previous)
{
this.previous = previous;
}
/// <summary>
/// Sets the data for this node
/// </summary>
/// <param name="data">The new data for this node</param>
public void SetData(String data)
{
this.data = data;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinkedStoryList
{
class Program
{
static void Main(string[] args)
{
StoryList test = new StoryList();
test.Add("1");
test.Add("3");
test.Add("2", 1);
Console.WriteLine(test.GetElement(0));
Console.WriteLine(test.GetElement(1));
Console.WriteLine(test.GetElement(2));
}
}
}
Revise this Paste