public void DeleteLast(SingleLinkedList linkedList) { SNode SecondLastNode = GetSecondLastNode(linkedList); SecondLastNode.Next = null; }
static void Main(string[] args) { int choice, data, a, b; bool state = true; SingleLinkedList list = new SingleLinkedList(); list.CreateList(); while (state) { Console.WriteLine("Select which algorithm you want to run :-)"); Console.WriteLine("1. Display List"); Console.WriteLine("2. Show the number of nodes in list"); Console.WriteLine("3. Search for an element"); Console.WriteLine("4. Insert in the begining/empty list"); Console.WriteLine("5. Insert a node at the end of the list"); Console.WriteLine("6. Insert a node after a specified node"); Console.WriteLine("7. Insert a node before a specified node"); Console.WriteLine("8. Insert a node in a given position"); Console.WriteLine("9. Delete first node"); Console.WriteLine("10. Delete last node"); Console.WriteLine("11. Delete a node"); Console.WriteLine("12. Reverse the list"); Console.WriteLine("Enter your choice"); choice = Convert.ToInt32(Console.ReadLine()); if (choice == 19) { break; } switch (choice) { case 1: list.DisplayList(); break; case 2: list.CountNodes(); break; case 3: Console.WriteLine("Enter the element to search for:"); data = Convert.ToInt32(Console.ReadLine()); list.SearchList(data); break; case 4: Console.WriteLine("Enter the element to insert to the list:"); data = Convert.ToInt32(Console.ReadLine()); list.InsertAtBeginning(data); break; case 5: Console.WriteLine("Enter the element to insert to the list:"); data = Convert.ToInt32(Console.ReadLine()); list.InsertAtEnd(data); break; case 6: Console.WriteLine("Enter the element to insert to the list:"); data = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the node[element] you want to insert it after:"); a = Convert.ToInt32(Console.ReadLine()); list.InsertAfterNode(data, a); break; case 7: Console.WriteLine("Enter the element to insert to the list:"); data = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the node[element] you want to insert it before:"); a = Convert.ToInt32(Console.ReadLine()); list.InsertBeforeNode(data, a); break; case 8: Console.WriteLine("Enter the element to insert to the list:"); data = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the position you want to insert it:"); a = Convert.ToInt32(Console.ReadLine()); list.InsertNodeSpecific(data, a); break; case 9: list.DeleteFirstNode(); break; case 10: list.DeleteLastNode(); break; case 11: Console.WriteLine("Enter the element you want to delete:"); data = Convert.ToInt32(Console.ReadLine()); list.DeleteNode(data); break; case 12: list.ReverseList(); break; default: Console.WriteLine("Wrong Choice"); break; } Console.WriteLine("Enter 1 to continue or 0 to quit"); try{ int stateCode = Convert.ToInt16(Console.ReadLine()); if (stateCode == 0) { state = false; } }catch (Exception) { } } Console.WriteLine("Exiting....."); Console.WriteLine("Bye :-/"); }
public SingleLinkedList(int x) { value = x; next = null; }
static void Main(string[] args) { SingleLinkedList linkedList = new SingleLinkedList(); int userInput = -1; do { userInput = DisplayMenu(); Console.Clear(); switch (userInput) { case 1: // create list int size = getNumber("Enter the number of elements you would like to add: "); for (int i = 1; i <= size; i++) { linkedList.insertAtTheEnd(getNumber("Enter value for element " + i + ": ")); } break; case 2: // display list if (linkedList.hasCycle()) { Console.WriteLine("Please remove cycle before displaying the list."); } else { linkedList.DisplayList(); } break; case 3: // add/remove at beginning of the list switch (getNumber("1) Add \n2) Remove \n3) Back \n")) { case 1: try { linkedList.insertAtTheBeginning(getNumber("Enter value to be entered: ")); Console.WriteLine("Successfully added element."); } catch (Exception ex) { Console.WriteLine("Unexpected error encountered:" + ex.Message); } break; case 2: try { linkedList.deleteFirstNode(); Console.WriteLine("Successfully removed element."); } catch (Exception ex) { Console.WriteLine("Unexpected error encountered:" + ex.Message); } break; case 3: // going back to main menu break; default: Console.WriteLine("Invalid number!"); break; } break; case 4: // add/remove at end of the list switch (getNumber("1) Add \n2) Remove \n3) Back \n")) { case 1: try { linkedList.insertAtTheEnd(getNumber("Enter value to be entered: ")); Console.WriteLine("Successfully added element."); } catch (Exception ex) { Console.WriteLine("Unexpected error encountered:" + ex.Message); } break; case 2: try { linkedList.deleteLastNode(); Console.WriteLine("Successfully removed element."); } catch (Exception ex) { Console.WriteLine("Unexpected error encountered:" + ex.Message); } break; case 3: // going back to main menu break; default: Console.WriteLine("Invalid number!"); break; } break; case 5: // add/remove nth position switch (getNumber("1) Add \n2) Remove \n3) Back \n")) { case 1: try { linkedList.insertAtIndex(getNumber("Enter position: "), getNumber("Enter value: ")); Console.WriteLine("Successfully added element."); } catch (Exception ex) { Console.WriteLine("Unexpected error encountered:" + ex.Message); } break; case 2: try { linkedList.deleteAtIndex(getNumber("Enter position: ")); Console.WriteLine("Successfully removed element."); } catch (Exception ex) { Console.WriteLine("Unexpected error encountered:" + ex.Message); } break; case 3: break; default: Console.WriteLine("Invalid number!"); break; } break; case 6: // reverse list try { linkedList.reserveList(); Console.WriteLine("Successfully reversed list."); } catch (Exception ex) { Console.WriteLine("Unexpected error encountered:" + ex.Message); } break; case 7: // bubble sort data try { linkedList.bubbleSortExData(); Console.WriteLine("Successfully sorted list by exchanging data."); } catch (Exception ex) { Console.WriteLine("Unexpected error encountered:" + ex.Message); } break; case 8: // bubble sort links try { linkedList.bubleSortExLinks(); Console.WriteLine("Successfully sorted list by exchange links."); } catch (Exception ex) { Console.WriteLine("Unexpected error encountered:" + ex.Message); } break; case 9: // insert cycle try { linkedList.insertCycle(getNumber("Enter element value: ")); Console.WriteLine("Successfully inserted cycle."); } catch (Exception ex) { Console.WriteLine("Unexpected error encountered:" + ex.Message); } break; case 10: // detect cycle try { if (linkedList.hasCycle()) { Console.WriteLine("Cycle dected."); } else { Console.WriteLine("No cycle found."); } } catch (Exception ex) { Console.WriteLine("Unexpected error encountered:" + ex.Message); } break; case 11: // remove cycle try { linkedList.removeCycle(); Console.WriteLine("\nSuccessfully removed cycle."); } catch (Exception ex) { Console.WriteLine("Unexpected error encountered:" + ex.Message); } break; default: Console.WriteLine("\nInvalid number!\n"); break; } } while (userInput != 0); }
static void Main(string[] args) { int choice, data, k, x; SingleLinkedList list = new SingleLinkedList(); list.CreateList(); while (true) { Console.WriteLine("1.Display List"); Console.WriteLine("2.Count the number of nodes"); Console.WriteLine("3.search for an element"); Console.WriteLine("4.Insert in empty list"); Console.WriteLine("5.Insert a node at the end of list "); Console.WriteLine("6.Insert a node after a specified node"); Console.WriteLine("7.Insert a node before a specified node"); Console.WriteLine("8.Insert a node at a given position"); Console.WriteLine("9.Delete first node"); Console.WriteLine("10.Delete last node"); Console.WriteLine("11.Delete any node"); Console.WriteLine("12.Reverse the last"); Console.WriteLine("13.Bubble sort by exchanging data"); Console.WriteLine("14.Merging with two lists"); Console.WriteLine("15.Merge sort"); Console.WriteLine("16.Insert cycle"); Console.WriteLine("17.delete cycle"); Console.WriteLine("18.Remove cycle"); Console.WriteLine("19.Quit"); Console.Write("Enter your choice:"); choice = Convert.ToInt32(Console.ReadLine()); if (choice == 19) { break; } switch (choice) { case 1: list.Display(); break; case 2: list.CountNode(); break; case 3: Console.Write("Enter element to be searched"); data = Convert.ToInt32(Console.ReadLine()); list.Search(data); break; case 4: Console.WriteLine("Enter Element to be inserted"); data = Convert.ToInt32(Console.ReadLine()); list.InsertAtBeginningToList(data); break; case 5: Console.WriteLine("Enter Element to be inserted"); data = Convert.ToInt32(Console.ReadLine()); list.InsertAtEnd(data);; break; case 6: Console.WriteLine("Enter Element to be inserted"); data = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter Element after which to be inserted:"); x = Convert.ToInt32(Console.ReadLine()); list.InsertAfter(data, x); break; case 7: Console.WriteLine("Enter Element to be inserted"); data = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter Element before which to be inserted:"); x = Convert.ToInt32(Console.ReadLine()); list.InsertBefore(data, x); break; case 8: Console.WriteLine("Enter Element to be inserted"); data = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter position which to be inserted:"); k = Convert.ToInt32(Console.ReadLine()); list.InsertAtPosition(data, k); break; case 9: list.DeleteFirstNode(); break; case 10: list.DeleteLastode(); break; case 11: Console.WriteLine("Enter Element to be deleted:"); data = Convert.ToInt32(Console.ReadLine()); list.deleteNode(data); break; case 12: list.ReverseList(); break; case 13: list.BubbleSort(); break; case 14: // list.BubbleSortExLinks(); SingleLinkedList list1 = new SingleLinkedList(); SingleLinkedList list2 = new SingleLinkedList(); list1.CreateList(); list2.CreateList(); list1.BubbleSort(); list2.BubbleSort(); Console.WriteLine(" First List:"); list1.Display(); Console.WriteLine(" Second List"); list2.Display(); SingleLinkedList list3; list3 = list1.Merge1(list2); break; case 15: list.MergeSort(); break; case 16: Console.WriteLine("Enter element at which cycle has to be inserted "); data = Convert.ToInt32(Console.ReadLine()); list.InsertCycle(data); break; case 17: if (list.HasCycle()) { Console.WriteLine("List has a cycle"); } else { Console.WriteLine("List does not have a cycle"); } break; case 18: list.RemoveCycle(); break; default: Console.WriteLine("Wrong choice:"); break; } Console.WriteLine(); } Console.WriteLine("Existing"); }