IS 2610: Data Structures
IS 2610: Data Structures
Data Type
n
Int Float Character Define values to operate (arguments of a function) Define operation (function definition)
Data Structure
n
What operations need to be performed? How to implement these operations? Arrays Structures
Defines data structures Declare functions to be used to manipulate the data structure Of the functions declared in Interface Program that uses the functions declared in the Interface to work at a higher level of abstraction
Arrays
n
Fixed collection of same-type data Access is made by using an index Contiguously stored
n n
Direct correspondence with memory systems Entire memory can be considered as an array of memory locations, with memory addresses corresponding to the array indices
In C array definition
q
A1[4]?
A1[i] = *(A1+i)?
Array
n Dynamic Memory #define N 1000 main() { int i, a[N]; } n
Allocation
#include <stdlib.h> main(int argc, char* argv) { int i, N = atoi(argv[1]); int *a = malloc(N*sizeof(int ); if (a==NULL) Insufficient memory
Sieve of Eratosthenes
#define N 20 main() { int i, j, a[N]; for (i = 1; i<N; i++) a[i]=1; for (i = 2; i<N; i++) if (a[i]) for (j = i; i*j<N; j++) a[i*j] = 0; for (j = 2; j<N; j++) if (a[i]) printf (%4d \n, i); }
Linked List
n
A set of items where each item is part of a node that also contains a link to a node
q q q
note that h->next denotes the 2 nd node and h->ch denotes The value a
h a e g m NULL
List traversal
n
m NULL
Linked Lists
t
n
h
Insert operation
f x a e g
(t after x)
m NULL
n
h
Delete Operation
a e
n
h
Exchange Operation
a t1 (exchange nodes after t1 and t2) e g h t2 m
String
n
Array based implementation in C Array of characters different from string associated with length
q
q q
Many applications involve processing textual data Computers provide access to bytes of memory that correspond directly to characters of strings
strlen(a)
for(i=0; a[i] != 0; i++); return i;
strcpy(a, b)
for(i=0; (a[i] = b[i]) != 0; i++); while (*a++ = *b++);
strcmp(a, b)
for(i=0; (a[i] == b[i]) != 0; i++); if (a[i] == 0) return 0; return a[i] b[i]
strcat(a, b)
strcpy(a+strlen(a), b)
Abstraction
n
Layers of abstraction
q q
Abstract model of a bit with binary 0-1 values Abstract model of a machine from from dynamic properties of the values of a certain set of bits Abstract model of a programming language that we realize by controlling the machine with a machine language program Abstract notion of algorithm implemented in C Develop abstract mechanisms for certain computational tasks at a higher level Define objects we want to manipulate
n
A data type that is access only through an interface Refer to a program that uses ADT as a client and program that specifies the data type as an implementation
q q
Provide an effective mechanism for organizing large software systems Provide ways to limit the size and complexity of interface between algorithms and associated data structures and programs that use the algorithms and data structures ADTs interface defines a precise means of communication
An ADT that comprises two basic operations: insert (push) a new item, and delete (pop) the item that was most recently inserted
q
push
Operator comes between the operands 4 + 5 is written as 4 5 + Operator comes after the operands 4 + 5 is written as 4 5 + Operator comes after the operands 4 + 5 is written as 4 5 +
Postfix expression
n n
Postfix expression
n n
store in STACK.h
Postfix notation
n
6 5 11
7 11 77
10
An ADT that comprises two basic operations: insert (put) a new item, and delete (get) the item that was least recently used
void QUEUEinit(int); int QUEUEempty(); void QUEUEput(Item); Item QUEUEget(); typedef struct QUEUEnode* link; struct QUEUEnode {Item item; link next;} static link head; link NEW(Item item, link next;} { link x = malloc(sizeof *x); x->item = item; x->next = next; return x; }
11
First-class ADT
q
q q
Clients use a single instance of STACK or QUEUE Only one object in a given program Could not declare variables or use it as an argument
A first-class data type is one for which we can have potentially many different instances, and which can assign to variables whichcan declare to hold the instances
Typedef struct {float r; float i;} Complex; Complex COMPLEXinit(float, float) float Re(float, float); float Im(float, float); Complex COMPLEXmult(Complex, Complex)
12
Recursive algorithm is one that solves a problem by solving one or more smaller instances of the same problem
q q q
Recursive function calls itself Factorial? Euclids method for finding the greatest Fibonacci numbers? Common divisor
int gcd(int m, int n){ if (n==0) return m; return gcd(n, m%n); }
13
Preorder
q q q
Visit a node, Visit left subtree, Visit right subtree Visit left subtree, Visit a node, Visit right subtree Visit left subtree, Visit right subtree Visit a node
Inorder
q q q
Postorder
q q q
14