DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Single Linked List Unit

01.07.2007
| 9892 views |
  • submit to reddit
        This code is a simple pascal library to handle single-linked-lists

function llsGetItem(id: cardinal; var start: pointer): pointer;

   Returns an item of specified ID, it remains in list

function llsTakeOutItem(id: cardinal; var start: pointer): pointer;

   Returns an item of specified ID, it is removed from list

procedure llsInsertItem(item: pointer; var start: pointer);

   Inserts an item to list(item is a valid header)

function llsGetItemCount(start: pointer): cardinal;

   Gets number of items

function llsNewSLLHeader: PSLLItem;

   Allocates list item header

procedure llsKillSLLHeader(hdr: pointer);

   Deallocated list item header
unit SLLMan;
interface
type
  PSLLItem = ^TSLLItem;
  TSLLItem = record
    Next: pointer;
    Data: pointer;
   end;

function llsGetItem(id: cardinal; var start: pointer): pointer;
function llsTakeOutItem(id: cardinal; var start: pointer): pointer;
procedure llsInsertItem(item: pointer; var start: pointer);
function llsGetItemCount(start: pointer): cardinal;
function llsNewSLLHeader: PSLLItem;
procedure llsKillSLLHeader(hdr: pointer);
       // These ids are numbered from 0
implementation
function malloc(size: cardinal): pointer;
begin
  GetMem(result,size);
end;
function llsGetItemCount(start: pointer): cardinal;
var
  cur: PSLLItem;
  tmp: cardinal;
begin
  if start = nil then begin llsGetItemCount := 0; Exit; end;
  tmp := 1;  cur := start;
  while (cur^.Next <> nil) do
  begin
    Inc(tmp);
    cur := cur^.Next;
  end;
  llsGetItemCount := tmp;
end;
procedure llsKillSLLHeader(hdr: pointer);
begin
  if hdr = nil then Exit;
  Free(hdr);
end;
function llsNewSLLHeader: PSLLItem;
var
  tmp: PSLLItem;
begin
  tmp := malloc(sizeof(TSLLItem));
  tmp^.Next := nil;
  tmp^.Data := nil;
  llsNewSLLHeader := tmp;
end;
function llsGetItem(id: cardinal; var start: pointer): pointer;
var
  cur: PSLLItem;
begin
  if start = nil then begin llsGetItem := nil; Exit; end;
  cur := start;
  while (id<>0) do
  begin
    if cur^.Next <> nil then
        begin
          Dec(id);
          cur := cur^.Next;
        end else
        begin
          llsGetItem := nil;
          Exit;
        end;
  end;
  llsGetItem := cur;
end;

function llsTakeOutItem(id: cardinal; var start: pointer): pointer;
var
  tmp: PSLLItem;
  last: PSLLItem;
begin
  if start = nil then begin llsTakeOutItem := nil; Exit; end;
  if (id = 0) then
    begin
      tmp := start;
      if tmp^.Next = nil then start := nil else start := tmp^.Next;
      llsTakeOutItem := tmp;
      Exit;
    end;
  tmp := start;
  repeat
    dec(id);
    last := tmp;
    tmp := tmp^.Next;
  until (id = 0);
  last^.Next := tmp^.Next;
  llsTakeOutitem := tmp;
end;
procedure llsInsertItem(item: pointer; var start: pointer);
var
  cur: PSLLItem;
begin
 if start = nil then
  begin
    start := item;
    exit;
  end;
 cur := start;
 while (cur^.Next<>nil) do cur := cur^.Next;
 cur^.Next := item;
end;

end.