10 - Data Types and Structures
10 - Data Types and Structures
Records:
They are composite data types which are formed by including several data items of the same or different
data types.
Declaration of records example (User-Defined Data Type)
TYPE
BOOK
DECLARE Name: STRING
DECLARE NoPages: INTEGER
DECLARE FICTION: BOOLEAN
ENDTYPE
Accessing the User-Defined Data Type:
Book1 : BOOK
Book1.Name “The Wimpy Kid”
Book1.NoPages 142
Book1.Fiction TRUE
Arrays
1
10- Data Types and Structures
1D Array
Declaration of a 1D Array:
DECLARE myArray : ARRAY [1:8] OF INTEGER
Accessing an element of a 1D array:
myArray[1] 19
2D Array
Declaration of a 2D Array:
DECLARE <arrayname> : ARRAY [LBR:UBR , LBC:UBC] OF <datatype>
DECLARE myArray : ARRAY [0:8,0:2] OF INTEGER
Accessing an element of a 2D Array:
myArray [7,0] 16
Linear Search
2
10- Data Types and Structures
Bubble Sort
Abstract Data Types (ADTs)
It is a collection of data and the operations perfromed on that data.
It has 3 important types:
1. Stack
2. Queue
3. Linked Lists
Stack:
It is a list of items which follow the principle that the first item to be input is the last item to be
ouptut.
It uses 2 pointers; A top pointer and a base pointer. The top pointer points at the last item in the
stack while the base pointer points at the first item in the stack.
Setting up a stack:
DECLARE stack ARRAY [1:10] OF INTEGER
DECLARE topPointer : INTEGER
DECLARE basePointer : INTEGER
DECLARE stackful : INTEGER
basePointer ← 1
topPointer ← 0
stackful ← 104
3
10- Data Types and Structures
To add (PUSH) an item in a stack:
Queue:
It is a list of items which follow the principle that the first item input is the first item to be output.
Items can be added to the queue (Enqueue)
Items can be removed from the queue (Dequeue).
It uses 2 pointers; front pointer and rear pointer. The front pointer points at the first item in the queue and
the rear pointer points at the last item in the queue.
The value of the front pointer changes after dequeue and the value of the back pointer changes after
enqueue.
4
10- Data Types and Structures
Circular Queue:
Setting up a queue
DECLARE queue ARRAY [1:10] OF INTEGER
DECLARE rearPointer : INTEGER
DECLARE frontPointer : INTEGER
DECLARE queueful : INTEGER
DECLARE queueLength : INTEGER
frontPointer ← 1
endPointer ← 0
upperBound ← 10
queueful ← 10
queueLength ← 0
5
10- Data Types and Structures
IF queueLength = 0 THEN
OUTPUT "Queue is empty, cannot dequeue"
ELSE
Item ← queue[frontPointer]
IF frontPointer = upperBound THEN
frontPointer ← 1
ELSE
frontPointer ← frontPointer + 1
ENDIF
queueLength ← queueLength - 1
ENDIF
Linked List:
It is a set of items in a list in which each item points at the next item in the list.
A new item is always added to the start of the list.