#include <iostream>
using namespace std;
class Node
{
public :
int data;
Node* next;
Node() { next = NULL; }
Node(int d) { data = d; next = NULL; }
};
class LinkList
{
private :
Node* first;
Node* last;
int size;
public :
LinkList() { first = last = NULL , size = 0; }
// baraye tamame tavabe dar surati ke betunin kar khaste shode ro anjam bedin true bargardunin
// dar gheyre in surat false bargardunid
bool AddFirst(int x)
{
// node jadid ba meghdare x ro be ebtedaye link ezafe konid
Node* t = new Node(x);
if (first == NULL)
first = last = t;
else
{
t->next = first;
first = t;
}
size++;
return true;
}
bool AddLast(int x)
{
// ... enteha ...
Node* t = new Node(x);
if (first == NULL)
first = last = t;
else
{
last->next = t;
last = t;
}
size++;
return true;
}
bool AddMiddle(int x , int pos)
{
// node jadid ro ruye andise pos garar bedin , yani :
// -> pos==0 : be avvale list ezafe konid
// -> pos==size : be entehaye list ezafe konid
// -> pos>0 && pos<size : node jadid ro ghabl az node x'th garar bedin ta andisesh mosaviye x beshe
// mesal : 7 -> 3 -> 6 -> 1
// add(5,2)
// 7 -> 3 -> 5 -> 6 -> 1
// -> else : andis na motabare va bayad false bargardunid;
if (pos<0 || pos>size) return false;
if (pos==0) return AddFirst(x);
if (pos==size) return AddLast(x);
Node* help = first;
for (int i=0 ; i<pos-1 ; i++)
help = help->next;
Node* t = new Node(x);
t->next = help->next;
help->next = t;
size++;
return true;
}
// baraye delete ha
// age emkane delete kardan vujud dashte bashe -> meghdare delete shode ro be komake x be karbar ettela bedid va true resturn konid
// dar gheyre insurat false bargardunid
// be onovane mesal be tabe zir nega konid
bool DeleteFirst(int& x)
{
// Hazfe ozve avval
if (first == NULL) return false;
Node* t = first; // copy az first baraye delete kardan
if (first == last) last = NULL;
first = first->next;
x = t->data; // zakhireye meghdare dakhele in node
delete t; // delete kardane hafezeye gerefte shode
size--;
return true;
}
bool DeleteLast(int& x)
{
// Hazve ozve akhar
if (first == NULL) return false;
x = last->data;
if (first == last)
{
delete first;
first = last = NULL;
}
else
{
Node* help = first;
while ( help->next != last )
help = help->next;
delete last;
last = help;
last->next = NULL;
}
size--;
return true;
}
bool DeleteMiddle(int& x, int pos)
{
// Hazfe ozve pos'th , andisha az sefr shuru mishan yani [0 .. size-1]
return true;
}
bool GetFirst(int& x) { return true; }
bool GetLast(int& x) { return true; }
bool GetMiddle(int&x , int pos) { return true; }
int Size() { return size; }
void Print()
{
cout<<"Size = "<<size<<" : ";
Node* help = first;
while (help!=NULL)
{
cout<<help->data<<" ";
help = help->next;
}
cout<<"\n";
}
void Clean()
{
// azaye link list ro kamel pak konid
}
void Reverse()
{
// link list ro makus konid
}
// ---------------------------------------------------------------- Ekhtiyari ha --------------------------------------------------------------
void Sort_n2() {}
void Sort_nlogn() {}
void Unique() {} // hazfe tekrari ha
LinkList Merge(LinkList l) // tarkibe 2 link liste morattab shode
{
LinkList ans;
// ...
return ans;
}
};
int main()
{
int x;
LinkList l;
l.AddFirst(3);
l.AddFirst(5);
l.AddFirst(7);
l.AddLast(10);
l.Print();
l.DeleteFirst(x);
l.DeleteLast(x);
l.DeleteFirst(x);
l.Print();
l.DeleteFirst(x);
l.DeleteFirst(x);
l.DeleteFirst(x);
l.DeleteFirst(x);
l.Print();
l.AddFirst(8);
l.AddLast(10);
l.AddMiddle(11 , 1);
l.AddMiddle(12 , 1);
l.AddMiddle(13 , 3);
l.Print();
return 0;
}