Week 9 wednesday
This presentation is the property of its rightful owner.
Sponsored Links
1 / 24

CS222 PowerPoint PPT Presentation


  • 75 Views
  • Uploaded on
  • Presentation posted in: General

Week 9 - Wednesday. CS222. Last time. What did we talk about last time? structs. Questions?. Project 4. Quotes. C combines the power and performance of assembly language with the flexibility and ease-of-use of assembly language. Anonymous. Example. struct s tudent {

Download Presentation

CS222

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Week 9 wednesday

Week 9 - Wednesday

CS222


Last time

Last time

  • What did we talk about last time?

  • structs


Questions

Questions?


Project 4

Project 4


Quotes

Quotes

C combines the power and performance of assembly language with the flexibility and ease-of-use of assembly language.

Anonymous


Example

Example

structstudent

{

char name[100];

double GPA;

intID;

};

  • Read in 100 student names, GPAs, and ID numbers

  • Sort them by ID numbers

  • Print out the values


Naming types

Naming types

  • You might have noticed that there are all these odd types floating around

    • time_t

    • size_t

  • On some systems, you will even see aliases for your basic types

    • FLOAT

    • INT32

  • How do people create new names for existing types?


Typedef

typedef

  • The typedef command allows you to make an alias for an existing type

  • You type typedef, the type you want to alias, and then the new name

  • Don't overuse typedef

  • It is useful for types like time_t which can have different meanings in different systems

typedefintSUPER_INT;

SUPER_INT value = 3; //has type int


T ypedef with structs

typedef with structs

  • The typedef command is commonly used with structs

    • Often it is built into the struct declaration process

  • It allows the programmer to leave off the stupid struct keyword when declaring variables

  • The type defined is actually struct _wombat

  • We can refer to that type as wombat

typedefstruct_wombat

{

char name[100];

double weight;

} wombat;

wombat martin;


Even more confusing

Even more confusing!

  • You can actually typedef the name of the struct to be the same without the struct part

  • Or, if you don't need the name of the struct inside itself, you can typedef an anonymous struct

typedefstructwombat

{

char name[100];

double weight;

} wombat;

typedefstruct

{

char name[100];

double weight;

} wombat;


Linked lists

Linked lists


Linked lists1

Linked lists

  • Since you have all taken CS122 (and many have taken CS221), you all know the power of the linked list

  • A linked list is a dynamic data structure with the following features:

    • Insertion, add, and delete can be O(1) time

    • Search is O(n) time

    • They are ideally suited for a merge sort

    • They are a pain to program


Singly linked list

Singly linked list

  • Node consists of data and a single next pointer

  • Advantages: fast and easy to implement

  • Disadvantages: forward movement only

head

23

47

58

X


Linked lists in c

Linked lists in C

  • Since C doesn't have classes, we can't make a self-contained linked list

  • But we can create nodes and a set of operations to use on them

  • Clearly, we will need a struct to make the node

    • It will contain data

    • It will contain a pointer to the next node in the list

  • Doubly-linked lists are possible too


An example node struct

An example node struct

  • We'll use this definition for our node for singly linked lists

  • Somewhere, we will have the following variable to hold the beginning of the list

typedefstruct_node

{

intdata;

struct_node* next;

} node;

node* head = NULL;


Add to front

Add to front

  • Let's define a function that takes a pointer to a (possibly empty) linked list and adds a value to the front

  • There are two possible ways to do it

    • Return the new head of the list

    • Take a pointer to a pointer and change it directly

node* add(node* head, int value);

void add(node** headPointer, int value);


Cs222

Find

  • Let's define a function that takes a pointer to a (possibly empty) linked list and a value and returns the node containing the value

    • Or NULL if there is no such node

node* find(node* head, int value);


Sum values

Sum values

  • Let's define a function that takes a pointer to a (possibly empty) linked list and returns the sum of the values inside

    • An empty list has a sum of 0

int sum(node* head);


Remove

Remove

  • Let's define a function that takes a pointer to a (possibly empty) linked list and deletes the first occurrence of a given value

    • List is unchanged if the value isn't found

  • There are two possible ways to do it

    • Return the new head of the list

    • Take a pointer to a pointer and change it directly

node* remove(node* head, int value);

void remove(node** headPointer, int value);


Insert in sorted order

Insert in sorted order

  • Let's define a function that takes a pointer to a (possibly empty) linked list and adds a value in sorted order (assuming that the list is already sorted)

  • There are two possible ways to do it

    • Return the new head of the list

    • Take a pointer to a pointer and change it directly

node* add(node* head, int value);

void add(node** headPointer, int value);


Cs222

Quiz


Upcoming

Upcoming


Next time

Next time…

  • The enumtype

  • Lab 9


Reminders

Reminders

  • Keep working on Project 4

  • Keep reading K&R chapter 6


  • Login