How to resolve the algorithm Singly-linked list/Element insertion step by step in the Action! programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Singly-linked list/Element insertion step by step in the Action! programming language

Table of Contents

Problem Statement

Using this method, insert an element C into a list comprised of elements A->B, following element A.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Singly-linked list/Element insertion step by step in the Action! programming language

Source code in the action! programming language

CARD EndProg ;required for ALLOCATE.ACT

INCLUDE "D2:ALLOCATE.ACT" ;from the Action! Tool Kit. You must type 'SET EndProg=*' from the monitor after compiling, but before running this program!

DEFINE PTR="CARD"
DEFINE NODE_SIZE="3"
TYPE ListNode=[CHAR data PTR nxt]

ListNode POINTER listBegin

PROC AddBegin(CHAR v)
  ListNode POINTER n

  n=Alloc(NODE_SIZE)
  n.data=v
  n.nxt=listBegin
  listBegin=n
RETURN

PROC AddAfter(CHAR v ListNode POINTER node)
  ListNode POINTER n

  IF node=0 THEN
    PrintE("The node is null!") Break()
  ELSE
    n=Alloc(NODE_SIZE)
    n.data=v
    n.nxt=node.nxt
    node.nxt=n
  FI
RETURN

PROC Clear()
  ListNode POINTER n,next

  n=listBegin
  WHILE n
  DO
    next=n.nxt
    Free(n,NODE_SIZE)
    n=next
  OD
  listBegin=0
RETURN

PROC PrintList()
  ListNode POINTER n

  n=listBegin
  Print("(")
  WHILE n
  DO
    Put(n.data)
    IF n.nxt THEN
      Print(", ")
    FI
    n=n.nxt
  OD
  PrintE(")")
RETURN

PROC TestAddBegin(CHAR v)
  AddBegin(v)
  PrintF("Add '%C' at the begin:%E",v)
  PrintList()
RETURN

PROC TestAddAfter(CHAR v ListNode POINTER node)
  AddAfter(v,node)
  PrintF("Add '%C' after '%C':%E",v,node.data)
  PrintList()
RETURN

PROC TestClear()
  Clear()
  PrintE("Clear the list:")
  PrintList()
RETURN

PROC Main()
  Put(125) PutE() ;clear screen
  
  AllocInit(0)
  listBegin=0

  PrintList()
  TestAddBegin('A)
  TestAddAfter('B,listBegin)
  TestAddAfter('C,listBegin)
  TestClear()
RETURN

  

You may also check:How to resolve the algorithm XML/DOM serialization step by step in the Raku programming language
You may also check:How to resolve the algorithm Yellowstone sequence step by step in the Lua programming language
You may also check:How to resolve the algorithm Nested function step by step in the Jsish programming language
You may also check:How to resolve the algorithm Loops/Wrong ranges step by step in the R programming language
You may also check:How to resolve the algorithm Enforced immutability step by step in the PHP programming language