1 / 42

Abstract Data Type

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.

collinseric
Télécharger la présentation

Abstract Data Type

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Atomic Data

  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.

  4. Composite Data Structures

  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

  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.)

  7. Some Data Structures

  8. 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

  9. Data Structures: A Pseudocode Approach with C

  10. Data Structures: A Pseudocode Approach with C

  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.

  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.

  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

  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.

  15. 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.

  16. Typical ADTs: • Lists • Stacks • Queues • Trees • Heaps • Graphs

  17. 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.

  18. 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.

  19. 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.

  20. 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.

  21. 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.

  22. 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.

  23. 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.

  24. 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

  25. 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.

  26. 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.

  27. 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.

  28. Pointer to void

  29. Pointer to void

  30. Pointer to void • Important remark: a pointer to void cannot be dereferencedunless it is cast. • In other words, we cannot use *p without casting (without connection of the pointer with particular data type).

  31. Functionmalloc • 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))

  32. Pointer to Node

  33. (Continued)

  34. Homework • Sections 1.1-1.5

More Related