Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up
for free
CSE240 Lecture 13
Javier Gonzalez
January 13, 2017
Programming
0
1.2k
CSE240 Lecture 13
Introduction to Programming Languages
Data Structures
(202009)
Javier Gonzalez
January 13, 2017
Tweet
Share
More Decks by Javier Gonzalez
See All by Javier Gonzalez
javiergs
0
520
javiergs
0
360
javiergs
0
330
javiergs
0
340
javiergs
0
260
javiergs
0
210
javiergs
0
130
javiergs
0
190
javiergs
0
480
Other Decks in Programming
See All in Programming
oleindesign
1
200
christianweyer
PRO
0
280
martysuzuki
1
390
grapecity_dev
0
170
grapecity_dev
0
170
aftiopk
0
100
itok
1
580
lovee
8
2.7k
ntaro
0
160
grapecity_dev
0
170
manfredsteyer
PRO
0
130
abeta
1
200
Featured
See All Featured
cromwellryan
104
6.1k
paulrobertlloyd
71
3.6k
brad_frost
157
6.4k
addyosmani
311
21k
malarkey
193
8.6k
brianwarren
82
4.7k
colly
66
3k
sachag
267
17k
jensimmons
207
10k
skipperchong
8
710
jasonvnalue
81
8.1k
rmw
11
810
Transcript
jgs CSE 240 Introduction to Programming Languages Lecture 13: Data
Structures in C Javier Gonzalez-Sanchez javiergs@asu.edu PERALTA 230U Office Hours: By appointment
jgs Data Structures Linked List (malloc and free)
Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 3 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; } }
Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 4 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); }
Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 5 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; }
Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 6 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; }
Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 7 jgs
Part 4 void print_list(struct node * head) { struct node * current = head; while (current != NULL) { printf("%d\n", current->val); current = current->next; } }
jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu
Spring 2018 Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.