procedure Push(var L: List; const I: integer);
procedure Pop(var L: List);
function StackTop(const L: List): integer;
function Empty(const L: List): boolean;
procedure Init(var L: List);
procedure Clear(var L: List);
function Count(const L: List): integer;
C++Builder
Note: This is the header file LinkListUnit.h
//-----------------------------------------------------
#ifndef LinkListUnitH
#define LinkListUnitH
struct ListNode
{ int Item;
ListNode* next;
};
// note: a List is of type ListNode*
void Push(ListNode*& L, int I);
void Pop(ListNode*& L);
int StackTop(ListNode* L);
bool Empty(ListNode* L);
void Init(ListNode*& L);
void Clear(ListNode*& L);
int Count(ListNode* L);
//-----------------------------------------------------
#endif
Stack Implementation
Delphi
Note: This is located in the implementation part of LinkListUnit.pas
procedure Push(var L: List; const I: integer);
var temp: List;
begin
new(temp);
temp^.Item := I;
temp^.next := L;
L := temp;
end; // Push
procedure Pop(var L: List);
var temp: List;
begin
temp := L;
L := L^.next;
dispose(temp);
end; // Pop
function StackTop(const L: List): integer;
begin
result := L^.Item;
end; // Top
function Empty(const L: List): boolean;
begin
result := L = nil;
end; // Empty
procedure Init(var L: List);
begin
L := nil;
end; // Init
procedure Clear(var L: List);
begin
while L <> nil do
Pop(L);
end; // Clear
function Count(const L: List): integer;
var temp: List;
N: integer;
begin
N := 0;
temp := L;
while temp <> nil do
begin
inc(N);
temp := temp^.next;
end;
result := N;
end;
C++Builder
Note: This is located in the file LinkListUnit.cpp
void Push(ListNode*& L, int I)
{ ListNode* temp = new ListNode;
(*temp).Item = I;
(*temp).next = L;
L = temp;
}
void Pop(ListNode*& L)
{ ListNode* temp = L;
L = (*L).next;
delete temp;
}
StackTop, Init, Empty, Clear, and Count have been left for the student
to complete.
Post Fix Evaluation
Delphi
Note: The including unit must have a use LinkListUnit for
this to be valid.
procedure TPostFixForm.EvaluateButtonClick(Sender: TObject);
var S: string;
J, A, B, Answer: integer;
Stack : List;
begin
S := ExpressionEdit.text;
Init(Stack);
for J := 1 to length(S) do
begin
case S[J] of
'0'..'9':
Push(Stack,
ord(S[J])-ord('0'));
'+':
begin
if not empty(Stack) then
begin
A := StackTop(Stack);
Pop(Stack);
end
else
begin
ShowMessage('Expected digit at location ' + IntToStr(J));
exit
end;
if not empty(Stack) then
begin
B := StackTop(Stack);
Pop(Stack);
end
else
begin
ShowMessage('Expected digit at location ' + IntToStr(J));
exit
end;
Push(Stack, A + B);
end;
// '+'
// the cases for '-', '*', and '/' have been omitted
else
begin
ShowMessage('Invalid character at position '+IntToStr(J));
exit;
end;
end; //case
end; // for
if Empty(Stack) then
ShowMessage('Expression does not
exist')
else
begin
Answer := StackTop(Stack);
Pop(Stack);
if not Empty(Stack) then
ShowMessage('Not enough
operators in expression')
else
ResultLabel.caption
:= 'Result is ' + IntToStr(Answer);
end;
end;
C++Builder
Note: the including unit must have a #include "LinkListUnit.h"
for this to be valid.
void __fastcall TPostFixForm::EvaluateButtonClick(TObject *Sender)
{ AnsiString S = ExpressionEdit->Text;
ListNode* Stack;
Init(Stack);
for (int J = 1; J <= S.Length(); J++)
{ switch ( S[J] ){
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
Push(Stack,
S[J] - '0');
break;
case '+':
{int A;
int B;
if (! Empty(Stack))
{ A = StackTop(Stack);
Pop(Stack);
}
else
{ ShowMessage("Expected digit at location " + IntToStr(J));
return;
}
if (! Empty(Stack))
{ B = StackTop(Stack);
Pop(Stack);
}
else
{ ShowMessage("Expected digit at location " + IntToStr(J));
return;
}
Push(Stack,
A + B);
break;
}
// the cases for '-', '*', and '/' have been omitted
default:
ShowMessage("Invalid
character at position " + IntToStr(J));
return;
} // end switch
} // end for
if (Empty(Stack))
ShowMessage("Expression does not exist");
else
{ int Answer = StackTop(Stack);
Pop(Stack);
if (! Empty(Stack))
ShowMessage("Not enough
operators in expression");
else
ResultLabel -> Caption
= "Result is " + IntToStr(Answer);
}
}