arrays
arrays
DATA STRUCTURES
Arrays in Data Structures
An array is a linear data structure that collects elements of the same data type and stores them
in contiguous and adjacent memory locations. Arrays work on an index system starting from 0 to
(n-1), where n is the size of the array.
You can imagine a 1d array as a row, where elements are stored one after another.
Two-Dimensional Arrays:
You can imagine it like a table where each cell contains elements.
Arrays are typically defined with square brackets with the size of the arrays as its argument.
Here is the syntax for arrays:
1D Arrays: int arr[n];
2D Arrays: int arr[m][n];
How Do You Initialize an Array?
You can initialize an array in four different ways:
• Method 1:
int a[6] = {2, 3, 5, 7, 11, 13};
• Method 2:
int arr[]= {2, 3, 5, 7, 11};
• Method 3:
int n;
scanf(“%d”,&n);
int arr[n];
for(int i=0;i<5;i++)
{
scanf(“%d”,&arr[i]);
}
• Method 4:
int arr[5];
arr[0]=1;
arr[1]=2;
arr[2]=3;
arr[3]=4;
arr[4]=5;
You can access elements with the help of the index at which you stored them. Let's discuss it with
a code:
#include<stdio.h>
int main()
{
int a[5] = {2, 3, 5, 7, 11};
printf(“%d\n”,a[0]); // we are accessing
printf(“%d\n”,a[1]);
printf(“%d\n”,a[2]);
printf(“%d\n”,a[3]);
printf(“%d”,a[4]);
return 0;
}
Code:
#include<stdio.h>
int main() for (int i=0;i<n-1;i++)
{
int a[5] = {2, 3, 5, 7, 11};
for(int i=0;i<5;i++)
{
//traversing ith element in the array
printf(“%d\n”,a[i]);
}
return 0;
}
Insertion:
Insertion in an array is the process of including one or more elements in an array.
Insertion of an element can be done:
• At the beginning
• At the end and
• At any given index of an array.
At the Beginning:
Code:
#include<stdio.h>
int main()
{
int array[10], n,i, item;
printf("Enter the size of array: ");
scanf("%d", &n);
printf("\nEnter Elements in array: ");
for(i=0;i<n;i++)
{
scanf("%d", &array[i]);
}
printf("\n enter the element at the beginning");
scanf("%d", &item);
n++;
for(i=n; i>1; i--)
{
array[i-1]=array[i-2];
}
array[0]=item;
printf("resultant array element");
for(i=0;i<n;i++)
{
printf("\n%d", array[i]);
}
getch();
return 0;
}
At the End:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int array[10], i, values;
printf("Enter 5 Array Elements: ");
for(i=0; i<5; i++)
scanf("%d", &array[i]);
printf("\nEnter Element to Insert: ");
scanf("%d", &values);
array[i] = values;
printf("\nThe New Array is:\n");
for(i=0; i<6; i++)
printf("%d ", array[i]);
getch();
return 0;
}
Lists
The arrangement of elements in some sequence is called list. There are two types of list.
i) Ordered List
ii) Unordered List
Example
i) Pile of Trays in Cafeteria
ii) Stack of iron shorts (Box)
iii) Pile of bricks
iv) Pile of books in Library
1. Push: Push operation is used to add new elements into the stack. At the time of addition first
check the stack is full or not. If the stack is full it generates an error message "stack overflow".
2. Pop: Pop operation is used to delete elements from the stack. At the time of deletion first
check the stack is empty or not. If the stack is empty it generates an error message "stack
underflow".
All insertions and deletions take place at the same end, so the last element added to the stack
will be the first element removed from the stack. When a stack is created, the stack base remains
fixed while the stack top changes as elements are added and removed. The most accessible
element is the top and the least accessible element is the bottom of the stack.
Let us consider a stack with 5 elements capacity. This is called as the size of the stack. The number
of elements to be added should not exceed the maximum size of the stack. If we attempt to add
new element beyond the maximum size, we will encounter a stack overflow condition. Similarly,
you cannot remove elements beyond the base of the stack. If such is the case, we will reach a
stack underflow condition.
1.push(): When an element is added to a stack, the operation is performed by push(). Below
Figure shows the creation of a stack and addition of elements using push().
Initially top=-1, increment the top value i.e top=top+1, and insert element into the stack. We can
insert an element into the stack first check the condition is stack isfull or not. i.e top>=size-1.
Otherwise add the element in to the stack.
Step 1: START
Step 2: if top>=size-1 then
Write “ Stack is Overflow”
Goto Step-4
Step 3: Otherwise
3.1: read data value ‘x’
3.2: top=top+1;
3.3: stack[top]=x;
3.4: Goto Step-2
Step 4: END
CODE:
void push()
{
int x;
if(top >= n-1)
{
printf("\n\nStack Overflow..");
return;
}
else
{
printf("\n\nEnter data: ");
scanf("%d", &x);
top = top + 1;
stack[top] = x;
printf("\n\nData Pushed into the stack");
}
}
2.Pop(): When an element is taken off from the stack, the operation is performed by pop(). Below
figure shows a stack initially with three elements and shows the deletion of elements using pop().
We can delete/remove an element from the stack, decrement the top value i.e top=top-1.
We can delete element from the stack first check the condition is stack isempty or not. i.e top= -
1. Otherwise remove the element from the stack.
Step 1: START
Step 2: if top==-1 then
Write “Stack is Underflow”
Goto Step-4
Step 3: otherwise
3.1: print “deleted element”
3.2: top=top-1;
3.3: Goto Step-2
Step 4: END
CODE:
Void pop()
{
If(top==-1)
{
Printf(“Stack is Underflow”);
}
else
{
printf(“Delete data %d”,stack[top]);
top=top-1;
}
}
3.display(): This operation performed display the elements in the stack. We display the element
in the stack check the condition is stack is empty or not i.e top==-1.Otherwise display the list of
elements in the stack.
Algorithms Display:
Step 1: START
Step 2: if top==-1 then
Write “Stack is Underflow”
Goto Step-4
Step 3: otherwise
3.1: print “Display elements are”
3.2: for top to 0
Print ‘stack[i]’
Step 4: END
CODE:
void display()
{
If(top==-1)
{
Printf(“Stack is Underflow”);
}
else
{
printf(“Display elements are:);
for(i=top;i>=0;i--)
printf(“%d”,stack[i]);
}
}
Applications of stack:
1. Stack is used by compilers to check for balancing of parentheses, brackets and braces.
2. Stack is used to evaluate a postfix expression.
3. Stack is used to convert an infix expression into postfix/prefix form.
4. In recursion, all intermediate arguments and return values are stored on the processor’s stack.
5. During a function call the return address and arguments are pushed onto a stack and on return
they are popped off.
An algebraic expression can be represented using three different notations. They are infix, postfix
and prefix notations:
Infix: It is the form of an arithmetic expression in which we fix (place) the arithmetic operator in
between the two operands.
Example: A + B
Prefix: It is the form of an arithmetic notation in which we fix (place) the arithmetic operator
before (pre) its two operands. The prefix notation is called as polish notation.
Example: + A B
Postfix: It is the form of an arithmetic expression in which we fix (place) the arithmetic operator
after (post) its two operands. The postfix notation is called as suffix notation and is also referred
to reverse polish notation.
Example: A B +
Conversion from infix to postfix:
Procedure to convert from infix expression to postfix expression is as follows:
1. Scan the infix expression from left to right.
2. a) If the scanned symbol is left parenthesis, push it onto the stack.
b) If the scanned symbol is an operand, then place directly in the postfix expression
(output).
c) If the symbol scanned is a right parenthesis, then go on popping all the items from the
stack and place them in the postfix expression till we get the matching left parenthesis.
d) If the scanned symbol is an operator, then go on removing all the operators from the
stack and place them in the postfix expression, if and only if the precedence of the
operator which is on the top of the stack is greater than (or greater than or equal) to the
precedence of the scanned operator and push the scanned operator onto the stack
otherwise, push the scanned operator onto the stack.
The three important features of postfix expression are:
1. The operands maintain the same order as in the equivalent infix expression.
2. The parentheses are not needed to designate the expression unambiguously.
3. While evaluating the postfix expression the priority of the operators is no longer relevant.
We consider five binary operations: +, -, *, / and $ or ↑ (exponentiation). For these binary operations,
the following in the order of precedence (highest to lowest):
Evaluation of postfix expression:
The postfix expression is evaluated easily by the use of a stack.
1. When a number is seen, it is pushed onto the stack;
2. When an operator is seen, the operator is applied to the two numbers that are popped from the
stack and the result is pushed onto the stack.
3. When an expression is given in postfix notation, there is no need to know any precedence rules;
this is our obvious advantage.