Abstract Data Type

42
Abstract Data Type Data Structures. Main Notions and Definitions.

description

Abstract Data Type. Data Structures. Main Notions and Definitions. Atomic Data. Data Structure. A Data Structure is an aggregation of atomic and composite data into a set with defined relationships. Structure means a set of rules that holds the data together. - PowerPoint PPT Presentation

Transcript of Abstract Data Type

Page 1: Abstract Data Type

Abstract Data Type

Data Structures.

Main Notions and Definitions.

Page 2: Abstract Data Type

2

Atomic Data

Page 3: Abstract Data Type

3

Data Structure

• A Data Structure is an aggregation of atomic and composite data into a set with defined relationships.

• Structure means a set of rules that holds the data together.

• Taking a combination of data and fit them into such a structure that we can define its relating rules, we create a data structure.

Page 4: Abstract Data Type

4

Composite Data Structures

Page 5: Abstract Data Type

5

Data Structure is:

• A combination of elements in which each is either a data type or another data structure

• A set of associations or relationships involving the combined elements

Page 6: Abstract Data Type

6

Data Structures: Properties

• Most of the modern programming languages support a number of data structures.

• In addition, modern programming languages allow programmers to create new data structures for an application.

• Data structures can be nested. A data structure may contain other data structures (array of arrays, array of records, record of records, record of arrays, etc.)

Page 7: Abstract Data Type

7

Some Data Structures

Page 8: Abstract Data Type

Pseudocode

• Pseudocode is a pseudo programming language, which is commonly used to define algorithms

• Pseudocode is a natural language-like representation of the algorithm logic

• Its structure is close to the structure of the most high level programming languages, but it is free from many unnecessary details

8

Page 9: Abstract Data Type

Data Structures: A Pseudocode Approach with C

9

Page 10: Abstract Data Type

Data Structures: A Pseudocode Approach with C

10

Page 11: Abstract Data Type

11

The Abstract Data Type (ADT)

• We know what a data type can do

• How it is done is hidden for the user

The concept of abstraction means:

With an ADT users are not concerned with how the task is done but rather with what it can do.

Page 12: Abstract Data Type

12

ADT: Example

• The program code to read/write some data is ADT. It has a data structure (character, array of characters, array of integers, array of floating-point numbers, etc.) and a set of operations that can be used to read/write that data structure.

Page 13: Abstract Data Type

13

The Abstract Data Type (ADT)

• A data declaration packaged together with the operations that are meaningful for the data type.

• In other words, we encapsulate the data and the operations on the data, and then we hide them from the user.

The Abstract Data Type (ADT) is:

Declaration of data

Declaration of operations

Encapsulation of data and operations

Page 14: Abstract Data Type

14

The Abstract Data Type (ADT)

• All references to and manipulation of the data in a structure must be handled through defined interfaces to the structure.

• Allowing the application program to directly reference the data structure is a common fault in many implementations.

• It is necessary for multiply versions of the structure to be able to coexist.

• We must hide the implementation from the user while being able to store different data.

Page 15: Abstract Data Type

15

Page 16: Abstract Data Type

16

ADT Operations

• Data are entered, accessed, modified and deleted through the external interface, which is a “passageway” located partially “in” and partially out of the ADT.

• Only the public functions are accessible through this interface.

• For each ADT operation there is an algorithm that performs its specific task.

Page 17: Abstract Data Type

17

Typical ADTs:

• Lists

• Stacks

• Queues

• Trees

• Heaps

• Graphs

Page 18: Abstract Data Type

18

1-4 ADT Implementations

There are two basic structures we can use to implement an ADT list: arrays and linked lists. In this section we discuss the basic linked-list implementation.

Page 19: Abstract Data Type

19

Array Implementations

• In an array, the sequentiality of a list is maintained by the order structure of elements in the array (indexes).

• Although searching an array for an individual element can be very efficient, insertion and deletion of elements are complex and inefficient processes.

Page 20: Abstract Data Type

20

Linked Lists

• A Linked List is an ordered collection of data in which each element contains the location of the next element or elements.

• In a linked list, each element contains two parts: data and one or more links.

• The data part holds the application data – the data to be processed.

• Links are used to chain the data together. They contain pointers that identify the next element or elements in the list.

Page 21: Abstract Data Type

21

Linear and non-linear Linked Lists

• In linear linked lists, each element has only zero or one successor.

• In non-linear linked lists, each element can have zero, one or more successors.

Page 22: Abstract Data Type

22

Page 23: Abstract Data Type

23

Nodes A node is a structure that has two parts: the data and one or more

links.

The nodes in a linked list are called self-referential structures. In such a structure, each instance of the structure contains one or more pointers to other instances of the same structural type.

Page 24: Abstract Data Type

24

Nodes

• The data part in a node can be a single field, multiple fields, or a structure that contains several fields, but it always acts as a single field.

Page 25: Abstract Data Type

25

Page 26: Abstract Data Type

26

Linked Lists vs. Arrays

• The major advantage of the linked list over the array is that data are easily inserted and deleted.

• It is not necessary to shift elements of a linked list to make room for a new elements or to delete an element.

• However, because the elements are no longer physically sequenced in a linked list, we are limited to sequential searches.

Page 27: Abstract Data Type

27

1-5 Generic Code for ADT

In this section we discuss and provide examples of two tools that are required to implement an ADT.

• Pointer to Void• Pointer to Function

Page 28: Abstract Data Type

28

Generic Code

• In data structures we need to create generic code for abstract data types.

• Generic code allows us to write one set of code and apply it to any data type.

• For example, we can write generic functions to implement a stack structure. We can then use the generic functions to implement an integer stack, a float stack, etc.

Page 29: Abstract Data Type

29

Data Pointer

• A pointer is a programming language data type whose value refers directly to ("points to") another value stored elsewhere in the computer memory using its address. Obtaining the value that a pointer refers to is called dereferencing the pointer.

Page 30: Abstract Data Type

30

Pointer to void

• Casting of the pointer is its connection with particular data type.

• Major programming languages are strongly typed. This means that operations such as assign and compare must use compatible types or be cast to compatible types.

• The only exception is the pointer to void, which can be assigned without a cast.

• This means that a pointer to void is a generic pointer that can be used to represent any data type.

Page 31: Abstract Data Type

31

Pointer to void

Page 32: Abstract Data Type

32

Pointer to void

Page 33: Abstract Data Type

33

Pointer to void

• Important remark: a pointer to void cannot be dereferenced unless it is cast.

• In other words, we cannot use *p without casting (without connection of the pointer with particular data type).

Page 34: Abstract Data Type

34

Page 35: Abstract Data Type

35

Function malloc• This function in C (a similar function is presented

in all modern programming languages) returns a pointer to void.

• This function is used to dynamically allocate any type of data.

• This is a generic function that returns a pointer to void (void*). It can be used for returning a pointer to any data type. For example, a pointer to an integer can be created using

intPtr = (int*)malloc (sizeof (int))

Page 36: Abstract Data Type

36

Pointer to Node

Page 37: Abstract Data Type

37

Page 38: Abstract Data Type

38

Page 39: Abstract Data Type

39

(Continued)

Page 40: Abstract Data Type

40

Page 41: Abstract Data Type

41

Page 42: Abstract Data Type

42

Homework

• Sections 1.1-1.5