public void RemoveEven() { Node cur = head; Node prev = null; while (cur != null) { if (cur.data % 2 == 0) { if (prev != null) { prev.SetNext(cur.GetNext()); } else { head = cur.GetNext(); } } else { prev = cur; } cur = cur.GetNext(); } }
public object this[int index] { get { int i = 0; Node temp = new Node(); Node current = head; while (current.Next != null) { if (i == index) { temp = current; break; } i++; current = current.Next; } return temp; } set { int i = 0; Node current = head; while (current != null) { if (i == index) { current.Data = value; break; } i++; current = current.Next; } } }
//define a method to delete the elements inside of the linkedlist public void EmptyList() { while (firstNode != null) { firstNode = lastNode = null; } }
public static void Main(string[] args) { Node myNode = new Node(1); myNode.addAnObject (2); myNode.addAnObject (3); myNode.addAnObject (4); myNode.PrintTheValues (); }
public void appendToTail(int d) { Node end = new Node(d); Node n = this; while (n.next != null) { n = n.next; } n.next = end; }
private void addHelper(string data) { if( data.CompareTo(head.data) < 0 ) { head = new Node(data, head); } else { this.addToEnd(data); } }
public void remove(string data) { if( head.data.CompareTo(data) == 0) { head = head.next; } else { //removeHelper(head.next, data); } }
public void add(string data) { if(this.head == null) { head = new Node(data, null); } else { addHelper(data); } }
public void AddToEnd(int data) { if (headNode == null) { headNode = new Node(data); } else { headNode.AddToEnd(data); } }
public void AddToEnd(int data) { if( next == null) { next = new Node(data); } else { next.AddToEnd(data); } }
public void AddToEnd(int data) { if (Head == null) { Head = new Node(data); } else { Head.AddToEnd(data); } }
public void addAnObject(int ValuetobeAdded) { if (next == null) { next = new Node (ValuetobeAdded); } else { next.addAnObject (ValuetobeAdded); } }
public static void print_node(Node to_print) { Console.WriteLine("Node level 0: " + to_print.data); Node runner = to_print.next; int level = 1; while (runner != null) { Console.WriteLine("Node level " + level + ": " + runner.data); runner = runner.next; level++; } }
public void AddToBeginning(int data) { if (Head == null) { Head = new Node(data); } else { var temp = new Node(data); temp.Next = Head; Head = temp; } }
public void Add(object item) { if (head == null) { head = new Node(item); tail = head; } else { Node newNode = new Node(item, tail); tail = newNode; } count++; }
public void AddSorted(int data) { if (Head == null) { Head = new Node(data); } else if (data < Head.Value) { AddToBeginning(data); } else { Head.AddSorted(data); } }
private void LinkNode(Node aNode) { Node nodePointer; if (Head == null) { Head = aNode; // First time have head point to new item Head = aNode; return; } nodePointer = Head; while (nodePointer.NextNode != null) // FIND THE END OF THE LIST { nodePointer = nodePointer.NextNode; } nodePointer.NextNode = aNode; // APPEND NEW NODE TO THE END OF THE LIST }
public void buildlist(int n) { Node tail = null; for (int i = 1; i <= n; i++) { if (tail == null) { tail = new Node(i); head = tail; } else { tail.next = new Node(i); tail = tail.next; } } }
public static void AddElement(int value) { Node n = new Node(value); if (head == null) { head = n; } else { Node traversingPointer = head; while (traversingPointer.Next != null) { traversingPointer = traversingPointer.Next; } traversingPointer.Next = n; } }
public Node deleteNode(Node head, int d) { Node n = head; if (n.data == d) { return head.next; } while (n.next != null) { if (n.next.data == d) { n.next = n.next.next; return head; } n = n.next; } return head; }
public void Delete(int data) { if (Head == null) { Console.WriteLine("Head is empty"); return; } if (Head.Value == data) //We delete the head { if (Head.Next == null) { Head = null; return; } Head = Head.Next; } else { Head.Delete(data); } }
static void Main(string[] args) { LinkedList<int, string> ll = new LinkedList<int, string>(); ll.AddNodeFirst(0, "a"); ll.AddNodeLast(3, "e"); ll.AddNodeLast(1, "b"); ll.AddNodeLast(2, "c"); foreach (string value in ll) { Console.WriteLine(value); } Console.WriteLine("/*******************/"); var deleteNode = new Node<int, string>(1, "b", null); ll.DeleteNode(deleteNode.Key); foreach (string value in ll) { Console.WriteLine(value); } }
public object Add(object obj, int index) { if (index < 0) throw new ArgumentOutOfRangeException("Index: " + index); if (index > count) index = count; Node current = this.head; if (this.Empty || index == 0) this.head = new Node(obj, this.head); else { for (int i = 0; i < index - 1; i++) current = current.Next; current.Next = new Node(obj, current.Next); } count++; return obj; }
static void Main(string[] args) { Node seth = new Node(5); Random rand = new Random(); for(int x = 0; x < 10; x++) { seth.appendToTail(rand.Next(0, 10)); } Console.WriteLine("Before delete: "); print_node(seth); // deleting the 4th element Node deleter = seth.next.next.next; seth = deleter.deleteNode(seth, deleter.data); Console.WriteLine("After delete (data=" + deleter.data + "): "); print_node(seth); Console.ReadLine(); }
public Node(object data, Node prevNode) { this.data = data; prevNode.next = this; }
public Node(object data) { this.data = data; this.next = null; }
public LinkedList() { this.head = null; this.tail = null; this.count = 0; }
public void RemoveAt(int index) { if (index >= count || index < 0) { throw new ArgumentOutOfRangeException("Index is out of range"); } int currentIndex = 0; Node current = head; Node prev = null; while (currentIndex < index) { prev = current; current = current.Next; currentIndex++; } if (index == 0) { if (count == 1) { head = head.Next; tail = null; } else { head = head.Next; } } else if (index == count-1) { prev.Next = current.Next; tail = prev; current = null; } else { prev.Next = current.Next; } count--; }
public void InsertAt(int index, object item) { if (index > count || index < 0) throw new ArgumentOutOfRangeException("Index is out of range"); Node newNode = new Node(item); int currentIndex = 0; Node current = head; Node prev = null; while (currentIndex < index) { prev = current; current = current.Next; currentIndex++; } if(count == 0) { head = newNode; tail = head; } else if (index == 0) { var temp = head; head = newNode; head.Next = temp; } else if (index == count) { tail.Next = newNode; tail = newNode; } else { newNode.Next = prev.Next; prev.Next = newNode; } count++; }
public void setNext(Node nextNode) { next = nextNode; }
//data: value //next: next linked node public Node(int data, Node next) { this.data = data; this.next = next; }
public Node(T value, Node next) { this.Value = value; this.Next = next; }
public Node(object dataValue, Node nextNode) { Data = dataValue; Link = nextNode; }
public MyLinkedList(Node head) { Head = head; }