. , , ,

,,,

,

.

-

. ..

.

:

: _____________

: _____________

: ..

: 3-2-26

1998

.

:


.

, :

1.   ;

2.   ;

3.   ;

4.   ;

5.   ;

6.   ;

7.   ;

8.   ;

9.   .

.

: .

, .

PC Borland 3.01 . , .

.

#include

#include

class List

{struct Tree

{int Body;

Tree *LP;

Tree *RP;

Tree(int Bdy=0) {Body=Bdy; LP=NULL; RP=NULL;}

~Tree() {Body=0; LP=NULL; RP=NULL;}

};

public:

List(int Digit=0);

Tree *Root;

List *LNext;

List *LPrev;

};

List::List(int Digit)

{Root=NULL;

for (int i=Digit*10; i

{Tree *PTree;

PTree=new Tree(i);

PTree->LP=NULL;

PTree->RP=NULL;

if (Root==NULL)

Root=PTree;

else

{Tree *PTree1=Root;

do

{if (PTree1->LP!=NULL)

PTree1=PTree1->LP;}

while (PTree1->LP!=NULL);

PTree1->LP=PTree;

PTree=NULL; PTree1=NULL;

}

}

}

class TreeWork : private List

{public:

void TreeWorkStart();

private:

int ElementQuantity;

int Mass;

int i;

List *BegP;

List *PList;

int MainMenu();

int Work(int Task);

int MakeNewList();

int AddElements();

int PrintList();

void EraseList();

int DeleteElement();

int FindElement();

int SubMenu();

int SubWork(int Task);

int SortByIncrease();

int SortByDecrease();

int SaveList();

int OpenList();

protected:

void GoThroughTree(Tree *L);

void Erase(Tree *L);

};

int TreeWork::MainMenu()

{cout<

cout<<" 1. Make New List." <

cout<<" 2. Add Element." <

cout<<" 3. Print List." <

cout<<" 4. Delete Element."<

cout<<" 5. Save List." <

cout<<" 6. Erase List." <

cout<<" 7. Open File." <

cout<<" 8. Find Element." <

cout<<" 9. Sort List." <

cout<<" 0. Exit." <

cout<

int i;

do

{cin>>i;

if (i<0 || i>9) cout<

}

while (i<0 || i>9);

return i;

}

int TreeWork::SubMenu()

{cout<

cout<<"1. Sort list by increase."<

cout<<"2. Sort list by decrease."<

int i;

cout<<"Your choice: ";

do

{cin>>i;

if (i<1 || i>2) cout<

}

while (i<1 || i>2);

return i;

}

int TreeWork::SubWork(int Task)

{switch (Task)

{case 1 : SortByIncrease(); break; //Increase

case 2 : SortByDecrease(); break; //Decrease

}

return 0;

}

int TreeWork::Work(int Task)

{switch (Task)

{case 1 : ElementQuantity=MakeNewList(); break; //Make New List

case 2 : ElementQuantity+=AddElements(); break; //Add Element

case 3 : PrintList(); break; //Print List

case 4 : DeleteElement(); break; //Delete Element

case 5 : SaveList(); break; //Save List

case 6 : ElementQuantity=0; EraseList(); break; //Erase List

case 7 : OpenList(); break; //Open File

case 8 : FindElement(); break; //Find Element

case 9 : SubWork(SubMenu()); break; //Sort List

case 0 : EraseList(); return -1; //Exit

}

return 0;

}

void TreeWork::TreeWorkStart()

{ElementQuantity=0;

do {} while (Work(MainMenu())!=-1);

}

int TreeWork::MakeNewList()

{if (BegP!=NULL)

{cout<

int Quant;

cout<

do

{cin>>Quant;

if (Quant<1)

cout<

}

while (Quant<1);

for (int i=0; i

{cout<

int Digit; cin>>Digit;

PList=new List(Digit);

if (BegP==NULL)

{BegP=PList;

BegP->LNext=BegP;

BegP->LPrev=BegP;

PList=NULL;}

else

{List *PList1=BegP->LPrev;

if (PList1==BegP)

{BegP->LNext=PList;

BegP->LPrev=PList;

PList->LNext=BegP;

PList->LPrev=BegP;

PList=NULL; PList1=NULL;}

else

{BegP->LPrev=PList;

PList1->LNext=PList;

PList->LNext=BegP;

PList->LPrev=PList1;

PList=NULL; PList1=NULL;}

}

}

return Quant;

}

int TreeWork::AddElements()

{if (BegP==NULL)

{MakeNewList(); return 0;}

int Quant;

cout<

do

{cin>>Quant;

if (Quant<1)

cout<

}

while (Quant<1);

for (int i=0; i

{cout<

int Digit;

cin>>Digit;

PList=new List(Digit);

List *PList1=BegP->LPrev;

if (PList1==BegP)

{BegP->LNext=PList;

BegP->LPrev=PList;

PList->LPrev=BegP;

PList->LNext=BegP;

PList1=NULL; PList=NULL;}

else

{BegP->LPrev=PList;

PList->LNext=BegP;

PList->LPrev=PList1;

PList1->LNext=PList;

PList=NULL; PList1=NULL;}

}

return Quant;

}

int TreeWork::PrintList()

{if (BegP==NULL)

{cout<

cout<

PList=BegP;

int i=1;

do

{cout<

GoThroughTree(PList->Root);

cout<

i++;

PList=PList->LNext;}

while (PList!=BegP);

return 0;

}

void TreeWork::GoThroughTree(Tree *L)

{Tree *PL=L, *PL1;

if (PL->LP!=NULL)

{PL1=PL;

PL=PL->LP;

cout<<"("<Body<<","<Body<<") ";

GoThroughTree(PL);}

if (PL->RP!=NULL)

{PL1=PL;

PL=PL->RP;

cout<<"("<Body<<","<Body<<") ";

GoThroughTree(PL);}

}

void TreeWork::Erase(Tree *L)

{Tree *PL=L;

if (PL->LP!=NULL)

{PL=PL->LP;

Erase(PL);}

if (PL->RP!=NULL)

{PL=PL->RP;

Erase(PL);}

PL->LP=NULL;

PL->RP=NULL;

}

void TreeWork::EraseList()

{if (BegP!=NULL)

{do

{List *PList1=BegP->LNext;

PList=PList1->LNext;

BegP->LNext=PList;

PList->LPrev=BegP;

Erase(PList1->Root);

delete [] PList1;

}

while (PList!=BegP);

BegP=NULL; PList=NULL;

}

}

int TreeWork::DeleteElement()

{cout<

int Number=0;

cin>>Number;

if (Number>ElementQuantity || Number<0)

{cout<

Number--;

PList=BegP;

for (int i=0; i

PList=PList->LNext;

List *PList1=PList->LNext, *PList2=PList->LPrev;

if (PList==BegP)

{PList1->LPrev=PList2;

PList2->LNext=PList1;

PList->LNext=NULL;

PList->LPrev=NULL;

delete [] PList;

BegP=PList1;

PList1=NULL; PList2=NULL;}

else

{PList1->LPrev=PList2;

PList2->LNext=PList1;

PList->LNext=NULL;

PList->LPrev=NULL;

delete [] PList;

PList1=NULL; PList2=NULL;}

ElementQuantity--;

return 0;

}

int TreeWork::FindElement()

{cout<

int Number=0;

cin>>Number;

PList=BegP;

do

{Tree *PT=PList->Root;

if (Number>PT->Body && NumberBody+10)

{cout<

GoThroughTree(PList->Root);

PList=NULL; cout<

PList=PList->LNext;

}

while (PList!=BegP);

cout<

PList=NULL;

return -1;

}

int TreeWork::SortByIncrease()

{

if(BegP==NULL) {cout<

List *PList1=BegP; PList=BegP;

do

{do

{if (PList1->Root->Body>PList->Root->Body)

{Tree *PT;

PT=PList1->Root;

PList1->Root=PList->Root;

PList->Root=PT;

PT=NULL;}

PList1=PList1->LNext;

}

while (PList1!=BegP);

PList=PList->LNext;

}

while (PList!=BegP);

return 0;

}

int TreeWork::SortByDecrease()

{

if(BegP==NULL) {cout<

List *PList1=BegP; PList=BegP;

do

{do

{if (PList1->Root->BodyRoot->Body)

{Tree *PT;

PT=PList1->Root;

PList1->Root=PList->Root;

PList->Root=PT;

PT=NULL;}

PList1=PList1->LNext;

}

while (PList1!=BegP);

PList=PList->LNext;

}

while (PList!=BegP);

return 0;

}

int TreeWork::SaveList()

{if (BegP==NULL)

{cout<

ofstream F;

char *FileName=new char[25];

cout<>FileName;

F.open(FileName);

PList=BegP;

do

{i=0;

Mass=PList->Root->Body;

PList=PList->LNext;

if (PList!=BegP)

F<

else

F<

}

while (PList!=BegP);

F.close();

delete [] FileName;

return 0;

}

int TreeWork::OpenList()

{if (BegP!=NULL)

{cout<

cout<

char *FileName=new char[25];

cin>>FileName;

ifstream f;

ElementQuantity=0;

f.open(FileName);

char Next;

Next=f.peek();

while (Next!=EOF)

{

f>>Mass;

PList=new List(Mass/10);

if (BegP==NULL)

{BegP=PList;

BegP->LNext=BegP;

BegP->LPrev=BegP;

PList=NULL;}

else

{List *PList1=BegP->LPrev;

if (PList1==BegP)

{BegP->LNext=PList;

BegP->LPrev=PList;

PList->LNext=BegP;

PList->LPrev=BegP;

PList=NULL; PList1=NULL;}

else

{BegP->LPrev=PList;

PList1->LNext=PList;

PList->LNext=BegP;

PList->LPrev=PList1;

PList=NULL; PList1=NULL;}

}

Next=f.peek();

ElementQuantity++;

}

f.close();

delete [] FileName;

return 0;

}

TreeWork TW;

void main()

{TW.TreeWorkStart();}

.

:

Main Menu:

1. Make New List.

2. Add Element.

3. Print List.

4. Delete Element.

5. Save List.

6. Erase List.

7. Open File.

8. Find Element.

9. Sort List.

0. Exit.

Your choice :

1:

Main Menu:

1. Make New List.

2. Add Element.

3. Print List.

4. Delete Element.

5. Save List.

6. Erase List.

7. Open File.

8. Find Element.

9. Sort List.

0. Exit.

Your choice : 1

Input kol-vo of elements:

( 4):

Main Menu:

1. Make New List.

2. Add Element.

3. Print List.

4. Delete Element.

5. Save List.

6. Erase List.

7. Open File.

8. Find Element.

9. Sort List.

0. Exit.

Your choice : 1

Input kol-vo of elements: 4

Input digit:

:

Input kol-vo of elements: 4

Input digit: 1

Input digit: 2

Input digit: 3

Input digit: 4

Main Menu:

1. Make New List.

2. Add Element.

3. Print List.

4. Delete Element.

5. Save List.

6. Erase List.

7. Open File.

8. Find Element.

9. Sort List.

0. Exit.

Your choice :

:

Input digit: 1

Input digit: 2

Input digit: 3

Input digit: 4

Main Menu:

1. Make New List.

2. Add Element.

3. Print List.

4. Delete Element.

5. Save List.

6. Erase List.

7. Open File.

8. Find Element.

9. Sort List.

0. Exit.

Your choice : 2

Input kol-vo of elements:

. 1 ( ):

Input digit: 2

Input digit: 3

Input digit: 4

Main Menu:

1. Make New List.

2. Add Element.

3. Print List.

4. Delete Element.

5. Save List.

6. Erase List.

7. Open File.

8. Find Element.

9. Sort List.

0. Exit.

Your choice : 2

Input kol-vo of elements: 1

Input digit:

.

, :

1 element: 1234

2 element: 2345

3 element: 3456

4 element: 4567

5 element: 2345

Main Menu:

1. Make New List.

2. Add Element.

3. Print List.

4. Delete Element.

5. Save List.

6. Erase List.

7. Open File.

8. Find Element.

9. Sort List.

0. Exit.

Your choice :

, :

2 element: 2345

3 element: 3456

4 element: 4567

5 element: 2345

Main Menu:

1. Make New List.

2. Add Element.

3. Print List.

4. Delete Element.

5. Save List.

6. Erase List.

7. Open File.

8. Find Element.

9. Sort List.

0. Exit.

Your choice : 4

Input number of element: 5

. , :

Main Menu:

1. Make New List.

2. Add Element.

3. Print List.

4. Delete Element.

5. Save List.

6. Erase List.

7. Open File.

8. Find Element.

9. Sort List.

0. Exit.

Your choice : 5

Input file name: demon13.txt

. , :

5. Save List.

6. Erase List.

7. Open File.

8. Find Element.

9. Sort List.

0. Exit.

Your choice : 5

Input file name: demon13.txt

Main Menu:

1. Make New List.

2. Add Element.

3. Print List.

4. Delete Element.

5. Save List.

6. Erase List.

7. Open File.

8. Find Element.

9. Sort List.

0. Exit.

Your choice : 6

, :

5. Save List.

6. Erase List.

7. Open File.

8. Find Element.

9. Sort List.

0. Exit.

Your choice : 6

Main Menu:

1. Make New List.

2. Add Element.

3. Print List.

4. Delete Element.

5. Save List.

6. Erase List.

7. Open File.

8. Find Element.

9. Sort List.

0. Exit.

Your choice : 7

Input file name: demon13.txt

- :

Main Menu:

1. Make New List.

2. Add Element.

3. Print List.

4. Delete Element.

5. Save List.

6. Erase List.

7. Open File.

8. Find Element.

9. Sort List.

0. Exit.

Your choice :8

Input number, you want to find: 2

The first element that we fined out:

1234

. /:

Main Menu:

1. Make New List.

2. Add Element.

3. Print List.

4. Delete Element.

5. Save List.

6. Erase List.

7. Open File.

8. Find Element.

9. Sort List.

0. Exit.

Your choice : 9

Sub Menu:

1. Sort list by increase.

2. Sort list by decrease.

Your choice:

( !). /! ! , .

. - . ..

 

 

 

! , , , .
. , :