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

CSE240 Lecture 13

CSE240 Lecture 13

Introduction to Programming Languages
Data Structures
(202009)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez
PRO

January 13, 2017
Tweet

More Decks by Javier Gonzalez

Other Decks in Programming

Transcript

  1. jgs CSE 240 Introduction to Programming Languages Lecture 13: Data

    Structures in C Dr. Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. jgs struct

  3. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 3 jgs

    struct type § A structure is is a composite data type declaration that defines a physically grouped list of variables to be placed under one name in a block of memory. § It is created by the keyword struct. § Similar to a Java class; but DOES NOT allow methods
  4. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 4 jgs

    struct type struct type_name { type1 element1; type2 element2; . . . typen elementn; }; struct type_name a, b; // Example #include <stdio.h> struct person { char name[30]; int id; }; void main( ) { struct person x, y; scanf("%s", x.name ); scanf("%d", &x.id); printf("%s", x.name); printf("\n"); printf("%d", x.id); }
  5. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 5 jgs

    Arrays of struct #include <stdio.h> struct contact { char name[30]; int phone; char email[30]; }; struct contact contact_book[100]; // an array of structures void main() { int index = 0; scanf("%d", &contact_book[index].phone); scanf("%s", contact_book[index].name ); scanf("%s", contact_book[index].email); printf("\n %d", contact_book[index].phone); printf("\n %s", contact_book[index].name); printf("\n %s", contact_book[index].email); }
  6. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 6 jgs

    Pointers to struct name phone email x struct contact { char name[30]; int phone; char email[30]; }; main () { struct contact x; struct contact *y; scanf("%s", x.name); //dot with x scanf("%d", &x.phone); scanf("%s", x.email); printf ("%d \n", x.phone); y = &x; (*y).phone = 101010; //asterisk and dot with y y->phone = 404040; // arrow with y printf ("%d \n", y->phone); } y
  7. jgs Data Structures Linked List (malloc and free)

  8. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 8 jgs

    Linked List #include <stdlib.h> struct node { int val; struct node * next; }; struct node * head = NULL; head = malloc(sizeof(struct node )); head->val = 1; head->next = malloc(sizeof(struct node )); head->next->val = 2; head->next->next = NULL; struct node * head = NULL; head = malloc(sizeof(struct node)); if (head == NULL) { return 1; } head->val = 1; head->next = NULL; void print_list(struct node * head) { node * current = head; while (current != NULL) { printf("%d\n", current->val); current = current->next; } }
  9. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 9 jgs

    Part 1 #include <stdio.h> #include <stdlib.h> struct node { int val; struct node * next; }; int main() { struct node * head = NULL; head = malloc(sizeof(struct node )); head->val = 1; head->next = malloc(sizeof(struct node )); head->next->val = 2; head->next->next = NULL; push (head, 3); push (head, 4); push (head, 5); print_list(head); printf("-----\n"); pop(&head); pop(&head); pop(&head); print_list(head); }
  10. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 10 jgs

    Part 2 void push(struct node * head, int val) { struct node * current = head; while (current->next != NULL) { current = current->next; } current->next = malloc(sizeof(struct node )); current->next->val = val; current->next->next = NULL; }
  11. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 11 jgs

    Part 3 int pop(struct node ** head) { int retval = -1; struct node * next_node = NULL; if (*head == NULL) { return -1; } next_node = (*head)->next; retval = (*head)->val; free(*head); *head = next_node; return retval; }
  12. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 12 jgs

    Part 4 void print_list(struct node * head) { struct node * current = head; while (current != NULL) { printf("%d\n", current->val); current = current->next; } }
  13. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 13 jgs

    Questions
  14. jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez, Ph.D.

    javiergs@asu.edu Fall 2021 Copyright. These slides can only be used as study material for the class CSE240 at Arizona State University. They cannot be distributed or used for another purpose.