C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN...

18
1 C Programming for Engineers Data Structure ICEN 360– Spring 2017 Prof. Dola Saha

Transcript of C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN...

Page 1: C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN 360–Spring 2017 Prof. Dola Saha

1

C Programming for Engineers

Data StructureICEN 360– Spring 2017Prof. Dola Saha

Page 2: C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN 360–Spring 2017 Prof. Dola Saha

2

Data StructuresØ We’ve studied fixed-size data structures such as single-

subscripted arrays, double-subscripted arrays and structs.Ø This topic introduces dynamic data structures with sizes

that grow and shrink at execution time.§ Linked lists are collections of data items “lined up in a row”—insertions

and deletions are made anywhere in a linked list.§ Stacks are important in compilers and operating systems—insertions

and deletions are made only at one end of a stack—its top.§ Queues represent waiting lines; insertions are made only at the back

(also referred to as the tail) of a queue and deletions are made only from the front (also referred to as the head) of a queue.

§ Binary trees facilitate high-speed searching and sorting of data, efficient elimination of duplicate data items, representing file system directories and compiling expressions into machine language.

Page 3: C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN 360–Spring 2017 Prof. Dola Saha

3

Linked ListØ Linked lists are collections of data items “lined up in a row”—

insertions and deletions are made anywhere in a linked list.Ø Linear Linked ListØ Doubly linked listØ Circular linked list

Page 4: C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN 360–Spring 2017 Prof. Dola Saha

4

StacksØ Stacks are important in compilers and operating systems—

insertions and deletions are made only at one end of a stack—its top.

Ø Stack is referred to as LIFO (last-in-first-out).Ø PUSHØ POP

Page 5: C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN 360–Spring 2017 Prof. Dola Saha

5

QueuesØ Queues represent waiting lines; insertions are made only at the back

(also referred to as the tail) of a queue and deletions are made only from the front (also referred to as the head) of a queue.

Ø Used in networking when packets are queued to move from one layer to another.

Ø EnqueueØ Dequeue

Page 6: C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN 360–Spring 2017 Prof. Dola Saha

6

TreesØ A tree is a nonlinear, two-dimensional data structure with special

properties.Ø Tree nodes contain two or more links.Ø Binary trees facilitate high-speed searching and sorting of data, efficient

elimination of duplicate data items, representing file system directories and compiling expressions into machine language.

Page 7: C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN 360–Spring 2017 Prof. Dola Saha

7

Dynamic Memory Allocation

Ø Creating and maintaining dynamic data structures requires dynamic memory allocation—the ability for a program to obtain more memory space at execution time to hold new nodes, and to release space no longer needed.

Ø Functions malloc and free, and operator sizeof, are essential to dynamic memory allocation.

Page 8: C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN 360–Spring 2017 Prof. Dola Saha

8

malloc()

Ø void * malloc (size_t size)Ø Input: number of bytes to be allocated Ø Output: a pointer of type void * (pointer to void) to the

allocated memory.Ø A void * pointer may be assigned to a variable of any

pointer type.Ø Example:

newPtr = malloc(sizeof(int));

Ø The allocated memory is not initialized.Ø If no memory is available, malloc returns NULL.

Page 9: C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN 360–Spring 2017 Prof. Dola Saha

9

free()

Ø Function free deallocates memory—i.e., the memory is returned to the system so that it can be reallocated in the future.

Ø To free memory dynamically allocated by the preceding malloc call, use the statemento free(newPtr);

Ø C also provides functions calloc and realloc for creating and modifying dynamic arrays.

Page 10: C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN 360–Spring 2017 Prof. Dola Saha

10

Dynamic Memory Allocation

Page 11: C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN 360–Spring 2017 Prof. Dola Saha

11

Classroom Assignment

Ø Create a dynamic array of N elements, where the user may choose the elements to be either integer, float or double. Once the array is created, fill the array with random numbers. Finally, print out the values.

Page 12: C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN 360–Spring 2017 Prof. Dola Saha

12

Self Referencing Structures

Ø A self-referential structure contains a pointer member that points to a structure of the same structure type.

Ø Example:o struct node {

int data;struct node *nextPtr;

};

defines a type, struct node.Ø A structure of type struct node has two members—

integer member data and pointer member nextPtr.

Page 13: C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN 360–Spring 2017 Prof. Dola Saha

13

Linked List graphical representation

Page 14: C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN 360–Spring 2017 Prof. Dola Saha

14

Insert a node in order in a list

Page 15: C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN 360–Spring 2017 Prof. Dola Saha

15

Insert a node – C code

Page 16: C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN 360–Spring 2017 Prof. Dola Saha

16

Delete a node from list

Page 17: C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN 360–Spring 2017 Prof. Dola Saha

17

Delete a node – C code

Page 18: C Programming for Engineers Data Structure · 1 C Programming for Engineers Data Structure ICEN 360–Spring 2017 Prof. Dola Saha

18

Classwork Assignment

Ø Write a function to print the elements of a list.