Advertisement
Hazem3529

Untitled

Dec 26th, 2015
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //using EL_Shorouk_Academy;
  7. namespace ConsoleApplication12
  8. {
  9.     public class Genericlist<T>
  10.     {
  11.         private class node
  12.         {
  13.             private T data;
  14.             private node next;
  15.  
  16.             public node(T t)
  17.             {
  18.                 next = null;
  19.                 data = t;
  20.             }
  21.             public node Next
  22.             {
  23.                 get { return next; }
  24.                 set { next = value; }
  25.             }
  26.             public T Data
  27.             {
  28.                 get { return data; }
  29.                 set { data = value; }
  30.             }
  31.         }
  32.  
  33.         node start;
  34.         public void Add(T value)
  35.         {
  36.             node n = new node(value);
  37.             if (start == null)
  38.             {
  39.                 start = n;
  40.             }
  41.             else
  42.             {
  43.                 node q = start;
  44.                 while (q.Next != null)
  45.                 {
  46.                     q = q.Next;
  47.                 }
  48.                 q.Next = n;
  49.                 n.Next = null;
  50.             }
  51.         }
  52.         public void Return()
  53.         {
  54.             Console.WriteLine(start.Data);
  55.         }
  56.  
  57.         public void Remove(T value)
  58.         {
  59.             if (start == null)
  60.             {
  61.                 Console.WriteLine("list is empty");
  62.             }
  63.  
  64.             if (start.Data == null)
  65.             {
  66.                 Console.WriteLine("data {0}not found", value);
  67.                 return;
  68.             }
  69.             if (Comparer<T>.Default.Compare(value, start.Data) >= 0)
  70.             {
  71.                 start = start.Next;
  72.                 return;
  73.             }
  74.             node q = start;
  75.             while (q.Next.Next != null)
  76.             {
  77.                 if (Comparer<T>.Default.Compare(value, q.Next.Data) >= 0)
  78.                 {
  79.  
  80.                     q.Next = q.Next.Next;
  81.                     return;
  82.                 }
  83.                 q = q.Next;
  84.             }
  85.             if (Comparer<T>.Default.Compare(value, q.Next.Data) >= 0)
  86.             {
  87.                 q.Next = null;
  88.                 return;
  89.             }
  90.             Console.WriteLine("element{0} not found ", value);
  91.  
  92.         }
  93.         public void Count()
  94.         {
  95.             node q = start;
  96.             int count = 0;
  97.             if (q == null)
  98.             {
  99.                 Console.WriteLine("list is empty");
  100.                 return;
  101.             }
  102.             while (q != null)
  103.             {
  104.                 count++;
  105.                 q = q.Next;
  106.  
  107.             }
  108.             Console.WriteLine("number of nodes are {0}", count);
  109.  
  110.  
  111.         }
  112.         public void Clear()
  113.         {
  114.             start = null;
  115.         }
  116.         public void display ()
  117.         {
  118.             node q = start;
  119.             while (q!=null)
  120.             {
  121.                 Console.WriteLine(q.Data);
  122.                 q = q.Next;
  123.             }
  124.         }
  125.  
  126.  
  127.     }
  128.    
  129.     class Program
  130.     {
  131.         static void Main(string[] args)
  132.         {
  133.             Genericlist<int> list = new Genericlist<int>();
  134.             list.Add(2);
  135.             list.Add(3);
  136.             list.Add(4);
  137.             list.Add(5);
  138.             list.Clear();
  139.             list.Add(2);
  140.             list.Add(3);
  141.             list.Return();
  142.             list.Remove(4);
  143.             list.Remove(10);
  144.             list.display();
  145.            
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement