?? demoform.cpp
字號:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "DemoForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
class list; //定義list類
class Node //結點類
{
public: //公有變量,函數,程序定義
Node* link; //結點指向指針
Node(String s):data(s),link(NULL){} //結點產生時的內容設置
String getdata(){return data;} //通過公有函數取得私有變量的數據
private: //私有變量,函數,程序定義
String data; //私有變量數據
};
class List //鏈結類
{
private:
Node* first; //設定首結點的鏈結
public:
void ist(String s) //插入新結點
{
Node* newnode=new Node(s); //產生新結點對象newnode
newnode->link=first;
first=newnode;
}
void dplist() //顯示鏈表
{
int i=1;
Node* P=first; //從頭開始
while(P!=NULL) //直到沒有結點為止
{
Form1->sg->Cells[1][i]=P->getdata(); //取得的私有數據在字符串表格中顯示
P=P->link;
i++; //字符串表格輔助計算器
}
}
int serh(String k) //鏈表的搜索函數
{
int i=1;
Node* P=first;
while(P->getdata()!=k) //當結點數據與搜索數據不同時執行循環
{
P=P->link;
i++;
if(P==NULL)
return 0; //沒找到返回 0
}
return i; //找到返回i
}
int del(String k) //鏈表刪除某結點的函數,即搜索到后再刪除
{
int i=1;
Node* P=first;
Node* T=first;
while(P->getdata()!=k) //搜索關鍵結點值
{
T=P;
P=P->link;
i++;
if(P==NULL)
return 0; //沒有找到,跳出函數
}
if(P==first)
first=first->link;
else
T->link=P->link; //T為P的前鏈結點
delete P; //刪除找到的結點
return i; //返回結點位置
}
void clear() //鏈表的清除函數
{
while(first!=NULL)
{
Node* P=first;
first=first->link;
delete P;
}
}
};
List H; //定義H為List類變量
String str; //定義str用來獲取輸入字符串
int r,i,p; //用r取得位置,用p決定箭頭變化
static int t,con; //t用來記錄窗體變化,com記錄結點累計
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::addClick(TObject *Sender) //加入按鈕的單擊事件
{
str=ein->Text;
H.ist(str); //調用清除鏈表的函數,將取得的字符串加入,構造1新結點
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; //增加窗體顯示結點的計數器
switch(t) //窗體上的鏈表顯示變化
{
case 1:p1->Visible=true; //設置各控件的相對反應及顯示數據
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; //窗體上的尾端箭頭將開始出現變化
break;
}
con++; //結點累加計數器
sg->Cells[0][con]=" 結點 " + IntToStr(con);
sg->RowCount=con+1;
for(i=1;i<con;i++)
sg->Cells[1][i]="";
H.dplist();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::endClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender) //窗體建立時的設置
{
int i;
sg->Cells[0][0]=" 結 點";
sg->Cells[1][0]=" 數 據";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::dspClick(TObject *Sender) //顯示按鈕的點擊事件
{
sg->RowCount=con+1;
for(i=1;i<con;i++)
sg->Cells[1][i]="";
H.dplist(); //先將字符串表格的數據清除,再顯示初鏈結內容
}
//---------------------------------------------------------------------------
void __fastcall TForm1::serhClick(TObject *Sender) //查找按鈕的單擊事件
{
String key=ein->Text;
r=H.serh(key); //調用搜索鏈表的函數,獲取搜索到key的鏈接地址
if(r==0) //沒有找到搜索值后,窗體的變化
{
lout->Font->Color=clRed;
lout->Caption="找不到匹配節點";
}
else //找到搜索值后,窗體的變化
{
lout->Font->Color=clBlack;
lout->Caption="查找結果: "+
key+" 在第 "+IntToStr(r)+" 結點";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::delClick(TObject *Sender)//刪除按鈕的單擊事件
{
str=ein->Text;
r=H.del(str); //調用刪除鏈表的函數,并將返回結果設置給r
if(r==0) //沒有找到要刪除的結點數據
{
lout->Font->Color=clRed;
lout->Caption="找不到匹配節點";
}
else
{
lout->Font->Color=clBlack;
lout->Caption="結果: "+
str+" 在第 "+IntToStr(r)+" 結點被刪除";
sp->Visible=true;
spl->Visible=true;
spd->Visible=true;
spn->Visible=true;
spd->Caption=str;
sg->RowCount=sg->RowCount-1;
switch(r) //窗體上表示刪除結點的表示變化
{
case 1:d1->Caption=d2->Caption;
d2->Caption=d3->Caption;
d3->Caption=d4->Caption;
d3->Caption=d5->Caption;
break;
case 2:d2->Caption=d3->Caption;
d3->Caption=d4->Caption;
d4->Caption=d5->Caption;
break;
case 3:d3->Caption=d4->Caption;
d4->Caption=d5->Caption;
break;
case 4:d4->Caption=d5->Caption;
break;
case 5:d5->Caption="";
break;
}
switch(con) //窗體上表示刪除結點的顯示變化
{
case 1:p1->Visible=false;
d1->Visible=false;
n1->Visible=false;
break;
case 2:p2->Visible=false;
d2->Visible=false;
n2->Visible=false;
break;
case 3:p3->Visible=false;
d3->Visible=false;
n3->Visible=false;
break;
case 4:p4->Visible=false;
d4->Visible=false;
n4->Visible=false;
break;
case 5:p5->Visible=false;
d5->Visible=false;
n5->Visible=false;
break;
case 6:p6->Visible=false;
p7->Visible=false;
p=0;
break;
}
con--; //結點計數器減1
t--; //窗體結點計數器減1(會影響加入的窗體狀態)
}
for(i=1;i<con;i++)
sg->Cells[1][i]="";
H.dplist();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::cleClick(TObject *Sender) //清除按鈕的點擊事件
{
H.clear(); //調用清除鏈表的函數
p=0; //還原所有變量的初始值
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -