Skip to content

Instantly share code, notes, and snippets.

Created February 3, 2013 17:09
Show Gist options
  • Save anonymous/4702627 to your computer and use it in GitHub Desktop.
Save anonymous/4702627 to your computer and use it in GitHub Desktop.
public class NodeContent
{
public NodeContent(int n)
{
this.Contenuto = n;
}
public int Contenuto { get; set; }
}
class DoubleLinkedListNode
{
public DoubleLinkedListNode Next { get; set; }
public NodeContent Content { get; set; }
public DoubleLinkedListNode Prev { get; set; }
}
class DoubleLinkedList
{
public DoubleLinkedList(NodeContent n, NodeContent m)
{
Head = new DoubleLinkedListNode();
DoubleLinkedListNode appo = new DoubleLinkedListNode();
Head.Content = n;
Head.Next = appo;
Head.Prev = null;
appo.Content = m;
appo.Next = null;
appo.Prev = Head;
}
public void add_Qb(NodeContent n)
{
DoubleLinkedListNode newnodo;
DoubleLinkedListNode temp=new DoubleLinkedListNode();
temp = Head;
newnodo=Head.Next;
while (newnodo.Next != null)
{
newnodo = newnodo.Next;
temp=temp.Next;
}
temp=temp.Next;
newnodo.Next = new DoubleLinkedListNode();
newnodo.Next.Content = n;
newnodo.Next.Prev = temp;
newnodo.Next.Next = null;
}
public void add_T(NodeContent n)
{
DoubleLinkedListNode newnodo = new DoubleLinkedListNode();
newnodo.Prev = null;
newnodo.Next = Head;
newnodo.Content = n;
Head = newnodo;
//DoubleLinkedListNode appo = new DoubleLinkedListNode();
//appo.Next = Head;
//appo.Content = n;
//Head.Next = appo;
//Head.Prev = null;
//appo.Prev = Head;
}
public void print_r()
{
DoubleLinkedListNode appo= new DoubleLinkedListNode();
appo = Head;
while (appo.Next != null)
appo = appo.Next;
while (appo.Prev != null)
{
Console.Write(appo.Content.Contenuto + "->");
appo = appo.Prev;
}
Console.WriteLine(appo.Content.Contenuto);
}
public void print()
{
DoubleLinkedListNode appo = new DoubleLinkedListNode();
appo = Head;
while (appo.Next != null)
{
Console.Write(appo.Content.Contenuto + "->");
appo = appo.Next;
}
Console.WriteLine(appo.Content.Contenuto);
}
private DoubleLinkedListNode Head { get; set; }
}
class Program
{
static void Main(string[] args)
{
LinkedList l = new LinkedList(new NodeContent(1));
DoubleLinkedList m = new DoubleLinkedList(new NodeContent(1), new NodeContent(2));
//l.Add_Q(new NodeContent(2));
//l.Add_Q(new NodeContent(3));
//l.Add_Q(new NodeContent(4));
//l.Add_Q(new NodeContent(5));
//l.print();
//Console.WriteLine("-----senza aggiunta-----");
//l.add(new NodeContent(1000), 2);
//l.print();
//Console.WriteLine("-----aggiunta in mezzo----");
//l.cancella(3);
//l.print();
//Console.WriteLine("-----cancella-----");
Console.WriteLine("------multilinkata-----");
m.add_Qb(new NodeContent(3));
m.add_Qb(new NodeContent(4));
m.add_Qb(new NodeContent(5));
m.print();
Console.WriteLine("----multilinkata reverse----");
m.print_r();
Console.WriteLine("-----Inserimento in testa-----");
m.add_T(new NodeContent(0));
m.print();
m.print_r();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment