Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. // my first linkedlist
  6.  
  7. LinkedList MyList = new LinkedList();
  8. MyList.InsertFirst(100);
  9. MyList.InsertFirst(55);
  10. MyList.InsertFirst(75);
  11. MyList.DisplayList();
  12. Console.WriteLine("Insert last ");
  13. MyList.InsertLast(800);
  14. MyList.DisplayList();
  15. Console.WriteLine(" to delete first one ");
  16. MyList.DeleteFirst();
  17. MyList.DisplayList();
  18. Console.ReadLine();
  19.  
  20. }
  21. }
  22. public class LinkedList
  23. {
  24. private Node First;
  25. public bool IsEmpty()
  26. {
  27. return (First==null);
  28. }
  29. public void InsertFirst(int data)
  30. {
  31. Node newnode = new Node();
  32. newnode.data = data;
  33. newnode.next = First;
  34. First = newnode;
  35. }
  36. public Node DeleteFirst()
  37. {
  38. Node temp = First;
  39. First = First.next;
  40. return temp;
  41. }
  42.  
  43. public void DisplayList()
  44. {
  45. Console.WriteLine("from first to last");
  46. Node current = First;
  47. while (current != null)
  48. {
  49. current.DisplayNode();
  50. current = current.next;
  51. }
  52. Console.WriteLine();
  53. }
  54.  
  55. public void InsertLast(int data)
  56. {
  57. Node current = First;
  58. while (current.next != null)
  59. {
  60. current = current.next;
  61.  
  62. }
  63. Node newnode = new Node();
  64. newnode.data = data;
  65. current.next = newnode;
  66.  
  67. }
  68. }
  69. public class Node
  70. {
  71. public int data;
  72. public Node next;
  73. public void DisplayNode()
  74. {
  75. Console.WriteLine("<" + data + " >");
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement