Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LinkedList : IList
- {
- private Node head, tail, current;
- private int count;
- public int Count
- {
- get { return count; }
- set { count = value; }
- }
- public object First
- {
- get
- {
- try
- {
- return head.Data;
- }
- catch (NullReferenceException)
- {
- Console.Write("List is Empty");
- return (-32767);
- }
- }
- }
- public object Last
- {
- get
- {
- try
- {
- return tail.Data;
- }
- catch
- {
- Console.Write("List is Empty");
- return (-32767);
- }
- }
- }
- public bool IsEmpty()
- {
- return (head == null && tail == null);
- }
- public void Clear()
- {
- this.head = null;
- tail = null;
- current = null;
- this.count = 0;
- }
- public void Insert(object item)
- {
- Node NeoNude = new Node(item, null);
- count++;
- if (IsEmpty())
- {
- head = NeoNude;
- }
- else
- {
- tail.Next = NeoNude;
- }
- tail = NeoNude;
- }
- public void InsertAt(object item, int pos)
- {
- if (!IsEmpty())
- {
- Node NeoNude = new Node(item, null);
- if (pos == 0) // if index is head
- {
- NeoNude.Next = head;
- head = NeoNude;
- }
- else if (pos <= count - 1) // if index is in middle
- {
- current = head;
- Node temp = null;
- for (int i = 0; i < pos; i++)
- {
- temp = current;
- current = current.Next;
- }
- NeoNude.Next = current;
- temp.Next = NeoNude;
- }
- else if (pos == count - 1)// if index is last
- Insert(item);
- else
- {
- Console.WriteLine("FAILED");
- return;
- }
- Count++;
- }
- }
- public void DisplayList()
- {
- string list = "";
- current = head;
- while (current != null)
- {
- list = list + current.Data + " ";
- current = current.Next;
- }
- Console.Write(list);
- }
- public bool Find(object item)
- {
- current = head;
- //bool flag = false;
- if (!IsEmpty())
- {
- while (current != null)
- {
- if (item.Equals(current.Data))
- {
- return true;
- }
- else
- {
- current = current.Next;
- }
- }
- }
- return false;
- }
- public void Remove(object item)
- {
- if (!IsEmpty())
- {
- current = head;
- while(current != null)
- {
- if (head.Data.Equals(item))
- {
- head = current.Next;
- current.Next = null;
- break;
- }
- Node temp = current.Next;
- if (temp.Data.Equals(item))
- {
- if (temp.Next == null)
- {
- tail = current;
- current.Next = temp.Next;
- }
- else if (temp.Next != null)
- {
- current.Next = temp.Next;
- }
- break;
- }
- current = current.Next;
- }
- count--;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement