fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5.  
  6.  
  7. class LinkedList
  8. {
  9. struct Node
  10. {
  11. int item;
  12. Node*next;
  13. };
  14. Node*first;
  15. Node*last;
  16. int length;
  17. public:
  18. LinkedList()
  19. {
  20. first =last=NULL;
  21. length=0;
  22. }
  23.  
  24. bool IsEmpty()
  25. {
  26. return length==0 ;
  27. }
  28.  
  29. void InsertFirst (int element)
  30. {
  31. Node*newNode=new Node;
  32. newNode->item=element;
  33. if (IsEmpty())
  34. {
  35. first=last=newNode;
  36. newNode->next =NULL;
  37. }
  38. else
  39. {
  40. newNode->next =first;
  41. first=newNode;
  42. }
  43. length++;
  44. }
  45.  
  46.  
  47. void InsertLast(int element)
  48. {
  49. Node*newNode=new Node;
  50. newNode->item=element;
  51. if (IsEmpty())
  52. {
  53. first=last=newNode;
  54. newNode->next =NULL;
  55. }
  56. else
  57. {
  58. last->next=newNode;
  59. newNode->next=NULL;
  60. }
  61. length++;
  62. }
  63.  
  64. void insertAtpos(int pos, int element)
  65. {
  66. if (pos<0||pos>length)
  67. {
  68. cout << "Invalid position Out of range"<<endl;
  69. }
  70. else
  71. {
  72. Node*newNode=new Node;
  73. newNode->item =element;
  74.  
  75. if (pos==0)
  76. {
  77. InsertFirst(element);
  78. }
  79. else if (pos==length)
  80. {
  81. InsertLast(element);
  82. }
  83. else
  84. {
  85. Node*current = first;
  86. for (int i = 0; i < pos - 1; i++)
  87. {
  88. current = current->next;
  89. }
  90. newNode->next = current->next;
  91. current->next = newNode;
  92. }
  93. length++;
  94.  
  95. }
  96.  
  97. }
  98.  
  99.  
  100. int removeAtFirst()
  101. {
  102. if (IsEmpty())
  103. {
  104. cout << "List is empty." << endl;
  105. }
  106. else if (length==1)
  107. {
  108. delete first;
  109. first=last=NULL;
  110. length--;
  111. }
  112. else
  113. {
  114. Node*curr =first->next;
  115. Node*prev=first;
  116. while (curr!=last)
  117. {
  118. prev=curr;
  119. curr=curr->next;
  120. }
  121. delete curr;
  122. prev->next =NULL;
  123. last=prev;
  124. length--;
  125.  
  126. }
  127. }
  128.  
  129.  
  130. void removeAtLast()
  131. {
  132.  
  133. if (length==0)
  134. {
  135. return;
  136. }
  137. else if (length==1)
  138. {
  139. delete first;
  140. last=first=NULL;
  141. length--;
  142. }
  143.  
  144. else
  145. {
  146. Node*curr=first->next;
  147. Node*prev=first ;
  148. while (curr != last)
  149. {
  150. prev=curr;
  151. curr=curr->next;
  152. }
  153. delete curr;
  154. prev->next=NULL;
  155. last=prev;
  156. length --;
  157.  
  158. }
  159.  
  160. }
  161.  
  162.  
  163. void removeAtPos(int pos)
  164. {
  165. if (pos < 0 || pos >= length)
  166. {
  167. cout << "Invalid position. Out of range." << endl;
  168. return;
  169. }
  170.  
  171. if (pos == 0)
  172. {
  173. removeAtFirst();
  174. return;
  175. }
  176.  
  177. Node* current = first;
  178. for (int i = 0; i < pos - 1; i++)
  179. {
  180. current = current->next;
  181. }
  182.  
  183. Node* temp = current->next;
  184. current->next = temp->next;
  185.  
  186. if (temp == last)
  187. {
  188. last = current;
  189. }
  190.  
  191. delete temp;
  192. length--;
  193. }
  194.  
  195. bool search(int element)
  196. {
  197. Node* current = first;
  198. while (current != NULL)
  199. {
  200. if (current->item == element)
  201. {
  202. cout << "Element found in the list." << endl;
  203. return true;
  204. }
  205. current = current->next;
  206. }
  207. cout << "Element not found in the list." << endl;
  208. return false;
  209. }
  210.  
  211. void Display()
  212. {
  213. Node* current = first;
  214. while (current!= NULL) // Keep looping until we reach the end of the list
  215. {
  216. cout << current->item << " ";
  217. current = current->next;
  218. }
  219. cout << endl;
  220. }
  221.  
  222.  
  223.  
  224. };
  225.  
  226. int main(int argc, char const *argv[])
  227. {
  228. LinkedList ibrahem;
  229. ibrahem.InsertFirst(15);
  230. ibrahem.InsertFirst(20);
  231. ibrahem.InsertLast(25);
  232. ibrahem.insertAtpos(1,100);
  233. ibrahem.removeAtLast();
  234. ibrahem.search(100);
  235. ibrahem.Display();
  236.  
  237. return 0;
  238. }
  239.  
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
Element found in the list.
20 100