#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
struct Point
{
int x,y;
friend std::ostream &operator << (std::ostream &output,const Point &p);
friend std::istream &operator >> (std::istream &input,Point &p);
};
bool operator == (Point a,Point b)
{
return (a.x == b.x && a.y == b.y);
}
bool operator < (Point a,Point b)
{
return (a.x < b.x || a.x == b.x && a.y < b.y);
}
bool operator > (Point a,Point b)
{
return !(a < b||a == b);
}
std::ostream &operator << (std::ostream &output,const Point &p)
{
output<<"( "<<p.x<<" , "<<p.y<<" ) ";
return output;
}
std::istream &operator >> (std::istream &input,Point &p)
{
input>>p.x>>p.y;;
return input;
}
template<class T>
class Link
{
private:
T data;
Link<T> *next;
Link<T> *previous;
public:
Link(T data);
void displayLink();
T getData();
void setData(T data);
Link<T> *getNext();
void setNext(Link<T> *next);
Link<T> *getPrevious();
void setPrevious(Link<T> *previous);
template<class U> friend std::ostream& operator<< (std::ostream& print, Link<U>& obj);
};
template<class T>
Link<T>::Link(T data)
{
setData(data);
setNext(NULL);
setPrevious(NULL);
}
template<class T>
void Link<T>::displayLink()
{
std::cout<<getData()<<" ";
}
template<class T>
T Link<T>::getData()
{
return data;
}
template<class T>
void Link<T>::setData(T data)
{
this->data = data;
}
template<class T>
Link<T> *Link<T>::getNext()
{
return next;
}
template<class T>
void Link<T>::setNext(Link<T> *next)
{
this->next = next;
}
template<class T>
Link<T> *Link<T>::getPrevious()
{
return previous;
}
template<class T>
void Link<T>::setPrevious(Link<T> *previous)
{
this->previous = previous;
}
template <class U>
std::ostream& operator<<(std::ostream& print, Link<U> & L)
{
print << L.getData();
return print;
}
template<class T>
class DoublyLinkedList
{
private:
Link<T> *first;
Link<T> *last;
public:
DoublyLinkedList();
~DoublyLinkedList();
Link<T> *getFirst();
void setFirst(Link<T> *first);
Link<T> *getLast();
void setLast(Link<T> *first);
bool isEmpty();
Link<T> *find(T key);
void insertFirst(T dd);
void insertLast(T dd);
void insertSorted(T dd);
void deleteFirst();
void deleteLast();
bool insertAfter(T key,T dd);
void deleteKey(T key);
void displayForward();
void displayBackward();
};
template<class T>
DoublyLinkedList<T>::DoublyLinkedList()
{
setFirst(NULL);
setLast(NULL);
}
template<class T>
DoublyLinkedList<T>::~DoublyLinkedList()
{
while(!isEmpty())
{
deleteFirst();
}
}
template<class T>
Link<T> *DoublyLinkedList<T>::getFirst()
{
return first;
}
template<class T>
void DoublyLinkedList<T>::setFirst(Link<T> *first)
{
this->first = first;
}
template<class T>
Link<T> *DoublyLinkedList<T>::getLast()
{
return last;
}
template<class T>
void DoublyLinkedList<T>::setLast(Link<T> *last)
{
this->last = last;
}
template<class T>
bool DoublyLinkedList<T>::isEmpty()
{
return getFirst()==NULL;
}
template<class T>
Link<T> *DoublyLinkedList<T>::find(T key)
{
Link<T> *current = getFirst();
while(current != NULL && !(current->getData == key))
current = current->getNext();
return current;
}
template<class T>
void DoublyLinkedList<T>::insertFirst(T dd)
{
Link<T> *newLink = new Link<T>(dd);
if(isEmpty())
setLast(newLink);
else
getFirst()->setPrevious(newLink);
newLink->setNext(getFirst());
setFirst(newLink);
}
template<class T>
void DoublyLinkedList<T>::insertLast(T dd)
{
Link<T> *newLink = new Link<T>(dd);
if(isEmpty())
{
setFirst(newLink);
}
else
{
getLast()->setNext(newLink);
newLink->setPrevious(getLast());
}
setLast(newLink);
}
template<class T>
void DoublyLinkedList<T>::deleteFirst()
{
if(isEmpty())
return;
Link<T> *temp = getFirst();
if(getFirst()->getNext() == NULL)
setLast(NULL);
else
getFirst()->getNext()->setPrevious(NULL);
setFirst(getFirst()->getNext());
delete temp;
}
template<class T>
void DoublyLinkedList<T>::deleteLast()
{
if(isEmpty())
return;
Link<T> *temp = getLast();
if(getFirst()->getNext() == NULL)
setFirst(NULL);
else
getLast()->getPrevious()->setNext(NULL);
setLast(getLast()->getPrevious());
delete temp;
}
template<class T>
bool DoublyLinkedList<T>::insertAfter(T key,T dd)
{
Link<T> *current = find(key);
if(current == NULL)
return false;
Link<T> *newLink = new Link<T>(dd);
if(current->getNext() == NULL)
{
newLink->setNext(NULL);
setLast(newLink);
}
else
{
newLink->setNext(current->getNext());
current->getNext()->setPrevious(newLink);
}
newLink->setPrevious(current);
current->setNext(newLink);
return true;
}
template<class T>
void DoublyLinkedList<T>::insertSorted(T key)
{
Link<T> *newLink = new Link<T>(key);
Link<T> *previous = NULL;
Link<T> *current = getFirst();
while(current != NULL && key > current->getData())
{
previous = current;
current = current->getNext();
}
if(previous == NULL)
setFirst(newLink);
else
previous->setNext(newLink);
newLink->setNext(current);
if(current == NULL)
setLast(newLink);
else
current->setPrevious(newLink);
newLink->setPrevious(previous);
}
template<class T>
void DoublyLinkedList<T>::deleteKey(T key)
{
Link<T> *current = find(key);
if(current==NULL)
return;
if(current->getPrevious() == NULL)
setFirst(current->getNext());
else
current->getPrevious()->setNext(current->getNext());
if(current->getNext() == NULL)
setLast(current->getPrevious());
else
current->getNext()->setPrevious(current->getPrevious());
delete current;
}
template<class T>
void DoublyLinkedList<T>::displayForward()
{
std::cout<<"List (first-->last): ";
Link<T> *current = getFirst();
while(current != NULL)
{
current->displayLink();
current = current->getNext();
}
std::cout<<"\n";
}
template<class T>
void DoublyLinkedList<T>::displayBackward()
{
std::cout<<"List (last-->first): ";
Link<T> *current = getLast();
while(current != NULL)
{
current->displayLink();
current = current->getPrevious();
}
std::cout<<"\n";
}
int vect(Point a1,Point a2,Point b1,Point b2)
{
return (a2.x - a1.x)*(b2.y - b1.y) - (b2.x - b1.x)*(a2.y - a1.y);
}
int dist2(Point a1,Point a2)
{
return (a2.x - a1.x)*(a2.x - a1.x) + (a2.y - a1.y)*(a2.y - a1.y);
}
void Jarvis(DoublyLinkedList<Point> &A,DoublyLinkedList<Point> &B)
{
Link<Point> *p,*q,*m,*min;
if(!A.isEmpty())
{
m = A.getFirst();
p = m->getNext();
while(p != NULL)
{
if(p->getData().y < m->getData().y)
m = p;
else if(p->getData().y == m->getData().y && p->getData().x > m->getData().x)
m = p;
p = p->getNext();
}
if(m->getPrevious() == NULL)
A.setFirst(m->getNext());
else
m->getPrevious()->setNext(m->getNext());
if(m->getNext() == NULL)
A.setLast(m->getPrevious());
else
m->getNext()->setPrevious(m->getPrevious());
if(A.isEmpty())
A.setLast(m);
else
A.getFirst()->setPrevious(m);
m->setNext(A.getFirst());
m->setPrevious(NULL);
A.setFirst(m);
B.insertLast(A.getFirst()->getData());
min = A.getFirst()->getNext();
do
{
q = A.getFirst()->getNext();
while(q != NULL)
{
if((vect(B.getLast()->getData(),min->getData(),B.getLast()->getData(),q->getData()) < 0)||
(vect(B.getLast()->getData(),min->getData(),B.getLast()->getData(),q->getData()) == 0)&&
(dist2(B.getLast()->getData(),min->getData()) < dist2(B.getLast()->getData(),q->getData())))
min = q;
q = q->getNext();
}
B.insertLast(min->getData());
min = A.getFirst();
}
while(!(B.getLast()->getData() == B.getFirst()->getData()));
B.deleteLast();
}
}
int main(int argc, char *argv[])
{
DoublyLinkedList<Point> A;
DoublyLinkedList<Point> B;
stringstream ss;
Point p;
string line;
int counter;
while(getline(cin,line))
{
ss.clear();
ss.str(line);
counter = 1;
while(ss)
{
try
{
switch(counter)
{
case 1:
{
ss>>p.x;
break;
}
case 0:
{
ss>>p.y;
A.insertSorted(p);
break;
}
}
}
catch(...)
{
}
counter = (counter + 1)%2;
}
//Jarvis(A,B);
cout<<"List A \n";
A.displayForward();
A.displayBackward();
/* cout<<"List B \n";
B.displayForward();
B.displayBackward(); */
while(!A.isEmpty())
A.deleteFirst();
while(!B.isEmpty())
B.deleteFirst();
}
return EXIT_SUCCESS;
}