Data Structures and Algorithms for Engineers

Size: px
Start display at page:

Download "Data Structures and Algorithms for Engineers"

Transcription

1 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa Data Structures and Algorithms for Engineers 1 Carnegie Mellon University Africa

2 Lecture 9 Containers and Dictionaries Containers Dictionaries List ADT Array implementation Linked list implementation Data Structures and Algorithms for Engineers 2 Carnegie Mellon University Africa

3 Aside: Linked Lists Using Pointers Data Structures and Algorithms for Engineers 3 Carnegie Mellon University Africa

4 Why Pointer-Based Implementation? Linked lists are used avoid excessive data movement with insertions and deletions Elements are not necessarily stored in contiguous memory locations Makes efficient use of memory space Allocate space when needed Deallocate space when finished & return it to the free store Failure to deallocate space will cause memory leakage Data Structures and Algorithms for Engineers 4 Carnegie Mellon University Africa

5 Why Pointer-Based Implementation? Some guidelines when writing programs that dynamically allocate memory Use malloc or new to create data-structures of the appropriate size Remember to avoid memory leakage by always using free and delete to deallocate dynamically-created data-structures Check every call to malloc or new to see if it returned NULL (i.e. chec if the allocation was unsuccessful) Data Structures and Algorithms for Engineers 5 Carnegie Mellon University Africa

6 Why Pointer-Based Implementation? Some guidelines when writing programs that dynamically allocate memory You must expect free or delete to alter the contents of the memory that was freed or deleted Never access a data structure after it has been freed or deleted If malloc fails in a non-interactive program, make that a fatal error In an interactive program, it is better to abort the current command and return to the command reader loop Data Structures and Algorithms for Engineers 6 Carnegie Mellon University Africa

7 A Linked List A linked list is a list in which the order of the components is determined by an explicit link member in each node The nodes are structs each node contains a component member and also a link member that gives the location of the next node in the list Data Structures and Algorithms for Engineers 7 Carnegie Mellon University Africa

8 Pointer-Based (Dynamic) Linked List A pointer-based linked list is a dynamic linked list where nodes are linked together by pointers, and an external pointer (or head pointer) points to the first node in the list Data Structures and Algorithms for Engineers 8 Carnegie Mellon University Africa

9 Nodes can be located anywhere in memory The link member holds the memory address of (or a reference to) of the next node in the list Data Structures and Algorithms for Engineers 9 Carnegie Mellon University Africa

10 Declarations for a Dynamic Linked List // Type DECLARATIONS struct NodeType { char info; NodeType* next; typedef NodeType* NodePtr; // Variable DECLARATIONS NodePtr head; NodePtr ptr; Data Structures and Algorithms for Engineers 10 Carnegie Mellon University Africa

11 Pointer Dereferencing and Member Selection ptr ptr A info. next ptr is a pointer to a node *ptr ptr A info. next *ptr is the entire node pointed to by ptr ptr A info. next (*ptr).info ~ ptr->info (*ptr).next ~ ptr->next ptr A info. next ptr->info is a node member ptr->next is a node member Data Structures and Algorithms for Engineers 11 Carnegie Mellon University Africa

12 Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 12 Carnegie Mellon University Africa

13 Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 13 Carnegie Mellon University Africa

14 Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 14 Carnegie Mellon University Africa

15 Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 15 Carnegie Mellon University Africa

16 Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 16 Carnegie Mellon University Africa

17 Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 17 Carnegie Mellon University Africa

18 Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 18 Carnegie Mellon University Africa

19 Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 19 Carnegie Mellon University Africa

20 Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 20 Carnegie Mellon University Africa

21 Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 21 Carnegie Mellon University Africa

22 Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 22 Carnegie Mellon University Africa

23 Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 23 Carnegie Mellon University Africa

24 Using Operator new If memory is available in an area called the free store (or heap), operator new allocates the requested object, and returns a pointer to the memory allocated. The dynamically allocated object exists until the delete operator destroys it. Data Structures and Algorithms for Engineers 24 Carnegie Mellon University Africa 24

25 Inserting a Node at the Front of a List Data Structures and Algorithms for Engineers 25 Carnegie Mellon University Africa

26 Inserting a Node at the Front of a List Data Structures and Algorithms for Engineers 26 Carnegie Mellon University Africa

27 Inserting a Node at the Front of a List Data Structures and Algorithms for Engineers 27 Carnegie Mellon University Africa

28 Inserting a Node at the Front of a List Data Structures and Algorithms for Engineers 28 Carnegie Mellon University Africa

29 Inserting a Node at the Front of a List Data Structures and Algorithms for Engineers 29 Carnegie Mellon University Africa

30 Inserting a Node at the Front of a List Data Structures and Algorithms for Engineers 30 Carnegie Mellon University Africa

31 Using Operator delete The object currently pointed to by the pointer is deallocated, and the pointer is considered undefined. The object s memory is returned to the free store. Data Structures and Algorithms for Engineers 31 Carnegie Mellon University Africa 31

32 Deleting the first node from a list Data Structures and Algorithms for Engineers 32 Carnegie Mellon University Africa

33 Deleting the first node from a list Data Structures and Algorithms for Engineers 33 Carnegie Mellon University Africa

34 Deleting the first node from a list link Data Structures and Algorithms for Engineers 34 Carnegie Mellon University Africa

35 Deleting the first node from a list Data Structures and Algorithms for Engineers 35 Carnegie Mellon University Africa

36 Deleting the first node from a list Data Structures and Algorithms for Engineers 36 Carnegie Mellon University Africa

37 End of Aside: Linked Lists Using Pointers Data Structures and Algorithms for Engineers 37 Carnegie Mellon University Africa

38 Header node element pointer NULL pointer List Data Structures and Algorithms for Engineers 38 Carnegie Mellon University Africa

39 Header node An empty list!!! List Data Structures and Algorithms for Engineers 39 Carnegie Mellon University Africa

