Cs Programmming
Cs Programmming
solving: Answers
Syllabus sections covered: 4.1 (Sections 4.1.1 – 4.1.3)
Python MyList = []
for Index in range(7):
MyList.append( int(input("Enter a number: ")))
MaxIndex = 7
n = MaxIndex - 1
NoMoreSwaps = False
while NoMoreSwaps == False:
NoMoreSwaps = True
for j in range(n):
if MyList[j] > MyList[j + 1]:
Temp = MyList[j]
MyList[j] = MyList[j + 1]
MyList[j + 1] = Temp
NoMoreSwaps = False
n = n - 1
Sub Main()
For Index = 1 To 7
Console.Write("Enter a number: ")
MyList(Index) = Console.ReadLine()
Next
For Index = 1 To 7
Console.Write(MyList(Index) & " ")
Next
Console.ReadLine()
End Sub
End Module
begin
for Index := 1 to 7 do
begin
Write('Enter a number: ');
ReadLn(MyList[Index]);
end;
MaxIndex := 7;
n := MaxIndex - 1;
repeat
NoMoreSwaps := True;
for j := 1 to n do
if MyList[j] > MyList[j + 1]
then
begin
Temp := MyList[j];
MyList[j] := MyList[j + 1];
MyList[j + 1] := Temp;
NoMoreSwaps := False;
end;
n := n - 1;
until NoMoreSwaps;
for Index := 1 to 7 do
write(MyList[Index], ' ');
ReadLn;
end.
List(CurrentItem + 1) = List(CurrentItem)
CurrentItem = CurrentItem - 1
End While
List(CurrentItem + 1) = ItemToBeInserted
Next
End Module
{$APPTYPE CONSOLE}
uses
SysUtils;
var Pointer, NumberOfItems, ItemToBeInserted,
CurrentItem : integer;
List : array[1..6] of integer;
begin
NumberOfItems := 6;
List[1] := 53;
List[2] := 21;
List[3] := 60;
List[4] := 18;
List[5] := 42;
List[6] := 19;
for Pointer := 1 to NumberOfItems do
write(List[Pointer], ' ');
writeln;
for Pointer := 2 to NumberOfitems do
begin
ItemToBeInserted := List[Pointer];
CurrentItem := Pointer - 1;
while (List[CurrentItem] > ItemToBeInserted)
and (CurrentItem > 0) do
begin
List[CurrentItem + 1] :=
List[CurrentItem];
CurrentItem := CurrentItem - 1;
end;
List[CurrentItem + 1] := ItemToBeInserted;
end;
for Pointer := 1 to NumberOfItems do
Task 23.03
List
[1 [2 [3 [4 [5 [6 [7 [8 [9 [1 [1 [1 [1 [1 [1 [1 [1 [1 [1 [2
] ] ] ] ] ] ] ] ] 0] 1] 2] 3] 4] 5] 6] 7] 8] 9] 0]
7 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 8 9 9
2 9 3 7 3 7 1 5 6 9 0 2 1 5 0 4 8 2 9
Task 23.04
Python # NullPointer should be set to -1 if using array element with index 0
NULLPOINTER = -1
def InitialiseList() :
List = [ListNode() for i in range(8)]
StartPointer = NULLPOINTER # set start pointer
FreeListPtr = 0 # set starting position of free list
for Index in range(7) : # link all nodes to make free list
List[Index].Pointer = Index + 1
List[7].Pointer = NULLPOINTER #last node of free list
return(List, StartPointer, FreeListPtr)
if PreviousNodePtr == NULLPOINTER :
# insert new node at start of list
List[NewNodePtr].Pointer = StartPointer
StartPointer = NewNodePtr
else : # insert new node between previous node and this node
List[NewNodePtr].Pointer = List[PreviousNodePtr].Pointer
List[PreviousNodePtr].Pointer = NewNodePtr
else :
print("no space for more data")
return(List, StartPointer, FreeListPtr)
def GetOption() :
print("1: insert a value")
print("2: delete a value")
print("3: find a value")
print("4: output list")
print("5: end program")
option = input("Enter your choice: ")
return(option)
Option = GetOption()
while Option != "5" :
if Option == "1" :
Data = input("Enter the value: ")
List, StartPointer, FreeListPtr = InsertNode(List, StartPointer,
FreeListPtr, Data)
OutputAllNodes(List, StartPointer)
elif Option == "2" :
Data = input("Enter the value: ")
List, StartPointer, FreeListPtr = DeleteNode(List, StartPointer,
FreeListPtr, Data)
Sub InitialiseList()
StartPointer = NULLPOINTER ' set start pointer
FreeListPtr = 0 ' set starting position of
free list
For Index = 0 To 7 'link all nodes to make free
list
List(Index).Pointer = Index + 1
Next
List(7).Pointer = NULLPOINTER 'last node of free list
End Sub
Sub DeleteNode(DataItem)
Dim ThisNodePtr, PreviousNodePtr As Integer
ThisNodePtr = StartPointer
Try
' start at beginning of list
Do While ThisNodePtr <> NULLPOINTER And
List(ThisNodePtr).Data <> DataItem
' while not end of list and item not found
PreviousNodePtr = ThisNodePtr ' remember this node
Sub InsertNode(NewItem)
Dim ThisNodePtr, NewNodePtr, PreviousNodePtr As Integer
If FreeListPtr <> NULLPOINTER Then
' there is space in the array
' take node from free list and store data item
NewNodePtr = FreeListPtr
List(NewNodePtr).Data = NewItem
FreeListPtr = List(FreeListPtr).Pointer
' find insertion point
PreviousNodePtr = NULLPOINTER
ThisNodePtr = StartPointer ' start at beginning
of list
Try
Do While (ThisNodePtr <> NULLPOINTER) And
(List(ThisNodePtr).Data < NewItem)
' while not end of list
PreviousNodePtr = ThisNodePtr ' remember this node
' follow the pointer to the next node
ThisNodePtr = List(ThisNodePtr).Pointer
Loop
Catch ex As Exception
End Try
Sub OutputAllNodes()
Dim CurrentNodePtr As Integer
CurrentNodePtr = StartPointer ' start at beginning of list
If StartPointer = NULLPOINTER Then
Function GetOption()
Dim Choice As Char
Console.WriteLine("1: insert a value")
Console.WriteLine("2: delete a value")
Console.WriteLine("3: find a value")
Console.WriteLine("4: output list")
Console.WriteLine("5: end program")
Console.Write("Enter your choice: ")
Choice = Console.ReadLine()
Return (Choice)
End Function
Sub Main()
Dim Choice As Char
Dim Data As String
Dim CurrentNodePtr As Integer
InitialiseList()
Choice = GetOption()
Do While Choice <> "5"
Select Case Choice
Case "1"
Console.Write("Enter the value: ")
Data = Console.ReadLine()
InsertNode(Data)
OutputAllNodes()
Case "2"
Console.Write("Enter the value: ")
Data = Console.ReadLine()
DeleteNode(Data)
OutputAllNodes()
Case "3"
Console.Write("Enter the value: ")
Data = Console.ReadLine()
CurrentNodePTr = FindNode(Data)
Case "4"
OutputAllNodes()
Console.WriteLine(StartPointer & " " &
FreeListPtr)
For i = 0 To 7
Console.WriteLine(i & " " & List(i).Data & "
" & List(i).Pointer)
Next
End Select
Choice = GetOption()
Loop
End Sub
End Module
{$APPTYPE CONSOLE}
uses
SysUtils;
procedure InitialiseList;
var Index : integer;
begin
StartPointer := NULLPOINTER; // set start pointer
FreeListPtr := 0; // set starting position of free
list
For Index := 0 To 7 do // link all nodes to make free list
List[Index].Pointer := Index + 1;
List[7].Pointer := NULLPOINTER; // last node of free list
End;
procedure OutputAllNodes();
var CurrentNodePtr : Integer;
begin
CurrentNodePtr := StartPointer; // start at beginning of list
If StartPointer = NULLPOINTER
Then WriteLn('No data in list');
While CurrentNodePtr <> NULLPOINTER do // while not end of list
begin
WriteLn(CurrentNodePtr , ' ' , List[CurrentNodePtr].Data);
// follow the pointer to the next node
CurrentNodePtr := List[CurrentNodePtr].Pointer;
end;
End;
procedure Main();
var Choice : Char;
Data : String;
CurrentNodePtr, i : Integer;
begin
InitialiseList;
Choice := GetOption();
While Choice <> '5' do
begin
Case Choice of
'1': begin
Write('Enter the value: ');
ReadLn(Data);
InsertNode(Data);
OutputAllNodes();
end;
'2': begin
Write('Enter the value: ') ;
begin
main;
end.
Task 23.05
Python # NullPointer should be set to -1 if using array element with index
0
NULLPOINTER = -1
def InitialiseStack() :
Stack = [Node() for i in range(8)]
TopOfStack = NULLPOINTER # set start pointer
FreeListPtr = 0 # set starting position of
free list
for Index in range(7) : # link all nodes to make
free list
Stack[Index].Pointer = Index + 1
Stack[7].Pointer = NULLPOINTER #last node of free list
return(Stack, TopOfStack, FreeListPtr)
def GetOption() :
print("1: push a value")
print("2: pop a value")
print("3: output stack")
print("4: end program")
option = input("Enter your choice: ")
return(option)
Option = GetOption()
while Option != "4" :
if Option == "1" :
Data = input("Enter the value: ")
Stack, TopOfStack, FreeListPtr = Push(Stack, TopOfStack,
FreeListPtr, Data)
OutputAllNodes(Stack, TopOfStack)
elif Option == "2" :
Stack, TopOfStack, FreeListPtr, Value = Pop(Stack,
TopOfStack, FreeListPtr)
print("Data popped: ", Value)
OutputAllNodes(Stack, TopOfStack)
elif Option == "3" :
OutputAllNodes(Stack, TopOfStack)
print(TopOfStack, FreeListPtr)
for i in range(8) :
print(i, " ", Stack[i].Data, " ", Stack[i].Pointer)
Option = GetOption()
Sub InitialiseStack()
TopOfStack = NULLPOINTER ' set start pointer
FreeListPtr = 0 ' set starting position of
free list
For Index = 0 To 7 'link all nodes to make free
Function Pop()
Dim ThisNodePtr As Integer
Dim Value As String
If TopOfStack = NULLPOINTER Then
Console.WriteLine("no data on stack")
Value = ""
Else
Value = Stack(TopOfStack).Data
ThisNodePtr = TopOfStack
TopOfStack = Stack(TopOfStack).Pointer
Stack(ThisNodePtr).Pointer = FreeListPtr
FreeListPtr = ThisNodePtr
End If
Return Value
End Function
Sub Push(NewItem)
Dim NewNodePtr As Integer
If FreeListPtr <> NULLPOINTER Then
' there is space in the array
' take node from free list and store data item
NewNodePtr = FreeListPtr
Stack(NewNodePtr).Data = NewItem
FreeListPtr = Stack(FreeListPtr).Pointer
' insert new node at top of stack
Stack(NewNodePtr).Pointer = TopOfStack
TopOfStack = NewNodePtr
Else
Console.WriteLine("no space for more data")
End If
End Sub
Sub OutputAllNodes()
Dim CurrentNodePtr As Integer
CurrentNodePtr = TopOfStack ' start at beginning of list
If TopOfStack = NULLPOINTER Then
Console.WriteLine("No data on stack")
End If
Do While CurrentNodePtr <> NULLPOINTER ' while not end of list
Console.WriteLine(CurrentNodePtr & " " &
Stack(CurrentNodePtr).Data)
' follow the pointer to the next node
CurrentNodePtr = Stack(CurrentNodePtr).Pointer
Loop
End Sub
Function GetOption()
Dim Choice As Char
Console.WriteLine("1: push a value")
Console.WriteLine("2: pop a value")
Console.WriteLine("3: output stack")
Console.WriteLine("4: end program")
Console.Write("Enter your choice: ")
Choice = Console.ReadLine()
Return (Choice)
End Function
InitialiseStack()
Choice = GetOption()
Do While Choice <> "4"
Select Case Choice
Case "1"
Console.Write("Enter the value: ")
Data = Console.ReadLine()
Push(Data)
OutputAllNodes()
Case "2"
Data = Pop()
Console.WriteLine("Data popped: " & Data)
OutputAllNodes()
Case "3"
OutputAllNodes()
Console.WriteLine(TopOfStack & " " & FreeListPtr)
For i = 1 To 7
Console.WriteLine(i & " " & Stack(i).Data & "
" & Stack(i).Pointer)
Next
End Select
Choice = GetOption()
Loop
End Sub
End Module
{$APPTYPE CONSOLE}
uses
SysUtils;
procedure InitialiseStack;
var Index : integer;
begin
TopOfStack := NULLPOINTER; // set start pointer
FreeListPtr := 0; // set starting position
of free list
For Index := 0 To 7 do // link all nodes to make
free list
Stack[Index].Pointer := Index + 1;
Stack[7].Pointer := NULLPOINTER; // last node of free
list
End;
procedure OutputAllNodes();
var CurrentNodePtr : Integer;
begin
CurrentNodePtr := TopOfStack; // start at beginning of list
If TopOfStack = NULLPOINTER
Then WriteLn('No data on stack');
While CurrentNodePtr <> NULLPOINTER do // while not end of
list
begin
WriteLn(CurrentNodePtr , ' ' ,
Stack[CurrentNodePtr].Data);
// follow the pointer to the next node
CurrentNodePtr := Stack[CurrentNodePtr].Pointer;
end;
End;
procedure Main();
begin
main;
end.
Task 23.06
Python # NullPointer should be set to -1 if using array element with index
0
NULLPOINTER = -1
def InitialiseQueue() :
Queue = [Node() for i in range(8)]
HeadOfQueue = NULLPOINTER # set Head of Queue pointer
EndOfQueue = NULLPOINTER # set End of Queue pointer
FreeListPtr = 0 # set starting position of
free list
for Index in range(7) : # link all nodes to make
free list
Queue[Index].Pointer = Index + 1
Queue[7].Pointer = NULLPOINTER #last node of free list
return(Queue, HeadOfQueue, EndOfQueue, FreeListPtr)
def GetOption() :
print("1: join queue")
print("2: leave queue")
print("3: output queue")
print("4: end program")
option = input("Enter your choice: ")
return(option)
Option = GetOption()
while Option != "4" :
if Option == "1" :
Data = input("Enter the value: ")
Queue, HeadOfQueue, EndOfQueue, FreeListPtr =
JoinQueue(Queue, HeadOfQueue, EndOfQueue, FreeListPtr, Data)
OutputAllNodes(Queue, HeadOfQueue)
elif Option == "2" :
Queue, HeadOfQueue, EndOfQueue, FreeListPtr, Value =
LeaveQueue(Queue, HeadOfQueue, EndOfQueue, FreeListPtr)
print("data leaving queue: ",Value)
OutputAllNodes(Queue, HeadOfQueue)
elif Option == "3" :
OutputAllNodes(Queue, HeadOfQueue)
print(HeadOfQueue, EndOfQueue, FreeListPtr)
for i in range(8) :
print(i, " ", Queue[i].Data, " ", Queue[i].Pointer)
Option = GetOption()
Sub InitialiseQueue()
HeadOfQueue = NULLPOINTER ' set start pointer
EndOfQueue = NULLPOINTER
FreeListPtr = 0 ' set starting position of
free list
For Index = 0 To 7 'link all nodes to make free
list
Queue(Index).Pointer = Index + 1
Next
Queue(7).Pointer = NULLPOINTER 'last node of free list
End Sub
Sub JoinQueue(NewItem)
Dim NewNodePtr As Integer
If FreeListPtr <> NULLPOINTER Then
' there is space in the array
' take node from free list and store data item
NewNodePtr = FreeListPtr
Queue(NewNodePtr).Data = NewItem
FreeListPtr = Queue(FreeListPtr).Pointer
Queue(NewNodePtr).Pointer = NULLPOINTER
If EndOfQueue = NULLPOINTER Then
HeadOfQueue = NewNodePtr
Else
Queue(EndOfQueue).Pointer = NewNodePtr
End If
EndOfQueue = NewNodePtr
Else
Sub OutputAllNodes()
Dim CurrentNodePtr As Integer
CurrentNodePtr = HeadOfQueue ' start at beginning of queue
If HeadOfQueue = NULLPOINTER Then
Console.WriteLine("No data in queue")
End If
Do While CurrentNodePtr <> NULLPOINTER ' while not end of
list
Console.WriteLine(CurrentNodePtr & " " &
Queue(CurrentNodePtr).Data)
' follow the pointer to the next node
CurrentNodePtr = Queue(CurrentNodePtr).Pointer
Loop
End Sub
Function GetOption()
Dim Choice As Char
Console.WriteLine("1: join queue")
Console.WriteLine("2: leave queue")
Console.WriteLine("3: output queue")
Console.WriteLine("4: end program")
Console.Write("Enter your choice: ")
Choice = Console.ReadLine()
Return (Choice)
End Function
Sub Main()
Dim Choice As Char
Dim Data As String
Dim CurrentNodePtr As Integer
InitialiseQueue()
Choice = GetOption()
Do While Choice <> "4"
Select Case Choice
Case "1"
Console.Write("Enter the value: ")
Data = Console.ReadLine()
JoinQueue(Data)
OutputAllNodes()
Case "2"
Data = LeaveQueue()
Console.WriteLine("Data popped: " & Data)
OutputAllNodes()
Case "3"
OutputAllNodes()
Console.Write(HeadOfQueue & " " & " " &
EndOfQueue & " ")
Console.WriteLine(FreeListPtr)
For i = 0 To 7
Console.Write(i & " " & Queue(i).Data & "
")
Console.WriteLine(Queue(i).Pointer)
Next
End Select
Choice = GetOption()
Loop
End Sub
{$APPTYPE CONSOLE}
uses
SysUtils;
procedure InitialiseQueue;
var Index : integer;
begin
HeadOfQueue := NULLPOINTER; // set start pointer
EndOfQueue := NULLPOINTER;
FreeListPtr := 0; // set starting position
of free list
For Index := 0 To 7 do // link all nodes to make
free list
Queue[Index].Pointer := Index + 1;
Queue[7].Pointer := NULLPOINTER; // last node of free
list
End;
procedure OutputAllNodes();
var CurrentNodePtr : Integer;
begin
CurrentNodePtr := HeadOfQueue; // start at beginning of list
If HeadOfQueue = NULLPOINTER
Then WriteLn('No data in queue');
While CurrentNodePtr <> NULLPOINTER do // while not end of
list
begin
WriteLn(CurrentNodePtr , ' ' ,
Queue[CurrentNodePtr].Data);
// follow the pointer to the next node
CurrentNodePtr := Queue[CurrentNodePtr].Pointer;
end;
End;
procedure Main();
var Choice : Char;
Data : String;
i : Integer;
begin
InitialiseQueue;
Choice := GetOption();
While Choice <> '4' do
begin
Case Choice of
'1': begin
Write('Enter the value: ');
ReadLn(Data);
JoinQueue(Data);
OutputAllNodes();
end;
'2': begin
Data := LeaveQueue;
writeln('Data popped: ', Data);
OutputAllNodes();
end;
'3' : begin
OutputAllNodes();
WriteLn(HeadOfQueue , ' ', EndOfQueue, ' ' ,
begin
main;
end.
Task 23.07
Python # NullPointer should be set to -1 if using array element with index
0
NULLPOINTER = -1
def InitialiseTree() :
Tree = [TreeNode() for i in range(8)]
RootPointer = NULLPOINTER # set Root pointer
FreePtr = 0 # set starting position of free
list
for Index in range(7) : # link all nodes to make
free list
Tree[Index].LeftPointer = Index + 1
return(Tree, RootPointer, FreePtr)
def GetOption() :
print("1: add data")
print("2: find data")
print("3: traverse tree")
print("4: end program")
option = input("Enter your choice: ")
return(option)
Option = GetOption()
while Option != "4" :
if Option == "1" :
Data = input("Enter the value: ")
Tree, RootPointer, FreePtr = InsertNode(Tree, RootPointer,
FreePtr, Data)
TraverseTree(Tree, RootPointer)
elif Option == "2" :
Data = input("Enter search value: ")
ThisNodePtr = FindNode(Tree, RootPointer, Data)
if ThisNodePtr == NULLPOINTER :
print("value not found")
else :
print("value found at ", ThisNodePtr)
print(RootPointer, FreePtr)
for i in range(8) :
print(i, " ", Tree[i].LeftPointer, " ", Tree[i].Data, "
", Tree[i].RightPointer)
elif Option == "3" :
TraverseTree(Tree, RootPointer)
Option = GetOption()
Sub InitialiseTree()
RootPointer = NULLPOINTER ' set start pointer
FreePtr = 0 ' set starting position of
free list
For Index = 0 To 7 'link all nodes to make free
list
Tree(Index).LeftPointer = Index + 1
Tree(Index).RightPointer = NULLPOINTER
Tree(Index).Data = ""
Next
Tree(7).LeftPointer = NULLPOINTER 'last node of free
list
End Sub
Sub InsertNode(NewItem)
Dim NewNodePtr, ThisNodePtr, PreviousNodePtr As Integer
Dim TurnedLeft As Boolean
If FreePtr <> NULLPOINTER Then
' there is space in the array
' take node from free list and store data item
NewNodePtr = FreePtr
Tree(NewNodePtr).Data = NewItem
FreePtr = Tree(FreePtr).LeftPointer
Tree(NewNodePtr).LeftPointer = NULLPOINTER
' check if empty tree
If RootPointer = NULLPOINTER Then
RootPointer = NewNodePtr
Else ' find insertion point
ThisNodePtr = RootPointer
Do While ThisNodePtr <> NULLPOINTER
PreviousNodePtr = ThisNodePtr
If Tree(ThisNodePtr).Data > NewItem Then
TurnedLeft = True
ThisNodePtr = Tree(ThisNodePtr).LeftPointer
Else
TurnedLeft = False
ThisNodePtr = Tree(ThisNodePtr).RightPointer
End If
Loop
If TurnedLeft Then
Tree(PreviousNodePtr).LeftPointer = NewNodePtr
Sub TraverseTree(RootPointer)
If RootPointer <> NULLPOINTER Then
TraverseTree(Tree(RootPointer).LeftPointer)
Console.WriteLine(Tree(RootPointer).Data)
TraverseTree(Tree(RootPointer).RightPointer)
End If
End Sub
Function GetOption()
Dim Choice As Char
Console.WriteLine("1: add data")
Console.WriteLine("2: find data")
Console.WriteLine("3: traverse tree")
Console.WriteLine("4: end program")
Console.Write("Enter your choice: ")
Choice = Console.ReadLine()
Return (Choice)
End Function
Sub Main()
Dim Choice As Char
Dim Data As String
Dim ThisNodePtr As Integer
InitialiseTree()
Choice = GetOption()
Do While Choice <> "4"
Select Case Choice
Case "1"
Console.Write("Enter the value: ")
Data = Console.ReadLine()
InsertNode(Data)
TraverseTree(RootPointer)
Case "2"
Console.Write("Enter search value: ")
Data = Console.ReadLine()
ThisNodePtr = FindNode(Data)
If ThisNodePtr = NULLPOINTER Then
Console.WriteLine("Value not found")
Else
Console.WriteLine("value found at: " &
ThisNodePtr)
End If
Console.WriteLine(RootPointer & " " & FreePtr)
For i = 0 To 7
Console.WriteLine(i & " " &
Tree(i).LeftPointer & " " & Tree(i).Data & " " &
Tree(i).RightPointer)
Next
Case "3"
TraverseTree(RootPointer)
End Select
Choice = GetOption()
End Module
{$APPTYPE CONSOLE}
uses
SysUtils;
procedure InitialiseTree;
var Index : integer;
begin
RootPointer := NULLPOINTER; // set Root pointer
FreePtr := 0; // set starting position of
free list
for Index := 0 to 7 do
begin // link all nodes to make free list
Tree[Index].LeftPointer := Index + 1;
Tree[Index].RightPointer := NULLPOINTER;
Tree[Index].Data := '';
end;
Tree[7].LeftPointer := NULLPOINTER;
end;
end;
Choice := GetOption();
end;
End;
begin
main;
end.
Task 23.08
</code>
<code>
FUNCTION FrenchFor(EnglishWord) RETURNS STRING
Index ← Hash(EnglishWord)
WHILE (EnglishFrench[Index].Key <> EnglishWord)
AND (EnglishFrench[Index] NOT empty)
Index ← Index + 1 // go to next slot
IF Index > 999 // beyond table boundary?
THEN // wrap around to beginning of hash table
Index ← 0
ENDIF
ENDWHILE
IF EnglishFrench[Index] NOT empty // if English word found
THEN
RETURN EnglishFrench[Index].Value // return the
French word
ELSE
RETURN "Word does not exist in dictionary"
ENDIF
ENDFUNCTION
</code>
b the binary search does not work if the data in the array being searched is
not sorted in ascending order.
c i the function returns the index of the search item
ii the function returns -1
2 a i and ii
FrontOfQueue Apple
Pear
EndOfQueue Banana ∅
StartOfFreeList
Figure 23.04
b i
Python class Node :
def __init__(self) :
self.Data = ""
self.Pointer = 0
ii
Python Queue = [Node() for i in range(51)]
FrontOfQueue = 0
EndOfQueue = 0
StartOfFreeList = 0
for i in range(1, 50) :
Queue[i].Pointer = i + 1
FrontOfQueue = 0
EndOfQueue = 0
StartOfFreeList = 1
For i = 1 to 49
Queue(i) = i + 1
Next
Queue(50) = 0
FrontOfQueue := 0;
EndOfQueue := 0;
StartOfFreeList := 1;
for i := 1 to 49 do
Queue[i] := i + 1;
Queue[50] := 0;
c i
Data Type Description
Identifier
NullPointer INTEGER constant set to -1
ii
PROCEDURE JoinQueue(NewItem : STRING)