440 likes | 603 Views
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
E N D
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
Top of Stack Push A Push B Push C C B A Stack
Top of Stack Top of Stack Top of Stack Pop Pop Pop C B A Stack
Top of Stack Bottom of 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 Bottom of Stack 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 Bottom of Stack LIFO • The last number in is always the first number out. • A stack is therefore called a LIFO structure • Last In First Out
Exercise 1 Consider the following stack sequence… Stack Pointer 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
Stack Overflow • 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 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 • 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). The queue now becomes… 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. In this case if 12 is popped then the situation becomes.. End of queue
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. End of queue
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
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 A record can be considered as a two dimensional array with different data types.
Records In the computer’s memory, records are stored as 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 Dim BookInfo(100) As Books ‘100 records } Fields 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")
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 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. • Define a suitable record structure to store each competitor’s details. (3) • Describe a variable based on the record structure that could store the set of eight competitors. (3)