Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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:

1 Data Structures AH Computing

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

3 Data Constructs Stack Queue Record

4 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

5 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

6 Stacks Data elements can only be added or deleted from the top of the stack, akin to a real pile of plates or coins

7 Two operations Push: an item is added to the top of the stack Pop: the top item is taken from the stack

8 Push A Stack A B Push B Push C C Top of Stack

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

10 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

11 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

12 LIFO 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

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

14 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

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

16 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

17 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

18 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

19 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

20 Extension Page 123 Reverse Polish Notation

21 The Queue

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

23 Queue Example 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…

24 Queue Example 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..

25 Queue Example 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.

26 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

27 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

28 Queues These are used when multiple printing jobs have to be processed During scheduling of tasks in a multitasking environment

29 Exercise Review questions 5 and 6 on page 128

30 Records

31 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

32 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

33 Record Example FieldType SurnameString FirstNameString GenderCharacter AddressString PostCodeString Customer NumberString A record can be considered as a two dimensional array with different data types.

34 Record Example SurnameFirstNameSexAddress 1PostCode‘phone TodAndyM35 Brookside Dr TY7 8UK BoydMaryF27 The Grange OB7 RF BellCharlesM2 Larch RdHT5 WA

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

36 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

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

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

39 FilePut(1, BookInfo, Pointer) 'Write data to file FileGet(1, BookInfo) ' get book details from file and display

40 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

41 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

42 Task 1

43 Task 2 Page 147 Program 4

44 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


Ads by Google