0% found this document useful (0 votes)
36 views

10 - Data Types and Structures

Uploaded by

Aayan Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

10 - Data Types and Structures

Uploaded by

Aayan Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

10- Data Types and Structures

Data Types Description Example


String Multiple characters. “Hello”
Integer Whole number. 10
Real Number with decimal. 10.89
Boolean True/ False values. Yes/No
Date Contains date. 30/3/2023
Char Single alphabetic character. “a”

Declaration of a variable/ identifier:


DECLARE (Name of variable/ identifier): (Data type)
DELCARE age: STRING

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.

 Items can be added to the stack (PUSH).

 Items can be removed from the stack (POP).

 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.

 The value of the base pointer always remains the same.

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:

IF topPointer < stackful THEN


topPointer ← topPointer + 1
stack[topPointer] ← item
ELSE
OUTPUT "Stack is full, cannot push"
ENDIF

To remove (POP) an item from a stack:

IF topPointer = basePointer - 1 THEN


OUTPUT "Stack is empty, cannot pop"
ELSE
Item ← stack[topPointer]
topPointer ← topPointer - 1
ENDIF

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

To add an item to the queue (Enqueue)


IF queueLength < queueful THEN
IF rearPointer < upperBound THEN
rearPointer ← rearPointer + 1
ELSE
rearPointer ← 1
ENDIF
queueLength ← queueLength + 1
queue[rearPointer] ← item
ELSE
OUTPUT "Queue is full, cannot enqueue"
ENDIF

To remove an item from the queue (Dequeue)

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.

You might also like