# Data Structure and File Processing

Document Sample

```					Data Structure and File
Processing
Introduction and Overview
Introduction
Basic Terminology: Elimination data organization
Data Structure Approach
Brief History of File Processing
Introduction
Data structure cover with different operation
such as:
• Notion of an algorithm and its complexity
• Time space trade off that may occur in
choosing a particular algorithm.
• Data structure for given problems
Basic Terminology: Elementary Data
Organization
• Data are set of values.
• A data item refers to a single unit of values
• Data item that divided into sub items are called
group items.

• Example: employee name divide into three sub
items:
First name, middle initial and last name.
Continue…Basic Terminology
• Entity has certain attribute and values may be numeric or
non numeric. Example:

Attribute: Name        Age   Sex   Social Security Numbers
Values Rohland, Gail    34    F          134-24-5533

• A field is a single elementary unit of information
representing an attribute of an entity.
• Also known as a key or a keys.
• A records is a collection of fields values of a given entity
• A File if the collection of records of an entity in a given
entity set.
Data Structure Approach

What is Data Structure?
• Data model that is reach enough in structure to mirror
the actual relationship.
• Simple enough to process effectively when necessary.
• Example: Arrays
One dimensional array – STUDENT consist of six names
STUDENT [1] denotes John Brown
STUDENT [2] denotes Sandra Gold
STUDENT [3] denotes Tom Jones
STUDENT [4] denotes June Kelly
STUDENT [5] denotes Mary Reed
STUDENT [6] denotes Alan Smith

One dimensional array also known as linear array.
Continue.. Data Structure Approach
• Two dimensional array is a collection of similar data
elements .
• Example:
SALES[1,1] = 2872          SALES[1,2] = 805

Stor\Dep              1    2        3         4
1                   2872   805      542       356
2                   1234   587      244       687
3                   1478   256      741       547
4                   1652   566      587       445
.                   .      .        .         .
.                   .      .        .         .
.                   .      .        .         .
28                  1254   256      458       789
• It consists of a sequence of nodes, each containing
arbitrary data fields and one or two references ("links")
pointing to the next and/or previous nodes.
• benefit of a linked list over a conventional array is that
the order of the linked items may be different from the
order that the data items are stored in memory or on
disk, allowing the list of items to be traversed in a
different order.
• A linked list is a self-referential data type because it
contains a pointer or link to another datum of the same
type.
• Linked lists permit insertion and removal of nodes at
any point in the list in constant time, but do not allow
random access.

 Suppose a brokerage firm              Customer   Salesperson
maintains a file where each
record contains a customer’s        1 Adams      Smith
name & salesperson.                 2 Brown      Ray
 The file contains the data.         3 Clark      Jones
 Another way of storing data is to
4 Drew       Ray
have to separate array for
salesperson & an entry (called a    5 Evans      Smith
pointer).                           6 Farmer     Jones
 An integer used as a pointer
requires less space than a name;    7 Geller     Ray
hence this representation saves     8 Hill       Smith
space.                              9 Infeld     Ray
Pointer   Customer   Salesperson
2         Brown      Ray              Jones      1

3         Clark      Jones            Ray        2

4         Drew       Ray             Smith       3

5         Evans      Smith
6         Farmer     Jones
7         Geller     Ray
8         Hill       Smith
9         Infeld     Ray

Salesperson   Pointer
1 Jones         3,6
2 Ray           2,4,7,9
3 Smith         1,5,8

2 Brown       4             Ray          2
3 Clark       6            Smith          1
4 Drew        7
5 Evans       8
6 Farmer      0
7 Geller      9
8 Hill        0
9 Infeld      0
TREES

 Data frequently contain a hierarchical relationship between
various elements.
 The data structure which reflects this r’ship is called a rooted
tree graph @ tree.
 Kind of trees - record structure & algebraic expression
Record Structure
• Consider the Employee records that contain the
following items:

IC no., Name, Address, Age, Salary, Dependents
Record Structure

Employee

IC           Name       Address            Age        Salary   Dependent

First    Last   Street     Area

City    State     Postcode
Record Structure

 Another way of picturing such a tree structure is in terms of levels

01   Employee
02   IC Number
02   Name
03   First
03   Last
03   Street
03   Area
04   City
04   State
04   Postcode
02   Age
02   Salary
02   Dependents
Algebraic Expression
• Consider the following algebraic expression:
– (2x + y)(a – 7b)
• Using vertical arrow ( ) for exponentiation and an asterisk
(*) for multiplication.
Algebraic Expression

*

+

*             y           -           3

2       x
a       *

7       b
STACK
• Also called a last-in-first-out (LIFO) system, is a linear list
in which insertions & deletions can take place only at
one end, called the top.
• Stacks are used extensively at every level of a modern
computer system. For example, a modern PC uses stacks
at the architecture level, which are used in the basic
design of an operating system for interrupt handling
and operating system function calls.
• Among other uses, stacks are used to run a Java Virtual
Machine, and the Java language itself has a class called
"Stack", which can be used by the programmer.
QUEUE
• Also called a first-in-first-out (FIFO) system, is a
linear list in which deletions can take place only at
one end of the list, the front of the list and insertions
can take place only at the other end of the list, the
rear of the list.
GRAPH
• Data sometimes contain a relationship between
pairs of elements which is not necessarily hierarchical
in nature. For example, suppose an airline flies only
between the cities connected by lines.
• The data structure which reflects this type of r’ship is
called a graph.
DATA STRUCTURE OPERATIONS
• Traversing: accessing each record exactly once so
that certain items in the record may be processed.
• Searching: finding the location of the record with a
given key value @ finding the locations of all records
which satisfy one or more condition.
• Inserting: adding a new record to the structure.
• Deleting: removing a record from the structure.
• Sorting: arranging the records in some logical order.
• Merging: combining the records in 2 different sorted
files into a single sorted file.
ALGORITHMS
• An algorithm is a well-defined list of steps for solving
a particular problem.
Brief History of File Processing
• File processing consists of creating, storing, and/or
retrieving, the contents of a file to or from a
recognizable medium. For example, it is used to save
word processed files to a hard drive, to store a
presentation on floppy disk, or to open a file from a
CD-ROM.
• File processing is traditionally performed using the
FILE class. In the strict C sense, FILE is a structure and
it is defined in the stdio.h header file. This object is
equipped with variables used to indicate what
operation would be performed. To use this structure,
first declare an instance of the FILE structure.
Example: FILE *Starter;
Continue.. File processing
• In C++ file processing is performed using the fstream
class. Unlike the structure, fstream is a complete C++
class with constructors, a destructor and overloaded
operators.
• To perform file processing, declare an instance of an
fstream object. If the name of the file is not known
during the process, use the default constructor.
• Unlike the FILE structure, the fstream class provides
two distinct classes for file processing. One is used to
write to a file and the other is used to read from a file.
Continue.. File processing
•   When processing a file, you will typically specify the type of operation you want to perform.
The operation is specified using what is referred to as a file mode. It can be one of the
following:
Mode       Description
ios::app    If FileName is a new file, data is written to it.
If FileName already exists and contains data, then it is opened, the
compiler goes to the end of the file and adds the new data to it.
ios::ate    If FileName is a new file, data is written to it and subsequently added to
the end of the file.
If FileName already exists and contains data, then it is opened and data
is written in the current position.
ios::in    If FileName is a new file, then it gets created fine as an empty file.
If FileName already exists, then it is opened and its content is made
available for processing
ios::out    If FileName is a new file, then it gets created fine as an empty file.
Once/Since it gets created empty, you can write data to it.
If FileName already exists, then it is opened, its content is destroyed, and
the file becomes as new. Therefore you can create new data to write to it.
Then, if you save the file, which is the main purpose of this mode, the
new content is saved it.*This operation is typically used when you want
to save a file
Continue.. File processing

ios::trunc   If FileName already exists, its content is destroyed and the file
becomes as new
ios::nocreate If FileName is a new file, the operation fails because it cannot create
a new file.
If FileName already exists, then it is opened and its content is made
available for processing
ios::noreplace If FileName is a new file, then it gets created fine.
If FileName already exists and you try to open it, this operation would
fail because it cannot create a file of the same name in the same
location.
Continue.. File processing
• One of the operations can be perform on a file is saving it, which is
equivalent to storing its value(s) to a medium. To save a file, first declare
an instance of the ofstream class using one of its constructors from the
following syntaxes: ofstream();
• ofstream(const char* FileName, int FileMode);
• The default constructor allows to initiate file processing without giving
details. If using the default constructor, one of the methods can be call
and will perform the necessary operation.
• The ofstream(const char* FileName, int FileMode) constructor
provides a complete mechanism for creating a file. It does this with the
help of its two arguments.
• The first argument, FileName, is a string that specifies the name of the file
that needs to be saved.
• The second argument, FileMode, specifies the kind of operation in order
to perform on the file. It can be one of the modes that listed above.
Continue.. File processing
Example:

#include <fstream>
#include <iostream>
using namespace std;

int main()
{
char FirstName[30], LastName[30];
int Age;
char FileName[20];

cout << "Enter First Name: ";
cin >> FirstName;
cout << "Enter Last Name: ";
cin >> LastName;
cout << "Enter Age:     ";
cin >> Age;

cout << "\nEnter the name of the file you want to create: ";
cin >> FileName;
ofstream Students(FileName, ios::out);
Students << FirstName << "\n" << LastName << "\n" << Age;

cout << "\n\n";
return 0;
}
Continue.. File processing
•   If using default constructor to declare an ofstream variable, call the open()
method to actually process the file. The syntax of the open method is:

void open(const char* FileName, int Mode, int nProt = filebuf::openprot );

•   This method behaves exactly like, and uses the same arguments as, the
constructor described above. The first argument represents the name of the file
you are dealing with and the FileMode argument follows the modes of the above
table.
•   Because the fstream class in this case is declared as ofstream, the compiler is
aware that to save a file (in reality, the use of ofstream means to write to a file,
in other words the FileMode same with a value of ios::out), second constructor
can be used with just the FileName as argument or can call the open() method
with only the name of the file.
•   After using a file, close the file. This is taken care by using the ofstream::close()
method whose syntax is: void close();
Continue.. File processing
•   Another operation that can perform consists of opening existing file to have access to its contents. To do
this, C++ provides the ifstream class. Like ofstream, the ifstream class provides various constructors can
be use, two of which are particularly important. If you have enough information about the file that want
to be open, use the following constructor:
ifstream(const char* FileName, int FileMode);

•   The first argument of the constructor, FileName, is a constant string that represents the file that will be
open. The FileMode argument is a natural number that follows the table of modes as described above.
•   If necessary, declare an empty instance of the ifstream class using the default constructor: ifstream();
•   After declaring this constructor, use the ifstream::open() method to formally open the intended file. The
syntax of the open() method is:
open(const char* FileName, int FileMode);

•   This method uses the same arguments as the above constructor. By default, when declaring an instance of
the ifstream class, it is assumed that the file will be open; use the FileMode attribute with a value of
ios::in. Therefore, the second argument is already set to ios::in value. This allows to call the open()
method with just the FileName value.

After using the ifstream class, you can close it using the ifstream::close() method. Here is an example:
Continue.. File processing
After using the ifstream class, you can close it using the ifstream::close() method. Here is an example:

#include <fstream>
#include <iostream>
using namespace std;
int main()
{
char FirstName[30], LastName[30];
int Age;
char FileName[20];
/*
cout << "Enter First Name: ";
cin >> FirstName;
cout << "Enter Last Name: ";
cin >> LastName;
cout << "Enter Age:        ";
cin >> Age;
cout << "\nEnter the name of the file you want to create: ";
cin >> FileName;
ofstream Students(FileName, ios::out);
Students << FirstName << "\n" << LastName << "\n" << Age;
*/
cout << "Enter the name of the file you want to open: ";
cin >> FileName;
ifstream Students(FileName);
Students >> FirstName >> LastName >> Age;
cout << "\nFirst Name: " << FirstName;
cout << "\nLast Name: " << LastName;
cout << "\nEnter Age: " << Age;
cout << "\n\n";
return 0;
}

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 10 posted: 4/26/2012 language: English pages: 31
How are you planning on using Docstoc?