Week 9 - Friday.  What did we talk about last time?  typedef  Linked lists.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
© red ©
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
Week 9 - Friday.  What did we talk about last time?  typedef  Linked lists.
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Week 10 - Wednesday.  What did we talk about last time?  Linked list implementations  Started doubly linked lists.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
Week 9 - Wednesday.  What did we talk about last time?  structs.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Enumerated Types and Strings Types.
Chapter 9 A Second Look at Classes and Objects - 4.
Lecture 10 union, enumeration, bit
C++ Lesson 1.
Values vs. References Lecture 13.
SETS AND VENN DIAGRAMS.
Week 4 - Friday CS221.
Introduction to Linked Lists
Elementary Programming
Week 3 - Friday CS222.
Week 9 - Wednesday CS222.
UNIT-3 LINKED LIST.
May 2017 Four Year Old Class S&T: red 2 S&T: yellow 3 S&T: blue 4 5 6
Days of the week NEXT.
សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management
Days of the Week Les docs d’Estelle –
Enumerated DATA Types Enum Types Enumerated Data Types
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Passing Functions as Parameters
Introduction to Linked Lists
DAYS OF THE WEEK.
Introduction to Programming
Time management School of Rock.
Namespaces, Typedefs & Enums
Sunday Monday Tuesday Wednesday Sunday Monday Tuesday Wednesday
Enumerations CS Fall 1999 Enumerations.
INC 161 , CPE 100 Computer Programming
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
Week of March th grade.
Reading Blocks.
Enumerated DATA Types Enum Types Enumerated Data Types
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Reading Calendar: Hatchet by Gary Paulson
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Kindergarten Homework
What Color is it?.
Kindergarten Homework
Days of the Week Monday Tuesday Wednesday Friday Thursday Saturday
Monday Tuesday Wednesday Thursday Friday Super student Great job Good choices Ready to learn Think about it Teacher’s choice Parent contact Monday.
Reading Calendar: Hatchet by Gary Paulson
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
5. 3 Coding with Denotations
Engineering H192 Winter 2005 Structs and Enumeration Prof. Almas Ansari Dept of Computer Science Engineering Anjuman College of Engineering & Technology..
Standard Version of Starting Out with C++, 4th Edition
CMPE 152: Compiler Design February 21 Class Meeting
Time 1.
Contact
NOVEMBER READING LOG Student: When you have read, record your minutes and have your parent initial the proper box (each day). At the end of the month,
WEEK 1 Monday Tuesday Wednesday Thursday Friday Saturday Sunday
2011年 5月 2011年 6月 2011年 7月 2011年 8月 Sunday Monday Tuesday Wednesday

BOOKINGS – Monday, 2nd July BAYWAVE
Abstract Data Types Stacks CSCI 240
Week 2 - Friday CS222.
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Interfaces, Enumerations, Boxing, and Unboxing
Week 7 - Monday CS 121.
Presentation transcript:

Week 9 - Friday

 What did we talk about last time?  typedef  Linked lists

One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs. Robert Firth One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs. Robert Firth

 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 typedef struct _node { int data; struct _node* next; } node; typedef struct _node { int data; struct _node* next; } node; node* head = NULL;

 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);

 There are situations where you'd like to have a set of named constants  In many cases, you'd like those constants to be different from each other  What if there were a way to create such a list of constants easily?  Enter enum !

 To create these constants, type enum and then the names of your constants in braces  Then in your code, you can use these values (which are stored as integers) enum { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }; int day = FRIDAY; if( day == SUNDAY ) printf("My 'I don't have to run' day"); int day = FRIDAY; if( day == SUNDAY ) printf("My 'I don't have to run' day");

 You can also create named enum types  Then you can declare variables of these types  Naturally, because they are constants, it is traditional to name enum values in ALL CAPS enum Color { BLACK, BLUE, GREEN, ORANGE, PURPLE, RED, WHITE, YELLOW }; enum Color color; color = YELLOW; enum Color color; color = YELLOW;

 If you want to declare enum types (and there isn't much reason to, since C treats them exactly like int values), you can use typedef to avoid typing enum all the time typedef enum { C, C_PLUS_PLUS, C_SHARP, JAVA, JAVASCSRIPT, LISP, ML, OBJECTIVE_C, PERL, PHP, PYTHON, RUBY, VISUAL_BASIC } Language; Language language1 = C; Language language2 = JAVA; typedef enum { C, C_PLUS_PLUS, C_SHARP, JAVA, JAVASCSRIPT, LISP, ML, OBJECTIVE_C, PERL, PHP, PYTHON, RUBY, VISUAL_BASIC } Language; Language language1 = C; Language language2 = JAVA;

 enum values by default start at 0 and increase by one with each new constant  In this case, the constants have the following numbering  SUNDAY : 0  MONDAY : 1  TUESDAY : 2  WEDNESDAY : 3  THURSDAY : 4  FRIDAY : 5  SATURDAY : 6 enum { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };

 You can even specify the values in the enum  If you assign values, it is possible to make two or more of the constants have the same value (usually bad)  A common reason that values are assigned is so that you can do bitwise combinations of values enum { ANIMAL = 7, MINERAL = 9, VEGETABLE = 11 }; enum { PEPPERONI = 1, SAUSAGE = 2, BACON = 4, MUSHROOMS = 8, PEPPER = 16, ONIONS = 32, OLIVES = 64, EXTRA_CHEESE = 128 }; int toppings = PEPPERONI | ONIONS | MUSHROOMS; enum { PEPPERONI = 1, SAUSAGE = 2, BACON = 4, MUSHROOMS = 8, PEPPER = 16, ONIONS = 32, OLIVES = 64, EXTRA_CHEESE = 128 }; int toppings = PEPPERONI | ONIONS | MUSHROOMS;

 One of the most common uses of enum is to specify a Boolean type  It's not a perfect system, since you can assign values other than 0 and 1 to a BOOLEAN  Likewise, other values are also true in C typedef enum { FALSE, TRUE } BOOLEAN; BOOLEAN value = TRUE; BOOLEAN flag = FALSE; typedef enum { FALSE, TRUE } BOOLEAN; BOOLEAN value = TRUE; BOOLEAN flag = FALSE;

 More on linked lists and data structures using structs and pointers

 Finish Project 4  Due tonight by midnight!  Keep reading K&R chapter 6