How to resolve the algorithm Queue/Definition step by step in the XPL0 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Queue/Definition step by step in the XPL0 programming language

Table of Contents

Problem Statement

Implement a FIFO queue. Elements are added at one side and popped from the other in the order of insertion.

Operations:

Errors:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Queue/Definition step by step in the XPL0 programming language

Source code in the xpl0 programming language

include c:\cxpl\codes;
def Size=8;
int Fifo(Size);
int In, Out;            \fill and empty indexes into Fifo

proc Push(A);           \Add integer A to queue
int  A;                 \(overflow not detected)
[Fifo(In):= A;
In:= In+1;
if In >= Size then In:= 0;
];

func Pop;               \Return first integer in queue
int  A;
[if Out=In then                   \if popping empty queue
    [Text(0, "Error");  exit 1];  \ then exit program with error code 1
A:= Fifo(Out);
Out:= Out+1;
if Out >= Size then Out:= 0;
return A;
];

func Empty;             \Return 'true' if queue is empty
return In = Out;

[In:= 0;  Out:= 0;
Push(0);
Text(0, if Empty then "true" else "false");  CrLf(0);
IntOut(0, Pop);  CrLf(0);
Push(1);
Push(2);
Push(3);
IntOut(0, Pop);  CrLf(0);
IntOut(0, Pop);  CrLf(0);
IntOut(0, Pop);  CrLf(0);
Text(0, if Empty then "true" else "false");  CrLf(0);

\A 256-byte queue is built in as device 8:
OpenI(8);  OpenO(8);
ChOut(8, ^0);                   \push
ChOut(0, ChIn(8));  CrLf(0);    \pop
ChOut(8, ^1);                   \push
ChOut(8, ^2);                   \push
ChOut(8, ^3);                   \push
ChOut(0, ChIn(8));  CrLf(0);    \pop
ChOut(0, ChIn(8));  CrLf(0);    \pop
ChOut(0, ChIn(8));  CrLf(0);    \pop
]

  

You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the Wren programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the LiveCode programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Rascal programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the Nemerle programming language
You may also check:How to resolve the algorithm Gray code step by step in the Action! programming language