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

📄 cd4_4u.cpp

📁 C++ Builder程序员学习数据结构第4章
💻 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 + -