LEC20081117

# LEC20081117 - Introduction to Computer Programming November...

This preview shows pages 1–5. Sign up to view the full content.

Introduction to Computer Programming November 17, 2008 CSC180 Fall 2008, University of Toronto

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Implement singly-linked lists Implementation: /* list.h */ #ifndef LIST_H #define LIST_H struct node { int data; struct node *next; }; /* function prototypes */ int size_list(struct node **head); void add_node(struct node **head, int data); int delete_node(struct node **head, int data); void append_node(struct node **head, int data); void print_list(struct node **head); void sorted_add_node(struct node **head, int data); void sort_list(struct node **head); CSC180 Fall 2008, University of Toronto 1
void reverse_list(struct node **head); void free_list(struct node **head); #endif /* list.c */ #include <stdio.h> #include <stdlib.h> #include "list.h" /* safe malloc, from GNU */ void *xmalloc (size_t size) { register void *value = malloc(size); if (value == 0) { fprintf(stderr, "memory exhausted"); exit(1); } return value; } /* list size, i.e., number of nodes */ int size_list(struct node **head) { CSC180 Fall 2008, University of Toronto 2

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
struct node *p; for (p = *head; p != NULL; p = p->next, ++n) ; return n; } /* add a node to the front of a list */ void add_node(struct node **head, int data) { struct node *node = xmalloc(sizeof(struct node)); node->data = data; node->next = *head; *head = node;
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/10/2011 for the course CSC 180 taught by Professor Na during the Fall '01 term at University of Toronto- Toronto.

### Page1 / 12

LEC20081117 - Introduction to Computer Programming November...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online