# Data Structures AH Computing. Description and exemplification of the following variable types/data structures: 2-D arrays, records, queues, stacks.

## Presentation on theme: "Data Structures AH Computing. Description and exemplification of the following variable types/data structures: 2-D arrays, records, queues, stacks."— Presentation transcript:

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 Stack A B Push B Push C C Top of Stack

Stack A B C Top of Stack Pop Top of Stack Pop

LIFO 12 55 8 27 16 Consider the integers 16, 27, 8, 55 and 12. Pushing them into a stack in the order given would produce… Top of Stack Bottom of Stack

LIFO 35 12 55 8 27 16 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 Bottom of Stack

LIFO 35 12 55 8 27 16 The last number in is always the first number out. A stack is therefore called a LIFO structure Last In First Out Top of Stack Bottom of Stack

Exercise 1 17 39 6 17 39 6 88 17 39 6 Consider the following stack sequence… 39 6 Stack Pointer Explain the stack operations in terms of PUSH, POP and pointer changes

Stack Underflow 39 6 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

Stack Overflow 39 6 Stack size usually limited If the maximum size is exceeded then a Stack Overflow will occur

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 If Stack_Pointer> Maximum Then Output “Stack Overflow” Else Stack_Pointer=Stack_Pointer + 1 Stack(Stack_Pointer)=Data item EndIf Push a new item on a stack

Implementation of a Stack If Stack_Pointer< Minimum Then Output “Stack Underflow” Else Data item =Stack(Stack_Pointer) Stack_Pointer=Stack_Pointer - 1 EndIf Pop an item off a stack

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

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 12 55 8 27 16 Head of queue End of queue If the number 35 is to be added then it joins the end of the queue (Pushed). The queue now becomes…

Queue Example 12 55 8 27 16 35 Head of queue End of queue If a data item has to be removed from the queue then it is popped from the head of the queue. In this case if 12 is popped then the situation becomes..

Queue Example 55 8 27 16 35 Head of queue End 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.

Implementation of a 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 Adding an item to the queue

Implementation of a 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 Removing an item from the queue

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 FieldType SurnameString FirstNameString GenderCharacter AddressString PostCodeString Customer NumberString A record can be considered as a two dimensional array with different data types.

Record Example SurnameFirstNameSexAddress 1PostCode‘phone TodAndyM35 Brookside Dr TY7 8UK 225 3625 BoydMaryF27 The Grange OB7 RF1 335 2901 BellCharlesM2 Larch RdHT5 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 Structure Books Dim Title As String Dim Author As String Dim ISBN As String Dim Price As Decimal End Structure Dim BookInfo As Books Dim BookInfo(100) As Books ‘100 records } Fields Record Datatype Declare variable to hold 1 record

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")

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 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 ppt "Data Structures AH Computing. Description and exemplification of the following variable types/data structures: 2-D arrays, records, queues, stacks."

Similar presentations