Professional Documents
Culture Documents
Data Structures
Unit III
Basic Concepts of Data Structure
Data Structure is a way of collecting and organizing data in such a way that we can perform operations
on these data in an effective way. Data Structures is about rendering data elements in terms of some
relationship, for better organization and storage.
For example, In the data, player's name "Hitesh" and age 26. Here "Hitesh" is of String data type and 26 is
of integer data type.
We can organize this data as a record like Player record. Now we can collect and store player's records
in a file or database as a data structure.
For example: "Gayle" 30, "Sachin" 31, "Parth" 33
In simple language, Data Structures are structures programmed to store ordered data, so that various
operations can be performed on it easily.
Data Definition
Data Definition defines a particular data with following characteristics.
• Atomic − Definition should define a single concept
• Traceable − Definition should be be able to be mapped to some data element.
• Accurate − Definition should be unambiguous.
• Clear and Concise − Definition should be understandable.
Basic Concepts of Data Structure…Cont’d
Data Object
Data Object represents an object having a data.
Basic Concepts of Data Structure…Cont’d
Those data types for which a language has built-in support are known as Built-in Data types. For
example, most of the languages provides following built-in data types.
• Integers
• Boolean (true, false)
• Floating (Decimal numbers)
• Character and Strings
Basic Concepts of Data Structure…Cont’d
2. Derived Data Type
Those data types which are implementation independent as they can be implemented in one or other
way are known as derived data types. These data types are normally built by combination of primary or built-
in data types and associated operations on them. For example
• List
• Array
• Stack
• Queue
Basic Operations
The data in the data structures are processed by certain operations. The data structure chosen largely
depends on the frequency of the operation that needs to be performed on the data structure.
• Traversing
• Searching
• Insertion
• Deletion
• Sorting
• Merging
Basic Concepts of Data Structure…Cont’d
Types Of Data Structures
Data Structures are broadly classified into 2 types.
1. Primitive data structures
2. Non-primitive data structures
Basic Concepts of Data Structure…Cont’d
1. Primitive Data Structures
Primitive Data Structures are the basic data structures that directly operate upon the machine
instructions. They have different representations on different computers. Integers, Floating point
numbers, Character constants, String constants and Pointers come under this category.
2. Non-primitive Data Structures
Non-primitive data structures are more complicated data structures and are derived from primitive
data structures. They emphasize on grouping same or different data items with relationship between
each data item. Arrays, Lists and Files come under this category.
Asymptotic Notations
Asymptotic notations are method used to estimate and represent the efficiency of an algorithm using
simple formula. This ca be useful for separating algorithms that leads to different amounts of work for
large inputs.
Following are commonly used asymptotic notations used in calculating running time complexity of an
algorithm.
• Ο Notation
• Ω Notation
• θ Notation
Basic Concepts of Data Structure…Cont’d
Omega(Ω) Notation
The Ω(n) is the formal way to express the lower bound
of an algorithm's running time. It measures the best case
time complexity or best amount of time an algorithm can
possibly take to complete.
for a function f(n):-
Ω(f(n)) ≥ {g(n) : there exists c > 0 and n0 such that
g(n) ≤ c.f(n) for all n > n0.}
Basic Concepts of Data Structure…Cont’d
Theta(θ) Notation
The θ(n) is the formal way to express both the lower
bound and upper bound of an algorithm's running time.
for a function f(n):-
θ(f(n)) = {g(n) if and only if g(n) = Ο(f(n)) and g(n) =
Ω(f(n)) for all n > n0.}
Arrays
An array is a collection of similar items stored in
contiguous memory locations. In programming,
sometimes a simple variable is not enough to hold all
the data. For example, let’s say we want to store the
marks of 500 students, having 500 different variables
for this task is not feasible, we can define an array
with size 500 that can hold the marks of all students.
Array Representation
Following are the important terms to understand the
concept of Array.
• Element − Each item stored in an array is called an
element.
• Index − Each location of an element in an array has
a numerical index, which is used to identify the
element.
Arrays…Cont’d
Declaring and Accessing Array Elements
Array index starts with 0, which means the first array element is at index 0, second is at index 1
and so on. We can use this information to display the array elements.
Arrays can be declared in various ways in different languages. For illustration, let's take C++ array
declaration.
Arrays can be declared in various ways in different languages. For illustration, let's take C++ array
declaration.
Arrays…Cont’d
As per the above illustration, following are the important points to be considered.
• Index starts with 0.
• Array length is 10 which means it can store 10 elements.
• Each element can be accessed via its index. For example, we can fetch an element at index 6 as 9.
Basic Operations
Following are the basic operations supported by an array.
• Traverse − print all the array elements one by one.
• Insertion − Adds an element at the given index.
• Deletion − Deletes an element at the given index.
• Search − Searches an element using the given index or by the value.
• Update − Updates an element at the given index.
Arrays…Cont’d
Traverse Operation: This operation is to traverse through the elements of an array.
Example
Following program traverses and prints the elements of an array:
#include <iostream.h>
main()
{
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
cout << "The original array elements are :\n"; Output:
for(i = 0; i<n; i++) The original array elements are :
{ LA[0] = 1
cout << "LA[“ << i << “ ] = “ << LA[i] << endl; LA[1] = 3
LA[2] = 5
} LA[3] = 7
} LA[4] = 8
Arrays…Cont’d
Insertion Operation: Insert operation is to insert one or more data elements into an array. Based on the requirement, a new
element can be added at the beginning, end, or any given index of array.
Here, we see a practical implementation of insertion operation, where we add data at the end of the array
#include <iostream.h>
main() {
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
Output:
cout << "The original array elements are :\n";
The original array elements are :
for(i = 0; i<n; i++) {
LA[0] = 1
cout << "LA[“ << i << “ ] = “ << LA[i] << endl;
LA[1] = 3
}
LA[2] = 5
n = n + 1;
LA[3] = 7
while( j >= k) {
LA[4] = 8
LA[j+1] = LA[j]; The array elements after insertion :
j = j - 1; LA[0] = 1
} LA[1] = 3
LA[k] = item; LA[2] = 5
cout << "The array elements after insertion :\n"; LA[3] = 10
for(i = 0; i<n; i++) { LA[4] = 7
cout << "LA[“ << i << “ ] = “ << LA[i] << endl; LA[5] = 8
}
}
Arrays…Cont’d
Deletion Operation: Deletion refers to removing an existing element from the array and re-organizing all elements of an array.
Here is the practical implementation of delete an element available at the Kth position of LA. (Consider LA is a linear array with
N elements and K is a positive integer such that K<=N.)
#include <iostream.h>
void main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5;
int i, j;
cout << "The original array elements are :\n";
for(i = 0; i<n; i++) {
cout << "LA[“ << i << “ ] = “ << LA[i] << endl; Output:
} The original array elements are :
j = k; LA[0] = 1
while( j < n) { LA[1] = 3
LA[2] = 5
LA[j-1] = LA[j];
LA[3] = 7
j = j + 1;
LA[4] = 8
}
The array elements after deletion :
n = n -1; LA[0] = 1
cout << "The array elements after deletion :\n"; LA[1] = 3
for(i = 0; i<n; i++) { LA[2] = 7
cout << "LA[“ << i << “ ] = “ << LA[i] << endl; LA[3] = 8
}
}
Arrays…Cont’d
Search Operation: You can perform a search for an array element based on its value or its index.
Here is the practical implementation to find an element with a value of ITEM using sequential search. (Consider
LA is a linear array with N elements and K is a positive integer such that K<=N.)
#include <iostream.h>
void main() {
int LA[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0, j = 0;
cout << "The original array elements are :\n";
for(i = 0; i<n; i++) {
cout << "LA[“ << i << “ ] = “ << LA[i] << endl;
} Output:
while( j < n){
The original array elements are :
LA[0] = 1
if( LA[j] == item ) {
LA[1] = 3
break;
LA[2] = 5
}
LA[3] = 7
j = j + 1; LA[4] = 8
} Found element 5 at position 3
cout << "Found element ” << item << “at position ” << j+1;
}
Arrays…Cont’d
Update Operation: Update operation refers to updating an existing element from the array at a given
index.
Here is the practical implementation to update an element available at the Kth position of LA. (Consider
LA is a linear array with N elements and K is a positive integer such that K<=N.)
#include <iostream.h>
void main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5, item = 10; Output:
int i, j; The original array elements are :
LA[0] = 1
cout << "The original array elements are :\n"; LA[1] = 3
for(i = 0; i<n; i++) { LA[2] = 5
cout << "LA[“ << i << “ ] = “ << LA[i] << endl; LA[3] = 7
LA[4] = 8
}
The array elements after updation :
LA[k-1] = item; LA[0] = 1
cout << "The array elements after updation :\n"; LA[1] = 3
for(i = 0; i<n; i++) { LA[2] = 10
LA[3] = 7
cout << "LA[“ << i << “ ] = “ << LA[i] << endl; LA[4] = 8
}
}
Arrays…Cont’d
Types of array
Array can be single or multidimensional. The number of subscript or index determines the dimensions of
the array. An array of one dimension is known as a one-dimensional array or 1-D array, while an array of two
dimensions is known as a two-dimensional array or 2-D array
One-dimensional array:
Conceptually you can think of a one-dimensional array as a row, where elements are stored one after another.
Syntax: datatype array_name[size];
datatype: It denotes the type of the elements in the array.
array_name: Name of the array. It must be a valid identifier.
Size: Number of elements an array can hold.
Two dimensional array:
In two dimensional array, elements are stored as row and column.
Syntax: datatype array_name[size1][size2];
datatype: It denotes the type of the elements in the array.
array_name: Name of the array. It must be a valid identifier.
size1: Number of rows in an array.
Size2: Number of columns in an array.
Arrays…Cont’d
Multidimensional array:
Multidimensional arrays are also known as array of arrays. The data in multidimensional array is
stored in a tabular form as shown in the diagram below:
Arrays…Cont’d
Advantages of an Array
1. Random access of elements using array index.
2. Use of less line of code as it creates a single array of multiple elements.
3. Easy access to all the elements.
4. Traversal through the array becomes easy using a single loop.
5. Sorting becomes easy as it can be accomplished by writing less line of code.
Disadvantages of an Array
6. Allows a fixed number of elements to be entered which is decided at the time of declaration.
Unlike a linked list, an array is not dynamic.
7. Insertion and deletion of elements can be costly since the elements are needed to be managed
in accordance with the new memory allocation.
Ordered list
An ordered list is a list in which the order of the items is significant. However, the items in
an ordered list are not necessarily sorted. Consequently, it is possible to change the order of
items and still have a valid ordered list.
Consider a list of the titles of the chapters in this book. The order of the items in the list
corresponds to the order in which they appear in the book. However, since the chapter titles
are not sorted alphabetically, we cannot consider the list to be sorted. Since it is possible to
change the order of the chapters in book, we must be able to do the same with the items of
the list. As a result, we may insert an item into an ordered list at any position.
Working with Files
The I/O system of C++ handles file operations which are very much similar to the console input and
output operations. It uses file streams as an interface between the program and the files. The stream that
supplies data to the program is known as input stream and the one that receives data from the program is
known as output stream. In the other words, the input stream extracts(read) data from the file and the
output stream inserts (writes) data to the file.
The input operation involves the creation of an input stream and linking it with the program and the
input file. Similarly, the output operation involves establishing an output stream with necessary links with
the program and the output file.
Working with Files…Cont’d
.
Working with Files…Cont’d
Class Contents
Its purpose is to set the file buffers to read and write. Contains Openprot
filebuf
constant used in the open() of file stream classes. Also contain close() and
open() as members.
fstreambase Provides operations common to file streams. Serves as a base for fstream,
ifstream and ofstream class. Contains open() and close() functions.
Provides input operations. Contains open() with default input mode.
.ifstream Inherits the functions get(), getline(), read(), seekg(), tellg() functions from
istream.
ofstream Provides output operations. Contains open() with default output mode.
Inherits put(), seekp(), tellp() and write() functions from ostream.
Provides support for simultaneous input and output operations. Contains
fstream
open() with default input mode. Inherits all the functions from istream and
ostream classes through iostream.
Working with Files…Cont’d
Opening and closing a file:
If we want to use a disk file, we need to decide the following about the file and its intended use.
1. Suitable name for the file
2. Data type and structure
3. Purpose
4. Opening Method
The filename is a string of characters that makeup a valid filename for the operating system. It may
contain two parts, a primary name and optional period with extension.
Examples: Input.data Test.doc Student
For opening a file, we must first create a file stream and then linked it to the filename. A file stream can
be defined using the classes ifstream, ofstream and fstream that are contained in the header file fstream.
The class to be used depends upon the purpose, that is, whether we want to read data from the file or write
data to it. A file can be opened in two ways
1. Using the constructor function of the class.
2. Using the member function open() of the class.
The first method is useful when we use only one file in the stream. The second method is used when we
want to manage multiple files using one stream.
Working with Files…Cont’d
Opening Files using Constructor:
While using constructor for opening file, filename is used to initialize the file stream object. This involves the
following steps
1. Create a file stream object to manage the stream using the appropriate class. That is to say, the
class ofstream is used to create the output stream and the class ifstream to create the input
stream.
2. Initialize the file object with the desired file name.
For example, the following statement opens a file named “results” for output:
ofstream outfile(“results”); //output only
This create outfile as an ofstream object that manages the output stream. This object can beany valid
C++ name. This statement also opens the file results and attaches it to the output stream outfile.
Working with Files…Cont’d
Similarly, the following statement declares infile as an ifstream object and attaches it to the file data for
reading (input).
ifstream infile(“data”); //input only
The program may contain statements like
outfile << “Total”:
outfile << sum;
infile >> number;
infile >> string;
The same file name can be used for both reading and writing data as shown below.
Working with Files…Cont’d
Program1
……………….
……………….
ofstream outfile (“salary”); //creates outfile and connects salary to it
………………
………………
Program 2
………………
………………
ifstream infile (“salary”); //creates infile and connects salary to it
………………..
…………………
The connection with a file is closed automatically when the stream object expires(when a program
terminates). In the above statement, when the program1 is terminated, the salary file is disconnected from
the outfile stream. Similar action takes place when the program 2 terminates.
Working with Files…Cont’d
Instead of using two programs, one for writing data and another for reading data ,a single program can
be used to do both operations on a file.
…………
…………….
outfile.close(); //disconnect salary from outfile
ifstream infile (“salary”); // and connect to infile
………….
……………
infile.close(); // Disconnect salary from infile
Although we have uses a single program, we created two files stream objects, outfile and infile.
Working with Files…Cont’d
Opening Files using Open():
The function open() can be used to open multiple files that use the same stream object. For example to
process a set of files sequentially. in such cases, a single stream object can be created and can be used to
open each file in turn. This can be done as follows;
File-stream-class stream-object;
stream-object.open (“filename”);
Example:
ofstream outfile; // Create stream (for output)
outfile.open(“DATA1”) // Connect stream to DATA1
………………….
………………….
outfile.close(); // Disconnect stream from DATA1
Outfile.open(“DATA2); // Connect stream to DATA2
…………………
…………………
outfile.close(); // Disconnect stream from DATA2
…………………
…................
Above program segment opens two files in sequence for writing data. Note that the first file closes before
opening the second one.
Working with Files…Cont’d
Detecting end-of-file:
Detection of the end-of-file condition is necessary for preventing any further attempt to read data from
the file. An ifstream object, such as fin, returns a value of 0 if any error occurs in the file operation including
the end-of-file condition. Thus, the while loop terminates when fin returns a value of zero on reaching the
end-of –file condition.
There is an another approach to detect the end-of-file condition. In the below statement
if(fin1.eof() !=0 )
{
exit(1);
}
eof() is a member function of ios class. It returns a non zero value if end-of-file(EOF) condition is
encountered, and a zero, otherwise. Therefore, the above statement terminates the program on reaching the
end of the file.
Working with Files…Cont’d
‘Seek’ functions seekg() and seekp() can also be used with two arguments as follows:
seekg (offset, refposition);
seekp (offset, refposition);
The parameter offset represents the number of bytes the file pointer is to be moved from the location
specified by the parameter refposition. The refposition takes one of the following three constants defined in
the ios class:
• ios::beg Start of file
• ios::cur Current position of the pointer
• ios::end End of file
Working with Files…Cont’d
The seekg() function moves the associated file’s ‘get’ pointer while the seekp() function moves the
associated file’s ‘put‘ pointer. The following listshows some sample pointer offset calls and their actions. fout
is an ofstream object.
Seek call Action
fout.seekg(o,ios::beg) Go to start
fout.seekg(o,ios::cur) Stay at the current position
fout.seekg(o,ios::end) Go to the end of file
fout.seekg(m,ios::beg) Move to (m+1)th byte in the file
fout.seekg(m,ios::cur) Go forward by m byte from current position
fout.seekg(-m,ios::cur) Go backward by m bytes from current position.
fout.seekg(-m,ios::end) Go backward by m bytes from the end
Working with Files…Cont’d
An int takes two bytes to store its value in the binary form, irrespective of its size. But a 4 digit int will take
four bytes to store it in the character form.
The binary input and output functions takes the following form:
infile.read (( char * ) & V, sizeof (V));
outfile.write (( char *) & V, sizeof (V));
These functions take two arguments. The first is the address of the variable V, and the second is the length
of that variable in bytes. The address of the variable must be cast to type char*(i.e pointer to character
type).The following program illustrates how these two functions are used to save an array of floats numbers
and then recover them for display on the screen.
Working with Files…Cont’d
Updating a file:
Updating is a routine task in the maintenance of any data file. The updating would include one or more of the
following tasks.
• Displaying the contents of a file
• Modifying an existing item
• Adding a new item
• Deleting an existing item
These actions requires the file pointers to move to a particular location that corresponds to the item/object
under consideration. This can be easily implemented if the file contains a collection of items/objects of equal lengths.
In such cases the size of the object can be obtained using the statement
int object_length = sizeof (Object);
Then, the location of a desired object, say the m’th object, may be obtained as follows
int location = m * object_length;
The location gives the byte number of the first byte of the m’th object. Now, we can set the file pointer to reach
this byte with the help of seekg() or seekp()
We can also find out the total number of objects in a file using the object_length as follows
int n = file_size/object_length;
The file size can be obtained using the function tellg() or tellp() when the file pointer is located at the end of the
file.
Working with Files…Cont’d
These functions may be used in the appropriate places in a program to locate the status of a file
stream and thereby to take the necessary corrective measures.
Working with Files…Cont’d
Command Line Arguments
Like C,C++ also support a feature that facilitates the supply of arguments to the main() function. These
arguments are supplied at the time of invoking the program. They are typically used to pass the names of
data files. Example:
C > exam data results
Here, exam is the name of file containing the program to be executed, and data and results are the
filenames passed to program as command line arguments.
The command line arguments are typed by the user and are delimited by a space. The first argument is
always the filename(command name) and contains the program to be executed.
The main() functions which have been using up to now without any argument can take two arguments
as shown below:
main(int argc, char * argv[])
The first argument argc (known as argument counter) represents the number of arguments in
command line. The second argument argv (known as argument vector) is an array of char type pointers
that points to the command-line arguments. The size of this array will be equal to the value of argc. For
instance, for command line
C>exam data results
Command Line Arguments…Cont’d
The value of argc would be 3 and the argv would be an array of three pointers to string as shown
below:
argv[0] ---> exam
argv[1] ---> data
argv[2] ---> results
Note that argv[0] always represents the command name that invokes the program. The character
pointer argv[1] and argv[2] can be used as file names in the file opening statements as shown below:
…………
…………
inline.open(argv[1]); //open data file for reading
…………..
…………..
outfile.open(argv[2]); //open result file for writing
…………..
…………..
End of Unit III