Date post: | 07-Apr-2018 |
Category: |
Documents |
Upload: | irina-preda |
View: | 218 times |
Download: | 0 times |
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 1/24
Data Structures & Algorithms
Mugurel Ionuì Andreica
Spring 2010
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 2/24
Grading
� Activity during the Laboratory ± 20%
� Homework Assignments ± 30% ± Must be presented at the laboratory
� Tests during the Lectures ± 10% (bonus)
� Exam ± 50%
� If you¶re repeating the class ± Activity during the Laboratory ± 5%
� Activity during previous years can be counted towards this grade (please talk to meduring the breaks)
± Homework Assignments ± 45%� Must still be presented at the laboratory
± Tests during the Lecture ± 10% (bonus) ± Exam ± 50%
� Must obtain at least 25% of the final grade (from Lab Activity + Homework Assignments + Course Attendance) in order to be allowed to participate inthe exam
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 3/24
Course Topics
� Introduction to C/C++ Programming
� Abstract Data Type ± concept
� Stack
� Queue� Dynamic Memory Allocation
� Linked Lists
� Graphs
� Hash Tables� Trees
� Binary Search Trees
� Heaps
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 4/24
Introduction to C/C++ Programming
� Similar to Java
� The basic structure of a C program: ± Inclusion of headers
± Definition of types/classes
± Declaration of global variables
± Definition of functions
± The main function
� Similarities to the structure of a Java program: ± Inclusion of headers is similar to importing packages or classes
± C++ classes may contain both variables (fields) and functions withpublic/private/protected access specifications
� Differences from the structure of a Java program: ± Functions and variables may also be defined outside of a class
± The main function is not part of a class
± Arrays can also be allocated statically in C/C++
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 5/24
Example of a C/C++ Program
#include <stdio.h> // inclusion of the stdio.h header
int a, b, c; // global variables of the type int: a, b, c
int main() { // beginning of the main functiona = 10;
scanf("%d", &b); // read the value of b from the standard input
c = a + b; // assign the sum of a and b to the variable c
printf("%d\n", c); // print the value of c to the standard output
return 0; // finish the main function successfully
}
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 6/24
Common C/C++ Types� Basic types
± int
± char
± float
± double
± void ± only for function return values
� Structured data types
± struct s { int x; char y[2], z; double w };� Classes
� Pointer types ± int*, int**, ...
± char*, char**, ...
± float*, float**, ...
± double*, double**, ...
± void*, void**, ...
± Pointers to structs ± Pointers to classes
� Defining (multidimensional) arrays ± int v[100] // a static array named v with 100 elements, from 0 to 99
± int u[100][150], v[10][20][30], w[10][20][30][40], ...
± char u[100], v[10][20], w[10][20][30], ...
± float u[100], v[10][20], w[10][20][30], ...
± struct s u[100], v[10][20], w[10][20][30], ...
± ...
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 7/24
Commonly Used Headers
� stdio.h ± I/O functions
± scanf(format, ...) ± used for reading data� format=a string containing characters or format specifiers
± %d ± int
± %f ± float ± %lf ± double
� variations: fscanf, sscanf
± printf(string_with_format, ...) ± used for printing data� string_with_format=a string containing characters and format specifiers
± %d ± int
± %.4f ± prints a float with 4 decimal places
± %.4lf ± print a double with 4 decimal places
� variations: fprintf, sprintf
� stdlib.h ± Functions for dynamic memory allocation
� string.h ± Functions for string processing
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 8/24
C/C++ Programs using Arrays
int a[100];
int i, n;
int main() {
a[2] = 3; // is it correct ?a[99] = 6; // is it correct ?
a[100] = 5; // is it correct ?
a[101] = 13; // is it correct ?
n = 50;
for (i = 0; i < n; i++)a[i] = 2*i;
return 0;
}
#include <stdio.h>
double a[100], b[100], c[100];
int i, n;
int main() {scanf(³%d´, &n);
for (i = 0; i < n; i++) scanf(³%lf´, &a[i]);
for (i = 0; i < n; i++) scanf(³%lf´, &b[i]);
for (i = 0; i < n; i++) c[i] = a[i] + b[i];
for (i = 0; i < n; i++) printf(³%.3lf ³, c[i]);printf(³\n´);
return 0;
}
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 9/24
Bubble-Sort & Selection Sort#include <stdio.h>
float a[100];
int i, n, sw;
int main() {
scanf(³%d´, &n);
for (i = 0; i<n; i++) scanf(³%f´, &a[i]);
sw = 1;
while (sw) {
sw = 0;
for (i = 0; i < n - 1; i++)
if (a[i] > a[i + 1]) { // swap a[i] and a[i+1]
// option 1 - is this correct ?
a[i + 1] = a[i]; a[i] = a[i + 1];// option 2 - is this correct ?
float x = a[i]; a[i] = a[i+1]; a[i+1] = x;
sw = 1;
}
}
return 0;
}
#include <stdio.h>
float a[100];
int i, j, n;
int main() {
scanf(³%d´, &n);
for (i = 0; i<n; i++) scanf(³%f´, &a[i]);
for (i = 0; i < n; i++) {
int vmin_poz = i;
for (j = i + 1; j < n; j++)
if (a[j] < a[vmin_poz])
vmin_poz = j;
// swap a[i] and a[vmin_poz]
float x = a[i];
a[i] = a[vmin_poz];
a[vmin_poz] = x;
}
return 0;
}
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 10/24
Insertion Sort & Matrix
Multiplication#include <stdio.h>
double a[100];
int i, j, k, n;
int main() {
scanf(³%d´, &n);
for (i = 0; i<n; i++) scanf(³%lf´, &a[i]);
for (i = 1; i < n; i++) // why i=1 ?
for (j = 0; j < i; j++)
if (a[i] < a[j]) {
double x = a[i]; // really needed ?
// option 1 ± is this correct ?
for (k = j + 1; k <= i; k++) a[k] = a[k - 1];
// option 2 ± is this correct ?
for (k = i; k >= j+1; k--) a[k] = a[k - 1];
a[j] = x;
break;
}
// if, in the end, j == i => no insertion
return 0;}
#include <stdio.h>
#include <string.h>
#define NMAX 100
double a[NMAX][NMAX], b[NMAX][NMAX], c[NMAX][NMAX];
int i, j, k, n;
int main() {
scanf("%d", &n);
for (i = 0; i < n; i++) for (j = 0; j < n; j++) scanf("%lf", &a[i][j]);
for (i = 0; i < n; i++) for (j = 0; j < n; j++) scanf("%lf", &b[i][j]);
memset(c, 0, sizeof(c));
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
for (k = 0; k < n; k++)
c[i][j] += a[i][k] * b[k][j];
// question: does changing the order of the fors still produce
// a correct result ? (there are 3!=6 possible orderings)
// ex.1: for (k=0;k<n;k++) for (i=0;i<n;i++) for(j=0;j<n;j++)
// ex.2: for (i=0;i<n;i++) for (k=0;k<n;k++) for(j=0;j<n;j++)
return 0;
}
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 11/24
The struct Data Type
� struct struct_name {
variables (fields of the struct type)
}
� Examples:
± struct mystruct { int x; double y[10], z; float w; };
struct mystruct a, b[10], c[20][30];
a.x=3; b[2].y[3]=0.7; c[3][5].w=1.1; ± struct mystruct2 { int x; struct mystruct y; };
struct mystruct2 a, b[10], c[20][30];
a.x=3; b[2].y.x=4; c[3][5].y.y[3]=0.5;
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 12/24
Sample C/C++ Programs
#include <stdio.h>
struct s {
int u;
char z[89];
}
struct s a[4][5][6][7];
int main() {
int i, j, k, l, m;
for (i = 0; i < 4; i++)
for (j = 0; j < 5; j++)
for (k = 0; k < 6; k++)
for (l = 0; l < 7; l++) {
scanf("%d", &a[i][j][k][l].u);
for (m = 0; m < 89; m++)a[i][j][k][l].z[m] = 'a' + (m % ('z'-'a'+1));
}
return 0;
}
#include <stdio.h>
#include <math.h>
struct point {
double x, y; };
struct point v[100];
int i, j, k, n;
int main() {
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%lf %lf", &v[i].x, &v[i].y);
for (i = 0; i < n; i++)
for (j = i + 1; j < n; j++)
for (k = j + 1; k < n; k++) {
double area = 0.5 * fabs(v[i].x * v[j].y - v[i].y * v[j].x +v[j].x * v[k].y - v[j].y * v[k].x +
v[k].x * v[i].y - v[k].y * v[i].x);
printf("The area of the triangle (%d,%d,%d) is %.4lf\n", i, j, k, area);
}
return 0;
}
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 13/24
Functions
� return_type function_name(type1 pname1, type2 pname2,..., typen pnamen) {
body of the function
}
� Examples: ± int sum(int a, int b) { return a+b; } ± int isPrime(int x) {
for (int i = 2; i*i <= x; i++)
if (x % i == 0) return 0;
return 1;
} ± int factorial(int n) {
if (n <= 1) return 1; else return n*factorial(n-1); }
± int gcd(int a, int b) {
if (b == 0) return a; else return gcd(b, a % b); }
± double mult(double x, double y) { return x*y; }
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 14/24
C/C++ Programs using Functions
#include <stdio.h>
double u[100], v[100], sum[100];
void readArrays(double a[], double b[], int n) {
int i;
for (i = 0; i < n; i++)
scanf("%lf", &a[i]);
for (i = 0; i < n; i++)scanf("%lf", &b[i]);
}
void addArrays(double a[], double b[], double c[], int n) {
int i;
for (i = 0; i < n; i++)
c[i] = a[i] + b[i];
}
int main() {
int n;
scanf("%d", &n);
readArrays(u, v, n);
addArrays(u, v, sum, n);
return 0;
}
#include <stdio.h>
#include <math.h>
struct point {
double x, y; };
double area(struct point p1, struct point p2, struct point p3) {
return 0.5 * fabs(p1.x * p2.y - p1.y * p2.x +
p2.x * p3.y - p2.y * p3.x +p3.x * p1.y - p3.y * p1.x);
}
struct point v[100];
int i, j, k, n;
int main() {
scanf("%d", &n);for (i = 0; i < n; i++) scanf("%lf %lf", &v[i].x, &v[i].y);
for (i = 0; i < n; i++)
for (j = i + 1; j < n; j++)
for (k = j + 1; k < n; k++)
printf("The area of the triangle (%d,%d,%d) is %.4lf\n", i,
j, k, area(v[i], v[j], v[k]));
return 0;
}
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 15/24
The Fibonacci Sequence
#include <stdio.h>
int numCalls = 0;
int fibo(int n) {
numCalls++;
if (n <= 1)
return 1;
else
return fibo(n-1) + fibo(n-2);
}
int main() {
int n;
scanf("%d", &n);
printf("Fibonacci(%d)=%d\n", n, fibo(n));
printf("Total number of calls=%d\n", numCalls);
return 0;
}
#include <stdio.h>
#define NMAX 50
int numCalls = 0;
int memoFib[NMAX];
int fibo(int n) {
numCalls++;
if (memoFib[n] >= 0)return memoFib[n];
if (n <= 1)
return (memoFib[n] = 1);
else
return (memoFib[n] = fibo(n-1) + fibo(n-2));
}
int main() {
int i, n;
scanf("%d", &n);
for (i = 0; i <= n; i++) memoFib[i] = -1;
printf("Fibonacci(%d)=%d\n", n, fibo(n));
printf("Total number of calls=%d\n", numCalls);
return 0;}
� F(0)=F(1)=1
� F(n�2)=F(n-1)+F(n-2)
� 1, 1, 2, 3, 5, 8, 13, ...
V1
V2
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 16/24
The Fibonacci Sequence (cont.)� V1
± Fibonacci(20) = 10946
± numCalls = 21891
� Tree of Calls for V1 (n=4) :
� V2 ± Fibonacci(20) = 10946
± numCalls = 39
� Tree of Calls for V2 (n=4) :
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 17/24
The Fibonacci Sequence (cont.)
� What if we change theorder of the callsfibo(n-1) and fibo(n-2)? ± We use: fibo(n-2) +
fibo(n-1)
± Does the resut change? (V1, V2)
± Does the total number of calls change ? (V1,V2)
� A non-recursivefunction on the right
#include <stdio.h>
int fibo(int n) {
int i, fminus1, fminus2, fcurr;
fminus1 = 1;
fcurr = 1;
i = 1;
while (i < n) {
i++;
fminus2 = fminus1;
fminus1 = fcurr;
fcurr = fminus1 + fminus2;
}
return fcurr;
}
int main() {
int i, n;
scanf("%d", &n);
printf("Fibonacci(%d)=%d\n", n, fibo(n));
return 0;}
V3
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 18/24
Classes in C++
� class class_name {
access_specifier_1:
members;
methods;
access_specifier_2:members;
methods;
...
constructor // same name as the class
destructor // ~class_name}
� Access specifier = public / private / protected
� A class may contain variables and methods
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 19/24
Sample C++ Program with Classes
#include <stdio.h>
class MyClass {
private:
int x, cnt;
double v[100];
public:int y;
void setX(int value) {
x = value; }
int getX() {
return x; }
void addToV(int value) {v[cnt] = value;
cnt++; }
double getFromV(int pos) {
return v[pos]; }
MyClass(int value) {
printf("Calling the constructor\n");
x = value;
cnt = 0; }
~MyClass() {
printf("Calling the destructor\n");}};
int main() {
int i;
MyClass c(7);
printf("%d\n", c.getX());
c.setX(19);
c.y = 17
printf("%d\n", c.getX());
for (i = 0; i < 10; i++) c.addToV((double) i);
for (i = 9; i >= 0; i--)
printf("%.3lf\n", c.getFromV(i));
return 0;
}
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 20/24
Abstract Data Type
� A collection of axioms + operations
� Operations = what actions can be performed upon thedata type (implemented as functions in C/C++) ± for each operation we know:
� its name� its arguments (types and, possibly, names)
� its return type
� Axioms specify connections between operations (i.e. theoperations are related to one another)
� Does not contain information regarding theimplementation of the operations
� Similar to a Java interface (except that only theoperations are specified in an interface and no axioms)
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 21/24
Abstract Data Types - examples
� Stack
± Operations: push, pop, peek, isEmpty
± Axioms: a pop() call returns the argument x of the most
recent push(x) operation called on the data type for which no corresponding pop() has been called before
(or an error, otherwise)
� Queue
± Operations: enqueue, dequeue, peek, isEmpty ± Axioms: a dequeue() call returns the argument x of the
oldest enqueue(x) call for which no corresponding
dequeue() was called (or an error, otherwise)
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 22/24
From Abstract Data Types to Data
Structures
� Data structures will be initially handled as abstract datatypes
� First we will specify the operations and axioms (many
times, the axioms will be given implicitly)� Then we will discuss possible implementations(occasionally more than just one)
� Data structures store elements ± Sometimes, the elements may have any type
± Other times, the elements must obey some specific properties(e.g. they must be comparable)
� In order to store any type of elements => we will useclass t em pl at es (in C++)
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 23/24
Class Templates
� t em pl at e<ty pename T> class class_name { ... } ± A normal class definition will be pref i xed by
t em pl at e<ty pename T>
± The type T can now be used as a valid type within theclass
� we can have variables, function arguments and functionreturn values of type T
� The class cl ass_name is par amet er iz ed with thetype T
� Most of the times: T=the type of the elementsstored by the data structure
� Similar to Java generics
8/6/2019 1 Introduction to C C++ Programming+ADT
http://slidepdf.com/reader/full/1-introduction-to-c-c-programmingadt 24/24
Class Templates - Example
#include <stdio.h>
template<typename T> class MyGenericContainer {
private:
T privateObject;
public:
void setPrivateObject(T value) {
privateObject = value;}
T getPrivateObject() {
return privateObject;
}
MyGenericContainer(T value) {
privateObject = value;}
};
struct mystruct {
int x;
char y[32];
double z;
};
int main() {
MyGenericContainer<int> c1(7);
printf("%d\n", c1.getPrivateObject());
c1.setPrivateObject(9);
printf("%d\n", c1.getPrivateObject());
MyG
enericContainer<double> c2(7.9);printf("%.3lf\n", c2.getPrivateObject());
c2.setPrivateObject(9.902);
printf("%.3lf\n", c2.getPrivateObject());
struct mystruct a;
a.x = 3; a.y[4] = 'z'; a.y[5] = 90;
a.z = 90.234;
MyGenericContainer<struct mystruct> c3(a);
printf("%.3lf\n", (c3.getPrivateObject()).z);
a.z++;
c3.setPrivateObject(a);
printf("%.3lf\n", (c3.getPrivateObject()).z);
return 0;
}