fork download
  1. #include <iostream>
  2. using namespace std ;
  3. //we name it likethis becuase simply there is only one pointer
  4. //every node point to next node only
  5. //we can name simply or singly
  6.  
  7. struct node {
  8. int data;// the data in the node
  9. node *next ;//pointer point to the next element in the array
  10. node (int d , node *n=0)//this is a constructor for our class
  11. {
  12. data =d ;
  13. next = n ;
  14. }
  15. };
  16.  
  17. class list {
  18. node *head ; // pointer point to hte first elements in the array
  19. node *tail ; //point to the last elemnts in the array and it is optional to add just for simplification
  20.  
  21. public:
  22. list();
  23. bool is_empty();
  24. void print();
  25. int size();
  26. void add_begin (int el);
  27. void add_end (int el);
  28. bool add_pos (int el , int pos );//to know the postion of the elements
  29. // this function need 2 parameters what is the el and where i should put it
  30. // we put bool because he will return if he put it or not becuase to pos we put is not found
  31. void add_sorted (int el);
  32. bool delete_el(int el);//this to spicify the elements to delete
  33. bool delete_begain ();// becuase soetome the 1 elements is empty so he will return false in that cause
  34. bool delete_end();
  35. bool delete_pos(int pos);//this function will take as parameter the position of the element that we want to delete from the list
  36. // we can replace it with enumric data type
  37. bool search (int el);// we can replace it with int
  38. // ~list() ;//destructor that will be called when we close the programm
  39. void operator= (list &o);//overloading to avoid the shallow copy
  40. list (const list& o);//copy constractor to make sure that no one will change the original list
  41. };
  42.  
  43. list::list() {
  44. head = tail = 0;
  45. }
  46.  
  47. bool list::is_empty() {
  48. return head == 0;
  49. }
  50.  
  51. void list :: print(){
  52. node *tmp;// this is a pointer to point to head
  53. for (tmp=head ;tmp!=0; tmp=tmp->next) //the last one will keep the pointer jump to
  54. //the next element until it is reach the end
  55. cout << tmp->data<<"\t"<<tmp ->next << endl;
  56. }
  57.  
  58. int list::size(){
  59. node *tmp;
  60. int c=0;// this is the counter
  61. for(tmp=head;tmp!=0;tmp=tmp->next)
  62. c++;
  63. return c;
  64. }
  65.  
  66. void list::add_end(int el) {
  67. if (is_empty())
  68. head = tail = new node(el);
  69. else
  70. tail = tail->next = new node(el);//this to add the in the last of the list
  71. }
  72.  
  73. void list::add_begin(int el){
  74.  
  75. if(is_empty())
  76. head=tail=new node (el);//bcayse we have a tail we can not caahnge the head and keep the tail 0 or null
  77. else
  78. head = new node(el,head);
  79.  
  80. }
  81.  
  82. bool list ::add_pos (int el , int pos ){
  83. if(pos<1 || pos > size()+1 )//check if it is not the in zero postion or +size postion
  84. return false ;
  85. if (pos==1)//check if it is in the first postion
  86. add_begin(el);//add to it
  87. else
  88. if (pos==size()+1)//if it is in the last postion
  89. add_end(el);//add to it
  90. else{
  91. node *t1= head , *t2;//we use 2 pointer to check the prev and next oaction of the elements we use to add
  92. for(int i=1; i<pos-1 ; i++)// this to make pointer one walk to the perv postion
  93. t1=t1->next ;//the to make it the perv one
  94. t2 =t1 ->next;// the to make t2 the next one
  95. t1->next =new node (el,t2) ;//this to add the new node to the
  96. } // list and make th t2 is the new loaction for it
  97. // we did not make for loop for the t2 because we make it =t1
  98. //which we use for loop for it alredy then er make point to the
  99. //next location using this synatx (t2 =t1 ->next)
  100. return true ;
  101. }
  102.  
  103. int main(){
  104. list l;
  105. cout<<l.is_empty()<<endl;
  106. l.add_end(9);
  107. l.add_end(1);
  108. l.add_end(8);
  109. l.add_end(4);
  110. cout<<l.is_empty()<<endl;
  111. l.print();
  112. cout<<"--------------------------------------------\n";
  113. cout<<"size the list is "<<l.size ()<<endl;
  114. l.add_begin(6);
  115. l.add_begin(7);
  116. l.print();
  117. cout<<"--------------------------------------------\n";
  118. l.add_pos(100,4);
  119. l.print();
  120. return 0;
  121.  
  122. }
  123.  
  124.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
1
0
9	0x55bdc7553ea0
1	0x55bdc7553ec0
8	0x55bdc7553ee0
4	0
--------------------------------------------
size the list is 4
7	0x55bdc7553f00
6	0x55bdc7553e80
9	0x55bdc7553ea0
1	0x55bdc7553ec0
8	0x55bdc7553ee0
4	0
--------------------------------------------
7	0x55bdc7553f00
6	0x55bdc7553e80
9	0x55bdc7553f40
100	0x55bdc7553ea0
1	0x55bdc7553ec0
8	0x55bdc7553ee0
4	0