• Log in or sign up for an account here: https://www.sythe.org/login

Learn Pascal - Linking Lists

Status
Not open for further replies.

xk1llxk1llx

awesomeness lurks within this playa ;)

$100 USD Donor New
Joined
Apr 13, 2008
Sythe Gold
0
Messages
212
Tier 1 Prizebox
Pascal is Delphi, but is more object oriented, and is used for beginners.
Lighter than Java, C, C#, C++.

Well Pascal is used for Simba runescape bots, and SCAR bots.

Today we'll learn about Linked Lists....

What is a linked list

A linked list is like an array except that the amount of elements in a linked list can change unlike an array. A linked list uses pointers to point to the next or previous element.
Single linked lists

There are 2 types of single linked lists which are called queues and stacks.
Queues

Its like this item -> going to next then next until stopped at end.

Each item of a linked list is a record which has the data and a pointer to the next or previous item. Here is an example of how to declare the record for a queue and a pointer to a queue record as well as the variables needed:

program queue;

type
pQueue = ^tqueue;
tQueue = record
data: integer;
next: pQueue;
end;

var
head, last, cur: pQueue;

begin
end.

We will now make 3 procedures. The first procedure will add items to the list, the second will view the list and the third will free the memory used by the queue. Before we make the procedures lets first take a look at the main program.

begin
head := nil; {Set head to nil because there are no items in the queue}
add(1) {Add 1 to the queue using the add procedure};
add(2);
add(3);
view; {View all items in the queue}
destroy; {Free the memory used by the queue}
end.

The add procedure will take an integer as a parameter and add that integer to the end of the queue.

procedure add(i: integer);
begin
new(cur); // made a new queue, new(cur//varible) making a new queue
cur^.data := i; {Set the value of the queue item to i}
cur^.next := nil; {Set the next item in the queue to nil because it doesn't exist}
if head = nil then {If there is no head of the queue then}
head := cur {Current is the new head because it is the first item being added to the list}
else
last^.next := cur; {Set the previous last item to current because it is the new last item in the queue}
last := cur; {Make the current item the last item in the queue}
end;

The view procedure uses a loop to display the data from the first item to the last item of the queue.

procedure view;
begin
cur := head; {Set current to the beginning of the queue}
while cur <> nil do {While there is a current item}
begin
writeln(cur^.data); {Display current item}
cur := cur^.next; {Set current to the next item in the queue}
end;
end;

The destroy procedure will free the memory that was used by the queue.

procedure destroy;
begin
cur := head; {Set current to the beginning of the queue}
while cur <> nil do {While there is a item in the queue}
begin
cur := cur^.next; {Store the next item in current}
dispose(head); {Free memory used by head}
head := cur; {Set the new head of the queue to the current item}
end;
end;
 
Status
Not open for further replies.
Runescape Gamblingrunescape gambling
Back
Top