Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- //using EL_Shorouk_Academy;
- namespace ConsoleApplication12
- {
- public class Genericlist<T>
- {
- private class node
- {
- private T data;
- private node next;
- public node(T t)
- {
- next = null;
- data = t;
- }
- public node Next
- {
- get { return next; }
- set { next = value; }
- }
- public T Data
- {
- get { return data; }
- set { data = value; }
- }
- }
- node start;
- public void Add(T value)
- {
- node n = new node(value);
- if (start == null)
- {
- start = n;
- }
- else
- {
- node q = start;
- while (q.Next != null)
- {
- q = q.Next;
- }
- q.Next = n;
- n.Next = null;
- }
- }
- public void Return()
- {
- Console.WriteLine(start.Data);
- }
- public void Remove(T value)
- {
- if (start == null)
- {
- Console.WriteLine("list is empty");
- }
- if (start.Data == null)
- {
- Console.WriteLine("data {0}not found", value);
- return;
- }
- if (Comparer<T>.Default.Compare(value, start.Data) >= 0)
- {
- start = start.Next;
- return;
- }
- node q = start;
- while (q.Next.Next != null)
- {
- if (Comparer<T>.Default.Compare(value, q.Next.Data) >= 0)
- {
- q.Next = q.Next.Next;
- return;
- }
- q = q.Next;
- }
- if (Comparer<T>.Default.Compare(value, q.Next.Data) >= 0)
- {
- q.Next = null;
- return;
- }
- Console.WriteLine("element{0} not found ", value);
- }
- public void Count()
- {
- node q = start;
- int count = 0;
- if (q == null)
- {
- Console.WriteLine("list is empty");
- return;
- }
- while (q != null)
- {
- count++;
- q = q.Next;
- }
- Console.WriteLine("number of nodes are {0}", count);
- }
- public void Clear()
- {
- start = null;
- }
- public void display ()
- {
- node q = start;
- while (q!=null)
- {
- Console.WriteLine(q.Data);
- q = q.Next;
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Genericlist<int> list = new Genericlist<int>();
- list.Add(2);
- list.Add(3);
- list.Add(4);
- list.Add(5);
- list.Clear();
- list.Add(2);
- list.Add(3);
- list.Return();
- list.Remove(4);
- list.Remove(10);
- list.display();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement