Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit UDeque;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, Buttons, StdCtrls, ExtCtrls;
- type
- No = Record
- Valor: String[10];
- LE, LD: Pointer;
- end;
- TFDeque = class(TForm)
- EditValor: TEdit;
- Label1: TLabel;
- EditDeque: TEdit;
- Label2: TLabel;
- Panel1: TPanel;
- Button1: TButton;
- Button2: TButton;
- BitBtn1: TBitBtn;
- Memo1: TMemo;
- Memo2: TMemo;
- procedure FormShow(Sender: TObject);
- procedure Button1Click(Sender: TObject);
- procedure RGClick(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- private
- { Private declarations }
- procedure MostraDeque;
- procedure InsereEsq;
- procedure InsereDir;
- procedure RetiraEsq;
- procedure RetiraDir;
- public
- { Public declarations }
- end;
- var
- D, Aux: ^No;
- PDir, PEsq: Pointer;
- FDeque: TFDeque;
- LadoI: String[1];
- LadoR: String[1];
- X: integer;
- implementation
- {$R *.dfm}
- procedure TFDeque.FormShow(Sender: TObject);
- begin
- PDir := nil;
- PEsq := nil;
- LadoI := 'E';
- LadoR := 'E';
- X := 0;
- end;
- procedure TFDeque.Button1Click(Sender: TObject);
- begin
- if EditValor.Text = '' then
- begin
- ShowMessage ('Valor inválido!');
- EditValor.SetFocus;
- end
- else
- begin
- try
- New(D);
- Except
- ShowMessage('Erro ao criar nó!');
- EditValor.Clear;
- EditValor.SetFocus;
- Exit;
- end;
- if LadoI = 'E' then
- InsereEsq
- else
- InsereDir;
- end;
- end;
- procedure TFDeque.InsereEsq;
- begin
- D.Valor := EditValor.Text;
- if PEsq = nil then
- begin
- PEsq := D;
- PDir := D;
- D.LE := D;
- D.LD := D;
- end
- else
- begin
- Aux := PEsq;
- Aux.LE := D;
- D.LD := Aux;
- D.LE := PDir;
- PEsq := D;
- Aux := PDir;
- Aux.LD := PEsq;
- end;
- if X < 1 then
- Inc(X)
- else
- begin
- X := 0;
- LadoI := 'D';
- end;
- EditValor.Clear;
- EditValor.SetFocus;
- MostraDeque;
- end;
- procedure TFDeque.MostraDeque;
- begin
- EditDeque.Text := '| ';
- Aux := PEsq;
- While Aux <> PDir do
- begin
- EditDeque.Text := EditDeque.Text + Aux.Valor + ' | ';
- Aux := Aux.LD;
- end;
- EditDeque.Text := EditDeque.Text + Aux.Valor + ' |';
- end;
- procedure TFDeque.InsereDir;
- begin
- D.Valor := EditValor.Text;
- if PDir = nil then
- begin
- PDir := D;
- PEsq := D;
- D.LE := D;
- D.LD := D;
- end
- else
- begin
- D.LE := PDir;
- Aux := PDir;
- Aux.LD := D;
- PDir := D;
- Aux := D;
- Aux.LD := PEsq;
- Aux := PEsq;
- Aux.LE := PDir;
- end;
- if X < 1 then
- Inc(X)
- else
- begin
- X := 0;
- LadoI := 'E';
- end;
- EditValor.Clear;
- EditValor.SetFocus;
- MostraDeque;
- end;
- procedure TFDeque.RGClick(Sender: TObject);
- begin
- EditValor.SetFocus;
- end;
- procedure TFDeque.Button2Click(Sender: TObject);
- begin
- if PDir = nil then
- begin
- ShowMessage('Deque vazio!');
- EditValor.SetFocus;
- end
- else
- begin
- if PDir = PEsq then
- begin
- PDir := nil;
- PEsq := nil;
- EditDeque.Clear;
- EditValor.SetFocus;
- end
- else
- begin
- if LadoR = 'E' then
- RetiraEsq
- else
- RetiraDir;
- end;
- end;
- end;
- procedure TFDeque.RetiraEsq;
- begin
- Aux := PEsq;
- Aux := Aux.LD;
- PEsq := nil;
- PEsq := Aux;
- Aux.LE := PDir;
- Aux := PDir;
- Aux.LD := PEsq;
- MostraDeque;
- LadoR := 'D';
- end;
- procedure TFDeque.RetiraDir;
- begin
- Aux := PDir;
- Aux := Aux.LE;
- PDir := nil;
- PDir := Aux;
- Aux.LD := PEsq;
- Aux := PEsq;
- Aux.LE := PDir;
- MostraDeque;
- LadoR := 'E';
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment