YureSoaresq

Deque Duplamente Encadeado Alternado

Feb 11th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.65 KB | None | 0 0
  1. unit UDeque;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, Buttons, StdCtrls, ExtCtrls;
  8.  
  9. type
  10.   No = Record
  11.     Valor: String[10];
  12.     LE, LD: Pointer;
  13.   end;
  14.   TFDeque = class(TForm)
  15.     EditValor: TEdit;
  16.     Label1: TLabel;
  17.     EditDeque: TEdit;
  18.     Label2: TLabel;
  19.     Panel1: TPanel;
  20.     Button1: TButton;
  21.     Button2: TButton;
  22.     BitBtn1: TBitBtn;
  23.     Memo1: TMemo;
  24.     Memo2: TMemo;
  25.     procedure FormShow(Sender: TObject);
  26.     procedure Button1Click(Sender: TObject);
  27.     procedure RGClick(Sender: TObject);
  28.     procedure Button2Click(Sender: TObject);
  29.   private
  30.     { Private declarations }
  31.     procedure MostraDeque;
  32.     procedure InsereEsq;
  33.     procedure InsereDir;
  34.     procedure RetiraEsq;
  35.     procedure RetiraDir;
  36.   public
  37.     { Public declarations }
  38.   end;
  39.  
  40. var
  41.   D, Aux: ^No;
  42.   PDir, PEsq: Pointer;
  43.   FDeque: TFDeque;
  44.   LadoI: String[1];
  45.   LadoR: String[1];
  46.   X: integer;
  47.  
  48. implementation
  49.  
  50. {$R *.dfm}
  51.  
  52.  
  53. procedure TFDeque.FormShow(Sender: TObject);
  54. begin
  55.   PDir := nil;
  56.   PEsq := nil;
  57.   LadoI := 'E';
  58.   LadoR := 'E';
  59.   X := 0;
  60. end;
  61.  
  62. procedure TFDeque.Button1Click(Sender: TObject);
  63. begin
  64.   if EditValor.Text = '' then
  65.   begin
  66.     ShowMessage ('Valor inválido!');
  67.     EditValor.SetFocus;
  68.   end
  69.   else
  70.   begin
  71.     try
  72.       New(D);
  73.     Except
  74.       ShowMessage('Erro ao criar nó!');
  75.       EditValor.Clear;
  76.       EditValor.SetFocus;
  77.       Exit;
  78.     end;
  79.     if LadoI = 'E' then
  80.       InsereEsq
  81.     else
  82.       InsereDir;
  83.   end;
  84. end;
  85.  
  86. procedure TFDeque.InsereEsq;
  87. begin
  88.   D.Valor := EditValor.Text;
  89.   if PEsq = nil then
  90.   begin
  91.     PEsq := D;
  92.     PDir := D;
  93.     D.LE := D;
  94.     D.LD := D;
  95.   end
  96.   else
  97.   begin
  98.     Aux := PEsq;
  99.     Aux.LE := D;
  100.     D.LD := Aux;
  101.     D.LE := PDir;
  102.     PEsq := D;
  103.     Aux := PDir;
  104.     Aux.LD := PEsq;
  105.   end;
  106.   if X < 1 then
  107.     Inc(X)
  108.   else
  109.   begin
  110.     X := 0;
  111.     LadoI := 'D';
  112.   end;
  113.   EditValor.Clear;
  114.   EditValor.SetFocus;
  115.   MostraDeque;
  116. end;
  117.  
  118. procedure TFDeque.MostraDeque;
  119. begin
  120.   EditDeque.Text := '| ';
  121.   Aux := PEsq;
  122.   While Aux <> PDir do
  123.   begin
  124.     EditDeque.Text := EditDeque.Text + Aux.Valor + ' | ';
  125.     Aux := Aux.LD;
  126.   end;
  127.   EditDeque.Text := EditDeque.Text + Aux.Valor + ' |';
  128. end;
  129.  
  130. procedure TFDeque.InsereDir;
  131. begin
  132.   D.Valor := EditValor.Text;
  133.   if PDir = nil then
  134.   begin
  135.     PDir := D;
  136.     PEsq := D;
  137.     D.LE := D;
  138.     D.LD := D;
  139.   end
  140.   else
  141.   begin
  142.     D.LE := PDir;
  143.     Aux := PDir;
  144.     Aux.LD := D;
  145.     PDir := D;
  146.     Aux := D;
  147.     Aux.LD := PEsq;
  148.     Aux := PEsq;
  149.     Aux.LE := PDir;
  150.   end;
  151.   if X < 1 then
  152.     Inc(X)
  153.   else
  154.   begin
  155.     X := 0;
  156.     LadoI := 'E';
  157.   end;
  158.   EditValor.Clear;
  159.   EditValor.SetFocus;
  160.   MostraDeque;
  161. end;
  162.  
  163.  
  164. procedure TFDeque.RGClick(Sender: TObject);
  165. begin
  166.   EditValor.SetFocus;
  167. end;
  168.  
  169. procedure TFDeque.Button2Click(Sender: TObject);
  170. begin
  171.   if PDir = nil then
  172.   begin
  173.     ShowMessage('Deque vazio!');
  174.     EditValor.SetFocus;
  175.   end
  176.   else
  177.   begin
  178.     if PDir = PEsq then
  179.     begin
  180.       PDir := nil;
  181.       PEsq := nil;
  182.       EditDeque.Clear;
  183.       EditValor.SetFocus;
  184.     end
  185.     else
  186.     begin
  187.       if LadoR = 'E' then
  188.        RetiraEsq
  189.       else
  190.         RetiraDir;
  191.     end;
  192.   end;
  193. end;
  194.  
  195. procedure TFDeque.RetiraEsq;
  196. begin
  197.   Aux := PEsq;
  198.   Aux := Aux.LD;
  199.   PEsq := nil;
  200.   PEsq := Aux;
  201.   Aux.LE := PDir;
  202.   Aux := PDir;
  203.   Aux.LD := PEsq;
  204.   MostraDeque;
  205.   LadoR := 'D';
  206. end;
  207.  
  208. procedure TFDeque.RetiraDir;
  209. begin
  210.   Aux := PDir;
  211.   Aux := Aux.LE;
  212.   PDir := nil;
  213.   PDir := Aux;
  214.   Aux.LD := PEsq;
  215.   Aux := PEsq;
  216.   Aux.LE := PDir;
  217.   MostraDeque;
  218.   LadoR := 'E';
  219. end;
  220. end.
Advertisement
Add Comment
Please, Sign In to add comment