zl程序教程

您现在的位置是:首页 >  其它

当前栏目

需要对外公开的常量放在头文件中

需要 常量 头文件 公开 放在 对外
2023-09-14 09:12:04 时间

需要对外公开的常量放在头文件中,不需要对外公开的常量放在定义 文件的头部。

为便于管理,可以把不同模块的常量集中存放在一个公共的头文件中。

 

  1 #include <iostream>
  2 
  3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  4 using namespace std;
  5 //定义节点(数据对象)的接口
  6 class Node
  7 {
  8     //声明list类为本类的友元类
  9     friend class list;
 10 //私有成员 
 11 private:              
 12     int Data;       //节点数据
 13     Node *previous; //前趋指针
 14     Node *next;     //后继指针
 15 };
 16 
 17 //定义双向链表list的接口声明
 18 class list
 19 {
 20 //私有成员 
 21 private:     
 22     Node *Head;    //链表头指针
 23     Node *Tail;    //链表尾指针
 24 //定义接口函数
 25 public:
 26     //构造函数
 27     list();
 28     //析构函数
 29     ~list();
 30     //从链表尾后添加数据
 31     void Build_HT(int Data);
 32     //从链表前头添加数据
 33     void Build_TH(int Data);
 34     //从头到尾显示数据
 35     void Display_HT();
 36     //从尾到头显示数据
 37     void Display_TH();
 38     //清除链表的全部数据
 39     void Clear();
 40 };
 41 
 42 
 43 int main(int argc, char** argv) {
 44     
 45     list list1;
 46     int i;
 47    
 48     //从尾添加数据
 49     cout<<"Add to the back of the list1:"<<endl;
 50     for (i=1;i<=20;i=i+2) {
 51         list1.Build_HT(i);
 52         cout<<i<<" ";
 53     }
 54     cout<<endl;
 55 
 56     //从头添加数据
 57     cout<<"Add to the front of the list1:"<<endl;
 58     for (i=0;i<=20;i=i+2) {
 59         list1.Build_TH(i);
 60         cout<<i<<" ";
 61     }
 62     cout<<endl;
 63 
 64     //显示链表
 65     list1.Display_HT();
 66     list1.Display_TH();
 67     return 0;
 68 }
 69 
 70 //list类函数的定义
 71 //构造函数的定义
 72 list::list()
 73 {
 74      //初值
 75      Head=0;
 76      Tail=0;
 77 }
 78 //析构函数的定义
 79 list::~list()
 80 {
 81     Clear(); 
 82 }
 83 //从链表尾后添加数据
 84 void list::Build_HT(int Data)
 85 {
 86     Node *Buffer;
 87 
 88     Buffer=new Node;
 89     Buffer->Data=Data;
 90     if(Head==0)
 91     {
 92         Head=Buffer;
 93         Head->next=0;
 94         Head->previous=0;
 95     Tail=Head;
 96     }
 97     else
 98     {
 99         Tail->next=Buffer;
100         Buffer->previous=Tail;
101     Buffer->next=0;
102         Tail=Buffer;
103     }
104 }
105 //从链表前头添加数据
106 void list::Build_TH(int Data)
107 {
108     Node *NewNode;
109     NewNode=new Node;
110     NewNode->Data=Data;
111 
112     if(Tail==0)
113     {
114         Tail=NewNode;
115     Tail->next=0;
116         Tail->previous=0;
117         Head=Tail;
118     }
119     else
120     {
121         NewNode->previous=0;
122         NewNode->next=Head;
123         Head->previous=NewNode;
124         Head=NewNode;
125     }
126 }
127 //从头到尾显示数据
128 void list::Display_HT()
129 {
130     Node *TEMP;
131     TEMP=Head;
132     cout<<"Display the list from Head to Tail:"<<endl;
133     while(TEMP!=0)
134     {
135         cout<<TEMP->Data<<" ";
136         TEMP=TEMP->next;
137     }
138     cout<<endl;
139 }
140 //从尾到头显示数据
141 void list::Display_TH()
142 {
143     Node *TEMP;
144     TEMP=Tail;
145     cout<<"Display the list from Tail to Head:"<<endl;
146     while(TEMP!=0)
147     {
148         cout<<TEMP->Data<<" ";
149         TEMP=TEMP->previous;
150     }
151     cout<<endl;
152 }
153 //清除链表的全部数据
154 void list::Clear()
155 {
156     Node *Temp_head=Head;
157 
158     if (Temp_head==0) return;
159     do
160     {
161         Node *TEMP_NODE=Temp_head;
162         Temp_head=Temp_head->next;
163         delete TEMP_NODE;
164     }
165     while (Temp_head!=0);
166 }