DATA STRUCTURES CHAPTER 1 FOUNDATIONAL OF DATA STRUCTURES This unit introduces some basic concepts that the student needs to be familiar with before attempting to develop any software. It describes data type and data structures, explaining the operations that may be performed on them. The unit introduces you to the fundamental notions of data structures, thus guiding you through and facilitating your understanding of the subsequent units. OBJECTIVES By the end of this unit, you should be able to: describe and use the following notions; data type, abstract data type and data structure outline the classification of data type give typical examples of data type explain the relevance of data structures in programming. 0 Data Type In computer programming, a data type simply refers to a defined kind of data, that is, a set of possible values and basic operations on those values. When applied in programming languages, a data type defines a set of values and the allowable operations on those values. Data types are important in computer programmes because they classify data so that a translator (compiler or interpreter) can reserve appropriate memory storage to hold all possible values, e.g. integers, real numbers, characters, strings, and Boolean values, all have very different representations in memory. A data type consists of: 0 a domain (= a set of values) www.naarocom.com Page 1
1 a set of operations that may be applied to the values. 23 Data Type Classification Some data items may be used singly whilst others may be combined together and arranged to form other data items. The former are classified as simple data types whereas the latter are classified as data structures. However, the following classification is appropriate for study at this level.the simple data types are classified as follows: 0 Character 1 Numeric integer 2 Numeric real 3 Boolean (logical). 3.2.1 Examples of Data Types Almost all programming languages explicitly include the notion of data type, though different languages may use different terminology. Common data types in programming languages include those that represent integers, floating point numbers, and characters, and a language may support many more. Example 1: Boolean or logical data type provided by most programming languages. 0 Two values: true, false. 1 Many operations, including: AND, OR, NOT, etc. Example 2: In Java programming language, the int type represents the set of 32-bit integers ranging in value from -2,147, 483, 648 to 2,147, 483, 647 and the operation such as addition, subtraction and multiplication that can be performed on integers. 23 Abstract Data Type An Abstract Data Type commonly referred to as ADT, is a collection of data objects characterized by how the objects are accessed; it is an abstract human concept www.naarocom.com Page 2
meaningful outside of computer science. (Note that "object", here, is a general abstract concept as well, i.e. it can be an "element" (like an integer), a data structure (e.g. a list of lists), or an instance of a class. (e.g. a list of circles). A data type is abstract in the sense that it is independent of various concrete implementations. Object-oriented languages such as C++ and Java provide explicit support for expressing abstract data types by means of classes. A first class abstract data type supports the creation of multiple instances of ADT and the interface normally provides a constructor, which returns an abstract handle to new data, and several operations, which are functions accepting the abstract handle as an argument. 3.3.1 Examples of Abstract Data Type Common abstract data programming languages Queues, Stacks and Trees. types (ADT) typically implemented in (or their libraries) include: Arrays, Lists, www.naarocom.com Page 3
0 What is a Data Structure? A data structure is the implementation of an abstract data type in a particular programming language. Data structures can also be referred to as data aggregate. A carefully chosen data structure will allow the most efficient algorithm to be used. Thus, a well-designed data structure allows a variety of critical operations to be performed using a few resources, both execution time and memory spaces as possible. 23 Classification of Data Structures Data structures are broadly divided into two: Linear Data Structures Non-Linear Data Structures. 0 Linear Data Structures Linear data structures are data structures in which individual data elements are stored and accessed linearly in the computer memory. For the purpose of this course, the following linear data structures would be studied: lists, stacks, queues and arrays in order to determine how information is processed during implementation. 3.5.2 Non-Linear Data Structures A non-linear data structure, as the name implies, is a data structure in which the data items are not stored linearly in the computer memory, but data items can be processed using some techniques or rules. Typical non-linear data structures to be studied in this course are Trees. 0 Data Structures and Programmes The structure of data in the computer is very important in software programmes, especially where the set of data is very large. When data is properly structured and stored in the
computer, the accessibility of data is easier and the software programme routines that make do with the data are made simpler; time and storage spaces are also reduced. In the design of many types of programmes, the choice of data structures is a primary design consideration, as experience in building large systems has shown that the difficulty of implementation and the quality and performance of the final result depends heavily on choosing the best data structure. EXERCISE 1 What are the constituents of a Data Type? Give 2 typical examples of data types.
0 Arrays In Computer Science, an array is a data structure consisting of a group of elements that are accessed by indexing. Each data item of an array is known as an element, and the elements are referenced by a common name known as the array name. In Java, as in most programmeming languages, an array is a structure that holds multiple values of the same type. A Java array is also called an object. An array can contain data of the primitive data types. As it is an object, an array must be declared and instantiated. For example: int[] anarray; anarray = new int[10]; An array can also be created using a shortcut. For example: int[] anarray = {1,2,3,4,5,6,7,8,9,10} An array element can be accessed using an index value. For example: int i = anarray[5] The size of an array can be found using the length attribute. For example: int len = anarray.length Before any array is used in the computer, some memory locations have to be created for storage of the elements. This is often done by using the DIM instruction of BASIC
programming language or DIMENSION instruction of FORTRAN programming language. For example, the instruction: DIM LAGOS (45) will create 45 memory locations for storage of the elements of the array called LAGOS. In most programming languages, each element has the same data type and the array occupies a contiguous area of storage. Most programming languages have a built-in array data type. Some programming languages support array programming which generalises operations and functions to work transparently over arrays as they do with scalars, instead of requiring looping over array members. 0 Declaration of Arrays Variables normally only store a single value but, in some situations, it is useful to have a variable that can store a series of related values - using an array. For example, suppose a programme is required that will calculate the average age among a group of six students. The ages of the students could be stored in six integer variables in C This is a SAMPLE (Few pages have been extracted from the complete notes:-it s meant to show you the topics covered in the full notes and as per the course outline. Download more at our websites: www.naarocom.com
To get the complete notes either in softcopy form or in Hardcopy (printed & Binded) form, contact us on: Call/text/whatsApp +254 719754141/734000520 Email: naarocom@gmail.com info@naarocom.com sales@naarocom.com Get news and updates by liking our page on facebook and follow us on Twitter Sample/preview is NOT FOR SALE