4_4_3.
list _ base. h // :
//
#ifndef _LIST_BASE_H //
#define _LIST_BASE_H
#include <iostream>
using std::endl;
using std::cout;
struct ELEM //
{
int x; // ( )
ELEM *next; //
};
class LIST_BASE // LIST_BASE
{
//
Protected: // LIST _ BASE ;
ELEM *start;//
//
public:
// ( )
LIST_BASE(void): start(NULL) //
{
cout<<"\nThe base designer is called"<<endl;
}
// ( , '~').
// , :
// - ;
// - ;
// - delete , new;
// - .
// !
~ LIST _ BASE (void) //
{
Del_all(); //
cout<<"\nThe base destructor has completed operation "<<endl;
system (" pause "); // !!!
}
void Add _ end (con st int &d); //
void Print(void); //
void Del_all(void); //
void Del_end(void); //
}; // LIST_BASE
// LIST_BASE
//
void LIST _ BASE:: Add _ end (const int & d) // d -
{
ELEM *temp, // ()
* cur; //
temp = new ELEM; //
if (!temp)
{
cout<<"\n The unit of the list is not placed "<<endl;
R eturn;
}
temp->x = d; //
temp -> next = NULL; //
if (! start) // ()
start = temp; //
Else
{
cur = start;
while (cur->next) // ,
cur = cur->next; //
cur->next = temp; // ,
//
}
Return;
}
//
|
|
void LIST _ BASE:: Del _ end (void)
{
ELEM *prev, //
*end; //
if (! start) // ,
{
cout<<"\n The list is empty "<<endl;
Return;
}
if (!start->next) //
{
Delete start;
start = NULL;
Return;
}
end = start; //
while (end -> next) // ( )
{
prev = end;
end = end->next; //
}
delete end; //
prev->next = NULL; //
Return;
}
//
void LIST_BASE:: Print(void)
{
ELEM *prn; //
if (! start) // ,
{
cout<<"\n The list is empty "<<endl;
Return;
}
prn = start; //
while (prn) //
{
cout<<prn->x<<" "; //
prn = prn->next; //
}
cout<<endl;
Return;
}
//
void LIST_BASE:: Del_all(void)
{
if (! start) // ,
{
cout<<"\n The list is empty "<<endl;
Return;
}
while (start) //
Del_end(); //
Return;
}
#endif _LIST_BASE_H
list _2. h // 2,
// :
#ifndef _LIST_2_H //
#define _LIST_2_H
#include "list_base.h" //
class LIST _2: public LIST _ BASE //
{
// LIST_2, LIST_BASE,
// ( ).
// , ,
// ,
// , ,
// ( ).
//
// (.. ).
// ,
// ,
// .
//
//
public:
void After _ Add (int find, int add); // add find
void Before_Add(int find, int add); // add find
}; //
//
// add find
void LIST_2:: After_Add(int find, int add)
{
ELEM *temp=NULL, //
|
|
*cur; //
if (! start) // ,
{
cout<< "\n The list is empty "<<endl;
Return;
}
// , find,
// add
cur = start;
while (cur) //
{
if (cur -> x == find)
{ // ( )
temp = new ELEM; //
if (!temp)
{
cout<<"\n The unit of the list is not placed "<<endl;
Return;
}
temp->x = add; //
temp -> next = cur -> next; // ,
//
cur->next = temp; //
cur = temp; //
}
cur = cur->next; //
}
// (temp=NULL),
if (!temp) cout<< "\nThe element "<<find<<" is not found "<<endl;
Return;
}
// add find
void LIST_2:: Before_Add(int find, int add)
{
ELEM *temp = NULL, //
*cur, //
*prev; // ,
if (! start) // ,
{
cout<<"\n The list is empty "<<endl;
Return;
}
// , find,
// add
cur = start;
while (cur) //
{
if (cur -> x == find)
{ // ( )
temp = new ELEM //
if (!temp)
{
cout<< "\n The unit of the list is not placed "<<endl;
Return;
}
temp->x = add; //
temp -> next = cur; // find
// find
if (cur == start) // start ( )
start = temp;
else // , cur
prev->next = temp;
}
prev = cur; //
cur = cur -> next; //
}
// (temp=NULL),
if (!temp)cout<< "\nThe element "<<find<<" is not found "<<endl;
Return;
}
#endif _LIST_2_H
list_2.cpp // LIST_BASE LIST_2
//
# include " list _2. h "
int main () //
{
LIST_2 ls; // ls
// , LI S T_BASE
ls.Add_end(1); // 1
ls.Add_end(2); // 2
ls.Add_end(3); // 3
ls.Print(); //
ls.Del_end(); //
Ls.Print();
ls.Del_all(); //
for (int i=1; i<=2; i++) //
Ls.Add_end(5);
Ls.Print();
// LIS T _2: Befor_Add, After_Add
// 3 5
Ls.Before_Add(5, 3);
Ls.Print();
// 4 3
Ls.After_Add(3, 4);
Ls.Print();
// 4 2
ls.After_Add(2, 4);
Ls.Print();
system("pause");
|
|
return 0;
}
: