// List.cpp : Defines the entry point for the console application.
//
#include "iostream"
using namespace std ;
typedef int Items ;
struct Node
{
Items data ; // data
Node *next ; // pointer to next struct
Node *prev ; // pointer to previous struct
} ;
class List
{
private:
Node *end ; // pointer to last struct
Node *start ; // pointer to first struct
public:
List() ; // constructor
~List() ; // destructor
int IsEmpty() ; // if it is empty - 1, else - 0
void Show() ; // show data
void Show_back() ; // show data from back
void Add_back(Items) ; // add data to back ( only with Add_back(void) )
void Add_back() ; // enter and add data to back
void Del_back() ; // delete object from back
void Add_front() ; // enter and add object to front
void Add_front(Items) ; // add object to front ( only with Add_front(void) )
void Del_front() ; // delete object from front
int Count() ; // return number of objects
void Set() ; // enter and set data in entering number
void Set(int) ; // set data in number ( only with Set(void))
void Del_number(int) ; // delete number
void Del_number() ; // enter number and delete them
void Del_data(Items) ; // delete data when it will be found ( only one time )
void Del_data() ; // enter and delete data, when it will be found ( only one time )
void Del_data_full() ; // enter and delete every object with entering data
void Add_sort() ; // enter and set data before bigger data
int Search_data(Items) ; // if data is found - 1 , else - 0
Node* Del_adr(Node*) ; // take struct, finding such object, remove them from list and return struct. It is 0, when there is no such struct
void Insert(Node*) ; // take struct and insert before biggest struct
int Sorted() ; // if data of list is sorted - 1, else - 0
void Sort() ; // it sort's objects in a list
} ;
void main()
{
List a ;
cin.get() ;
cin.get() ;
}
List::List()
{
start = 0 ; // inizialization
end = 0 ; // inizialization
}
List::~List()
{
if ( IsEmpty() ) return ; // if it is empty, we don't need to delete smth
Node *current = start ; // current is a kind of count, because it is a pointer
while ( current ) // while it isn't null
{
current = start->next ; // move current to second object
delete start ; // delete first
start = current ; // and just move start-pointer
}
start = 0 ;
end = 0 ;
}
int List::IsEmpty()
{
if ( !start ) return 1 ; // if start is null, list is empty
return 0 ; // else
}
void List::Show()
{
if ( IsEmpty() ) // if it is empty
{
cout << "This is empty..." << endl ;
return ;
}
Node *current = start ; // counter
while ( current )
{
cout << "Data: " << current->data << endl ; // print every object in a list
current = current->next ; // move to the next object
}
}
void List::Show_back()
{
if ( IsEmpty() ) return ; // if it is empty
Node *current = end ; // counter from back
while ( current )
{
cout << "Data: " << current->data << endl ; // print every data
current = current->prev ; // move to previous
}
}
void List::Add_back(Items d)
{
if ( IsEmpty() ) // if it is empty
{
start = new Node ; // create start
start->data = d ; // set data
start->next = 0 ;
start->prev = 0 ;
end = start ; // end == start if list has one object
return ;
}
Node *temp = new Node ; // create new struct
temp->data = d ; // set data
temp->next = 0 ;
temp->prev = end ; // previous is end object
end->next = temp ; // next for end is our temp
end = temp ; // move end to last member of a list
}
void List::Add_back()
{
Items *d = new Items ; // create data
cout << "Enter data: " ;
cin >> *d ;
Add_back(*d) ; // calling another method
}
void List::Del_back()
{
if ( IsEmpty() ) return ; // if it is empty
if ( start == end ) // if list has only one member
{
delete start ;
start = 0 ;
end = 0 ;
return ;
}
Node *current = start ; // counter
while ( current->next != end ) // while next object isn't end
{
current = current->next ; // move
}
delete end ; // delete end
end = current ; // move it to previous member
end->next = 0 ; // inizialization of next for last member
// another kind of deleting from back
/*
Node *current = end->prev ; // create pointer to previous member from end
delete end ; // delete end
end = current ; // move end to previous member (current)
end->next = 0 ; // inizialization of next for last member
*/
}
void List::Add_front(Items d)
{
if ( IsEmpty() ) // if it is empty
{
start = new Node ; // create first object
start->data = d ; // set in data
start->next = 0 ;
start->prev = 0 ;
end = start ; // if there is one object, start always equally end ( start == end )
return ;
}
Node *temp = new Node ; // if start exist, create new object
temp->data = d ; // set in data
temp->next = start ; // bind it with start
start->prev = temp ; // start with this
start = temp ; // and just move start
}
void List::Add_front()
{
Items *d = new Items ; // create new data
cout << "Enter data: " ;
cin >> *d ;
Add_front(*d) ; // calling another method
}
void List::Del_front()
{
if ( IsEmpty() ) return ; // is it is empty
if ( start == end ) // if there is one object
{
delete start ;
start = 0 ;
end = 0 ;
return ;
}
Node *current = start->next ; // create new pointer to next object from start
delete start ; // delete it
start = current ; // and just move start
}
int List::Count()
{
if ( IsEmpty() ) return 0 ; // if it is empty
int k = 0 ; // counter of objects
Node *current = start ;
while ( current )
{
k++ ;
current = current->next ;
}
return k ;
}
void List::Set()
{
int *a = new int ; // create number
cout << "Enter position: " ;
cin >> *a ;
if ( ( *a < 1 ) || ( *a > Count()+1 ) ) // if it is 0 or less, if it more then ( number of objects + 1 ), because we can Add_back new object
{
delete a ;
cout << "Error position... " << endl ;
return ;
}
if ( *a == 1 ) { Add_front() ; return ; } // if it is start - Add_front
if ( *a == Count()+1 ) { Add_back() ; return ; } // if it is end+1 - Add_back
Set( *a ) ; // calling another method
}
void List::Set(int a)
{
int k = 1 ; // it is number of object
Node *current = start ;
while ( current )
{
current = current->next ;
k++ ;
if ( k == a ) // if it is thah number
{
Node *temp = new Node ; // create new data
cout << "Enter data: " ;
cin >> temp->data ;
current->prev -> next = temp ; // previos with temp
temp->prev = current->prev ; // temp with previous
temp->next = current ; // temp with next
current->prev = temp ; // next with temp
return ;
}
}
}
void List::Del_number()
{
int *a = new int ; // create number
cout << "Enter position: " ;
cin >> *a ;
if ( ( *a < 1 ) || ( *a > Count() ) ) // if it is 0 or less, if it is unexisting number
{
delete a ;
cout << "Error position... " << endl ;
return ;
}
if ( *a == 1 ) { Del_front() ; return ; } // if is is start - Del_front
if ( *a == Count() ) { Del_back() ; return ; } // if it is end - Del_back
Del_number( *a ) ; // calling another method
}
void List::Del_number(int a)
{
int k = 1 ; // k is a number of object
Node *current = start ;
while ( current )
{
current = current->next ;
k++ ;
if ( k == a )
{
Node *buf = current->next ;
buf->prev = current->prev ;
current->prev -> next = buf ;
buf->next = current->next->next ;
delete current ;
return ;
}
}
}
void List::Del_data()
{
if ( IsEmpty() ) // if it is empty
{
cout << "This is empty... " << endl ;
return ;
}
Items *a = new Items ; // create data
cout << "Enter data: " ;
cin >> *a ;
if ( start->data == *a ) { Del_front() ; return ; } // if it is in start - Del_front
if ( end->data == *a ) { Del_back() ; return ; } // if it is in the end - Del_back
Del_data(*a) ; // another method
}
void List::Del_data(Items a)
{
Node *current = start->next ; // because we don't need to check start: it is in another method
int k = 2 ; // because we begin's from second object (start is first)
while ( current ) // for start->next to end
{
if ( current->data == a ) { Del_number(k) ; return ; } // we delete it for number, because it is light :)
k++ ; // k is number of object
current = current->next ;
}
cout << "There is no " << a << " ..." << endl ;
}
void List::Del_data_full()
{
if ( IsEmpty() ) // if it is empty
{
cout << "This is empty... " << endl ;
return ;
}
Items *a = new Items ;
cout << "Enter data: " ;
cin >> *a ;
while ( start->data == *a && start->next ) Del_front() ; // while first data is true - delete
if ( start->next == 0 && start->data == *a ) // if than it is one object with true-data - delete
{
delete start ;
start = end = 0 ;
return ;
}
while ( end->data == *a ) Del_back() ; // while last data is true - delete
Node *current = start->next ;
while ( current )
{
while ( current->data == *a ) // while data is true - delete
{
Node *buf = current->next ; // we need buf to delete only one object
buf->prev = current->prev ;
current->prev -> next = buf ;
delete current ;
current = buf ;
}
current = current->next ;
}
}
void List::Add_sort()
{
Node *temp = new Node ; // new node for data
cout << "Enter data: " ;
cin >> temp->data ;
if ( temp->data <= start->data ) // if it is smallest than start - Add_front()
{
Add_front(temp->data) ;
delete temp ;
return ;
}
Node *current = start->next ;
while ( current->next ) // while current isn't end
{
if ( temp->data <= current->data )
{
current->prev -> next = temp ; // previos with temp
temp->prev = current->prev ; // temp with previous
temp->next = current ; // temp with next
current->prev = temp ; // next with temp
return ;
}
current = current->next ;
}
Add_back(temp->data) ; // if it is end
delete temp ;
}
int List::Search_data(Items a)
{
Node *current = start ;
while ( current ) // while this list has records
{
if ( current->data == a ) return 1 ; // if it is it - return
current = current->next ; // move, every time
}
return 0 ; // if there is no data - null
}
Node* List::Del_adr(Node *temp)
{
if ( start == temp ) // if it is start
{
start = start->next ; // just move start
return temp ; // and return this object
}
if ( end == temp ) // if it is end
{
end = end->prev ; // just move end
return temp ; // and return object
}
// template of delete
Node *current = start->next ; // it isn't start, because start is higher in code ^
while ( current->next ) // while current isn't end, because end is higher in code too ^
{
if ( current == temp ) // is it is that
{
current->prev -> next = current->next ; // we cut it
current->next -> prev = current->prev ; // from list
return temp ;
}
current = current->next ;
}
return 0 ;
}
void List::Insert(Node *temp)
{
if ( temp->data <= start->data ) // if it can be the first
{
temp->next = start ; // next with start
temp->prev = 0 ; // previous is null
start->prev = temp ; // previous for start is temp
start = temp ; // and first object is temp-struct
return ;
}
if ( temp->data >= end->data ) // if it can be the last
{
temp->prev = end ; // previous for temp is end
end->next = temp ; // next for end is temp
temp->next = 0 ; // temp has no next
end = temp ; // and the last object is temp
return ;
}
// if it can be before the last:
if ( ( temp->data <= end->data ) && ( end->prev->data <= temp->data ) )
{
end->prev -> next = temp ; // end has previous object; ist's next is temp
temp->prev = end->prev ; // previous for temp is previous for end
temp->next = end ; // next for temp is end
end->prev = temp ; // previous for end is temp
return ;
}
// template of inserting
Node *current = start->next ; // it isn't start, becouse start is higher in code ^
while ( current->next ) // while it isn't end, becouse end is higher too ^
{
if ( temp->data < current->data ) // if it can be here - insert
{
current->prev -> next = temp ; // current has previous; it's next is temp
temp->prev = current->prev ; // previous for temp is previous for current
temp->next = current ; // next for temp is current
current->prev = temp ; // previous for current is temp
return ;
}
current = current->next ;
}
}
int List::Sorted()
{
Node *current = start ;
while ( current->next ) // while it isn't end
{
if ( current->data > current->next->data ) return 0 ; // if it isn't right
current = current->next ;
}
return 1 ; // if it is right - sorted
}
void List::Sort()
{
Node *current = start ;
while ( !Sorted() ) // do while it isn't sorted
{
if ( current->data > current->next->data )
{
Node *buf = current ; // create a buf, because curent will be deleted
current = current->next ;
if ( current->next == 0 ) current = start ; // if current is end, it must be start
Insert( Del_adr(buf) ) ;
continue ; // because we don't need to change current pointer
}
current = current->next ;
if ( current->next == 0 ) current = start ;
}
}