Splux

C# linked list templated

Sep 13th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. /*
  2.  
  3. :)
  4.  
  5. Author: Splux
  6.  
  7. */
  8.  
  9. using System;
  10.  
  11. class node <T>
  12. {
  13.     //methods
  14.     public node()
  15.     {
  16.         next = null;
  17.     }
  18.     public node(T data)
  19.     {
  20.         next = null;
  21.         _data = data;
  22.     }
  23.     public T getData()
  24.     {
  25.         return _data;
  26.     }
  27.     //variables
  28.     public node<T> next;
  29.     private T _data;
  30. }
  31.  
  32. class list <T>
  33. {
  34.     //methods
  35.     public list() // just null root
  36.     {
  37.         root = null;
  38.     }
  39.     public list(T data)//create first node ( the root ) with data
  40.     {
  41.         root = new node<T>(data);
  42.     }
  43.    
  44.     public void add(T data)
  45.     {
  46.         if (root == null)
  47.         {
  48.             root = new node<T>(data);
  49.             return;
  50.         }
  51.         node<T> temp = root;
  52.         while(temp.next != null) // loop to last node and add one more
  53.         {
  54.             temp = temp.next;
  55.         }
  56.         temp.next = new node<T>(data);
  57.     }
  58.     public void delete()
  59.     {
  60.         if (root == null) // no point to go any further...
  61.             return;
  62.         else if(root.next == null) // no point in looping to find the last node
  63.         {
  64.             root = null;
  65.             return;
  66.         }
  67.         else
  68.         {
  69.             node<T> temp = root;
  70.             //loop through the whole list and find the last value
  71.             while (temp.next.next != null)  // since I already check for root.next I can go through the whole list and check for temp.next.next
  72.                                             // without screwing things up
  73.                 temp = temp.next;
  74.             temp.next = null; // and then null the last object and let the system clean it up
  75.         }
  76.     }
  77.     public void print_all()
  78.     {
  79.         node<T> temp = root;
  80.         // loop through and print out value of everything in the list that is not null
  81.         while(temp != null)
  82.         {
  83.             Console.WriteLine("{0}", temp.getData());
  84.             temp = temp.next;
  85.         }
  86.     }
  87.     //variables
  88.     private node<T> root;
  89. }
  90.  
  91. class app
  92. {
  93.     public static void Main()
  94.     {
  95.         list<int> enLista = new list<int>(3);
  96.         enLista.add(4);
  97.         enLista.add(6);
  98.         enLista.print_all();
  99.         enLista.delete();
  100.         enLista.print_all();
  101.  
  102.         list<char> chlist = new list<char>();
  103.         chlist.add('d');
  104.         chlist.add('r');
  105.         chlist.print_all();
  106.         chlist.delete();
  107.         chlist.print_all();
  108.         chlist.add('e');
  109.         chlist.print_all();
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment