⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cd4_3u.cpp

📁 C++ Builder程序员学习数据结构,里面涵盖了所有你要学习的数据结构的所有源码(二叉树、链表、单链表、双链表、红黑树、快速排序、冒泡排序、哈弗曼树、堆、集合、字典、散列、字典、跳表、图、最短路径
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "cd4_3u.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++;                                      //结点累加计数器
}

//---------------------------------------------------------------------------

void __fastcall TForm1::endClick(TObject *Sender)
{

   Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)         //窗体建立时的设置
{
   int i;
   sg->Cells[0][0]="  结 点";
   sg->Cells[1][0]="  数 据";
   for(i=1;i<20;i++)
      sg->Cells[0][i]=" 结点 "+IntToStr(i);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::dspClick(TObject *Sender) //显示按钮的点击事件
{
   for(i=1;i<20;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->Font->Size=15;
      lout->Caption="没有这笔数据

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -