Advertisement
Lirbo

C# Stack

Apr 23rd, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Assignment
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. Stack stk = new Stack();
  10. stk.Pop();
  11. stk.Peek();
  12. stk.PrintStack();
  13.  
  14.  
  15. for (int i = 0; i < stk.GetCustomer().Length; i++)
  16. {
  17. Customer c = new Customer();
  18. stk.Push(c);
  19. }
  20.  
  21. stk.PrintStack();
  22.  
  23.  
  24.  
  25. Customer t = new Customer();
  26. stk.Push(t);
  27.  
  28.  
  29.  
  30. for (int i = 0; i < 4; i++)
  31. stk.Pop();
  32.  
  33.  
  34. Console.WriteLine("Peek");
  35. stk.Peek();
  36.  
  37.  
  38. stk.Pop();
  39. stk.Peek();
  40.  
  41. }
  42. }
  43. }
  44.  
  45. -------------------------------------------------------------------------------------
  46.  
  47. using System;
  48. namespace Assignment
  49. {
  50. class Customer
  51. {
  52.  
  53.  
  54. private string FirstName, LastName;
  55.  
  56. public Customer()
  57. {
  58. Console.Write("Enter first name: ");
  59. FirstName = Console.ReadLine();
  60. Console.Write("Enter surname: ");
  61. LastName = Console.ReadLine();
  62. }
  63.  
  64.  
  65. public void SetFirstName(string firstName) { this.FirstName = firstName; }
  66. public void SetLastName(string lastName) { this.LastName = lastName; }
  67.  
  68.  
  69. public string GetFirstName() { return FirstName; }
  70. public string GetLastName() { return LastName; }
  71.  
  72. public void PrintCustomer(int index)
  73. {
  74. Console.WriteLine($"Customer {index}: {FirstName} {LastName}");
  75. }
  76. }
  77. }
  78.  
  79. --------------------------------------------------------------------------------------
  80.  
  81. using System;
  82. namespace Assignment
  83. {
  84.  
  85. class Stack
  86. {
  87.  
  88. private int pointer;
  89. private Customer[] customers;
  90.  
  91.  
  92. public Stack()
  93. {
  94. Console.Write("Enter clients amount: ");
  95. int cAm = int.Parse(Console.ReadLine());
  96. customers = new Customer[cAm];
  97. pointer = 0;
  98. }
  99.  
  100.  
  101. public void SetPointer(int pointer) { this.pointer = pointer; }
  102.  
  103.  
  104. public Customer[] GetCustomer() { return customers; }
  105.  
  106. public int GetPointer() { return pointer; }
  107.  
  108.  
  109.  
  110.  
  111. public bool IsFull()
  112. {
  113. if (pointer >= customers.Length) return true;
  114. else return false;
  115. }
  116.  
  117.  
  118. public bool IsEmpty()
  119. {
  120. if (pointer == 0) return true;
  121. else return false;
  122. }
  123.  
  124.  
  125. public void Push(Customer c)
  126. {
  127. if (IsEmpty() == true)
  128. Console.WriteLine("Can't push, no place.");
  129. else
  130. {
  131. customers[pointer] = c;
  132. pointer++;
  133. }
  134. }
  135.  
  136.  
  137. public Customer Pop()
  138. {
  139. if (IsEmpty() == true)
  140. {
  141. Console.WriteLine("Stack's empty.");
  142. return null;
  143. }
  144. else
  145. {
  146. Console.Write("Popping > ");
  147. customers[pointer - 1].PrintCustomer(pointer - 1);
  148. customers[pointer - 1].SetFirstName("");
  149. customers[pointer - 1].SetLastName("");
  150. pointer--;
  151. return null;
  152. }
  153.  
  154.  
  155. }
  156.  
  157.  
  158. public Customer Peek()
  159. {
  160. if (IsEmpty()==true)
  161. {
  162. Console.WriteLine("Stack's empty.");
  163. return null;
  164. }
  165. Console.WriteLine($"Last Customer: ");
  166. customers[pointer - 1].PrintCustomer(pointer - 1);
  167. return null;
  168. }
  169.  
  170.  
  171. public void PrintStack()
  172. {
  173. if (IsEmpty() == true)
  174. Console.WriteLine("Stack's empty.");
  175. else
  176. {
  177. for (int i = 0; i < pointer; i++)
  178. {
  179. customers[i].PrintCustomer(i);
  180. }
  181. }
  182.  
  183.  
  184. }
  185. }
  186.  
  187. }
  188.  
  189.  
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement