1 / 44

Data Structures

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

cosima
Download Presentation

Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

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

  10. 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…

  11. 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:

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

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

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

  15. Stack Overflow • 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 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

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

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

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

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

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

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

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

  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 A record can be considered as a two dimensional array with different data types.

  34. Record Example

  35. Records In the computer’s memory, records are stored as Each record is terminated by a CR, LF (indicated by *) and EOF control codes.

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

  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. • 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)

More Related