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

Arrays + Stack Notes

Uploaded by

aghiadhamchado
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Arrays + Stack Notes

Uploaded by

aghiadhamchado
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

DATA STRUCUTRE – MODULE-1

Array Representation: -
 One-Dimensional Array:

 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.

 Two-Dimensional Array:

int arrayName [row][column]


 Three-Dimensional Array:

int arrayName[row][column][depth]

Basic Operations: -

Following are the basic operations supported by an array.


1. Traverse: -print all the array elements one by one.
2. Insertion: - Adds an element at the given index.
3. Deletion: - Deletes an element at the given index.
4. Search: - Searches an element using the given index or by the
value.
5. Update: -Updates an element at the given index.

STACK
 A stack is a data structure consisting of a set of homogeneous
elements and is based on the principle of last in first out
(LIFO). It is a commonly used abstract data type with two
major operations, namely push and pop. Push and pop are
carried out on the topmost element, which is the item most
recently added to the stack. The push operation adds an
element to the stack while the pop operation removes an
element from the top position. The stack concept is used in
programming and memory organization in computers.

A stack can be visualized as follows: -

Basic Operations: -

 Stack operations may involve initializing the stack, using it


and then de-initializing it.
 Apart from these basic operations, a stack is used for the
following two primary operations –
1. push(): - Pushing (storing) an element on the stack.
2. pop(): - Removing (accessing) an element from the stack.
Arithmetic Expressions: Polish Notation
For most common arithmetic operations, the operator symbol is
placed between its two operands.
For example,
A + B, C - D, E * F, G/H
With above notation, we can distinguish between (A + B) * C and
A + (B * C) by using either parentheses or some operator
precedence levels. Hence, in infix notations, the order of the
operators and operands in the arithmetic expression does not
uniquely determine the order in which the operations are to be
performed.
Polish Notation: Postfix and Prefix Expressions : -
Postfix: An expression is called the postfix expression if the operator
appears in the expression after the operands. Simply of the form
(operand1 operand2 operator).

Examples:
AB+ , CD- , EF* , GH/
We translate, step by step, the following infix expression into postfix
natation using brackets [ ] to indicate a partial translation:
(A + B) * C = [AB+]*C = AB+C*
A + (B * C) = A + [BC*] = ABC*+
(A + B) / (C – D) = [AB+] / [CD-] = AB+CD-/
(A+B) * (C-D) = [AB+]*[CD-] = AB + CD -*

Prefix : An expression is called the prefix expression if the operator


appears in the expression before the operands. Simply of the form
(operator operand1 operand2).
For example,
+AB , -CD , *EF , /GH

We translate, step by step, the following infix expression into prefix


natation using brackets [ ] to indicate a partial translation:

(A + B) * C = [+AB]*C = *+ABC
A + (B * C) = A + [*BC] = +A*BC
(A + B) / (C – D) = [+AB] / [-CD] = /+AB -CD
(A+B) * (C-D) = [+AB]*[-CD] = *+AB-CD

Given a Postfix expression, convert it into a Prefix expression.


Conversion of Postfix expression directly to Prefix without going through
the process of converting them first to Infix and then to Prefix is much
better in terms of computation and better understanding the expression
(Computers evaluate using Postfix expression).

Examples:
Expression : (A+B) * (C-D)
Postfix : AB+CD-*
Prefix : *+AB-CD

Expression : ((A-(B/C))*((A/K)-L))
Postfix : ABC/-AK/L-*
Prefix : *-A/BC-/AKL
 Example of expression evaluation using stack: -
Applications of array: -
 Used in mathematical problems like matrices etc.
 They are used in the implementation of other data
structures like linked lists etc.
 Database records are usually implemented as arrays.
 Arrays are also used to implement CPU Scheduling
Algorithms.

Applications of STACK: -

 Expression Handling −
 Infix to Postfix or Infix to Prefix Conversion: -

 The stack can be used to convert some infix expression into its
postfix equivalent, or prefix equivalent. These postfix or prefix
notations are used in computers to express some expressions.
These expressions are not so much familiar to the infix
expression, but they have some great advantages also. We do
not need to maintain operator ordering, and parenthesis.

 Postfix or Prefix Evaluation: -

 After converting into prefix or postfix notations, we have to


evaluate the expression to get the result. For that purpose,
also we need the help of stack data structure.

 Backtracking Procedure :-
 Backtracking is one of the algorithms designing technique. For
that purpose, we dive into some way, if that way is not
efficient, we come back to the previous state and go into some
other paths. To get back from current state, we need to store
the previous state. For that purpose, we need stack. Some
examples of backtracking are finding the solution for Knight
Tour problem or N-Queen Problem etc.

 Another great use of stack is during the function call and


return process. When we call a function from one other
function, that function call statement may not be the first
statement. After calling the function, we also have to come
back from the function area to the place, where we have left
our control. So, we want to resume our task, not restart. For
that reason, we store the address of the program counter into
the stack, then go to the function body to execute it. After
completion of the execution, it pops out the address from
stack and assign it into the program counter to resume the
task again.
#include <iostream>

using namespace std;

int choice, value, stack[10], top=-1 , size=10 ,i;

push(int value)

if(top>(size-1))

cout<<"STACK OVERFLOW"<<endl;

else

top=top+1;

stack[top]=value;

pop()

if(top<0)

cout<<"No element in the stack pop";


}

else

top=top-1;

display()

for(i=0;i<=top;i++)

cout<<stack[i]<<endl;

int main()

cout<<"Enter 1. For push in the stack"<<endl;

cout<<"Enter 2. For pop from the stack"<<endl;

cout<<"Enter 3. to display stack"<<endl;


cout<<"4. Exit"<<endl;

do

cout<<endl<<"Enter the choice"<<endl;

cin>>choice;

switch(choice)

case 1:

cout<<"Enter value you want to push in the


stack"<<endl;

cin>>value;

push(value);

break;

case 2:

pop();

break;

case 3:
{

display();

break;

case 4:

cout<<"Exit"<<endl;

break;

}//end of switch

} while(choice!=4);

return 0;

You might also like