Advanced Data Structure II
Advanced Data Structure II
I. FILES
So far, all the output (formatted or not) has been written out to what is called standard
output(which is usually the monitor). Similarly, all input has come from standard input (usually
associated with the keyboard). The C programmer can also read data directly from files and write
directly to files. To write into files the following steps must be taken.
- ‘r+’ the existing file is opened to the beginning for both reading and writing.
- ‘w+’ same as ‘w’ except for both reading and writing.
- ‘a+’ same as ‘a’ except both for reading and writing.
The following useful tables list the different actions and requirements of the different
modes for opening a file.
r w a r+ w+ a+
File must exist before open √ √
Old file contents discarded on open √ √
Stream can be read √ √ √ √
Stream can be written √ √ √ √ √
Stream can be written on at the end √
2.
2. CLOSING A FILE
The ‘fclose’ function in a sense does the opposite of ‘fopen’ it tells the system that we no
longer need access to the file. This allows the operating system to clean up any resources
or buffers associated with the file. The syntax for file closing is simply;
Fclose (in-file);
FUNCTION RESULTS
Fgets File string input
fputs File string output
Getc (fp) File character input
Putc (fp) File character output
The standard I/O library provides similar routines for file I/O to those used for standard
I/O. the routine ‘getc(fp)’ is similar to ‘getchar()’ and ‘putc(c, fp)’ is similar to ‘putchar(c)’
thus the statement ‘c= getc(fp);’ reads the next character from the file referenced by (fp) and the
statement ‘putc(c, fp);’ writes the character into the referenced by (fp). Another useful function
for file I/O is ‘feof()’ i.e. file end of file. Which tests for the end of file condition. ‘feof()’ takes
one argument that is; the file pointer and returns a non-zero interval value ( TRUE ) if an … it
returns zero (FALSE) otherwise. A sample use is shown below;
If (feof(fp))
Printf(‘ no data\n’);
…appending
I. STACKS
1. DEFINITION
A stack is a linear list in which items are added and removed from the same end
(head). Items being added and removed from the same end means that the last
item to be added will be the first to be removed as such stacks are also called
‘Last In First Out’ (LIFO) or ‘First In Last Out’ (FILO).
2. STACK OPERATIONS
3. APPLICATION OF STACKS
a) Converting a decimal no to binary
… after the entire digit has been converted into binary form, we pop one digit at a time
from the stack and print it. Therefore, we get the decimal number converted into its
proper binary form.
A * (b + c) ?
2 * 6 + 5= (2 * 6) + 5 = 17?
2 * (6 + 5)= 22?
Infix notation requires the use of order of precedence of operators and parenthesis
making it complicated and difficult for computers to evaluate expressions in this form.
To ease evaluation of arithmetic expressions, other notations are used. That is the ‘prefix’
and ‘postfix’ notations.
In prefix notation also called ‘Polish notation’, operations are written before the
operand. For example; the expression (a * b * c)will be written as in prefix notation as
(a * b) + c = (* ab) + b
= +* abc
In postfix notation also known as ‘reversed polish notation’, operands come before
operators.
Prefix and Postfix do not require parenthesis rule