?? cd4_4u.cpp
字號:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "cd4_4u.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
class List;
class Node
{
public:
Node* next; //結點向后的指針
Node* ahead; //結點向前的指針
Node(String s):data(s),next(NULL),ahead(NULL){} //新結點產生時的內容設置
String getdata(){return data;} //外部取得結點數據需通過這個函數
private:
String data; //結點數據
};
class List
{
public:
void istf(String s) //由前方加入結點的函數
{
Node* newnode=new Node(s); //產生新結點,并作指向設置
if(first==NULL)
last=newnode;
else
first->ahead=newnode;
newnode->next=first;
first=newnode;
}
void istl(String s) //由后方加入結點的函數
{
Node* newnode=new Node(s);
if(first==NULL)
first=newnode;
else
{
last->next=newnode;
newnode->ahead=last;
}
last=newnode;
}
//---------------------------------------------------------------
void dpftb() //將鏈接由前往后顯示的函數
{
int i=1;
Node* P=first;
while(P!=NULL)
{
Form1->sg->Cells[1][i]=P->getdata(); //將結點數據輸出到字符串表格
P=P->next; //指針向后
i++;
}
}
//---------------------------------------------------------------
void dpbtf() //將鏈接由后往前顯示的函數
{
int i=1;
Node* P=last;
while(P!=NULL)
{
Form1->sg->Cells[1][i]=P->getdata(); //將結點數據輸出到字符串表格
P=P->ahead; //指針向前
i++;
}
}
//---------------------------------------------------------------
int del(String s) //刪除特定結點的函數
{
int i=1;
Node* P=first;
while(P->getdata()!=s) //當搜索值與結點內容值不同時繼續循環
{
P=P->next;
if(P==NULL)
return 0; //找不到返回零
i++;
}
if(P==first)
first=P->next;
else
P->ahead->next=P->next;
if(P==last)
last=P->ahead;
else
P->next->ahead=P->ahead;
delete P; //指針指到下一個結點后,可以將p刪除
return i; //返回刪除的結點位置
}
bool empty(){return first==NULL;} //判斷鏈表是否為空
int insert(String s, String s1) //插入某結點于某特定值后的函數
{
int i=1;
Node* P=first;
while(P->getdata()!= s ) //要先找到指定值的結點
{
P=P->next;
if(P==NULL)
return 0;
i++;
}
Node* newnode=new Node(s1); //找到指定結點后,接著構造一新結點
if(P==last) //特定結點是鏈表末端的鏈接設置
{
newnode=NULL;
last=newnode;
}
else //特定結點不是鏈表末端的鏈接設置
{
newnode->next=P->next;
P->next->ahead=newnode;
}
newnode->ahead=P;
P->next=newnode;
return i; //返回特定結點的位置
}
void clear() //清除鏈表
{
while(first!=NULL)
{
Node* T=first;
first=first->next;
delete T;
}
}
private:
Node* first; //雙向鏈表的首結點指針
Node* last; //雙向鏈表的尾結點指針
};
List list; //產生List類的新控件list
String str,str1;
int i;
static int num,c,t,r;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N1231Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) //結束事件
{
list.clear(); //調用清除鏈表
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::sbChange(TObject *Sender) //滾動軸的Change事件
{
pn->Color=sb->Position+ //改變畫板的背景色
sb->Position*256+
sb->Position*256*256;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
prg->ItemIndex=0; //指向預設為"由前"
img1->Canvas->Pen->Width=3; //設定繪制各Image控件的線條
img1->Canvas->Pen->Color=clBlue;
img1->Canvas->MoveTo(48,0);
img1->Canvas->LineTo(48,32);
img1->Canvas->MoveTo(48,32); //這一行可有可無
img1->Canvas->LineTo(128,32);
img2->Canvas->Pen->Width=3;
img2->Canvas->Pen->Color=clBlue;
img2->Canvas->MoveTo(48,0);
img2->Canvas->LineTo(48,32);
img2->Canvas->MoveTo(48,32);
img2->Canvas->LineTo(200,32);
img3->Canvas->Pen->Width=3;
img3->Canvas->Pen->Color=clBlue;
img3->Canvas->MoveTo(48,0);
img3->Canvas->LineTo(48,32);
img3->Canvas->MoveTo(48,32);
img3->Canvas->LineTo(272,32);
img4->Canvas->Pen->Width=3;
img4->Canvas->Pen->Color=clBlue;
img4->Canvas->MoveTo(48,0);
img4->Canvas->LineTo(48,32);
img4->Canvas->MoveTo(48,32);
img4->Canvas->LineTo(344,32);
img5->Canvas->Pen->Width=3;
img5->Canvas->Pen->Color=clBlue;
img5->Canvas->MoveTo(48,0);
img5->Canvas->LineTo(48,32);
img5->Canvas->MoveTo(48,32);
img5->Canvas->LineTo(416,32);
imgn->Canvas->Pen->Width=3;
imgn->Canvas->Pen->Color=clBlue;
imgn->Canvas->Pen->Width=3;
imgn->Canvas->Pen->Color=clBlue;
imgn->Canvas->MoveTo(416,32);
imgn->Canvas->LineTo(480,32);
sg->Cells[0][0]=" 結點位置";
sg->Cells[1][0]=" 結點內容";
for(i=1;i<20;i++)
sg->Cells[0][i]="第"+IntToStr(i)+" 個結點";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N1Click(TObject *Sender) //加入結點事件
{
t=(t%5)+1; //窗體顯示及結點計數器
r++;
str=InputBox("新結點輸入框 ","請輸入數據 ","");
if(c==1) //當結點超過預設顯示時的窗體變化
{
s5->Visible=false;
b5->Visible=false;
imgn->Visible=true;
if(t%2==0)
{
p6->Visible=true;
p7->Visible=false;
}
else
{
p7->Visible=true;
p6->Visible=false;
}
}
switch(r) //顯示新結點的構造
{
case 1:sp1->Visible=true;
p01->Visible=true;
s1->Visible=true;
img1->Visible=true;
t1->Visible=true;
a1->Visible=true;
d1->Visible=true;
if(c==0)
{
p1->Visible=true;
b1->Visible=true;
}
break;
case 2:sp2->Visible=true;
p02->Visible=true;
s1->Visible=false;
s2->Visible=true;
b1->Visible=false;
img2->Visible=true;
t2->Visible=true;
a2->Visible=true;
d2->Visible=true;
if(c==0)
{
p2->Visible=true;
b2->Visible=true;
}
break;
case 3:sp3->Visible=true;
p03->Visible=true;
s2->Visible=false;
s3->Visible=true;
b2->Visible=false;
img3->Visible=true;
t3->Visible=true;
a3->Visible=true;
d3->Visible=true;
if(c==0)
{
p3->Visible=true;
b3->Visible=true;
}
break;
case 4:sp4->Visible=true;
p04->Visible=true;
s3->Visible=false;
s4->Visible=true;
b3->Visible=false;
img4->Visible=true;
t4->Visible=true;
a4->Visible=true;
d4->Visible=true;
if(c==0)
{
p4->Visible=true;
b4->Visible=true;
}
break;
case 5:sp5->Visible=true;
p05->Visible=true;
s4->Visible=false;
s5->Visible=true;
b4->Visible=false;
img5->Visible=true;
t5->Visible=true;
a5->Visible=true;
d5->Visible=true;
if(c==0)
{
p5->Visible=true;
b5->Visible=true;
}
c=1;
break;
}
if(prg->ItemIndex==0) //RadioGroup索引值為0時,即"由前"
{
list.istf(str); //調用"由前"加入的函數
switch(t)
{
case 1:d5->Caption=d4->Caption; //顯示新結點的內容
d4->Caption=d3->Caption;
d3->Caption=d2->Caption;
d2->Caption=d1->Caption;
d1->Caption=str;
break;
case 2:d5->Caption=d4->Caption;
d4->Caption=d3->Caption;
d3->Caption=d2->Caption;
d2->Caption=d1->Caption;
d1->Caption=str;
break;
case 3:d5->Caption=d4->Caption;
d4->Caption=d3->Caption;
d3->Caption=d2->Caption;
d2->Caption=d1->Caption;
d1->Caption=str;
break;
case 4:d5->Caption=d4->Caption;
d4->Caption=d3->Caption;
d3->Caption=d2->Caption;
d2->Caption=d1->Caption;
d1->Caption=str;
break;
case 5:d5->Caption=d4->Caption;
d4->Caption=d3->Caption;
d3->Caption=d2->Caption;
d2->Caption=d1->Caption;
d1->Caption=str;
break;
}
}
else //RadioGroup索引值不為0時,即往后
{
list.istl(str); //調用"向后"加入的函數
switch(r)
{
case 1:d1->Caption=str;
break;
case 2:d2->Caption=str;
break;
case 3:d3->Caption=str;
break;
case 4:d4->Caption=str;
break;
case 5:d5->Caption=str;
break;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N3Click(TObject *Sender)
{
for(i=1;i<20;i++) //
sg->Cells[1][i]="";
list.dpftb(); //調用由前往后顯示的函數
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N4Click(TObject *Sender)
{
for(i=1;i<20;i++)
sg->Cells[1][i]="";
list.dpbtf(); //調用由后往前顯示的函數
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N2Click(TObject *Sender) //刪除結點事件
{
str=InputBox("刪除結點輸入框","請輸入結點內容 ","");
if(! list.empty() ) //
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -