This week in CS 12 - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

This week in CS 12

Description:

This week in CS 12. Monday C wrap-up. Take a closer look at ... Search strategy typifies list traversal: start by pointing to first node; process that node; ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 14
Provided by: michaelc7
Category:
Tags: typifies | week

less

Transcript and Presenter's Notes

Title: This week in CS 12


1
This week in CS 12
  • Monday C wrap-up
  • Take a closer look at Assignment 2
  • Look at more KR demo programs at
    mikec/cs12/demo01
  • Discuss 1st midterm exam a little bit
  • Wednesday 1st midterm exam
  • Friday start linked data structures

2
Linked data structures
  • Made up of nodes and links between nodes
  • As purpose is data storage/retrieval, also
    contains information field(s) inside nodes
  • Simplest is a linear linked list with single
    links
  • Define node structure to hold info and a link
  • typedef char AirportCode4 / e.g., LAX /
  • typedef struct NodeTag
  • AirportCode airport
  • struct NodeTag link
  • NodeType, NodePointer
  • By convention, link NULL if last node in list

3
So what is a linked list, really?
  • Answer a sequence of zero or more nodes, with
    each node pointing to the next one
  • Need a pointer to the first node
  • Often referred to as the list
  • Might be NULL means it is an empty list
  • So define EMPTY(list) (list)NULL

4
Inserting a new second node
  • e.g., have (DUS?ORD?SAN),
  • want (DUS?BRU?ORD?SAN)
  • or have (DUS), want (DUS?BRU)
  • or have (), want (BRU)
  • Any other special cases?
  • A strategy
  • create new node to hold BRU call it n
  • if empty list point list at n return
  • else set n.link to list.link
  • set list.link to n return

5
Code to insert new 2nd node
  • Assume external variable for list
  • NodePointer list
  • And assume list already initialized and has at
    least one node (i.e., no special case of empty
    list), then
  • void insertNewSecondNode(void)
  • NodePointer n
  • n (NodePointer)malloc(sizeof(NodeType))
  • strcpy(n-gtairport, BRU)
  • n-gtlink list-gtlink
  • list-gtlink n

6
Searching a list for some info
  • Idea is to return a pointer to the node that
    contains the info we are searching for, or return
    NULL if the info is not in the list
  • Strategy
  • declare local node pointer - call it n
  • point n at first node in list
  • while (n points to non-null node)
  • if (ns referent has the info)
  • return n
  • else advance n to n-gtlink
  • return NULL if get this far

7
Maybe moreMaybe less
8
List traversal other notes
  • Search strategy typifies list traversal
  • start by pointing to first node
  • process that node
  • change pointer to that nodes link
  • keep going until success (e.g., found info), or
  • until end (i.e., pointing at NULL)
  • Same idea works for lots of list operations
  • e.g., print list immediately applicable
  • To append, first must get to last node
  • To remove a node, must get to it first
  • But also usually consider potential special cases
  • e.g., first node, last node, empty list, just one
    node,

9
Strategy to delete last node
  • declare 2 local node pointers current, previous
  • / then handle special cases first /
  • just return (i.e., do nothing) if list is empty
  • free(list) and return if just one node in list
  • / otherwise traverse list to find
    second-to-last node /
  • point previous at first node
  • point current at previous-gtlink
  • while (current-gtlink does not point to null)
  • advance both pointers
  • / finally, set link of second-to-last, and
    destroy last /
  • set previous-gtlink NULL
  • free (current) / Done. /

10
Code to delete last node (pt. 1)
  • void deleteLastNode(NodePointer l)
  • / note pointer to pointer allows changing
    original pointer /
  • NodePointer previous, current
  • if (l ! NULL) / case of empty list do
    nothing /
  • if ((l)-gtlink NULL) / list with 1 node
    /
  • free(l)
  • l NULL
  • else / general case (i.e., all other
    cases) /
  • previous l
  • current (l)-gtlink
  • / continued next slide /

11
Code to delete last node (pt. 2)
  • while (current-gtlink ! NULL)
  • / i.e., not at last node yet /
  • previous current
  • current current-gtlink
  • / now previous points to
    next-to-last, so make it last /
  • previous-gtlink NULL
  • / current points to old last, so
    recycle the storage /
  • free(current)
  • / end general case /
  • / end case of non-empty list /
  • / end function /

12
btw other linked structures
  • More elaborate linked lists are often useful
  • e.g., nodes with 2 links previous and next
  • Easy reverse traversal, insertion before a node,
  • But 2 links to worry about for insert, remove,
  • e.g., circular lists last points to first (and
    first points to last for 2-way circular list)
  • Choice depends on problem and efficiency (more to
    come in later chapters maybe upcoming project
    too)
  • Trees see figure 2.23 (p. 56) more later
  • Graphs chapter 10 not part of CS 12 though

13
REMINDER FROM LAST WEEK
  • The linked list data types in Standish chapter 2
  • typedef char AirportCode4
  • typedef struct NodeTag
  • AirportCode airport
  • struct NodeTag link
  • NodeType, NodePointer
Write a Comment
User Comments (0)
About PowerShow.com