Dynamic and Static Data Structures Stacks Queues A

  • Slides: 15
Download presentation
Dynamic and Static Data Structures Stacks & Queues A Level Computing

Dynamic and Static Data Structures Stacks & Queues A Level Computing

Data structures • A data structure is a – method of storing data. •

Data structures • A data structure is a – method of storing data. • The structure will be either – static or dynamic. • Static means that the – amount of data that can be stored is fixed by the programmer. • Dynamic means the – amount of data stored can vary.

Stacks • A LIFO structure – the last item of data entered – is

Stacks • A LIFO structure – the last item of data entered – is the first to be removed. • A stack works like a stack of papers waiting to be filed.

Uses of Stacks n A stack is used to store instructions needed to handle

Uses of Stacks n A stack is used to store instructions needed to handle interrupts. n The processor is working on one task but is interrupted and needs to work on another. n For example, if the printer runs out of paper, the operating system sends a signal to the processor. n The processor will stop what it is doing and instructions that handle the request for paper are pushed onto the stack.

Setting up a stack • Data items will be: – – added to the

Setting up a stack • Data items will be: – – added to the stack Push moved from the stack Pop • Instead there is a stack pointer which points to the data item that is the top of the stack. Stack pointer Colin Amar Mena Paul

Pushing and Popping the stack • The data item Simon is added (pushed) onto

Pushing and Popping the stack • The data item Simon is added (pushed) onto the stack. Stack pointer Simon Stack pointer Amar • The data Simon is Mena removed (popped) Paul from the stack. Colin Amar Mena Paul Colin

Pushing and Popping the stack • The data item Simon is removed from the

Pushing and Popping the stack • The data item Simon is removed from the stack and Mehran is added to the stack. Stack pointer Simon Amar Mena Paul Colin Stack pointer Mehran Amar Mena Paul Colin

Code to push to a stack Check if Stack is Full If Full Report

Code to push to a stack Check if Stack is Full If Full Report Error and Stop Increment Stack Pointer Insert Data. Item @ Stack. Pointer

Code to pop from a stack Check if Stack is Empty If Full Report

Code to pop from a stack Check if Stack is Empty If Full Report Error and Stop Decrement Stack Pointer

Linear Queue • A queue is a FIFO structure – the first item of

Linear Queue • A queue is a FIFO structure – the first item of data entered is the first to be removed. • It works like a queue in a bank or at a supermarket.

Setting up a queue • Data items will be: – added to the queue

Setting up a queue • Data items will be: – added to the queue – removed from the queue. • Data items are not physically removed from the queue. • Instead there is a queue pointer which points to a data item that is the top of the queue. End Pointer Start Pointer Mehran 6 5 Amar Mena 4 Pavi 3 Colin 2 1

Adding and removing data from the queue • The data item Bill is added

Adding and removing data from the queue • The data item Bill is added into the queue. End Pointer • Start Pointer Bill Mehran Amar Mena Paul Colin 6 5 4 3 2 1 The data item Colin is removed from the queue. End Pointer Start Pointer Bill Mehran Amar Mena Paul Colin 6 5 4 3 2 1

Code to remove from a queue Check if queue is empty If queue empty

Code to remove from a queue Check if queue is empty If queue empty then Error Message Increment Start Pointer Stop

Code to add to a queue Check if queue is full If queue full

Code to add to a queue Check if queue is full If queue full then Error Message Insert Data Item at End Pointer Increment End Pointer Stop

Uses of queues • The common use of queues is to create a print

Uses of queues • The common use of queues is to create a print queue on networked printers. • They are used when the program needs to run through routine processes on different items of data, such as processing bills or wages. • Batch Processing.