CS 261 - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

CS 261

Description:

... Implementation struct ListStack ... How would this compare to a DynArr (a dynamic array implementation ... Queues Read the lesson on queues ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 11
Provided by: Eric4334
Category:

less

Transcript and Presenter's Notes

Title: CS 261


1
CS 261 Data Structures
  • Linked Lists Introduction
  • Implementation of Stack

2
Linked Lists Characteristics
  • Data elements held in structures called links
  • Like a chain each link is tied to the next
  • Links are 1 1 with elements, allocated and
    released as necessary
  • Each link points to next link in sequence,
    sometimes to previous link
  • Lots of variations on a simple idea

link
link
data
data

next link
next link
3
Typical Link Structure Single Linked List
  • struct Link / Single link. /
  • TYPE val / Data contained by this
    link. /
  • struct Link next / Pointer to next link. /

next
val
4
Linked List Variations
  • All linked lists consists of links but there
    are other design decisions
  • Header (special value to point to start) or no
    header?
  • Use null as terminator, or special value
    (sentinel) for end?
  • Use single or double links?
  • Pointer to first element only, or pointer to
    first and last?

5
Link List Stack Simplest List Structure
  • Implementing a stack interface with a linked
    list
  • Header with head reference only null if empty
  • No sentinel null terminated
  • Singly linked
  • Elements added or removed from front
  • Only access first element

Very Simple
6
List Stack Illustration
head
val 2 next
val 7 next
val 4 next null
7
List Stack Implementation
  • struct ListStack
  • struct Link head / Initialize routine sets
    to zero/NULL. /
  • void pushListStack(struct ListStack s, double
    val)
  • / You are going to write this
  • 1. Allocate (malloc) a new link (check that it
    works!).
  • 2. Set data fields in the new link.
  • 3. Change head to point to new link. /

8
Other Operations?
  • How do you tell if stack is empty?
  • How do you return first element (i.e., head)?
  • How do you remove an element?

9
Linked List Stack Time Complexity
  • Time complexity of ListStack operations
  • Push O(1) always
  • Pop O(1) always
  • Top O(1) always
  • How would this compare to a DynArr (a dynamic
    array implementation of a stack)?
  • Push O( )
  • Pop O( )
  • Top O( )
  • In practice, dynamic array is slightly faster in
    real timings

10
List Stack Computational Complexity
  • Compare linked list stack to dynamic array
    stack
  • In practice dynArr is slightly faster in real
    timings

ListStack DynArr
push O(1) always O(1) expected
pop O(1) always O(1) always
top O(1) always O(1) always
11
Your turn
  • Complete worksheet on linked list stack
  • Next time New ADT topic ? Queues
  • Read the lesson on queues posted on web site
Write a Comment
User Comments (0)
About PowerShow.com