40 List window To place the window at this position we provide a link to the previous node (this is why we need a header node) Data Structures and Algorithms for Engineers 40 Carnegie Mellon University Africa

41 List window To place the window at end of the list we provide a link to the last node Data Structures and Algorithms for Engineers 41 Carnegie Mellon University Africa

42 List window To insert a node at this window position we create the node and re-arrange the links Data Structures and Algorithms for Engineers 42 Carnegie Mellon University Africa

43 List window temp To insert a node at this window position we create the node and re-arrange the links Data Structures and Algorithms for Engineers 43 Carnegie Mellon University Africa

44 List window To delete a node at this window position we re-arrange the links and free the node Data Structures and Algorithms for Engineers 44 Carnegie Mellon University Africa

45 temp List window To delete a node at this window position we re-arrange the links and free the node Data Structures and Algorithms for Engineers 45 Carnegie Mellon University Africa

46 List window To delete a node at this window position we re-arrange the links and free the node Data Structures and Algorithms for Engineers 46 Carnegie Mellon University Africa

47 type elementtype type LIST type Boolean type windowtype Data Structures and Algorithms for Engineers 47 Carnegie Mellon University Africa

48 /* linked-list implementation of LIST ADT */ #include <stdio.h> #include <math.h> #include <.h> #define FALSE 0 #define TRUE 1 typedef struct { int ; char *; ELEMENT_TYPE; Data Structures and Algorithms for Engineers 48 Carnegie Mellon University Africa

