0% found this document useful (0 votes)
27 views

Advanced Data Structure II

1. Stacks are linear data structures that follow a Last In First Out (LIFO) approach, where items are added and removed from the same end. 2. Common stack operations include push to add an item, pop to remove an item, checking if the stack is empty, and evaluating arithmetic expressions using stacks. 3. Stacks have various applications including converting decimal numbers to binary by pushing digits onto a stack and popping them off to construct the binary number.

Uploaded by

Ngunde Akisseh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Advanced Data Structure II

1. Stacks are linear data structures that follow a Last In First Out (LIFO) approach, where items are added and removed from the same end. 2. Common stack operations include push to add an item, pop to remove an item, checking if the stack is empty, and evaluating arithmetic expressions using stacks. 3. Stacks have various applications including converting decimal numbers to binary by pushing digits onto a stack and popping them off to construct the binary number.

Uploaded by

Ngunde Akisseh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

ADVANCED DATA STRUCTURE

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.

1. DEFINING AND OPENING THE FILE


The data structure of a file is defined as ‘FILE’. In the library of standard I/O
functions. Therefore all files should be declared as type ‘file’ before they are used.
‘FILE’ is a defined data type. Constants such as ‘FILE’, ‘EOF’ and ‘NULL’ are
defined in <stdio.h>. the following is the general format for declaring and opening a
file;
FILE *fp;
Fp = fopen (‘filename’, ‘mode’);

Mode in which we open a file can be one of the following;


- ‘r’ open the file for reading only.
- ‘w’ open the file for writing only.
- ‘a’ open the file for appending (or adding) data to it.

The additional modes of operations are;

- ‘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);

3. REAING AND WRITING ON A FILE


The functions ‘fprintf’ and ‘fscanf’ are provided by C to perform the analogous
operations for the ‘printf’ and ‘scanf’ functions but on a file.
Opening a file for reading implies that the file already exists. If it does not exist the file
pointer will be set to NULL and can be checked by the program;

Fscanf (fp, ‘%f%d’, &x,&m);


When a file is opened for writing, it will be created if it does not already exist and it will
be reset if it does, resulting in the deletion of any data already there. Using a ‘w’ indicates
that the file to be opened is assumed to be text.

Fprintf (fp, ‘%s’, ‘this is an example of file:’);

4. ADDITIONAL FILE I/O FUNCTIONS


Many of the specialised I/O functions for characters and strings that we have discussed in
this course have analogs which can be used for file I/O. here is a list of those functions.

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

a) Stack (S): it creates an empty stack named ‘S’.


b) Push(S,X): inserts the element ‘X’ at the top of the stack ‘S’ inserting an element into a
stack that is full leads to a situation known as OVERFLOW, that is (START==NULL).
c) Pop (S): removes an element from the top of the stack ‘S’. removing an element from an
empty stack leads to a situation known as UNDERFLOW, that is (START==HEAD).
d) IsEmpty (S): checks whether the stack ‘S’ is empty. Returning true if it is Empty and
false, otherwise.

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.

b) Evaluating arithmetic expressions


Arithmetic expressions are usually written in the form (a + b), where the operator is in
between the operands. This is known as the ‘infix notation’. This notation poses problems
for more complicated expressions. For example;
s
A*b+c= (a * b) + c ?

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

You might also like