Sie sind auf Seite 1von 26

Data Structures

cbonchis@info.uvt.ro

Organizational

Course objectives: present main data structures, improve programming. Final grade = sum of all: 1 p appreciation, 4 p homework, 2 p - laboratory test, 3 p - problem implementation, 2 p - final exam if you have >10 p the final grade is 10, if you have <5p fail

Attendance: recommended. Expect you: To be up to date with class material. To hand out programming assignments by the stated deadlines. Expect you: Work harder than for C programming language.

Important
Academic honesty: cheating leads to failing class and reporting. OK/encouraged: speak up in class. Two-way, rather than one-way communication. Request: be concise, to the point, respect time spent together in class. Disclaimer: I can make mistakes/be wrong. Let me know (in person, email) how I can improve things.

Further reference material

Introduction to Algorithms, Second Edition by: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein http://mitpress.mit.edu/algorithms/ Adam Drozdek Data structures and algorithms in C++, third edition, Thomson Course Technology, 2005. Michael T. Goodrich, Roberto Tamassia, David M. Mount Data Structures and Algorithms in C++.Second Edition Gabriel Istrate last year lectures: http://gabrielistrate.weebly.com/pentru-studenti.html other prefered Data Structures books

The plan
Linked Lists. Singly linked lists, Doubly linked lists. Stacks, queues, dequeues. Binary Search Trees AVL trees. Heaps. Red-black trees. Hash tables. Augmenting Data Structures.

Why data structures ?

Data structures: a way to define data types we want. Many tasks can be specified in terms of input and output. Input/output: data. Divided into types. Predefined (in C: int, Data types can involve packaging multiple elements of

long, short, double, etc), or user defined.

simpler types. Examples: complex numbers (real, imaginary), enums.

Performance requirements. Operations abstracted from requirements. Frequent operations should be fast.

Examples
Problem 1 Read a stream of words from an input. Maintainset of words, with frequencies. Want: quickly test if we have seen a word. Quickly update frequency. Problem 2 Evaluating postfix arithmetic expressions Problem 3 Computation of shortest paths from a Weighted Graph.

Example (operations from requirements)


1 2 3 6 7 4 8 5 10 14 13 11 12...

TCP: basis for much of Internet traffic. Data requirement: We need to buffer a packet that is out-of order. We need to pop elements that become in-order. We need to test emptyness of buffer. We need to produce first missing element Operation performance O(1)?.

What are data structures after all ?

(Abstract) sets of data ... E.g. complex numbers: two floats. ... together with operations one can perform on the data ... E.g. integer + (addition), - (subtraction), (multiplication) ... and performance guarantees. How to precisely implement operations is not a part of data structure specification. Concepts, not code.

Data types

All DS that share a common structure and expose the same set of operations. Predefined data types: array, structures, files. Scalar data type: ordering relation exists among elements. More complicated, dynamic DS: Lists, circular lists, trees, hash tables, graphs. Standard template library (STL): library of container classes, algorithms, and iterators; provides many of the basic algorithms and data structures of computer science

E.g. Standard data types: integers

Available on most hardware. Often subject to standardization. Integer data type: subset of integers. Operations: exact. In case of overflow computation stopped. Unsigned/signed integers, twos complement.
Value -128 -127 -1 0 127 128 255 Unsigned 00000000 01111111 10000000 11111111 Signed 11111111 10000001 00000000 01111111 Two's complement 10000000 10000001 11111111 00000000 01111111 -

Real numbers: data types

floating point representation. simple (32 bits), double (64 bits), extended precision (80 bits ). representation: three parts. sign, exponent, significand(mantissa). mantissa: normalized (by adjusting exponent) so that most significant bit is 1. e.g. double: 64 bits, one for the sign, 52 bits for mantissa (normalized), 11 bits exponent (base 2). arithmetic operations: +,-,*, / also % and bitwise operations.

Array data type/Vector

Ensures random access to its elements. Composed of objects of the same type. int myarray[10]; One dimensional arrays. Multidimensional arrays. type name[lim1]...[limn]; alternative in C++: vector.

Example using vector class


#include<vector> using namespace std; int main(){ static const int SIZE = 10000; vector<int> arr( SIZE ); ... }

some OOP

OOP: revolves around concept of object Objects: created using class definition Combining of data and related operations is called encapsulation. Allow us to conceal details of object operations from other objects (information-hiding).

Classes
class C{
public: ... (functions and data members) private: ... (functions and data members)

Classes: Constructors/Destructors
class C{
public: C(); // constructor C(const C &); // copy constructor ~C(); // destructor ...

Operations: message passing to objects


class person{ public: person(char *s="", int i=0, double d=1.0){ strcpy(name,s); age = i; credit = d; } void memberFunction1(){ ...} void memberFunction2(int i, char *s = "unknown") { } protected: char name[20]; int age; double credit; }; C object1("object1", 100,2000), object2("object2"), object3; object1.memberFunction1(); object1.memberFunction2(123);

Namespaces & Streams


using namespace std; string a; cout << "Hello world" << endl; cin >> a; or std::cout<<"Hello world" << std::endl; std::cin >> a;

Pointers

Variables that hold addresses of other variables. Dynamic memory allocation: Assignment: *p=20; Deallocation: delete p; <=> free(p); Delete reference: upon deallocation should assign p = 0;

int i=15, j, *p, *q;

p = new int; <=> p = (int *)malloc(sizeof(int));


Pointers and arrays


int a[5],*p; for(sum=a[0],i=1;i<5;i++) sum += a[i]; or for (sum=*a,i=1;i<5;i++) sum += *(a+i); or for(sum=*a,p=a+1;p<a+5;p++) sum += *p; p = new int[n]; delete [] p;

Pointers and reference variables


int n =5, *p = &n, &r =n;

r is a reference variable. Must be initialized in definition as reference to a particular variable. reference: different name for/constant pointer to variable.

cout << n << << *p<< << r<< endl; 555 n= 7; (*p = 7, r = 7) 777

BEST WAY TO PASS PARAMETER: const reference variables;

Generic Classes/Functions: Motivation

swap two integers

void swapint(int *a, int *b){ int temp = *a; *a = *b; *b = temp; } swap two reals void swapfloat(float *a, float *b){ float temp = *a; *a = *b; *b = temp; }

Generic Functions
template<class genType> (genType &el1, genType &el2){ genType tmp = el1; el1 = el2; el2 = tmp; }

more general code reusing code

Generic Classes
template<class genType> class genClass{ genType storage[50]; ... } template<class genType,int size=50> class genClass2{ genType storage[size]; ... } genClass <int> intObject1; genClass2<int,100> intObject2; E.g. see map from C++

Conclusions

some syntaxes of C++ For more details: read, practice!

Any questions?

Das könnte Ihnen auch gefallen