Last active
October 1, 2019 22:00
-
-
Save vasalf/38ba63566ab3003ebb916dafba60f60d to your computer and use it in GitHub Desktop.
Practice 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
typedef struct { | |
int x; | |
float y; | |
char s[256]; | |
} my_struct_t; | |
void f1(my_struct_t m) { | |
m.x = 10; | |
} | |
void f2(my_struct_t* m) { | |
m->x = 10; | |
} | |
int main() { | |
my_struct_t t1; | |
t1.x = 1; | |
t1.y = 2; | |
strncpy(t1.s, "3", 256); | |
my_struct_t t2; | |
t2.x = 4; | |
t2.y = 5; | |
strncpy(t2.s, "6", 256); | |
t1 = t2; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stddef.h> | |
#include <stdlib.h> | |
struct node_s { | |
int x; | |
struct node_s* next; | |
}; | |
typedef struct node_s node_t; | |
typedef struct { | |
node_t* head; | |
size_t len; | |
} list_t; | |
void init(list_t* l) { | |
l->head = NULL; | |
l->len = 0; | |
} | |
void destroy(list_t* l) { | |
node_t* next; | |
for (node_t* cur = l->head; cur != NULL; cur = next) { | |
next = cur->next; | |
free(cur); | |
} | |
l->head = NULL; | |
l->len = 0; | |
} | |
void insert(list_t* l, node_t* previous, int x) { | |
node_t* node = malloc(sizeof(node_t)); | |
node->x = x; | |
node->next = previous->next; | |
previous->next = node; | |
l->len++; | |
} | |
node_t* find(list_t* l, int x) { | |
for (node_t* cur = l->head; cur != NULL; cur = cur->next) { | |
if (cur->x == x) | |
return cur; | |
} | |
return NULL; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stddef.h> | |
#include <stdlib.h> | |
struct node_t { | |
void* x; | |
struct node_t* next; | |
}; | |
typedef struct node_t node_t; | |
typedef struct { | |
node_t* head; | |
size_t len; | |
} list_t; | |
void init(list_t* l) { | |
l->head = NULL; | |
l->len = 0; | |
} | |
void destroy(list_t* l) { | |
node_t* next; | |
for (node_t* cur = l->head; cur != NULL; cur = next) { | |
next = cur->next; | |
free(cur); | |
} | |
l->head = NULL; | |
l->len = 0; | |
} | |
void insert(list_t* l, node_t* previous, void* x) { | |
node_t* node = malloc(sizeof(node_t)); | |
node->x = x; | |
node->next = previous->next; | |
previous->next = node; | |
l->len++; | |
} | |
node_t* find(list_t* l, void* x) { | |
...??? | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stddef.h> | |
struct node_t { | |
node_t* next; | |
}; | |
typedef struct node_t node_t; | |
typedef struct { | |
node_t* head; | |
int len; | |
} list_t; | |
//=== | |
typedef struct { | |
int x; | |
node_t node; | |
} int_node_t; | |
//=== | |
void init(list_t* l) { | |
l->head = NULL; | |
l->len = 0; | |
} | |
void insert(list_t* l, node_t* previous, node_t* node) { | |
node->next = previous->next; | |
previous->next = node; | |
l->len++; | |
} | |
// usage: | |
// | |
// int_node_t* node = malloc(sizeof(int_node_t)); | |
// node->x = 179; | |
// insert(l, p, node); | |
void destroy(list_t* l, int offset) { | |
node_t* next; | |
for (node_t* cur = l->head; cur != NULL; cur = next) { | |
next = cur->next; | |
free((char*)cur - offset); | |
} | |
} | |
(&(((int_node_t*)NULL)->node)) | |
// usage: | |
// | |
// destroy(l, offsetof(int_node_t, node)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment