Linked Lists in C: Understanding the Data Structure and Implementation, Quizzes of Computer Science

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

Pre 2010

Uploaded on 08/30/2009

koofers-user-3sx-1
koofers-user-3sx-1 🇺🇸

10 documents

1 / 82

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Quiz
Wednesday?
Closed book
Based on homeworks and programs gone
through in class
No need to memorize syntax
If I want you to use a function/ technique, I will
provide a small example
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52

Partial preview of the text

Download Linked Lists in C: Understanding the Data Structure and Implementation and more Quizzes Computer Science in PDF only on Docsity!

Quiz

 Wednesday?

 Closed book

 Based on homeworks and programs gone

through in class

 No need to memorize syntax

 If I want you to use a function/ technique, I will

provide a small example

Arrays of strings

 Arrays of strings are just 2-D arrays of

characters

 Because strings are just arrays of characters

char *array2[] = {“Test.”, “Vacation!”, “”};

array2 (char **)

T E S T. \

V a c a t i o n! \

\

(char *)

(char *)

(char *)

Creating your own types

 C lets you name your own types using typedef

 Example:

 typedef syntax

 As if you’re declaring a variable of the type you

want to redefine

 Give the variable the name of the new type

 Put typedef in front

// create a new type called “scalar” typedef double scalar; scalar add(scalar a, scalar b) { return a + b; }

typedef example

 Use typedef to create alias for complex types

// 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]; }

Structures

 Structures aggregate variables of different types

 Like classes in Java, but they can only store data

 Defined with the keyword struct

// 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); }

(int) id

(int) year

(char) grade

struct student

struct student requires approximately 9 bytes

Structure syntax

 To define a structure

 To declare a variable of the new structure type

 The struct keyword is required in both places

struct structure_name { /* list of variable declarations */ };

int main() { struct structure_name variable_name; /* code */ }

Another example: 2-D points

// 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)); }

Structures in C

 C treats structures as if they were primitive data types

 (^) 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); }

Pointers to structures

 Function calls with structure arguments can be

costly

 Solution: Use pointers to structures. Only the

address of the structure is copied, not the entire

structure.

 Use &s to get the address of a structure s

 Use *p to get the structure pointed to by p

 Use (*p).field to access a field

 Or the cleaner syntax: p->fieldU

Pointers to structures

 Function calls with structure arguments can be costly

 (^) 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 ); }

Dynamically allocating structures

 Example

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; }

Practical example: Linked Lists

 A linked list is a very common data structure in CS

 (^) The list is not (necessarily) stored in contiguous memory

 Instead, each item is stored individually

 Each item includes a reference to the next item in the list

 The last item has a null reference

 (^) Advantage:

 (^) List can grow and shrink dynamically

 (^) Disadvantage:

 No random access to the data

 (^) Memory overhead from next pointers

Linked lists in C

 Function to add a cell to the head of the list

 returns a pointer to the new head of the list

 Function to print the contents of a list

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); }

Linked lists in C

 Function to destroy a ListCell

 like a destructor for ListCell in C++

 Function to destroy an entire list

void destroy_ListCell(ListCell *cell) { free(cell); }

void destroy_list(ListCell *head) { ListCell *next; for( ; head ; head = next) { next = head->next; destroy_ListCell(head); } }