Stacks and Queues - PowerPoint PPT Presentation

About This Presentation
Title:

Stacks and Queues

Description:

... the front of the list (a passenger at the front of the queue getting onto the bus) ... high-numbered port which is returned to the client and starts a new server ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 23
Provided by: richar219
Category:
Tags: getting | high | new | of | queues | stacks | ways

less

Transcript and Presenter's Notes

Title: Stacks and Queues


1
Stacks and Queues
  • Introduction to stacks
  • Stack implementation
  • 4 Standard operations on a stack
  • A stack program
  • Introduction to queues
  • Queue implementation
  • Basic operations on a queue
  • A queue program

2
Introduction to stacks 1
  • A stack implements a last in first out or LIFO
    data structure. It can be visualised as a pile of
    plates. Plates are added to the top of pile and
    the plate at the top of the pile which was the
    last to be added will be the first to be removed.
  • Stacks are used to keep track of a nested set of
    subroutines (otherwise referred to as functions
    or methods) internally within an executing
    program, so that when a subroutine exits and
    returns, execution will restart at the correct
    point within the program which called the
    subroutine.
  • These internal stacks will typically be
    implemented using an area of memory allocated by
    the compiler for the purpose and making use of a
    special stack pointer to record the location of
    the top of the pile. Modern microprocessor
    hardware directly supports this kind of stack
    maintenance and referencing to maximise
    performance.

3
An example stack application
  • This requires determining which sets of copper
    items (tracks, vias, lands, polygons) on a
    printed circuit board are connected into which
    electrically conducting networks. An item with no
    netnumber is found from the list of PCB items and
    given the next available netnumber. It is then
    scanned to find other connected items which are
    given the same netnumber. Every unscanned piece
    of copper found to be connected to it is pushed
    onto the stack. On finising scanning one piece it
    is marked as scanned.
  • The next item in the stack is popped and scanned
    for connected items, all of which are given the
    same netnumber if not already so allocated and
    pushed onto the stack if unscanned. This process
    repeats until the stack is empty. The next copper
    item with no netnumber is then found and scanned,
    and items found connected to it are marked,
    stacked and processed in the same way until all
    copper items have been assigned netnumbers.

4
Stack implementation
  • A standard method of implementing a stack within
    applications is by means of a single linked list.
    The stack can only access the head of the list
    which is known as the 'top' of the stack.

5
4 Standard operations on a stack
  • int isempty(TOP t)
  • /return 1 if stack is empty otherwise, return
    0. /
  • DATA vtop(TOP t)
  • /return the value of the top element. /
  • void pop(TOP t, DATA d)
  • / return the value of the top element and remove
    it./
  • void push(TOP t, DATA d)
  • / place the value d onto the top of the stack./

6
A stack program 1
7
A stack program 2
8
A stack program 3
9
A stack program 4
10
A stack program 5
11
A stack program 6
12
Introduction to Queues
  • A queue is a first in first out structure (FIFO).
    It can be visualised as a queue of people waiting
    for a bus. The person first in the queue is the
    first on to the bus (hopefully!).
  • A standard method of implementing a queue is a
    single linked list. A queue has a 'front' and a
    'rear'. New data items are inserted into the rear
    of the list (a bus passenger joining the queue)
    and are deleted from the front of the list (a
    passenger at the front of the queue getting onto
    the bus).

13
An example queue application
  • A network socket listening for TCP service port
    requests (on port 80) for a webserver, places
    incoming requests from clients up to a maximum
    queue length at the back of a queue, discarding
    requests if the queue is full.
  • A webserver queue handling thread processes each
    new session request at the front of the queue by
    allocating the session an individual
    high-numbered port which is returned to the
    client and starts a new server thread to handle
    this client using the high-numbered port.

14
4 Standard operations on a queue
  • int isempty(QUEUE t)
  • / return 1 if queue is empty otherwise, return
    0./
  • DATA vtop(QUEUE t)
  • / return the value of the first element. /
  • void dequeue(QUEUE t, DATA d)
  • / return the value of the first element and
    remove it. /
  • void enqueue(QUEUE t, DATA d)
  • /place the value d onto the rear of the queue./

15
A queue scheduler program 1
16
A queue scheduler program 2
17
A queue scheduler program 3
18
A queue scheduler program 4
19
A queue scheduler program 5
20
A queue scheduler program 6
21
Input of queue scheduler program
  • a234
  • a56
  • b43
  • a345
  • a89
  • b75
  • b32
  • Z

22
Output of queue scheduler program
  • A's schedule
  • JOB 1 is 234
  • JOB 2 is 56
  • JOB 3 is 345
  • JOB 4 is 89
  • B's schedule
  • JOB 1 is 43
  • JOB 2 is 75
  • JOB 3 is 32
Write a Comment
User Comments (0)
About PowerShow.com