Strumenti Utente

Strumenti Sito


Barra laterale

programmazione:pascal:code

Le code in pascal

Autore: Fabio Di Matteo
Ultima revisione: 05/05/2007

Come implementare le operazioni di enqueque-dequeue in una coda realizzata mediante una lista concatenata.

Program Code;
 
type Puntatore = ^cella;
	cella = record
		    info:integer;
		    next:puntatore;
		end;
 
var cont,elemento:integer;
    scelta:char;
 
    punt_testa: puntatore;
    punt_coda: puntatore;
 
 
 
 
 
    Function Enqueue(c:puntatore; var  t:puntatore;ele:integer):puntatore;
    var paus:puntatore;
    begin
	new(paus);
	paus^.info:=ele;
	paus^.next:=nil;
 
	    if c= nil then
	    begin
		t:=paus;
		c:=paus;
	    end
	    else
	    begin
		c^.next:=paus;
		c:=paus;
	    end;
 
	    Enqueue:=c;
    end;
 
 
 
    procedure visualizza(p:puntatore; var cont:integer);
    begin
    	    if p=nil then
	    begin
		{write('nil');}
		write('<--');
	    end
	    else
	    begin
 
	        write(p^.info);
		write('<--');
		cont:=cont+1;
		visualizza(p^.next,cont);
	    end;
    end;    
 
 
    procedure dequeue(var p:puntatore);
    var t:puntatore;
    begin
	new(t);
	t:=p;
	p:=p^.next;
	dispose(t);
    end;
 
 
begin {blocco principale}
cont:=0;
scelta:='0';
new(punt_testa);
new(punt_coda);
punt_testa:=nil;
punt_coda:=nil;
 
 
 
while scelta<>'q' do
begin
 
    writeln('');
    writeln(' Menu del programma ');
    writeln('');
    writeln('  1 - Enqueue ');
    writeln('  2 - Dequeque ');
    writeln('  3 - Visualizza ');
    writeln('');
    writeln('  q - Esci');
 
    write('comando: ');
    readln(scelta);
 
 
 
    if scelta='1'then
    begin
	write('elemento --> ');
	readln(elemento);
	punt_coda:=Enqueue(punt_coda,punt_testa,elemento);
    end;
 
 
 
    if scelta='2'then
    begin
	dequeue(punt_testa);
    end;
 
    if scelta='3'then
    begin
	cont:=0;
	write('Testa: ');
	visualizza(punt_testa,cont);
	writeln(' : Coda ');
	writeln('  ( ',cont,' elementi. )');
    end;
 
 
end;
writeln('Programma terminato.');
end.

programmazione/pascal/code.txt · Ultima modifica: 18/04/2018 - 15:48 (modifica esterna)