Data Structures - Shawlands Academy

advertisement
Data Structures
AH Computing
Description and exemplification of the
following variable types/data
structures:
 2-D arrays, records, queues, stacks.

Data Constructs
Stack
 Queue
 Record

Stacks
Useful where the situation calls for the
most recent item to accessed first
 Examples

Store code during compilation of HLL
 Store immediate results of
calculations
 During interrupts were the status of
the program and contents of registers
are stored

Stacks
Represented in the computer’s
memory as a 1D array
 Bottom fixed and a variable stack
pointer stores the current top location
 The stack pointer is a special register
that is updated each time the contents
of the stack changes

Stacks
Data elements can
only be added or
deleted from the top
of the stack, akin to
a real pile of plates
or coins
Two operations
 Push:
an item
is added to the
top of the
stack
 Pop: the top
item is taken
from the stack
Push A
Push B
Push C
Top of Stack
C
B
A
Stack
Pop
Pop
Pop
Top of Stack
Top of Stack
Top of Stack
C
B
A
Stack
LIFO
•Consider the integers 16,
27, 8, 55 and 12.
•Pushing them into a
stack in the order given
would produce…
Top of Stack
12
55
8
27
Bottom of
Stack
16
LIFO
•If the number 35 is to
be added to the list
then it is pushed onto
the top of the stack,
the situation now
looks like:
Top of Stack
35
12
55
8
27
Bottom of
Stack
16
LIFO
•The last number in is
always the first
number out.
Top of Stack
35
12
•A stack is therefore
called a LIFO structure
55
•Last In First Out
27
8
Bottom of
Stack
16
Exercise 1
Consider the following stack sequence…
Stack
Pointer
17
39
6
88
17
39
6
17
39
6
39
6
Explain the stack operations in terms of PUSH, POP and pointer changes
Stack Underflow
•If a further 2 POP operations took
place, then the top of stack would
become less than the bottom of
the stack.
•The stack is now empty
•Attempting any further stack
operations (before a PUSH
operation took place) would
result in an error known as a
Stack Underflow
39
6
Stack Overflow
•Stack size usually limited
•If the maximum size is exceeded
then a Stack Overflow will occur
39
6
Exercise 2
Given the output stream A,B,C,D,E,F
Write down the sequence of operations
(Push for stack and Pop for unstack)
which would produce the sequence
C,B,D,E,F,A
Implementation of a Stack
Push a new item on a stack
If Stack_Pointer> Maximum Then
Output “Stack Overflow”
Else
Stack_Pointer=Stack_Pointer + 1
Stack(Stack_Pointer)=Data item
EndIf
Implementation of a Stack
Pop an item off a stack
If Stack_Pointer< Minimum Then
Output “Stack Underflow”
Else
Data item =Stack(Stack_Pointer)
Stack_Pointer=Stack_Pointer - 1
EndIf
Stacks
Used
 To store code during the compilation
of a HLL
 To store the immediate results of
calculations
 Upon interrupts, the status of the
program and the contents of the
registers are stored on top of a stack
Extension
Page 123
 Reverse Polish Notation

The Queue
The Queue
Also a 1D array (linear list)
 Similar in structure to a stack
 Data items can be inserted and
deleted at different ends
 FIFO (First In First Out)

Queue Example
Head of queue
If the number 35 is to be
added then it joins the
end of the queue
(Pushed).
12
The queue now
becomes…
27
55
8
16
End of queue
Queue Example
Head of queue
If a data item has to be
removed from the queue
then it is popped from
the head of the queue.
12
In this case if 12 is
popped then the
situation becomes..
27
55
8
16
End of queue
35
Queue Example
Head of queue
An important aspect to
realise here is that the
data itself does not
move but merely the
pointers to the head and
end of the queue.
55
8
27
16
End of queue
35
Implementation of a Queue
Adding an item to the queue
If Rear=Maximum Then
Rear=1
Else
Rear=Rear+1
EndIf
If Rear = Start -1 Or (Rear =maximum and Start=1) Then
Output “Queue Full”
Else
queue(Rear) = Data
EndIf
Implementation of a Queue
Removing an item from the queue
If Rear=Start-1 or (Rear=Maximum and Start=1) Then
Output “Queue Empty”
Else
Data=queue(Start)
EndIf
If Start=Maximum Then
Start=1
Else
Start=Start+1
EndIf
Queues
These are used when
 multiple printing jobs have to be
processed
 During scheduling of tasks in a
multitasking environment
Exercise

Review questions 5 and 6 on page
128
Records
Definition of a Record
A
data type created/customisable by
the programmer
 consisting of a set of fields/multiple
data items
 which can be of different data types
Records
Arrays can only hold data items with
the same type
 Records can contain different types of
data
 This makes them more complex to
manipulate
 Associated with databases

Record Example
Field
Type
Surname
String
FirstName
String
Gender
Character
Address
String
PostCode
String
Customer Number
String
A record can be considered as a two dimensional array with
different data types.
Record Example
PostCode
‘phone
Surname
FirstName
Sex Address 1
Tod
Andy
M
35 Brookside TY7
Dr
8UK
225
3625
Boyd
Mary
F
27 The
Grange
OB7
RF1
335
2901
Bell
Charles
M
2 Larch Rd
HT5
WA3
213
1157
Records
In the computer’s memory, records are
stored as
Record
1
* Record 2
* Record
3
* Record
4
* Record
5
* EOF
Each record is terminated by a CR, LF (indicated
by *) and EOF control codes.
Records in Visual Studio 2005
Record Datatype
Structure Books
Dim Title As String
Dim Author As String
Dim ISBN As String
Dim Price As Decimal
End Structure
Dim BookInfo As Books
}
Fields
Declare variable to hold 1 record
Dim BookInfo(100) As Books ‘100 records
Assigning values
BookInfo.Title = InputBox("Enter the title of the book")
BookInfo.Author = InputBox("Enter the author's name")
BookInfo.ISBN = InputBox("Enter the ISBN")
BookInfo.Price = InputBox("Enter the price")
Displaying record values
ListBox1.Items.Add("Title-" & BookInfo.Title)
ListBox1.Items.Add("Author-" & BookInfo.Author)
ListBox1.Items.Add("ISBN-" & BookInfo.ISBN)
ListBox1.Items.Add("Price- £" & BookInfo.Price)
FilePut(1, BookInfo, Pointer)
'Write data to file
FileGet(1, BookInfo) ' get book details from file and display
Structure Competitor
dim Title as string
dim Rider as string
dim Round1score as single
dim Round2Score as single
dim Age as integer
End Structure
Dim CompetitorType(8) as Competitor
Comparison of Arrays v.
Records
Arrays (1D & 2D)– all data must be of
same type (Integer, string,..)
 Records – fields of different types
 Arrays – simple to implement
 Records – more complex to
implement

Task 1
Task 2

Page 147 Program 4
2007 example
Charlie has a list of all the scores and competitors from a snowboard big air
competition. Charlie has entered the results in a program which allows him
to sort the list in order of “country”, “round one score” or “round two score” by
clicking on the column heading. (Adapted)
The program uses a record data structure to store each competitor’s details.
(a) Define a suitable record structure to store each competitor’s details. (3)
(b) Describe a variable based on the record structure that could store the set
of eight competitors. (3)
Download