-8

Im a newbie with linked lists but I need fast help with it. I have 2 linked lists.

TOccurencyNodePtr = ^TOccurencyNode;
TOccurencyNode = record
 Occurency    : integer;
 Next : TOccurencyNodePtr;
end;

TIdentifierFoundNodePtr = ^TIdentifierFoundNode;
TIdentifierFoundNode = record
 Identifiers    : string;
 Next       : TIdentifierFoundNodePtr;
end;

and global variables: Identifier:string and Occurence:integer

and finally I need routine for adding it into lists like that

If Identifier is in list of identifiers then add occurence into occurence list if not add identifier to identifiers list and occurence into occurence list.

Please help me :((

  • 7
    Help you with what? What is your question? I suggest you visit http://stackoverflow.com/help/how-to-ask link first. –  Apr 18 '15 at 18:13
  • I know how to do it with tree only.. – Jakub Stibůrek Apr 18 '15 at 18:25
  • In addition to the above link I suggest you also consult the following link: http://stackoverflow.com/help/mcve. StackOverflow is THE resource once you learn how to use it. –  Apr 18 '15 at 18:28
  • Read both links and apply the info you see there to your question. Then we might help you. The info we share with you in those links is not for just having it, it's for using it. Apparently you do not use it. – ZygD Apr 18 '15 at 19:08
  • You can do a tree, but not a linked list? Look carefully: A linked list *is a* tree. It's simply a tree where each node has only one child. – Rob Kennedy Apr 18 '15 at 20:57

1 Answers1

3

Something like this:

type
  TOccurencyNodePtr = ^TOccurencyNode;
  TOccurencyNode = record
    Occurency: integer;
    Next: TOccurencyNodePtr;
  end;

  TIdentifierFoundNodePtr = ^TIdentifierFoundNode;
  TIdentifierFoundNode = record
    Identifier: string;
    Next: TIdentifierFoundNodePtr;
  end;

var
  Occurencies: TOccurencyNodePtr = nil;
  Identifiers: TIdentifierFoundNodePtr = nil;

procedure AddIdentifierOccurency(const Identifier: string; const Occurrency: Integer);
var
  IdentifierNode, LastIdentifierNode: TIdentifierFoundNodePtr;
  OccurencyNode, LastOccurencyNode: TOccurencyNodePtr;
begin
  IdentifierNode := Identifiers;
  LastIdentifierNode := nil;
  while (IdentifierNode <> nil) and (IdentifierNode.Identifier <> Identifier) do
  begin
    LastIdentifierNode := IdentifierNode;
    IdentifierNode := IdentifierNode.Next;
  end;
  if IdentiferNode = nil then
  begin
    New(IdentifierNode);
    IdentifierNode.Identifier := Identifier;
    IdentifierNode.Next := nil;
    if Identifiers = nil then
      Identifiers := IdentifierNode
    else
      LastIdentifierNode.Next := IdentifierNode;
  end;

  OccurencyNode := Occurencies;
  LastOccurencyNode := nil;
  while (OccurencyNode <> nil) and (OccurencyNode.Occurency <> Occurency) do
  begin
    LastOccurencyNode := OccurencyNode;
    OccurencyNode := OccurencyNode.Next;
  end;
  if OccurencyNode = nil then
  begin
    New(OccurencyNode);
    OccurencyNode.Occurency := Occurency;
    OccurencyNode.Next := nil;
    if Occurencies = nil then
      Occurencies := OccurencyNode
    else
      LastOccurencyNode.Next := OccurencyNode;
  end;
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770