📄 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 + -