Академический Документы
Профессиональный Документы
Культура Документы
Angela Finlayson
COMPILING
For compiling for normal use
gcc Wall Werror O o prog prog.c
Linked lists:
easy to re-arrange
Item
typedef
int
Item;
item
next
Sedgewick
item
next
here
= malloc(sizeof(link));
//WRONG
Traversing a list
OPERATIONS ON LISTS
item
next
item
next
item
next
item
list = deleteFirstItem(list);
list
item
next
item
next
item
next
next
OPERATIONS ON LISTS
Delete function
Delete after element curr:
curr
curr
item
next
item
next
item
next
PROBLEM: DELETION
link tmp;
link curr = list;
link rev = NULL;
while (curr != NULL) {
tmp = curr->next;
curr->next = rev;
rev = curr;
curr = tmp;
}
return rev;
}
homework!!!!
prev
item
next
prev
item
next
prev
item
next
Deleting nodes
#include <stdlib.h>
#include <stdio.h>
#include "lists.h"
FUNCTION POINTERS
C can pass functions by passing a pointer to them.
E.g. a pointer to a function mapping int int
int (*fun)(int)
typeOfReturnValue (*fname)(typeOfArguments)
variable called fp here, and its a function pointer, which is only allowed to point to functions that have input/output that are ints
int (*fp)(int);
fp = □
n = (*fp)(2);
n = fp(2);
mystery.c
#include <stdio.h>
by passing in a and b
example1.c
#include <stdio.h>
void printSomething(int x){
printf("%d\n",x);
}
int square(int x){
return x*x;
}
int absVal(int x){
if(x < 0) return -x;
return x;
}
variable fp is a
function pointer which
only allows us to point
to functions which have
one int as an input and
one int as an output
fp = printSomething;
return 0;
}
FUNCTION POINTERS
lists.h
printList(link list);
newList();
newNode(Item item);
insertEnd(link list, link n);
insertFront(link list, link n);
deleteFirstItem(link list);
deleteNext(link curr);
CONTINUED
main .c file of prev page
useList.c
#include <stdio.h>
#include <stdlib.h>
#include "lists.h"
int main(int argc, const char * argv[]){
int i;
link myList = newList();
for(i=-9; i < 100; i = i+9){
myList = insertEnd(myList,newNode(i));
}
printList(myList);
traverse(myList,printGrade);
traverse(myList,printNode);
traverse(myList,multByTwo);
traverse(myList,printValue);
traverse(myList,divideByTwo);
traverse(myList,printValue);
printf("Negative ");
traverseIf(myList,printNode,isNegative);
traverseIf(myList,printGrade,isOdd);
traverseIf(myList,printNode,isOdd);
traverseIf(myList,printNode,isFail);
return 0;
}
CONTINUED
lists.c
file with all the function implementation (page 1 of 2)
#include <stdlib.h>
#include <stdio.h>
#include "lists.h"
void printList(link list){
link curr;
for(curr = list; curr != NULL; curr= curr->next){
printf("%d ",curr->item);
}
printf("\n");
}
link newList(){
return NULL;
}
link newNode(Item item){
link n = (link) malloc(sizeof(*n));
n->item = item;
n->next = NULL;
return n;
}
//O(n)
link insertEnd(link list, link n){
link curr;
if(list == NULL){
list = n;
} else {
for(curr = list; curr->next != NULL; curr = curr->next){
}
curr->next = n;
}
return list;
}
//O(1)
link insertFront(link list, link n){
n->next = list;
return n;
}
// These functions were created to demonstrate the use of function pointers
void printValue(link ls){
if(ls != NULL){
printf("%d ",ls->item);
}
}
void printNode(link ls){
if(ls != NULL){
printf("%d->",ls->item);
}
}
CONTINUED
lists.c
page 2 of 2
void printGrade(link ls){
if(ls != NULL){
if(ls->item >= 85){
printf("HD ");
} else if (ls->item >= 50){
printf("PS ");
}else {
printf("FL ");
}
}
}
void multByTwo(link ls){
if(ls != NULL){
ls->item = ls->item * 2;
}
}
void divideByTwo(link ls){
if(ls != NULL){
ls->item = ls->item / 2;
}
}
int isOdd(Item item){
return (item%2 != 0);
}
int isNegative(Item item){
return (item < 0);
}
int isFail(Item item){
return (item < 50);
}
void traverse(link ls, void (*visit) (link)){
link curr;
for(curr = ls; curr!= NULL; curr = curr->next){
visit(curr);
}
printf("\n");
}
TRAVERSEIF
void traverseIf(link ls, void (*visit) (link),int (*f) (Item)){
link curr;
for(curr = ls; curr!= NULL; curr = curr->next){
//Do we want to visit this node ?
if(f(curr->item)){
//function pointer here determines
visit(curr);
we want to visit the node or not
}
}
//function pointer to actually
printf("\n");
visit the node
}
if