SINGLE LINK LIST :
Program singleLinkedList;
TYPE
E=record //element E disimpan di node ini
node:next;
int:integer;
var
head:=null; //node pertama dari list
tail:=null; //node terakhir dari list
size:=0; //jumlah value list pertama adalah kosong
function E getElement();
begin
return E;
end;
function setNext(node.E n);
begin
next=n;
end;
function getNext();
begin
return next;
end;
function size(size:integer);
begin
return size;
end;
function isEmpty(isEmpty:boolean);
begin
return size==0;
end;
procedure E first; begin
if isEmpty() return null;
return head;
end;
procedure E last; if isEmpty() return null;
return tail;
end;
procedure addFirst(E e);
type
node=record;
begin
new(node)
head==node^.getElement;
if size==0 then
tail=head;
size++;
end;
procedure addLast(E e); //menambah element dari e ke akhir list
type
node=record;
begin
new(newest)=new node<>(e,null);
if isEmpty()=null then
head=newest;
else
tail=newest;
size++;
end;
procedure E removeFirst;
type
answer=record;
begin
if isEmpty() then return null;
e answer=head.getElement;
head=head.new;
size--;
if size==0 then
tail=null;
return answer;
end;
Circularly Linked List :
Program CircularylyLinkedList;
TYPE
E=record
node:next;
int:integer;
var
head:=null;
tail:=null;
size:=0;
function setNext(node.E n);
begin
next=n;
end;
function getNext();
begin
return next;
end;
function size(size:integer);
begin
return size;
end;
function isEmpty(isEmpty:boolean);
begin
return size==0;
end;
procedure E first;
begin
if isEmpty() return null;
return tail.getNext().getElement();
end;
procedure E last;
if isEmpty() return null;
return tail.getElement;
end;
procedure rotate;
begin
if tail !=null then //jika kosong,tidak melakukan apapun
tail=tail.getNext();
end;
procedure addFirst(E e);
type
node=record;
begin
if size==0 then
tail=(new)node<>(e,null);
tail.setNext(tail);
else
node<E> newest=(new)node(e,tail.getNext());
tail.setNext(newest);
size++;
end;
procedure addLast(E e);
begin
addFirst(E);
tail=tail.getNext();
end;
procedure E removeFirst; //menghapus dan kembali ke element pertama
type
answer=record;
begin
if isEmpty() then return null; //kondisi jika tidak di hapus
Node<E> head=tail.getNext();
if head==tail then tail=null;
else tail.setNext(head.getNext()); //menghapus data pertama dari list
size--;
return head.getElement();
end;
Description: Tugas Pascal Linked List Rating: 4.5 Reviewer: Unknown ItemReviewed: Tugas Pascal Linked List
+ comments + 1 comments
141111058
Post a Comment
Correct Me If I Wrong,Please Comment?