COMP1927 16x1 Computing 2 C Outside the Style Guide, Linked Lists and Function Pointers 1.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Stacks, Queues, and Linked Lists
DATA STRUCTURES USING C++ Chapter 5
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Linked List Variations
CSE Lecture 12 – Linked Lists …
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Programming Languages and Paradigms The C Programming Language.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review Learn about linked lists
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
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.
Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Introduction to Data Structures Systems Programming.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Linked Lists. A linear linked list is a collection of objects, called nodes, each of which contains a data item and a pointer to the next node in the.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
C++ Programming:. Program Design Including
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists (cont) [Chapter 4; Chapter 6, pp ]
Linked List :: Basic Concepts
5.13 Recursion Recursive functions Functions that call themselves
Lectures linked lists Chapter 6 of textbook
UNIT-3 LINKED LIST.
Data Structures and Algorithms
Linked lists.
Programmazione I a.a. 2017/2018.
Lists.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Linked List Sudeshna Sarkar.
Lists.
Optimizing Malloc and Free
Dynamic Data Structures and Generics
Data Structures and Algorithms
Linked List.
Dynamic Data Structures and Generics
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
Linked lists.
Presentation transcript:

COMP x1 Computing 2 C Outside the Style Guide, Linked Lists and Function Pointers 1

Compiling For compiling for normal use gcc –Wall –Werror –O –o prog prog.c For compiling to run gdb or ddd or valgrind gcc –Wall –Werror –gdwarf-2 –o prog prog.c Compiling with more than 1 c file, normal use gcc –Wall –Werror –O –o prog prog.c f2.c f3.c

COMP1917 Style Required use of a restricted subset of C: layout, use of brackets (always) use only if and while no side-effects in expressions no conditional expressions all functions have one return statement But... this style used in no texts or real code

COMP1927 Style layout: consistent indentation still required use of brackets: can omit if control structure owns a single statement put function start bracket on line after function header can use all C control structures if, switch, while, for, break, continue

COMP1927 Style (cont) can use assignment statements in expressions but you should continue to avoid other kinds of side- effects can use conditional expressions but use x = c ? e1 : e2 with care functions may have several return statements, loops can have break, continue can make functions more concise, but possibly more cryptic and make proofs harder, so we still avoid

COMP1927 Style (cont) Good: gives you more freedom and power more choice in how you express programs can write code which is more concise (simpler) Bad: gives you more freedom and power can write code which is more cryptic can lead to incomprehensible, unmaintainable code

For Loop example while version sum = 0; i = 0; while (i < 10) { sum = sum + i; i++; //i = i+1; } for version sum = 0; for (i = 0; i < 10; i++) sum += i; //sum = sum + i;

Switch Statements If statements such as: if (colour == ‘r’){ print(“Red”); }else if(colour == ‘b’){ printf(“Blue”); }else if(colour == ‘g’){ printf(“Green”); }else{ printf(“Not valid”); }

Switch Statements (cont) Can be written as switch statements: switch(colour){ case ‘r’: printf(“Red”); break; case ‘b’: printf(“Blue”); break; case ‘g’: printf(“Green”); break; default : printf(“Not valid”); } Note break is critical; if not present, falls through to next case.

Exercise Write a function monthName(int) that Accepts a month number 1 = Jan..12=Dec Returns a string containing the month name Assume the string will be read-only Use a switch to decide on the month Suggest an alternative approach with an array

Jumping Around The return statement gives back result to caller of function terminates function (possibly "early") The break statement allows early termination of a loop The continue statement allows early termination of one loop iteration They all help to avoid deeply nested if statements.

Conditional Expressions If statements that compute a value if (y > 0) { x = z+1; } else { x = z -1; } can be written as a conditional expression: x = (y > 0) ? z+1 : z-1;

Exercise Conditionals Rewrite each of the following using or a conditional expression or state why it can’t be written that way //a if(x > 0) y = x – 1; else y = x + 1; //b if(x > 0) y = x – 1; else z = x + 1; //c if(x > 0){ y = x – 1; z = x + 1; }else{ y = x + 1; Z = x - 1; }

Assignments in Expressions C assignment statements are really expressions they return a result: the value being assigned the return value is generally ignored Frequently, assignment is used in loop continuation tests to combine the test with collecting the next value to make the expression of such loops more concise

Assignments in Expressions (cont) nchars = 0; ch = getchar(); while (ch != EOF) { nchars++; ch = getchar(); } can be written as nchars = 0; while ((ch = getchar()) != EOF) nchars++;

What does this code do? void whatDoesItDo(){ char ch; while ((ch = getchar()) != EOF){ if(ch == ‘\n’) break; if(ch == ‘q’) return; if(!isalpha(ch)) continue; printf(“%c”,ch); } printf(“Thanks!\n”); }

What is a linked list? sequential collection of items (i.e. no random access) we can only get to the second by accessing the first, and so on we can’t access the nth element of a list directly easy to re-arrange deleting or inserting is simple self-referent structure (may be cyclic) a list element contains a link to another list element

What is a linked list? A linked list is a set of items where each item is part of a node that also contains a link to a node. (We also call the list items list elements) The final element: 1. contains a null link, pointing to no node, or 2. refers to a dummy node (also called sentinel) containing no item 3. may be the first node (hence the list is circular)

typedef int Item; typedef struct node * link; struct node { Item item; link next; }; typedef int Item; typedef struct node * link; struct node { Item item; link next; }; Sedgewick What is a linked list? A possible C implementation insert definition for Item here itemnext 5 itemnext 3

for (curr = start; curr != NULL; curr = curr->next) { // do something with the node } for (curr = start; curr != NULL; curr = curr->next) { // do something with the node } Traversing a list link x = malloc (sizeof *x); // RIGHT link y = malloc(sizeof(struct node)); //RIGHT link z = malloc(sizeof(link)); //WRONG link x = malloc (sizeof *x); // RIGHT link y = malloc(sizeof(struct node)); //RIGHT link z = malloc(sizeof(link)); //WRONG Memory allocation Common Operations

Exercise Write a function to insert node at the beginning of the list link insertFront(link list, link newNode); Could we use this prototype instead ? void insertFront(link list, link newNode); Write a function to insert node at the end of the list link insertEnd(link list, link newNode);

link reverse (link list) { } link reverse (link list) { } Exercise Implement a function which given a linked list, reverses the order of items

link reverse (link list) { Solution link tmp; link curr = list; link rev = NULL; while (curr != NULL) { tmp = curr->next; curr->next = rev; rev = curr; curr = tmp; } return rev; }

Deletion on lists Delete an item from a linked list (see lecture code for implementation) //Remove a given node from the list //and return the start of the list link deleteItem (link ls, link n); //Remove a given node from the list //and return the start of the list link deleteItem (link ls, link n);

Problem: deletion Deletion is awkward, as we always have to keep track of the previous node Can we delete a node if we only have the pointer to the node itself? We may need to traverse the whole list (if we have a reference to the head) to find the predecessor of curr! Idea: every node stores a link to the previous node, in addition to the link to the next node

Doubly Linked Lists Move forward and backward in such a list Delete node in a constant number of steps prev item next typedef struct dnode * dlink; typedef struct dnode { Item item; dlink next; dlink prev; } ; typedef struct dnode * dlink; typedef struct dnode { Item item; dlink next; dlink prev; } ;

Doubly linked lists Deleting nodes easier, more efficient Other basic list operations pointer to previous node is necessary in many operations, doesn’t have to be maintained separately for doubly linked lists twice the number of pointer manipulations necessary for most list operations memory overhead to store additional pointer

Function Pointers C can pass functions by passing a pointer to them. Function pointers... are references to memory addresses of functions are pointer values and can be assigned/passed

Function Pointers E.g. a pointer to a function mapping int → int int (*fun)(int) Function pointer variables/parameters are declared as: typeOfReturnValue (*fname)(typeOfArguments)

Example int square(int x){ return x*x;} int timesTwo(int x){return x*2;} int (*fp)(int); fp = &square; //fp points to the square function int n = (*fp)(10); //call the square function with input 10 fp = timesTwo; //works without the & //fp points to the timesTwo function n = (*fp)(2); //call the timesTwo function with input 2 n = fp(2); //can also use normal function call //notation

Higher-order Functions Functions that get other functions as arguments, or return functions as a result Example: the function traverse takes a list and a function pointer as argument and applies the function to all nodes in the list void printList(link ls){ link curr = ls; while(curr != NULL){ printf(“%d “,curr->data); //Process the node curr = curr->next; } // apply function f to all nodes in ls void traverse (link ls, void (*f) (link)){ link curr = ls; while(curr != NULL){ (*f) (curr); curr = curr->next; } void printList(link ls){ link curr = ls; while(curr != NULL){ printf(“%d “,curr->data); //Process the node curr = curr->next; } // apply function f to all nodes in ls void traverse (link ls, void (*f) (link)){ link curr = ls; while(curr != NULL){ (*f) (curr); curr = curr->next; }

Using Function pointers void printNode(link ls){ if(ls != NULL){ printf("%d->",ls->data); } void traverse (link ls, void (*f) (link)); //To call the function //Function must have matching prototype traverse(myList,printNode); traverse(myList,printGrade); void printNode(link ls){ if(ls != NULL){ printf("%d->",ls->data); } void traverse (link ls, void (*f) (link)); //To call the function //Function must have matching prototype traverse(myList,printNode); traverse(myList,printGrade); void printGrade(link ls){ if(ls != NULL){ if(ls->data>= 85){ printf("HD "); } else { printf("FL "); } void printGrade(link ls){ if(ls != NULL){ if(ls->data>= 85){ printf("HD "); } else { printf("FL "); }

Valgrind Demo Valgrind is useful for Finding memory leaks Not freeing memory that you malloced Finding memory errors Memory errors Illegally trying access memory

Exercise Consider this alternate linked list definition: typedef int Item; typedef struct node * link; struct node { Item item; link next; }; typedef struct listImp * List; struct listImp{ link first; link last; } typedef int Item; typedef struct node * link; struct node { Item item; link next; }; typedef struct listImp * List; struct listImp{ link first; link last; }

Exercise (cont) Draw an empty list Suppose we insert items 1, 2 and 3 at the end of an empty list  Draw the list  How many struct nodes would we have?  How many struct listImps would we have?  Write code to create a new Empty List and to insert an element at the end of the list

Solutions next first list last NULL list first NULL last