Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ОПК, семинар №6

Ilya
March 05, 2015

ОПК, семинар №6

Массивы и списки

Ilya

March 05, 2015
Tweet

More Decks by Ilya

Other Decks in Education

Transcript

  1. void replace(char* source, const char* old, const char* new) {

    // ... } char* replace("Hello, World!", "!", "?"); H e l l o , W o r l d ! H e l l o , W o r l d !
  2. void replace(char* source, const char* old, const char* new) {

    // ... } char* replace("Hello, World!", "World", "Universe"); H e l l o , W o r l d ! H e l l o , U n i v e r s e !
  3. void replace(char* source, const char* old, const char* new) {

    // ... } char* replace("Hello, World!", "World", "Me"); H e l l o , W o r l d ! H e l l o , M e !
  4. char* replace(const char* source, const char* old, const char* new)

    { char* result; int size_diff = count(source, old) * (strlen(new) - strlen(old)); size_t resulting_length = strlen(source) + size_diff; result = calloc(resulting_length, sizeof(char)); // Замена return result; }
  5. H e l l o , W o r l

    d ! H e l l o , M e ! char* replace("Hello, World!", "World", "Me");
  6. H e l l o , W o r l

    d ! char* replace("Hello, World!", "World", "Universe"); H e l l o , U n i v e r s e !
  7. 1 2 3 4 5 6 8 9 10 7

    int* array = calloc(20, sizeof(int)); 1 2 3 4 5 6 8 9 10 7 int *new_array = calloc(40, sizeof(int)); memcpy(new_array, array, 40 * sizeof(int)); free(array); 1 2 3 4 5 6 8 9 10 7 1 2 3 4 5 6 8 9 10 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 memcpy
  8. void insert_sorted(int *array, int number); int array[20]; int input; do

    { scanf("%d", &input); insert_sorted(&array, input); } while(1); < 1 < 2 … < 60 1 2 3 4 10 20 40 50 60 30
  9. void insert_sorted(int *array, int number); int array[20]; int input; do

    { scanf("%d", &input); insert_sorted(&array, input); } while(1); < 1 < 2 … < 60 1 2 3 4 10 20 40 50 60 30 < 5
  10. void insert_sorted(int *array, int number); int array[20]; int input; do

    { scanf("%d", &input); insert_sorted(&array, input); } while(1); < 1 < 2 … < 60 1 2 3 4 10 20 40 50 60 30 < 5 5
  11. 5 10 NULL pListNode new_node(int value) { pListNode node =

    (pListNode) malloc( sizeof(ListNode)); if (node == NULL) { return NULL; } node->value = value; node->next = NULL; return node; } new_node(5); new_node(10);
  12. 5 10 NULL ListNode nodes[3] = {new_node(5), new_node(10), new_node(15)}; link_nodes(&nodes[0],

    &nodes[1]); link_nodes(&nodes[1], &nodes[2]); do { printf("Node %d\n", point->value); point = point->next; } while (point != NULL); > Node 5 > Node 10 > Node 15 15 NULL
  13. void insert_node(pListNode first, pListNode second) { pListNode third = first->next;

    first->next = second; second->next = third; } int main() { pListNode head, point, intermediate; head = new_node(5); link_nodes(head, new_node(10)); insert_node(head, new_node(7)); ... } 5 NULL
  14. void insert_node(pListNode first, pListNode second) { pListNode third = first->next;

    first->next = second; second->next = third; } int main() { pListNode head, point, intermediate; head = new_node(5); link_nodes(head, new_node(10)); insert_node(head, new_node(7)); ... } 5 NULL 5 10 NULL
  15. void insert_node(pListNode first, pListNode second) { pListNode third = first->next;

    first->next = second; second->next = third; } int main() { pListNode head, point, intermediate; head = new_node(5); link_nodes(head, new_node(10)); insert_node(head, new_node(7)); ... } 5 NULL 5 10 NULL 5 10 NULL 7 NULL
  16. int main() { pListNode head, point, intermediate; head = new_node(5);

    point = head; link_nodes(head, new_node(10)); insert_node(head, new_node(7)); do { printf("Node %d\n", point->value); point = point->next; } while (point != NULL); } > Node 5 > Node 7 > Node 10 5 NULL 5 10 NULL 5 10 NULL 7 NULL
  17. Двусвязные списки typedef struct ListNode ListNode; typedef struct ListNode* pListNode;

    struct ListNode { int value; pListNode next, prev; }; 5 10 NULL 15 NULL
  18. Двусвязные списки typedef struct ListNode ListNode; typedef struct ListNode* pListNode;

    struct ListNode { int value; pListNode next, prev; }; 5 10 NULL 15 NULL