Wednesday, December 5, 2007

Applications of Stack Data Structure

Application of stacks :-

The linear data structure stack can be used in the following situations.

1. It can be used to process function calls.

2. Implementing recursive functions in high level languages

3. Converting and evaluating expressions.

Function calls:

A stack is useful for the compiler/operating system to store local variables used inside a function block, so that they can be discarded once the control comes out of the function block.

Recursive functions:

The stack is very much useful while implementing recursive functions. The return values and addresses of the function will be pushed into the stack and the lastly invoked function will first return the value by popping the stack.

Representation of expressions :-

In general there are 3 kinds of expressions available depending on the placement of the operators & operands.

1) Infix expression :- It is the general notation used for representing expressions.

“In this expression the operator is fixed in between the operands”


Ex: a + bc

2) Post fix expression :- (Reverse polish notation)

“In this expression the operator is placed after the operands”.

Ex : abc+

3) Prefix expression :- (Polish notation)

“In this expression the operators are followed by operands i.e the operators are fixed before the operands”

Ex : +abc

All the infix expression will be converted into post fix notation with the help of stack in any program

The stack will be useful in evaluating the postfix expressions also.


Algorithm for evaluating post fix expression using stacks :-

step :- 1 start

step :- 2 for(each character ch in the postfix expression)

step :- 3 If operand is found push it into the stack

step :- 4 else

step :- 5 If operator is found then pop the stack 2 times

OP2 = pop ( ) OP1 = pop ( )

step :- 6 Perform the specified operation as result = OP1 operator OP2

step :- 7 Push the intermediate result back into the stack

step :- 8 Repeat the above steps until the end of the expression

step :- 9 pop the stack to obtain the final result

step :- 10 stop

Algorithm to convert infix to post fix expression: -

assumptions

§ Operands are single lowercase letters that represent integer values

§ Stack is a stack data structure

§ Precedence is the function to get the precedence of an operator

Steps

  1. for (each character ch in the infix expression) {
  2. switch (ch) {

i. case operand: // append operand to end of PE

postfixExp = postfixExp + ch; break

ii. case '(':

a Stack.push(ch) break

iii. case ')':

1. while (top of stack is not '(')

a. postfixExp = postfixExp + (Stack.pop())

2. Stack.pop() // remove the open parenthesis

3. break

iv. case operator:

1. while (!Stack.isEmpty and top of stack is not '(' and precendence(ch) <= precendence(top of aStack))

a. postfixExp = postfixExp + (Stack.pop())

2. Stack.push(ch) // save new operator

3. break

  1. }
  2. }
  3. // append to postfixExp the operators remaining in the stack
  4. while (!Stack.isEmpty())
  5. postfixExp = postfixExp + (Stack.pop())

Fox example the following figure shows how to convert the infix expression a - (b + c * d)/e to postfix form


Classification of Data structures


Data structures can be classified as

· Simple data structure

· Compound data structure

· Linear data structure

· Non linear data structure

Simple Data Structure:

Simple data structure can be constructed with the help of primitive data structure. A primitive data structure used to represent the standard data types of any one of the computer languages. Variables, arrays, pointers, structures, unions, etc. are examples of primitive data structures.

Compound Data structure:

Compound data structure can be constructed with the help of any one of the primitive data structure and it is having a specific functionality. It can be designed by user. It can be classified as

1) Linear data structure

2) Non-linear data structure

Linear data structure :

Collection of nodes which are logically adjacent in which logical adjacency is maintained by pointers

(or)

Linear data structures can be constructed as a continuous arrangement of data elements in the memory. It can be constructed by using array data type. In the linear Data Structures the relation ship of adjacency is maintained between the Data elements.

Operations applied on linear data structure :

The following list of operations applied on linear data structures

1. Add an element

2. Delete an element

3. Traverse

4. Sort the list of elements

5. Search for a data element

By applying one or more functionalities to create different types of data structures

For example Stack, Queue, Tables, List, and Linked Lists.

Non-linear data structure:

Non-linear data structure can be constructed as a collection of randomly distributed set of data item joined together by using a special pointer (tag). In non-linear Data structure the relationship of adjacency is not maintained between the Data items.

Operations applied on non-linear data structures :

The following list of operations applied on non-linear data structures.

1. Add elements

2. Delete elements

3. Display the elements

4. Sort the list of elements

5. Search for a data element

By applying one or more functionalities and different ways of joining randomly distributed data items to create different types of data structures. For example Tree, Decision tree, Graph and Forest

Data Structure Definitions

Data structure Definitions : -

In computer science, a data structure is a way of storing data in a computer so that it can be used efficiently. Often a carefully chosen data structure will allow the most efficient algorithm to be used. The choice of the data structure often begins from the choice of an abstract data type. A well-designed data structure allows a variety of critical operations to be performed, using as few resources, both execution time and memory space, as possible. Data structures are implemented by a programming language as data types and the references and operations they provide.


Definition:

“Collection of data elements organized in a specified manner and a set of functions to store, retrieve and manipulate the individual data elements.”
Or
“The way of representing data internally in the memory is called data structure” Or “A data structure is a way of store data in a computer so that it can be used efficiently”
Or
A data structure is a specialized format for organizing and storing data. General data structure types include the array, the file, the record, the table, the tree, and so on. Any data structure is designed to organize data to suit a specific purpose so that it can be accessed and worked with in appropriate ways. In computer programming, a data structure may be selected or designed to store data for the purpose of working on it with various algorithms.