public void Traverse(SingleList node) { if (node == null) { node = this; } while (node != null) { Console.WriteLine(node.data); node = node.next; } }
public int DeleteNext() { if (next == null) { return(0); } else { SingleList node = this.next; this.next = this.next.next; node = null; return(1); } }
public SingleList InsertNext(int value) { SingleList node = new SingleList(value); if (this.next == null) { this.next = node; node.next = null; } else { SingleList temp = this.next; node.next = temp; this.next = node; } return(node); }
static void Main(string[] args) { SingleList n1 = new SingleList(1); SingleList n2 = n1.InsertNext(2); SingleList n3 = n2.InsertNext(3); SingleList n4 = n3.InsertNext(4); Console.WriteLine("Traverse"); n1.Traverse(n1); Console.WriteLine("Deleting node 2"); n1.DeleteNext(); Console.WriteLine("Traverse"); n1.Traverse(null); GroceryList i1 = new GroceryList("item1"); GroceryList i2 = i1.InsertNext("item2"); GroceryList i3 = i2.InsertNext("item3"); GroceryList i4 = i3.InsertNext("item4"); Console.WriteLine("Traverse"); i1.Traverse(i1); Console.WriteLine("Deleting node 2"); i1.DeleteNext(); Console.WriteLine("Traverse"); i1.Traverse(null); DList dl = new DList(); for (int i = 0; i < 10; i++) { Console.WriteLine("Enter Number " + (i + 1) + " "); dl.insert(Convert.ToInt32(Console.ReadLine())); } Console.WriteLine(); dl.print(); dl.Delete(Convert.ToInt32(Console.ReadLine())); dl.print(); }
static void Main(string[] args) { SingleList list = new SingleList(); Console.Write("Enter the number of nodes to be inserted : "); int n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { Console.Write("Enter element {0} : ", i); int data = int.Parse(Console.ReadLine()); Console.WriteLine("Select :\n1.Insert in the Beginning 2.Insert at the end \n3.Insert After Specific Node 4.Insert Before Specific Node"); int choice = int.Parse(Console.ReadLine()); switch (choice) { case 1: list.InsertInBeginning(data); break; case 2: list.InsertAtEnd(data); break; case 3: Console.Write("Enter Node to insert after it : "); int x = int.Parse(Console.ReadLine()); list.InsertAfter(data, x); break; case 4: Console.Write("Enter Node to insert before it : "); x = int.Parse(Console.ReadLine()); list.InsertBefore(data, x); break; } Console.WriteLine(); } Console.Write("List is : "); list.Traverse(); Console.WriteLine(); Console.WriteLine("\nDo you want to delete a node? : 1.Yes 2.No"); int wann = int.Parse(Console.ReadLine()); switch (wann) { case 1: goto Here; case 2: Console.WriteLine("\nThanks, See u later"); goto Bye; } Here: Console.WriteLine("Select :\n1.Delete First Node 2.Delete Last Node 3.Delete Specific Node"); int choice2 = int.Parse(Console.ReadLine()); switch (choice2) { case 1: list.DeleteFirstNode(); break; case 2: list.DeleteLastNode(); break; case 3: Console.Write("Enter Node u wanna to delete : "); int x = int.Parse(Console.ReadLine()); list.DeleteSpecialNode(x); break; } Console.Write("New List is : "); list.Traverse(); Bye: Console.ReadKey(); }
public SingleList(int i) { data = i; next = null; }
public SingleList() { data = 0; next = null; }