CS 261 Fall 2006 - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

CS 261 Fall 2006

Description:

CS 261 Fall 2006 – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 11
Provided by: timoth90
Category:
Tags: fall | slink

less

Transcript and Presenter's Notes

Title: CS 261 Fall 2006


1
CS 261 - Fall 2006
  • Linked List Introduction

2
Characteristics of Linked Lists
  • Elements are held in objects termed Links
  • 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

3
A typical Link Structure
  • struct slink / single link /
  • EleType value
  • struct slink next

4
Some variations in Linked lists
  • List have header (special value to point to
    start)
  • Use null as terminator, or special value for end
  • Use single or double links?
  • Pointer to first element, or pointer to first and
    last

5
Simplest Example, List Stack
  • List stack is the simplest data structure that
    can be implemented using linked list idea
  • Keep pointer to first element (null if empty)
  • Elements are added or removed from front
  • can only access first element

6
Picture of list stack
7
Code for list stack
  • struct ListStack
  • struct slink firstLink / initialize
    routine sets to zero /
  • void listStackPush (struct listStack stk, double
    val)
  • / you are going to write this /
  • Malloc a new link (check that it works!)
  • Set data fields in the new link
  • Change firstLink to point to new link

8
What about other operations?
  • How do you tell if the stack is empty?
  • How do you return the first element?
  • How do you remove an element?

9
How fast is List Stack?
  • Compare to dynamic array Stack
  • push - list O(1) always, dyArray(1) expected
  • pop - list O(1) always, dyArray same
  • top - list O(1) always, dyArray same
  • In practice dyArray is slightly faster in real
    timings.

10
Your turn
  • Complete the worksheet on the linked list stack
  • Tomarrow - New topic - Queues
  • Read the chapter posted on web site.
Write a Comment
User Comments (0)
About PowerShow.com