#include <iostream>
using namespace std ;
//we name it likethis becuase simply there is only one pointer
//every node point to next node only
//we can name simply or singly
struct node {
int data;// the data in the node
node *next ;//pointer point to the next element in the array
node (int d , node *n=0)//this is a constructor for our class
{
data =d ;
next = n ;
}
};
class list {
node *head ; // pointer point to hte first elements in the array
node *tail ; //point to the last elemnts in the array and it is optional to add just for simplification
public:
list();
bool is_empty();
void print();
int size();
void add_begin (int el);
void add_end (int el);
bool add_pos (int el , int pos );//to know the postion of the elements
// this function need 2 parameters what is the el and where i should put it
// we put bool because he will return if he put it or not becuase to pos we put is not found
void add_sorted (int el);
bool delete_el(int el);//this to spicify the elements to delete
bool delete_begain ();// becuase soetome the 1 elements is empty so he will return false in that cause
bool delete_end();
bool delete_pos(int pos);//this function will take as parameter the position of the element that we want to delete from the list
// we can replace it with enumric data type
bool search (int el);// we can replace it with int
// ~list() ;//destructor that will be called when we close the programm
void operator= (list &o);//overloading to avoid the shallow copy
list (const list& o);//copy constractor to make sure that no one will change the original list
};
list::list() {
head = tail = 0;
}
bool list::is_empty() {
return head == 0;
}
void list :: print(){
node *tmp;// this is a pointer to point to head
for (tmp=head ;tmp!=0; tmp=tmp->next) //the last one will keep the pointer jump to
//the next element until it is reach the end
cout << tmp->data<<"\t"<<tmp ->next << endl;
}
int list::size(){
node *tmp;
int c=0;// this is the counter
for(tmp=head;tmp!=0;tmp=tmp->next)
c++;
return c;
}
void list::add_end(int el) {
if (is_empty())
head = tail = new node(el);
else
tail = tail->next = new node(el);//this to add the in the last of the list
}
void list::add_begin(int el){
if(is_empty())
head=tail=new node (el);//bcayse we have a tail we can not caahnge the head and keep the tail 0 or null
else
head = new node(el,head);
}
bool list ::add_pos (int el , int pos ){
if(pos<1 || pos > size()+1 )//check if it is not the in zero postion or +size postion
return false ;
if (pos==1)//check if it is in the first postion
add_begin(el);//add to it
else
if (pos==size()+1)//if it is in the last postion
add_end(el);//add to it
else{
node *t1= head , *t2;//we use 2 pointer to check the prev and next oaction of the elements we use to add
for(int i=1; i<pos-1 ; i++)// this to make pointer one walk to the perv postion
t1=t1->next ;//the to make it the perv one
t2 =t1 ->next;// the to make t2 the next one
t1->next =new node (el,t2) ;//this to add the new node to the
} // list and make th t2 is the new loaction for it
// we did not make for loop for the t2 because we make it =t1
//which we use for loop for it alredy then er make point to the
//next location using this synatx (t2 =t1 ->next)
return true ;
}
int main(){
list l;
cout<<l.is_empty()<<endl;
l.add_end(9);
l.add_end(1);
l.add_end(8);
l.add_end(4);
cout<<l.is_empty()<<endl;
l.print();
cout<<"--------------------------------------------\n";
cout<<"size the list is "<<l.size ()<<endl;
l.add_begin(6);
l.add_begin(7);
l.print();
cout<<"--------------------------------------------\n";
l.add_pos(100,4);
l.print();
return 0;
}