?? cd4_3u.cpp
字號(hào):
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "cd4_3u.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
class list; //定義list類
class Node //結(jié)點(diǎn)類
{
public: //公有變量,函數(shù),程序定義
Node* link; //結(jié)點(diǎn)指向指針
Node(String s):data(s),link(NULL){} //結(jié)點(diǎn)產(chǎn)生時(shí)的內(nèi)容設(shè)置
String getdata(){return data;} //通過(guò)公有函數(shù)取得私有變量的數(shù)據(jù)
private: //私有變量,函數(shù),程序定義
String data; //私有變量數(shù)據(jù)
};
class List //鏈結(jié)類
{
private:
Node* first; //設(shè)定首結(jié)點(diǎn)的鏈結(jié)
public:
void ist(String s) //插入新結(jié)點(diǎn)
{
Node* newnode=new Node(s); //產(chǎn)生新結(jié)點(diǎn)對(duì)象newnode
newnode->link=first;
first=newnode;
}
void dplist() //顯示鏈表
{
int i=1;
Node* P=first; //從頭開(kāi)始
while(P!=NULL) //直到?jīng)]有結(jié)點(diǎn)為止
{
Form1->sg->Cells[1][i]=P->getdata(); //取得的私有數(shù)據(jù)在字符串表格中顯示
P=P->link;
i++; //字符串表格輔助計(jì)算器
}
}
int serh(String k) //鏈表的搜索函數(shù)
{
int i=1;
Node* P=first;
while(P->getdata()!=k) //當(dāng)結(jié)點(diǎn)數(shù)據(jù)與搜索數(shù)據(jù)不同時(shí)執(zhí)行循環(huán)
{
P=P->link;
i++;
if(P==NULL)
return 0; //沒(méi)找到返回 0
}
return i; //找到返回i
}
int del(String k) //鏈表刪除某結(jié)點(diǎn)的函數(shù),即搜索到后再刪除
{
int i=1;
Node* P=first;
Node* T=first;
while(P->getdata()!=k) //搜索關(guān)鍵結(jié)點(diǎn)值
{
T=P;
P=P->link;
i++;
if(P==NULL)
return 0; //沒(méi)有找到,跳出函數(shù)
}
if(P==first)
first=first->link;
else
T->link=P->link; //T為P的前鏈結(jié)點(diǎn)
delete P; //刪除找到的結(jié)點(diǎn)
return i; //返回結(jié)點(diǎn)位置
}
void clear() //鏈表的清除函數(shù)
{
while(first!=NULL)
{
Node* P=first;
first=first->link;
delete P;
}
}
};
List H; //定義H為L(zhǎng)ist類變量
String str; //定義str用來(lái)獲取輸入字符串
int r,i,p; //用r取得位置,用p決定箭頭變化
static int t,con; //t用來(lái)記錄窗體變化,com記錄結(jié)點(diǎn)累計(jì)
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::addClick(TObject *Sender) //加入按鈕的單擊事件
{
str=ein->Text;
H.ist(str); //調(diào)用清除鏈表的函數(shù),將取得的字符串加入,構(gòu)造1新結(jié)點(diǎn)
ein->Text="";
if(p==1) //窗體上的箭頭顯示變化
{
if(t%2==0)
{
p6->Visible=true;
p7->Visible=false;
}
else
{
p6->Visible=false;
p7->Visible=true;
}
}
t=(t%5)+1; //增加窗體顯示結(jié)點(diǎn)的計(jì)數(shù)器
switch(t) //窗體上的鏈表顯示變化
{
case 1:p1->Visible=true; //設(shè)置各控件的相對(duì)反應(yīng)及顯示數(shù)據(jù)
d1->Visible=true;
n1->Visible=true;
d5->Caption=d4->Caption;
d4->Caption=d3->Caption;
d3->Caption=d2->Caption;
d2->Caption=d1->Caption;
d1->Caption=str;
break;
case 2:p2->Visible=true;
d2->Visible=true;
n2->Visible=true;
d5->Caption=d4->Caption;
d4->Caption=d3->Caption;
d3->Caption=d2->Caption;
d2->Caption=d1->Caption;
d1->Caption=str;
break;
case 3:p3->Visible=true;
d3->Visible=true;
n3->Visible=true;
d5->Caption=d4->Caption;
d4->Caption=d3->Caption;
d3->Caption=d2->Caption;
d2->Caption=d1->Caption;
d1->Caption=str;
break;
case 4:p4->Visible=true;
d4->Visible=true;
n4->Visible=true;
d5->Caption=d4->Caption;
d4->Caption=d3->Caption;
d3->Caption=d2->Caption;
d2->Caption=d1->Caption;
d1->Caption=str;
break;
case 5:p5->Visible=true;
d5->Visible=true;
n5->Visible=true;
d5->Caption=d4->Caption;
d4->Caption=d3->Caption;
d3->Caption=d2->Caption;
d2->Caption=d1->Caption;
d1->Caption=str;
p=1; //窗體上的尾端箭頭將開(kāi)始出現(xiàn)變化
break;
}
con++; //結(jié)點(diǎn)累加計(jì)數(shù)器
}
//---------------------------------------------------------------------------
void __fastcall TForm1::endClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender) //窗體建立時(shí)的設(shè)置
{
int i;
sg->Cells[0][0]=" 結(jié) 點(diǎn)";
sg->Cells[1][0]=" 數(shù) 據(jù)";
for(i=1;i<20;i++)
sg->Cells[0][i]=" 結(jié)點(diǎn) "+IntToStr(i);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::dspClick(TObject *Sender) //顯示按鈕的點(diǎn)擊事件
{
for(i=1;i<20;i++)
sg->Cells[1][i]="";
H.dplist(); //先將字符串表格的數(shù)據(jù)清除,再顯示初鏈結(jié)內(nèi)容
}
//---------------------------------------------------------------------------
void __fastcall TForm1::serhClick(TObject *Sender) //查找按鈕的單擊事件
{
String key=ein->Text;
r=H.serh(key); //調(diào)用搜索鏈表的函數(shù),獲取搜索到key的鏈接地址
if(r==0) //沒(méi)有找到搜索值后,窗體的變化
{
lout->Font->Color=clRed;
lout->Font->Size=15;
lout->Caption="沒(méi)有這筆數(shù)據(jù)
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -