Stacks, Queues, and Linked Lists - PowerPoint PPT Presentation

About This Presentation
Title:

Stacks, Queues, and Linked Lists

Description:

Times New Roman Arial Default Design Stacks, Queues, and Linked Lists Complexities Stack Implementation Stack: Array Implementation Stack: Linked ... – PowerPoint PPT presentation

Number of Views:124
Avg rating:3.0/5.0
Slides: 16
Provided by: harv87
Learn more at: http://webpages.sou.edu
Category:
Tags: linked | lists | queues | stack | stacks

less

Transcript and Presenter's Notes

Title: Stacks, Queues, and Linked Lists


1
Stacks, Queues, and Linked Lists
  • Stacks (Last in/First out List)
  • Operations Push, Pop, Test Empty, Test Full,
    Peek, Size
  • Queue(First in/First out List)
  • Operations Insert, Remove, Test Empty, Test
    Full, Peek, Size
  • Linked List(A list of elements connected by
    pointers)
  • Insert, Delete, Find, Traverse, Size
  • Advantages can grow, delete/insert with
    assignments

2
Complexities
  • Queue
  • Insert O(1)
  • Remove O(1)
  • Peek O(1)
  • isEmpty O(1)
  • isFull O(1)
  • List
  • Insert O(1)
  • Remove O(1)
  • Search O(N)
  • Stack
  • Push O(1)
  • Pop O(1)
  • Peek O(1)
  • isEmpty O(1)
  • isFull O(1)

3
Stack Implementation
  • With an array
  • Need stack array and top members.
  • Push (topltMAX) ? stacktop data lterrorgt
  • Pop (topgt0) ? return stack--toplterrorgt
  • With a linked list
  • Push insert at front of list.
  • Pop remove from front of list.
  • What are the complexities?

4
Stack Array Implementation
  • int top, arraySize Data array
  • void Stack(int s)
  • array malloc(sizeof(Data)s arraySize s
    top -1
  • int push(Data s)
  • if (isFull()) return false arraytop s
    return true
  • Data pop() if (isEmpty()) return null
    return arraytop--
  • int isEmpty() return (top lt 0)
  • int isFull() return top1
    arraySize
  • Data peek() if (isEmpty()) return null
    return arraytop

Note In C, non-zero true, zero false
5
Stack Linked List Implementation
  • Data top
  • Stack() top NULL
  • void push(Data d) d-gtlinktop topd
  • Data Pop()
  • if (isEmpty()) return null Data
    value top top value-gtlink return value
  • int isEmpty() return (top NULL)
  • Data peek() return top

6
Queue Implementation
  • With an array
  • Circular Queue
  • Need queue array, size, head, and tail pointers.
  • Insert (sizeltMAX) ? queuetailMAX data
    lterrorgt
  • Remove (size gt 0) ? return queueheadMAX
    lterrorgt
  • With a linked list
  • Insert Insert to back of queue
  • Remove Remove from front of queue.
  • What are the complexities?

7
Queue Array Implementation
  • int head, tail, entries
  • int size
  • Data array public Queue(int s)
    array malloc(sizeof(Data)s) size s
    head 0 tail -1 entries 0
  • public boolean insert(Data s) if
    (isFull()) return 0 if (tailsize) tail -1
  • arraytail s entries return
    1
  • public Data remove() if
    (isEmpty()) return NULL Data temp
    arrayhead head (head1)size
    entries-- return temp
  • int isEmpty() return entries 0
  • Data peek() if (isEmpty() return
    0 return
    arrayhead

8
Queue Linked List Implementation
  • Data head, tail
  • Queue(int s) head null tail
    null
  • int Insert(Data s) if (isEmpty())
    head tail s else last-gtlink
    value tail s return true
  • Data Remove() Data
    value head
  • if (isEmpty()) return NULL
    head head-gtlink if (head NULL) tail
    NULL return value
  • int isEmpty() return (head
    null
  • Data peek() return head

9
Linked List ImplementationSee Text Example
  • With dynamic memory
  • The data structure uses object links.
  • Insert/Delete Search assignments to change
    pointers
  • With an array (Use indices instead of data links)
  • Need to list array, size, and pointer to initial
    entry.
  • Initialization Create free entry chain.
  • Insert retrieve from free list if any and do
    normal insertion.
  • Delete Do normal insertion logic and then add to
    free list.
  • The data structure uses primitive links rather
    than object links.

10
Ordered Linked List Insertion
  • Item first
  • void insert(Item d )
  • Item previous null Item current
    firstwhile (current!NULL d-gtkey gt
    current-gtkey) previouscurrent
    currentcurrent-gtnext)
  • if (previous NULL) first d else
    previous-gtnext d
  • d-gtnext current

Note Duplicates OK in this implementation
11
Linked List Removal
  • Item remove(Item d)
  • Item current first previous NULL
  • do
  • if (current null) return NULL
  • if (!equals(current-gtkey, d-gtkey))
  • previous current current
    current-gtnext while (current!null
    !equals(current-gtkey, d-gtkey))
  • if (previous NULL) first
    first-gtnext else previous-gtnext
    current-gtnext
  • return current

12
Doubly Linked ListSee Text Example
  • Two links. One to next record and one to
    previous.
  • Characteristics.
  • More assignments to maintain links.
  • Dont need the previous temporary pointer.
  • More memory per record.
  • More secure. Used for operating systems.

13
Doubly Linked Insertion
  • void insert( Item d )
  • Item current first, previous
    NULLwhile (current!NULLcompareTo(d-gtkey,
    current-gtkey)lt0)
  • previous current currentcurrent-gtnext)
  • if (current NULL) if (previous
    NULL) first current else previous-gtnext
    d
  • else
  • if (current-gtpreviousNULL)
  • firstd d-gtnextcurrent
    current-gtpreviousd
  • else d-gtprevious
    current-gtprevious d-gtnext current
  • current-gtprevious-gtnext
    d current-gtprevious d

14
Doubly Linked Removal
  • int remove(Item d)
  • Item current first
  • do
  • if (current NULL) return NULL
  • if (current-gtkey ! d-gtkey) current
    current-gtnext while (!equals(current-gtkey
    , d-gtkey))
  • if (current-gtprevious NULL) first
    current-gtnext else current-gtprevious-gtnext
    current-gtnext
  • if (current-gtnext ! NULL)
    current-gtnext-gtprevious current-gtprevious
  • return true

15
Stack Examples
  • Matching pairs of delimiters
  • Evaluating infix expressions
  • Two stacks
  • First convert to postfix and then evaluate
  • Expression examples
  • ....lt...gt.....
  • 500/(12(345/2))(321)
Write a Comment
User Comments (0)
About PowerShow.com