Linked Structures, Project 1: Linked List Bryce Boe 2013/07/11 CS24, Summer 2013 C.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
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.
SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
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.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Elementary Data Structures: Part 2: Strings, 2D Arrays, Graphs
Data Structures: A Pseudocode Approach with C
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB.
Project 1 Overview (List ADT), Array List Bryce Boe 2013/10/14 CS24, Fall 2013.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Linked Structures, Project 1: Linked List Bryce Boe 2013/10/16 CS24, Fall 2013.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Review of Exam #2CS-2301, B-Term Review of Exam #2 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language,
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
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.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored.
Chapter 19 Data Structures Data Structures A data structure is a particular organization of data in memory. We want to group related items together.
Pointer Data Type and Pointer Variables
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
1 Chapter 5 Hashing General ideas Methods of implementing the hash table Comparison among these methods Applications of hashing Compare hash tables with.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
CS 450 MPX P ROJECT A Quick C Review. P OINTERS AND ADDRESSES IN C Check Manual I2 from Dr. Mooney’s website for a complete review of C topics you will.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Complexity Bryce Boe 2013/10/21 CS24, Fall Outline Compiler Review Project 1 Questions Complexity (Big-O) C++?
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
RUNNING TIME 10.4 – 10.5 (P. 551 – 555). RUNNING TIME analysis of algorithms involves analyzing their effectiveness analysis of algorithms involves analyzing.
CS 261 – Data Structures Introduction to C Programming.
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.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Separate Compilation Bryce Boe 2013/10/09 CS24, Fall 2013.
Individual Testing, Big-O, C++ Bryce Boe 2013/07/16 CS24, Summer 2013 C.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 Analysing Costs COMP 103.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
CPS 100e 6.1 What’s the Difference Here? l How does find-a-track work? Fast forward?
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CS 215 Final Review Ismail abumuhfouz Fall 2014.
CSCE 210 Data Structures and Algorithms
Circular Buffers, Linked Lists
Object Oriented Programming COP3330 / CGS5409
Dynamic Memory Allocation
Memory Allocation CS 217.
EECE.2160 ECE Application Programming
Review & Lab assignments
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Linked lists Low-level (concrete) data structure, used to implement higher-level structures Used to implement sequences/lists (see CList in Tapestry) Basis.
Presentation transcript:

Linked Structures, Project 1: Linked List Bryce Boe 2013/07/11 CS24, Summer 2013 C

Outline Separate Compilation Review “Things” from Lab 3 Linked Structures Project 1 Linked List Walk Through

SEPARATE COMPILATION REVIEW

Questions Why should you never #include a “.c” file? – Doing so doesn’t allow for separate compilation What is the purpose of the “#ifndef … #define … #endif” guard around the content of “.h” files? – Avoids structures and functions from being declared more than once

Another Question What is the primary purpose of separate compilation? – To reduce subsequent compilation time by reusing object files

“THINGS” FROM LAB 3

Code reduction tip How can we improve the following? if (size == 0) return 1; else return 0; return size == 0;

What’s the potential problem? struct List *list; if((list = malloc(sizeof(struct List))) == NULL) return NULL; if((list->_items = malloc(2*sizeof(char *))) == NULL) return NULL; list->_allocated = 2; list->_size = 0; return list; Memory leak of the memory assigned to list

What’s the potential problem? struct List *list; if((list = malloc(sizeof(struct List))) == NULL) return NULL; if((list->_items = malloc(2*sizeof(char *))) == NULL) { free(list); return NULL; } list->_allocated = 2; list->_size = 0; return list; Memory leak of the memory assigned to list

String Memory Question char msg[] = “hello world” list_push_back(msg); Should list_push_back make a copy of the string to store in the List? or Should the “user” be responsible for making a copy before calling list_push_back when necessary? list_push_back(strdup(msg));

sizeof(some_pointer) Using sizeof works for static arrays: – int nums[] = {1, 2, 3, 4, 5} – sizeof(nums) results in 20 (5 ints * 4 bytes) Using sizeof does not work for pointers (even if they are static arrays in a different scope) – int *nums = malloc(20); – sizeof(nums) results in 4 as the size of a pointer is 4 bytes (32 bit architecture)

LINKED STRUCTURES

Let’s talk about complexity When evaluating data structures and algorithms we often want to consider Time complexity – How long might an operation take as a function of the input size in the worst case, average case, best case Storage complexity – How much memory is required to complete an operation

big-O Notation We use O(?) to represent the complexity of an algorithm O(1) means the operation requires a constant time or space requirement (this is the best) – Accessing a random element in an array O(n) means the time (or space) required is linear with respect to the input size – Copying an array

Common Ordered Complexities O(1) – constant time O(log(n)) – logarithmic time O(n) – linear time O(nlog(n)) – linearithmic time O(n 2 ) – quadratic time O(2 n ) – exponential time O(n!) – factorial time

What’s wrong with using arrays to store data? Arrays require continuous chunks of memory Unless the array is full, there is wasted space Expanding the array is typically done by doubling the size – Worst case time: Have to copy all the existing items: BIG-O O(n) – Hint: realloc does this for you (think about how realloc is implemented)

How long does it take? Appending an item to a non-full array? Appending an item to a full-array? Removing an item from the end of the array? Removing an item from the beginning of the array? Accessing an element in the middle of the

Single-link Node structure struct Node { int _data; struct Node *_next; }

Node allocation walkthrough Add an initial node Add another node at the beginning Add another node at the end Remove a node at the beginning Remove a node at the end

PROJECT 1 LINKED WALKTHROUGH

Linked-implementation walk through struct List* list_construct() void list_destruct(struct List *list) int list_size(struct List *list) int list_is_empty(struct List *list) char *list_at(struct List *list, int position) int *list_push_back(struct List *list, char *ite) char *list_remove_at(struct List *list, int pos)