49 typedef struct node *NODE_TYPE; typedef struct node { ELEMENT_TYPE element; NODE_TYPE next; NODE; typedef NODE_TYPE LIST_TYPE; typedef NODE_TYPE WINDOW_TYPE; Data Structures and Algorithms for Engineers 49 Carnegie Mellon University Africa

50 typedef struct node *NODE_TYPE; /* alternative approach... */ /* but need to use sizeof(struct node) in malloc()*/ struct node { ELEMENT_TYPE element; NODE_TYPE next; ; typedef NODE_TYPE LIST_TYPE; typedef NODE_TYPE WINDOW_TYPE; Data Structures and Algorithms for Engineers 50 Carnegie Mellon University Africa

51 /*** position following last element in a list ***/ WINDOW_TYPE end(list_type *list) { WINDOW_TYPE q; q = *list; if (q == NULL) { error( non-existent list ); else { while (q->next!= NULL) { q = q->next; return(q); *list q Data Structures and Algorithms for Engineers 51 Carnegie Mellon University Africa

52 /*** position following last element in a list ***/ WINDOW_TYPE end(list_type *list) { WINDOW_TYPE q; q = *list; if (q == NULL) { error( non-existent list ); else { while (q->next!= NULL) { q = q->next; return(q); *list q Data Structures and Algorithms for Engineers 52 Carnegie Mellon University Africa

53 /*** position following last element in a list ***/ WINDOW_TYPE end(list_type *list) { WINDOW_TYPE q; q = *list; if (q == NULL) { error( non-existent list ); else { while (q->next!= NULL) { q = q->next; return(q); *list q Data Structures and Algorithms for Engineers 53 Carnegie Mellon University Africa

54 /*** empty a list ***/ WINDOW_TYPE empty(list_type *list) { WINDOW_TYPE p, q; if (*list!= NULL) { /* list exists: delete all nodes including header */ q = *list; while (q->next!= NULL) { p = q; q = q->next; free(p); free(q) *list q Data Structures and Algorithms for Engineers 54 Carnegie Mellon University Africa

55 /*** empty a list ***/ WINDOW_TYPE empty(list_type *list) { WINDOW_TYPE p, q; if (*list!= NULL) { /* list exists: delete all nodes including header */ q = *list; while (q->next!= NULL) { p = q; q = q->next; free(p); free(q) *list p q Data Structures and Algorithms for Engineers 55 Carnegie Mellon University Africa

56 /*** empty a list ***/ WINDOW_TYPE empty(list_type *list) { WINDOW_TYPE p, q; if (*list!= NULL) { /* list exists: delete all nodes including header */ q = *list; while (q->next!= NULL) { p = q; q = q->next; free(p); free(q) *list p q Data Structures and Algorithms for Engineers 56 Carnegie Mellon University Africa

57 /*** empty a list ***/ WINDOW_TYPE empty(list_type *list) { WINDOW_TYPE p, q; if (*list!= NULL) { /* list exists: delete all nodes including header */ q = *list; while (q->next!= NULL) { p = q; q = q->next; free(p); free(q) *list p q Data Structures and Algorithms for Engineers 57 Carnegie Mellon University Africa

58 /*** empty a list ***/ WINDOW_TYPE empty(list_type *list) { WINDOW_TYPE p, q; if (*list!= NULL) { /* list exists: delete all nodes including header */ q = *list; while (q->next!= NULL) { p = q; q = q->next; free(p); free(q) *list p q Data Structures and Algorithms for Engineers 58 Carnegie Mellon University Africa

59 /*** empty a list ***/ WINDOW_TYPE empty(list_type *list) { WINDOW_TYPE p, q; if (*list!= NULL) { /* list exists: delete all nodes including header */ q = *list; while (q->next!= NULL) { p = q; q = q->next; free(p); free(q) *list p q Data Structures and Algorithms for Engineers 59 Carnegie Mellon University Africa

60 /*** empty a list ***/ WINDOW_TYPE empty(list_type *list) { WINDOW_TYPE p, q; if (*list!= NULL) { /* list exists: delete all nodes including header */ q = *list; while (q->next!= NULL) { p = q; q = q->next; free(p); free(q) *list p q Data Structures and Algorithms for Engineers 60 Carnegie Mellon University Africa

61 /*** empty a list ***/ WINDOW_TYPE empty(list_type *list) { WINDOW_TYPE p, q; if (*list!= NULL) { /* list exists: delete all nodes including header */ q = *list; while (q->next!= NULL) { p = q; q = q->next; free(p); free(q) *list p q Data Structures and Algorithms for Engineers 61 Carnegie Mellon University Africa

62 /* now, create a new empty one with a header node */ if ((q = (NODE_TYPE) malloc(sizeof(node))) == NULL) error( function empty: unable to allocate memory ); else { q->next = NULL; *list = q; return(end(list)); *list p q Data Structures and Algorithms for Engineers 62 Carnegie Mellon University Africa

63 /* now, create a new empty one with a header node */ if ((q = (NODE_TYPE) malloc(sizeof(node))) == NULL) error( function empty: unable to allocate memory ); else { q->next = NULL; *list = q; return(end(list)); *list p q Data Structures and Algorithms for Engineers 63 Carnegie Mellon University Africa

64 /* now, create a new empty one with a header node */ if ((q = (NODE_TYPE) malloc(sizeof(node))) == NULL) error( function empty: unable to allocate memory ); else { q->next = NULL; *list = q; return(end(list)); *list p q Data Structures and Algorithms for Engineers 64 Carnegie Mellon University Africa

65 /*** test to see if a list is empty ***/ int is_empty(list_type *list) { WINDOW_TYPE q; q = *list; if (q == NULL) { error( non-existent list ); else { if (q->next == NULL) { return(true); *list else return(false); q *list q Data Structures and Algorithms for Engineers 65 Carnegie Mellon University Africa

66 /*** position at first element in a list ***/ WINDOW_TYPE first(list_type *list) { if (is_empty(list) == FALSE) { return(*list); else return(end(list)); *list end(list) Data Structures and Algorithms for Engineers 66 Carnegie Mellon University Africa

67 /*** position at next element in a list ***/ WINDOW_TYPE next(window_type w, LIST_TYPE *list) { if (w == last(list)) { return(end(list)); else if (w == end(list)) { error( can t find next after end of list ); else { return(w->next); *list w W->next end(list) Data Structures and Algorithms for Engineers 67 Carnegie Mellon University Africa

68 /*** position at previous element in a list ***/ WINDOW_TYPE previous(window_type w, LIST_TYPE *list) { WINDOW_TYPE p, q; if (w!= first(list)) { p = first(list); while (p->next!= w) { p = p->next; if (p == NULL) break; /* trap this to ensure */ /* we don t dereference */ if (p!= NULL) /* a null pointer in the */ return(p); /* while condition */ *list p Data Structures and Algorithms for Engineers 68 Carnegie Mellon University Africa w

69 /*** position at previous element in a list ***/ WINDOW_TYPE previous(window_type w, LIST_TYPE *list) { WINDOW_TYPE p, q; if (w!= first(list)) { p = first(list); while (p->next!= w) { p = p->next; if (p == NULL) break; /* trap this to ensure */ /* we don t dereference */ if (p!= NULL) /* a null pointer in the */ return(p); /* while condition */ *list p Data Structures and Algorithms for Engineers 69 Carnegie Mellon University Africa w

70 else { error( can t find previous to a non-existent node ); else { error( can t find previous before first element of list ); return(w); Data Structures and Algorithms for Engineers 70 Carnegie Mellon University Africa

71 /*** position at last element in a list ***/ WINDOW_TYPE last(list_type *list) { WINDOW_TYPE p, q; if (*list == NULL) { error( non-existent list ); else { /* list exists: find last node */ Data Structures and Algorithms for Engineers 71 Carnegie Mellon University Africa

72 /* list exists: find last node */ if (is_empty(list)) { p = end(list); else { p = *list; q = p->next; while (q->next!= NULL) { p = q; q = q->next; return(p); *list p q Data Structures and Algorithms for Engineers 72 Carnegie Mellon University Africa

73 /* list exists: find last node */ if (is_empty(list)) { p = end(list); else { p = *list; q = p->next; while (q->next!= NULL) { p = q; q = q->next; return(p); *list Data Structures and Algorithms for Engineers 73 Carnegie Mellon University Africa p q

74 /* list exists: find last node */ if (is_empty(list)) { p = end(list); else { p = *list; q = p->next; while (q->next!= NULL) { p = q; q = q->next; return(p); *list p q Data Structures and Algorithms for Engineers 74 Carnegie Mellon University Africa

75 /*** insert an element in a list ***/ LIST_TYPE *insert(element_type e, WINDOW_TYPE w, LIST_TYPE *list) { WINDOW_TYPE temp; if (*list == NULL) { error( cannot insert in a non-existent list ); Data Structures and Algorithms for Engineers 75 Carnegie Mellon University Africa

76 else { /* insert it after w */ temp = w->next; if ((w->next = (NODE_TYPE) malloc(sizeof(node))) = NULL) error( function insert: unable to allocate memory ); else { w->next->element = e; w->next->next = temp; return(list); *list w temp Data Structures and Algorithms for Engineers 76 Carnegie Mellon University Africa

77 else { /* insert it after w */ temp = w->next; if ((w->next = (NODE_TYPE) malloc(sizeof(node))) = NULL) error( function insert: unable to allocate memory ); else { w->next->element = e; w->next->next = temp; return(list); *list w temp Data Structures and Algorithms for Engineers 77 Carnegie Mellon University Africa

78 else { /* insert it after w */ temp = w->next; if ((w->next = (NODE_TYPE) malloc(sizeof(node))) = NULL) error( function insert: unable to allocate memory ); else { w->next->element = e; w->next->next = temp; return(list); e. e. *list w temp Data Structures and Algorithms for Engineers 78 Carnegie Mellon University Africa

79 /*** delete an element from a list ***/ LIST_TYPE *delete(window_type w, LIST_TYPE *list) { WINDOW_TYPE p; if (*list == NULL) { error( cannot delete from a non-existent list ); else { p = w->next; /* node to be deleted */ w->next = w->next->next; /* rearrange the links */ free(p); /* delete the node */ return(list); *list w p Data Structures and Algorithms for Engineers 79 Carnegie Mellon University Africa

80 /*** delete an element from a list ***/ LIST_TYPE *delete(window_type w, LIST_TYPE *list) { WINDOW_TYPE p; if (*list == NULL) { error( cannot delete from a non-existent list ); else { p = w->next; /* node to be deleted */ w->next = w->next->next; /* rearrange the links */ free(p); /* delete the node */ return(list); *list w p Data Structures and Algorithms for Engineers 80 Carnegie Mellon University Africa

81 /*** delete an element from a list ***/ LIST_TYPE *delete(window_type w, LIST_TYPE *list) { WINDOW_TYPE p; if (*list == NULL) { error( cannot delete from a non-existent list ); else { p = w->next; /* node to be deleted */ w->next = w->next->next; /* rearrange the links */ free(p); /* delete the node */ return(list); *list w p Data Structures and Algorithms for Engineers 81 Carnegie Mellon University Africa

82 /*** retrieve an element from a list ***/ ELEMENT_TYPE retrieve(window_type w, LIST_TYPE *list) { WINDOW_TYPE p; if (*list == NULL) { error( cannot retrieve from a non-existent list ); else { return(w->next->element); *list w Data Structures and Algorithms for Engineers 82 Carnegie Mellon University Africa

83 /*** print all elements in a list ***/ int print(list_type *list) { WINDOW_TYPE w; ELEMENT_TYPE e; printf( Contents of list: \n ); w = first(list); while (w!= end(list)) { e = retrieve(window_type w, LIST_TYPE *list); printf( %d %s\n, e., e.); w = next(w, list); printf( ---\n ); return(0); Data Structures and Algorithms for Engineers 83 Carnegie Mellon University Africa

84 /*** error handler: print message passed as argument and take appropriate action ***/ int error(char *s); { printf( Error: %s\n, s); exit(0); Data Structures and Algorithms for Engineers 84 Carnegie Mellon University Africa

85 /*** assign values to an element ***/ int assign_element_values(element_type *e, int, char s[]) { e-> = (char *) malloc(sizeof(char) * (strlen(s)+1)); strcpy(e->, s); e-> = ; e Data Structures and Algorithms for Engineers 85 Carnegie Mellon University Africa

86 /*** assign values to an element ***/ int assign_element_values(element_type *e, int, char s[]) { e-> = (char *) malloc(sizeof(char) * (strlen(s)+1)); strcpy(e->, s); e-> = ; e Data Structures and Algorithms for Engineers 86 Carnegie Mellon University Africa

87 /*** initialize the list pointer to make sure ***/ /*** all subsequent checks on its value are valid ***/ void initialize_list(list_type *list) { *list = NULL; *list Data Structures and Algorithms for Engineers 87 Carnegie Mellon University Africa

88 /*** main driver routine ***/ WINDOW_TYPE w; ELEMEN_TYPE e; LIST_TYPE list; int i; initialize_list(&list); empty(&list); print(&list); assign_element_values(&e, 1, String A ); w = first(&list); insert(e, w, &list); print(&list); Data Structures and Algorithms for Engineers 88 Carnegie Mellon University Africa

89 assign_element_values(&e, 2, String B ); insert(e, w, &list); print(&list); assign_element_values(&e, 3, String C ); insert(e, last(&list), &list); print(&list); assign_element_values(&e, 4, String D ); w = next(last(&list), &list); insert(e, w, &list); print(&list); Data Structures and Algorithms for Engineers 89 Carnegie Mellon University Africa

90 w = previous(w, &list); delete(w, &list); print(&list); Data Structures and Algorithms for Engineers 90 Carnegie Mellon University Africa

91 Key points: All we changed was the implementation of the data-structure and the access routines But by keeping the interface to the access routines the same as before, these changes are transparent to the user And we didn t have to make any changes in the main function which was actually manipulating the list Data Structures and Algorithms for Engineers 91 Carnegie Mellon University Africa

92 Key points: In a real software system where perhaps hundreds (or thousands) of people are using these list primitives, this transparency is critical We couldn t have achieved it if we manipulated the data-structure directly Data Structures and Algorithms for Engineers 92 Carnegie Mellon University Africa

93 Possible problems with the implementation: we have to run the length of the list in order to find the end (i.e. end(l) is O(n)) there is a (small) overhead in using the pointers On the other hand, the list can now grow as large as necessary, without having to predefine the maximum size Data Structures and Algorithms for Engineers 93 Carnegie Mellon University Africa

94 List We can also have a doubly-linked list; this removes the need to have a header node and make finding the previous node more efficient Data Structures and Algorithms for Engineers 94 Carnegie Mellon University Africa

95 List Lists can also be circular Data Structures and Algorithms for Engineers 95 Carnegie Mellon University Africa

96 Comparison: Linked Lists vs. Arrays Relative advantages of linked lists Overflow on linked structures can never occur unless memory is actually full Insertions and deletions are simpler than for contiguous (array) lists With large records, moving pointers is easier and faster than moving the items themselves Data Structures and Algorithms for Engineers 96 Carnegie Mellon University Africa

97 Comparison: Linked Lists vs. Arrays Relative advantages of arrays Linked structures require extra space for storing pointer fields Linked lists do not allow efficient random access to items Arrays allow better memory locality and cache performance than random pointer jumping Dynamic memory allocation provides us with flexibility on how and where to use limited storage resources Data Structures and Algorithms for Engineers 97 Carnegie Mellon University Africa

98 Comparison: Linked Lists vs. Arrays Both lists and arrays can be thought of a recursive objects: Lists: chopping off the first element of a linked list leaves a smaller linked list Lists are recursive objects Splitting the first k elements off an n element array give two smaller arrays, of size k and n-k, respectively Arrays are recursive objects This shows us that lists are amenable to efficient (recursive) divide-andconquer algorithms, such as binary search and quicksort Data Structures and Algorithms for Engineers 98 Carnegie Mellon University Africa

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers 04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University

More information

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers 04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University

More information

Cpt S 122 Data Structures. Data Structures

Cpt S 122 Data Structures. Data Structures Cpt S 122 Data Structures Data Structures Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Introduction Self Referential Structures Dynamic Memory Allocation

More information

Pointers, Dynamic Data, and Reference Types

Pointers, Dynamic Data, and Reference Types Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple

More information

PROGRAMMAZIONE I A.A. 2017/2018

PROGRAMMAZIONE I A.A. 2017/2018 PROGRAMMAZIONE I A.A. 2017/2018 LINKED LISTS LINKED LIST What are the problems with arrays? üsize is fixed üarray items are stored contiguously üinsertions and deletions at particular positions is complex

More information

Algorithms and Data Structures CS-CO-412

Algorithms and Data Structures CS-CO-412 Algorithms and Data Structures CS-CO-412 David Vernon Professor of Informatics University of Skövde Sweden david@vernon.eu www.vernon.eu Algorithms and Data Structures 1 Copyright D. Vernon 2014 Trees

More information

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers 04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University

More information

Data Structure with C. List

Data Structure with C. List Subject: Data Structure with C Topic: List Introduction list is a finite sequence of data items, i.e. a collection of data items arranged in a certain linear order. he basic operations performed on this

More information

Class Information ANNOUCEMENTS

Class Information ANNOUCEMENTS Class Information ANNOUCEMENTS Third homework due TODAY at 11:59pm. Extension? First project has been posted, due Monday October 23, 11:59pm. Midterm exam: Friday, October 27, in class. Don t forget to

More information

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES BBM 201 DATA STRUCTURES Lecture 8: Dynamically Allocated Linked Lists 2017-2018 Fall int x; x = 8; int A[4]; An array is stored as one contiguous block of memory. How can we add a fifth element to the

More information

Dynamic Allocation of Memory

Dynamic Allocation of Memory Dynamic Allocation of Memory Lecture 4 Sections 10.9-10.10 Robb T. Koether Hampden-Sydney College Fri, Jan 25, 2013 Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013

More information

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

COMP26120: Linked List in C (2018/19) Lucas Cordeiro COMP26120: Linked List in C (2018/19) Lucas Cordeiro lucas.cordeiro@manchester.ac.uk Linked List Lucas Cordeiro (Formal Methods Group) lucas.cordeiro@manchester.ac.uk Office: 2.28 Office hours: 10-11 Tuesday,

More information

CMSC 341 Lecture 7 Lists

CMSC 341 Lecture 7 Lists CMSC 341 Lecture 7 Lists Today s Topics Linked Lists vs Arrays Nodes Using Linked Lists Supporting Actors (member variables) Overview Creation Traversal Deletion UMBC CMSC 341 Lists 2 Linked Lists vs Arrays

More information

CSCI-UA /2. Computer Systems Organization Lecture 19: Dynamic Memory Allocation: Basics

CSCI-UA /2. Computer Systems Organization Lecture 19: Dynamic Memory Allocation: Basics Slides adapted (and slightly modified) from: Clark Barrett Jinyang Li Randy Bryant Dave O Hallaron CSCI-UA.0201-001/2 Computer Systems Organization Lecture 19: Dynamic Memory Allocation: Basics Mohamed

More information

Linked List. April 2, 2007 Programming and Data Structure 1

Linked List. April 2, 2007 Programming and Data Structure 1 Linked List April 2, 2007 Programming and Data Structure 1 Introduction head A linked list is a data structure which can change during execution. Successive elements are connected by pointers. Last element

More information

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures Data Structures in C C Programming and Software Tools N.C. State Department of Computer Science Data Structures in C The combination of pointers, structs, and dynamic memory allocation allow for creation

More information

Quiz 0 Review Session. October 13th, 2014

Quiz 0 Review Session. October 13th, 2014 Quiz 0 Review Session October 13th, 2014 Topics (non-exhaustive) Binary. ASCII. Algorithms. Pseudocode. Source code. Compiler. Object code. Scratch. Statements. Boolean expressions. Conditions. Loops.

More information

CA341 - Comparative Programming Languages

CA341 - Comparative Programming Languages CA341 - Comparative Programming Languages David Sinclair Dynamic Data Structures Generally we do not know how much data a program will have to process. There are 2 ways to handle this: Create a fixed data

More information

CS61C Midterm Review on C & Memory Management

CS61C Midterm Review on C & Memory Management CS61C Midterm Review on C & Memory Management Fall 2006 Aaron Staley Some material taken from slides by: Michael Le Navtej Sadhal Overview C Array and Pointer Goodness! Memory Management The Three Three

More information

Introduction to Linked Lists

Introduction to Linked Lists Introduction to Linked Lists In your previous programming course, you organized and processed data items sequentially using an array (or possibly an arraylist, or a vector). You probably performed several

More information

ECE 15B COMPUTER ORGANIZATION

ECE 15B COMPUTER ORGANIZATION ECE 15B COMPUTER ORGANIZATION Lecture 13 Strings, Lists & Stacks Announcements HW #3 Due next Friday, May 15 at 5:00 PM in HFH Project #2 Due May 29 at 5:00 PM Project #3 Assigned next Thursday, May 19

More information

Recitation #11 Malloc Lab. November 7th, 2017

Recitation #11 Malloc Lab. November 7th, 2017 18-600 Recitation #11 Malloc Lab November 7th, 2017 1 2 Important Notes about Malloc Lab Malloc lab has been updated from previous years Supports a full 64 bit address space rather than 32 bit Encourages

More information

Linked Lists Structures

Linked Lists Structures Linked Lists Structures 1. Motivation... 2 2. Declaration of a node... 3 2.1. Operations on pointers:... 4 2.2. Disposition of a node... 5 2.3. Singly Linked Lists... 5 3. One-way linked lists... 6 3.1.

More information

JTSK Programming in C II C-Lab II. Lecture 3 & 4

JTSK Programming in C II C-Lab II. Lecture 3 & 4 JTSK-320112 Programming in C II C-Lab II Lecture 3 & 4 Xu (Owen) He Spring 2018 Slides modified from Dr. Kinga Lipskoch Planned Syllabus The C Preprocessor Bit Operations Pointers and Arrays (Dynamically

More information

CS349/SE382 A1 C Programming Tutorial

CS349/SE382 A1 C Programming Tutorial CS349/SE382 A1 C Programming Tutorial Erin Lester January 2005 Outline Comments Variable Declarations Objects Dynamic Memory Boolean Type structs, enums and unions Other Differences The Event Loop Comments

More information

Chapter 17: Linked Lists

Chapter 17: Linked Lists Chapter 17: Linked Lists 17.1 Introduction to the Linked List ADT Introduction to the Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures list head

More information

Programming in C/C Lecture 2

Programming in C/C Lecture 2 Programming in C/C++ 2005-2006 Lecture 2 http://few.vu.nl/~nsilvis/c++/2006 Natalia Silvis-Cividjian e-mail: nsilvis@few.vu.nl vrije Universiteit amsterdam News Check announcements on the C/C++ website

More information

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is

More information

Dynamic Allocation of Memory

Dynamic Allocation of Memory Dynamic Allocation of Memory Lecture 5 Section 9.8 Robb T. Koether Hampden-Sydney College Wed, Jan 24, 2018 Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 1 / 34

More information

Low-Level C Programming. Memory map Pointers Arrays Structures

Low-Level C Programming. Memory map Pointers Arrays Structures Low-Level C Programming Memory map Pointers Arrays Structures Memory Map 0x7FFF_FFFF Binaries load at 0x20000 by default Stack start set by binary when started Stack grows downwards You will need one stack

More information

Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science

Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science Data Structures in C C Programming and Software Tools N.C. State Department of Computer Science Data Structures in C The combination of pointers, structs, and dynamic memory allocation allows for creation

More information

Data Structure Series

Data Structure Series Data Structure Series This series is actually something I started back when I was part of the Sweet.Oblivion staff, but then some things happened and I was no longer able to complete it. So now, after

More information

Lectures 13 & 14. memory management

Lectures 13 & 14. memory management Lectures 13 & 14 Linked lists and memory management Courtesy of Prof. Garcia (UCB) CS61C L05 Introduction to C (pt 3) (1) Review Pointers and arrays are virtually same C knows how to increment pointers

More information

MODULE 5: Pointers, Preprocessor Directives and Data Structures

MODULE 5: Pointers, Preprocessor Directives and Data Structures MODULE 5: Pointers, Preprocessor Directives and Data Structures 1. What is pointer? Explain with an example program. Solution: Pointer is a variable which contains the address of another variable. Two

More information

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues By: Pramod Parajuli, Department of Computer Science, St. Xavier

More information

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers School of Electrical and Computer Engineering Cornell University revision: 2017-09-23-11-06 1 Pointer Basics 2 2 Call by Value vs. Call

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

In Java we have the keyword null, which is the value of an uninitialized reference type

In Java we have the keyword null, which is the value of an uninitialized reference type + More on Pointers + Null pointers In Java we have the keyword null, which is the value of an uninitialized reference type In C we sometimes use NULL, but its just a macro for the integer 0 Pointers are

More information

CS 241 Data Organization Binary Trees

CS 241 Data Organization Binary Trees CS 241 Data Organization Binary Trees Brooke Chenoweth University of New Mexico Fall 2017 Binary Tree: Kernighan and Ritchie 6.5 Read a file and count the occurrences of each word. now is the time for

More information

Self-referential Structures and Linked List. Programming and Data Structure 1

Self-referential Structures and Linked List. Programming and Data Structure 1 Self-referential Structures and Linked List Programming and Data Structure 1 Linked List :: Basic Concepts A list refers to a set of items organized sequentially. An array is an example of a list. The

More information

o Code, executable, and process o Main memory vs. virtual memory

o Code, executable, and process o Main memory vs. virtual memory Goals for Today s Lecture Memory Allocation Prof. David August COS 217 Behind the scenes of running a program o Code, executable, and process o Main memory vs. virtual memory Memory layout for UNIX processes,

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is

More information

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Lecture 14 No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 1 Outline Static

More information

ADT: Design & Implementation

ADT: Design & Implementation CPSC 250 Data Structures ADT: Design & Implementation Dr. Yingwu Zhu Abstract Data Type (ADT) ADT = data items + operations on the data Design of ADT Determine data members & operations Implementation

More information

Dynamic Memory Allocation

Dynamic Memory Allocation Dynamic Memory Allocation Lecture 15 COP 3014 Fall 2017 November 6, 2017 Allocating memory There are two ways that memory gets allocated for data storage: 1. Compile Time (or static) Allocation Memory

More information

Linked List using a Sentinel

Linked List using a Sentinel Linked List using a Sentinel Linked List.h / Linked List.h Using a sentinel for search Created by Enoch Hwang on 2/1/10. Copyright 2010 La Sierra University. All rights reserved. / #include

More information

Memory Management. To do. q Basic memory management q Swapping q Kernel memory allocation q Next Time: Virtual memory

Memory Management. To do. q Basic memory management q Swapping q Kernel memory allocation q Next Time: Virtual memory Memory Management To do q Basic memory management q Swapping q Kernel memory allocation q Next Time: Virtual memory Memory management Ideal memory for a programmer large, fast, nonvolatile and cheap not

More information

CS201- Introduction to Programming Current Quizzes

CS201- Introduction to Programming Current Quizzes CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements

More information

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees Instructor: Krithika Venkataramani Semester 2, 2011-2012 1 Introduction to Linked Lists Each bead connected to the next through a

More information

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers 04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University

More information

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers Introduction to C Geared toward programmers Robert Escriva Slide heritage: Alin Dobra Niranjan Nagarajan Owen Arden Cornell CS 4411, August 30, 2010 1 Why C? 2 A Quick Example 3 Programmer s Responsibilities

More information

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links Linked-List Basic Examples A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links Accessed via a pointer to the first node of the list Subsequent

More information

Lecture 2, September 4

Lecture 2, September 4 Lecture 2, September 4 Intro to C/C++ Instructor: Prashant Shenoy, TA: Shashi Singh 1 Introduction C++ is an object-oriented language and is one of the most frequently used languages for development due

More information

CSCI 171 Chapter Outlines

CSCI 171 Chapter Outlines Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures

More information

However, in C we can group related variables together into something called a struct.

However, in C we can group related variables together into something called a struct. CIT 593: Intro to Computer Systems Lecture #21 (11/27/12) Structs Unlike Java, C++, and to some extent Python, C is not traditionally considered an objectoriented language. That is, there is no concept

More information

CP2 Revision. theme: dynamic datatypes & data structures

CP2 Revision. theme: dynamic datatypes & data structures CP2 Revision theme: dynamic datatypes & data structures structs can hold any combination of datatypes handled as single entity struct { }; ;

More information

Solution for Data Structure

Solution for Data Structure Solution for Data Structure May 2016 INDEX Q1 a 2-3 b 4 c. 4-6 d 7 Q2- a 8-12 b 12-14 Q3 a 15-18 b 18-22 Q4- a 22-35 B..N.A Q5 a 36-38 b N.A Q6- a 39-42 b 43 1 www.brainheaters.in Q1) Ans: (a) Define ADT

More information

C++ for Java Programmers

C++ for Java Programmers Lecture 6 More pointing action Yesterday we considered: Pointer Assignment Dereferencing Pointers to Pointers to Pointers Pointers and Array Pointer Arithmetic 2 Todays Lecture What do we know 3 And now

More information

CS24 Week 2 Lecture 1

CS24 Week 2 Lecture 1 CS24 Week 2 Lecture 1 Kyle Dewey Overview C Review Void pointers Allocation structs void* (Void Pointers) void* Like any other pointer, it refers to some memory address However, it has no associated type,

More information

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first Linked Lists Introduction to Linked Lists A data structure representing a Week 8 Gaddis: Chapter 17 CS 5301 Spring 2014 Jill Seaman A series of dynamically allocated nodes chained together in sequence

More information

Ashish Gupta, Data JUET, Guna

Ashish Gupta, Data JUET, Guna Categories of data structures Data structures are categories in two classes 1. Linear data structure: - organized into sequential fashion - elements are attached one after another - easy to implement because

More information

CA31-1K DIS. Pointers. TA: You Lu

CA31-1K DIS. Pointers. TA: You Lu CA31-1K DIS Pointers TA: You Lu Pointers Recall that while we think of variables by their names like: int numbers; Computer likes to think of variables by their memory address: 0012FED4 A pointer is a

More information

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

MM1_ doc Page E-1 of 12 Rüdiger Siol :21 Contents E Structures, s and Dynamic Memory Allocation... E-2 E.1 C s Dynamic Memory Allocation Functions... E-2 E.1.1 A conceptual view of memory usage... E-2 E.1.2 malloc() and free()... E-2 E.1.3 Create

More information

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists Lecture 5: Outline I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists Multidimensional arrays: 2D Declaration int a[3][4]; /*Conceptually 2D matrix

More information

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers 0-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University

More information

Programs in memory. The layout of memory is roughly:

Programs in memory. The layout of memory is roughly: Memory 1 Programs in memory 2 The layout of memory is roughly: Virtual memory means that memory is allocated in pages or segments, accessed as if adjacent - the platform looks after this, so your program

More information

Lecture Notes on Memory Management

Lecture Notes on Memory Management Lecture Notes on Memory Management 15-122: Principles of Imperative Computation Frank Pfenning Lecture 22 November 11, 2010 1 Introduction Unlike C0 and other modern languages like Java, C#, or ML, C requires

More information

Dynamic Memory Management! Goals of this Lecture!

Dynamic Memory Management! Goals of this Lecture! Dynamic Memory Management!!! 1 Goals of this Lecture! Help you learn about:! Dynamic memory management techniques! Garbage collection by the run-time system (Java)! Manual deallocation by the programmer

More information

Run-time Environments - 3

Run-time Environments - 3 Run-time Environments - 3 Y.N. Srikant Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler Design Outline of the Lecture n What is run-time

More information

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Chapter 2 (Dynamic variable (i.e. pointer), Static variable) Chapter 2 (Dynamic variable (i.e. pointer), Static variable) August_04 A2. Identify and explain the error in the program below. [4] #include int *pptr; void fun1() { int num; num=25; pptr= &num;

More information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #4 C Memory Management 2007-06-28 Scott Beamer, Instructor iphone Comes out Tomorrow CS61C L4 C Memory Management (1) www.apple.com/iphone

More information

Prepared by Mrs.D.Maladhy (AP/IT/RGCET) Page 1

Prepared by Mrs.D.Maladhy (AP/IT/RGCET) Page 1 Basics : Abstract Data Type(ADT) introduction to data structures representation - implementation Stack and list: representing stack implementation application balancing symbols conversion of infix to postfix

More information

Dynamic Memory Allocation and Linked Lists

Dynamic Memory Allocation and Linked Lists CSE 2421: Systems I Low-Level Programming and Computer Organization Dynamic Memory Allocation and Linked Lists Presentation I Read/Study: Reek Chapter 11 & 12 Gojko Babić 02-26-2017 Functions malloc and

More information

Chapter 17: Linked Lists

Chapter 17: Linked Lists Chapter 17: Linked Lists Copyright 2009 Pearson Education, Inc. Copyright Publishing as Pearson 2009 Pearson Addison-Wesley Education, Inc. Publishing as Pearson Addison-Wesley 17.1 Introduction to the

More information

Limitations of the stack

Limitations of the stack The heap hic 1 Limitations of the stack int *table_of(int num, int len) { int table[len+1]; for (int i=0; i

More information

Dynamic Memory Management

Dynamic Memory Management Dynamic Memory Management 1 Goals of this Lecture Help you learn about: Dynamic memory management techniques Garbage collection by the run-time system (Java) Manual deallocation by the programmer (C, C++)

More information

Motivation was to facilitate development of systems software, especially OS development.

Motivation was to facilitate development of systems software, especially OS development. A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.

More information

Lecture 2: C Programm

Lecture 2: C Programm 0 3 E CS 1 Lecture 2: C Programm ing C Programming Procedural thought process No built in object abstractions data separate from methods/functions Low memory overhead compared to Java No overhead of classes

More information

struct node{ int info; struct node *left, *right; }; typedef struct node nodeptr; A Linear Doubly Linked List

struct node{ int info; struct node *left, *right; }; typedef struct node nodeptr; A Linear Doubly Linked List 1 EEE 212 Algorithms & Data Structures Spring 05/06 Lecture Notes # 13 Outline Doubly Linked Lists Linear & Circular Doubly Linked Lists Primitive Functions in Doubly Linked Lists Application of the Doubly

More information

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS Syllabus: Pointers and Preprocessors: Pointers and address, pointers and functions (call by reference) arguments, pointers and arrays, address arithmetic, character pointer and functions, pointers to pointer,initialization

More information

Common Misunderstandings from Exam 1 Material

Common Misunderstandings from Exam 1 Material Common Misunderstandings from Exam 1 Material Kyle Dewey Stack and Heap Allocation with Pointers char c = c ; char* p1 = malloc(sizeof(char)); char** p2 = &p1; Where is c allocated? Where is p1 itself

More information

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra Linked List in Data Structure By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra Linked List Like arrays, Linked List is a linear data

More information

Memory Management. CS31 Pascal Van Hentenryck CS031. Lecture 19 1

Memory Management. CS31 Pascal Van Hentenryck CS031. Lecture 19 1 Memory Management CS31 Pascal Van Hentenryck CS031 Lecture 19 1 Memory Management What is it? high-level languages abstract many aspects of memory management Support varies with the language Java (ML/Prolog/Lisp/Smalltalk)

More information

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Cpt S 122 Data Structures. Course Review Midterm Exam # 1 Cpt S 122 Data Structures Course Review Midterm Exam # 1 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 1 When: Friday (09/28) 12:10-1pm Where:

More information

Run Time Environment

Run Time Environment CS 403 Compiler Construction Lecture 12 Run Time Environment and Management [Based on Chapter 7 of Aho2] 1 Run Time Environment From Lecture 1 to 11, we have seen many jobs that are done by a compiler.

More information

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables. When a program is run, memory space is immediately reserved for the variables defined in the program. This memory space is kept by the variables until the program terminates. These variables are called

More information

Lecture 8 Data Structures

Lecture 8 Data Structures Lecture 8 Data Structures 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning, André Platzer, Rob Simmons, Iliano Cervesato In this lecture we introduce the idea of imperative data

More information

CS 61c: Great Ideas in Computer Architecture

CS 61c: Great Ideas in Computer Architecture Arrays, Strings, and Some More Pointers June 24, 2014 Review of Last Lecture C Basics Variables, functioss, control flow, types, structs Only 0 and NULL evaluate to false Pointers hold addresses Address

More information

Memory management. Johan Montelius KTH

Memory management. Johan Montelius KTH Memory management Johan Montelius KTH 2017 1 / 22 C program # include int global = 42; int main ( int argc, char * argv []) { if( argc < 2) return -1; int n = atoi ( argv [1]); int on_stack

More information

Write a program that creates in the main function two linked lists of characters and fills them with the following values:

Write a program that creates in the main function two linked lists of characters and fills them with the following values: Write a program that creates in the main function two linked lists of characters and fills them with the following values: The first list will have 3 nodes with the following characters: A,B, and C. The

More information

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF

More information

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Dynamic Memory Management Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island 1 Dynamic Memory Allocation Dynamic memory allocation is used to

More information

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers Introduction to C Geared toward programmers Sean Ogden Slide heritage: Alin Dobra Niranjan Nagarajan Owen Arden Robert Escriva Zhiyuan Teo Ayush Dubey Cornell CS 4411, August 30, 2013 Administrative Information

More information

CS61, Fall 2012 Section 2 Notes

CS61, Fall 2012 Section 2 Notes CS61, Fall 2012 Section 2 Notes (Week of 9/24-9/28) 0. Get source code for section [optional] 1: Variable Duration 2: Memory Errors Common Errors with memory and pointers Valgrind + GDB Common Memory Errors

More information

Dynamic memory. EECS 211 Winter 2019

Dynamic memory. EECS 211 Winter 2019 Dynamic memory EECS 211 Winter 2019 2 Initial code setup $ cd eecs211 $ curl $URL211/lec/06dynamic.tgz tar zx $ cd 06dynamic 3 Oops! I made a mistake. In C, the declaration struct circle read_circle();

More information

Lecture 4: Elementary Data Structures Steven Skiena

Lecture 4: Elementary Data Structures Steven Skiena Lecture 4: Elementary Data Structures Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.stonybrook.edu/ skiena Problem of the Day Find two

More information

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers Introduction to C Geared toward programmers Ayush Dubey Slide heritage: Alin Dobra Niranjan Nagarajan Owen Arden Robert Escriva Zhiyuan Teo Cornell CS 4411, August 31, 2012 Administrative Information Outline

More information

Dynamic Data Structures (II)

Dynamic Data Structures (II) Lecture 23 Dynamic Data Structures (II) CptS 121 Summer 2016 Armen Abnousi Data Structure Data structures are different ways of organizing data in computer We design new data structures to make the programs

More information

Elementary Data Structures: Part 1: Arrays, Lists. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Elementary Data Structures: Part 1: Arrays, Lists. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Elementary Data Structures: Part 1: Arrays, Lists CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1 Basic Types Types like integers, real numbers, characters.

More information