










































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of linked lists in c programming language. It explains the concept of linked lists, their advantages and disadvantages, and the implementation using the listcell structure and various functions. Creating a new listcell, adding a cell to the head of the list, printing the list contents, and destroying a listcell and an entire list.
Typology: Quizzes
1 / 82
This page cannot be seen from the preview
Don't miss anything!











































































char *array2[] = {“Test.”, “Vacation!”, “”};
// create a new type called “scalar” typedef double scalar; scalar add(scalar a, scalar b) { return a + b; }
// create new types called “scalar” and “vector” typedef double scalar; scalar add_scalars(scalar a, scalar b) { return a + b; }
add_vectors(scalar result[10], scalar a[10], scalar b[10]) { int j; for(j=0; j<10; j++) result[j] = a[j] + b[j]; }
// define new structure struct Student { int id, year; char grade; };
int main() { struct Student s; s.id = 10001; s.year = 2010; s.grade = ‘B’; printf(“%d %d %c\n”, s.id, s.year, s.grade); }
struct student requires approximately 9 bytes
struct structure_name { /* list of variable declarations */ };
int main() { struct structure_name variable_name; /* code */ }
// define new structure typedef struct { double x, y; } Point;
double distance(Point p1, Point p2) { return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y); }
int main() { Point a = { 12.0, 42.0 }; Point b = { 15.0, 36.0 }; printf(“Distance: %lf\n”, distance(a, b)); }
(^) I.e. not like reference types in Java (^) Passing a structure as an argument to a function means that a temporary copy is made of the entire structure (^) Assignments do a deep copy of the entire structure
void reset(Point p) { p.x = p.y = 0; }
int main() { Point a = { 12.0, 42.0 }; Point b = a;
reset(a); b.x = 0; printf(“a: %d,%d\n”, a.x, a.y); printf(“b: %d,%d\n”, b.x, b.y); }
(^) Solution: Use pointers to structures. Only the address of the structure is copied, not the entire structure.
typedef struct { char name[10000]; int id; } Person;
void print_person(Person *p) { printf(“%d %s\n”, p->id, p->name); }
int main() { Person a = { “Mike Smith”, 1234 }; print( &a ); }
typedef struct { char name[10000]; int id; } Person;
int main() { Person *p; p = (Person *) malloc( sizeof(Person) );
strcpy(p->name, “George Washington”); p->id = 1776;
free(p); return 0; }
(^) The list is not (necessarily) stored in contiguous memory
(^) Advantage:
(^) List can grow and shrink dynamically
(^) Disadvantage:
(^) Memory overhead from next pointers
ListCell *add_front(ListCell *head, int data) { return make_ListCell(data, head); }
void print_list(ListCell *head) { for( ; head ; head = head->next) printf(“%d\n”, head->data); }
void destroy_ListCell(ListCell *cell) { free(cell); }
void destroy_list(ListCell *head) { ListCell *next; for( ; head ; head = next) { next = head->next; destroy_ListCell(head); } }