Week 9 - Wednesday.  What did we talk about last time?  structs.

Slides:



Advertisements
Similar presentations
Linked List Alternate approach to maintaining an array of elements Rather than allocating one large group of elements, allocate elements as needed Q: how.
Advertisements

EENG212 Algorithms and Data Structures
Linked Lists.
Linked List Variations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
C Language.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
Chapter 9 Times and Timers Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
System Files and Process Environment Password file Group file System identification Time Process environment.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
1 Homework / Exam HW7 due class 25 Exam 3 - class 26 –Open Book, Open Notes –Covers up through end of K&R 7 –and Appendix B Standard Library –Plus UNIX.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
CS Data Structures Chapter 4 Lists.
Week 8 - Friday.  What did we talk about last time?  String to int conversions  Users and groups  Password files.
Review of C++ Programming Part II Sheng-Fang Huang.
1 The UNIX date command myclock.cpp example The C/C++ time() and ctime() functions myclock2.cpp example Inline function definitions Inline class member.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
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.
C Programming - Lecture 6 This lecture we will learn: –Error checking in C –What is a wrappered function? –How to assess efficiency. –What is a clean interface?
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Timers and Clocks.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
Interacting with Unix. Getting the Process ID u Synopsis #include pid_t getpid(void); u Example: #include int main(){ pid_t n = getpid(); printf("Process.
1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Week 4 - Friday.  What did we talk about last time?  Continued doubly linked list implementation  Linked lists with iterators.
Week 4 - Wednesday.  What did we talk about last time?  Started linked lists.
Week 4 - Monday.  What did we talk about last time?  Precedence  Selection statements  Loops  Lab 3.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Advanced Programming in the UNIX Environment Hop Lee.
LINKED LISTS.
Week 6 - Friday.  What did we talk about last time?  Pointers  Passing values by reference.
Computer Science 516 Addressing Modes. Addressing modes are how our programs get to data Multiple addressing modes created for specific uses Function.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Week 9 - Friday.  What did we talk about last time?  typedef  Linked lists.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo
Week 9 - Monday CS222.
LINKED LISTS.
Lesson #8 Structures Linked Lists Command Line Arguments.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
User-Written Functions
Week 4 - Friday CS221.
C Interview Questions Prepared By:.
A bit of C programming Lecture 3 Uli Raich.
UNIT – I Linked Lists.
Sequences 6/18/2018 8:51 PM C201: Linked List.
Week 9 - Wednesday CS222.
Data Structures Interview / VIVA Questions and Answers
Week 4 - Monday CS222.
Sequences 9/18/ :20 PM C201: Linked List.
Chapter 16-2 Linked Structures
LINKED LISTS.
Govt. Polytechnic,Dhangar
Chapter 16 Linked Structures
閨怨 ~王昌齡 閨中少婦不知愁, 春日凝妝上翠樓; 忽見陌頭楊柳色, 悔教夫婿覓封侯。.
閨怨 ~王昌齡 閨中少婦不知愁, 春日凝妝上翠樓; 忽見陌頭楊柳色, 悔教夫婿覓封侯。.
Data Structures & Programming
Presentation transcript:

Week 9 - Wednesday

 What did we talk about last time?  structs

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

 What about printing out a human-readable version of the time?  ctime() takes a time_t value and returns a string giving the day and time  Alternatively, strftime() has a set of specifiers (similar to printf() ) that allow for complex ways to format the date and time printf(ctime(time(NULL)); //prints Fri Mar 15 14:22: printf(ctime(time(NULL)); //prints Fri Mar 15 14:22:

struct tm { int tm_sec; // Seconds (0-60) int tm_min; // Minutes (0-59) int tm_hour; // Hours (0-23) int tm_mday; // Day of the month (1-31) int tm_mon; // Month (0-11) int tm_year; // Year since 1900 int tm_wday; // Day of the week (Sunday = 0) int tm_yday; // Day in the year (0-365; 1 Jan = 0) int tm_isdst; /* Daylight saving time flag > 0: DST is in effect; = 0: DST is not effect; < 0: DST information not available */ }; struct tm { int tm_sec; // Seconds (0-60) int tm_min; // Minutes (0-59) int tm_hour; // Hours (0-23) int tm_mday; // Day of the month (1-31) int tm_mon; // Month (0-11) int tm_year; // Year since 1900 int tm_wday; // Day of the week (Sunday = 0) int tm_yday; // Day in the year (0-365; 1 Jan = 0) int tm_isdst; /* Daylight saving time flag > 0: DST is in effect; = 0: DST is not effect; < 0: DST information not available */ };

 gmtime() and localtime() convert a time_t value to a struct that contains "broken down" time  gmtime() gives UTC time (used to be called Greenwich Mean Time)  localtime() gives the local time, assuming it is set up correctly  mktime() can convert from a broken down time back into time_t time_t seconds = time(NULL); struct tm* brokenDownTime = NULL; brokenDownTime = localtime(&seconds); if( (*brokenDownTime).tm_wday == 1 ) printf("It's just another manic Monday.\n"); time_t seconds = time(NULL); struct tm* brokenDownTime = NULL; brokenDownTime = localtime(&seconds); if( (*brokenDownTime).tm_wday == 1 ) printf("It's just another manic Monday.\n");

 How accurate is the microsecond part of gettimeofday() ?  It depends on the accuracy of the software clock in your system  This clock measures time in units called jiffies  A jiffy used to be 10 milliseconds (100 Hz)  They raised the accuracy to 1 millisecond (1000 Hz)  Now, it can be configured for your system to 10, 4 (the default), , and 1 milliseconds

 For optimization purposes, it can be useful to know how much time a process spends running on the CPU  This time is often broken down into  User time: the amount of time your program spends executing its own code  System time: the amount of time spent in kernel mode executing code for your program (memory allocation, page faults, file opening)

 You can time a program's complete execution by running it with the time command  It will give the real time taken, user time, and system time  Let's say you've got a program called timewaster  Run it like this:  Output might be: time./timewaster real 0m4.84s user 0m1.030s sys 0m3.43s real 0m4.84s user 0m1.030s sys 0m3.43s

 Read in 100 student names, GPAs, and ID numbers  Sort them by ID numbers  Print out the values struct student { char name[100]; double GPA; int ID; }; struct student { char name[100]; double GPA; int ID; };

 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?

 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 typedef int SUPER_INT; SUPER_INT value = 3; //has type int typedef int SUPER_INT; SUPER_INT value = 3; //has type int

 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 typedef struct _wombat { char name[100]; double weight; } wombat; typedef struct _wombat { char name[100]; double weight; } wombat; wombat martin;

 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 typedef struct wombat { char name[100]; double weight; } wombat; typedef struct wombat { char name[100]; double weight; } wombat; typedef struct { char name[100]; double weight; } wombat; typedef struct { char name[100]; double weight; } wombat;

 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

 Node consists of data and a single next pointer  Advantages: fast and easy to implement  Disadvantages: forward movement only X head

 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

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

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

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

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

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

 The enum type  Lab 9

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