Unit-2_Array+P1
Unit-2_Array+P1
Unit-II
[Linear Data Structures and their Sequential Representation: Array, stack, queue, circular queue and
their operations and applications.]
Array
Students have already studied this concept in earlier semesters. So, They are expected to revise
following questions and answers.
Types of array:
➢ Single Dimensional Array: Single or One Dimensional array is used to represent
and store data in a linear form. It has only one subscript variable and it is also called
Linear Array.
➢ Multi Dimensional Array: Array having more than one subscript variable is called
Multi-Dimensional array. It is also called as Matrix.
1. Arrays are the most convenient way of storing the fixed amount of data that can
be accessed in an unpredictable fashion
2. Another advantage of array is that iterating through an array is more faster than
compared to other data structure types like linked list because of its good
locality of reference.
Consider an array:
int arr[4];
In C programming, name of the array always points to address of the first element of an
array. In the above example, arr and &arr[0] points to the address of the first element.
➢ &arr[0] is equivalent to arr and arr[0] is equivalent to *arr (value of an address of
the pointer)
Similarly,
➢ &arr[1] is equivalent to (arr + 1) AND, arr[1] is equivalent to *(arr + 1).
➢ &arr[2] is equivalent to (arr + 2) AND, arr[2] is equivalent to *(arr + 2).
➢ &arr[3] is equivalent to (arr + 3) AND, arr[3] is equivalent to *(arr + 3)
..............
..............
➢ &arr[i] is equivalent to (arr + i) AND, arr[i] is equivalent to *(arr + i).
Using array:
/* Program to find average of the numbers entered in an array */
#include <stdio.h>
/* function declaration */
float getAverage(int *arr, int size);
void main () {
int arr[50], size, i;
float avg;
/* ask user for size of array */
printf("\nEnter the size of array.. \t");
scanf("%d", &size);
/* ask user for elements of array */
printf("Enter the elements of array:\t");
for(i = 0; i < size; i++){
scanf("%d", &arr[i]);
}
/* display the array elements */
printf("Array elements are:\t\t[ ");
for(i = 0; i < size; i++){
printf("%d ", arr[i]);
}
printf("]\n");
/* pass pointer to the array as an argument */
avg = getAverage( arr, size ) ;
/* output the returned value */
printf( "\nAverage value is: %f \n", avg );
}
/* function definition */
float getAverage(int *arr, int size) {
int i;
float avg;
float sum = 0;
for (i = 0; i < size; i++) {
sum = sum + *(arr + i);
//sum = sum + arr[i];
}
avg = sum / size;
return avg;
}
OUTPUT
5. Write a C program to search an element in the 2-D array using linear search.
OUTPUT
Assignments on Array
STACK
Stack is a special type of data structure where elements are inserted from one end and elements are
deleted from the same end. The position from where elements a are inserted and from where
elements are deleted is termed as top of the stack.
Thus stack is a homogeneous collection of elements of any one type, arranged linearly with access
at one end only. Stack is also called Last In First Out (LIFO) data structure.
OPERATION ON STACK:
• Push
• Pop
1. Push Operation: The procedure of inserting a new element to the top of the stack is known
as push operation. A pointer TOP is used to point the top element in the stack, which gets
incremented by 1 after every insertion.
If the stack is full and does not contain enough space to accept the given element or data, the
stack is then considered to be in an overflow state.
2. Pop Operation: The procedure of removing element from the top of the stack is called pop
operation. Only one element can be deleted at a time and element has to be deleted only
from the top of the stack. After every deletion, TOP gets decremented by 1.
When elements are being deleted, there is a possibility of stack being empty. When stack is
empty, it is not possible to delete any element. Trying to delete an element from an empty
stack results in stack underflow.
Implementation of stack:
Push Operation:
Description: Here STACK is an array with MAX locations. TOP points to the top most
element and ITEM is the value to be inserted.
Pop Operation:
Description: Here STACK is an array with MAX locations. TOP points to the top most
element.
To implement stack using linked list, we need to set the following things before implementing
actual operations.
• Define a 'Node' structure with two members data and next.
• Define a Node pointer 'top' and set it to NULL.
Push Operation:
Pop Operation:
Display Operation:
// Node Structure
struct Node
{
int data;
struct Node *next;
}*top = NULL; // Initially top is NULL
//Function declaration
void push(int);
void pop();
void display();
void main()
{
int choice, value;
/* Push operation */
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode>data = value;
if(top == NULL)
newNode>next = NULL;
else
newNode>next = top;
top = newNode;
printf("\nInsertion is Success!!!\n");
display();
} // End of Push
/* Pop operation */
void pop()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
printf("\nDeleted element: %d\n", temp>data);
top = temp>next;
free(temp);
}
display();
} // End of Pop
/* Display */
void display()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
while(temp>next != NULL){
printf("%d>",temp>data);
temp = temp > next;
}
printf("%d>NULL \n",temp>data);
}
} // End of display
Output
Insertion is Success!!!
10>NULL
Insertion is Success!!!
30>10>NULL
Insertion is Success!!!
60>30>10>NULL
Insertion is Success!!!
20>60>30>10>NULL
Insertion is Success!!!
50>20>60>30>10>NULL
Deleted element: 50
20>60>30>10>NULL
3. Display
4. Exit
Enter your choice: 2
Deleted element: 20
60>30>10>NULL
Deleted element: 60
30>10>NULL
Deleted element: 30
10>NULL
Deleted element: 10
Stack is Empty!!!
Stack is Empty!!!
Stack is Empty!!!
APPLICATION 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.
1. Infix to postfix:
5. If the incoming operator has higher precedence than the top of the stack, push it on
the stack.
6. If the incoming operator has lower precedence than the operator on the top of the
stack, pop the stack and print the top operator. Then test the incoming operator
against the new top of stack.
7. At the end of the expression, pop and print all operators on the stack. (No
parentheses should remain.)
Let us see how the above algorithm will be imlemented using an example.
Infix String : a+b*c-d
Initially the Stack is empty and our Postfix string has no characters.
• Now, the first character scanned is 'a'. 'a' is added to the Postfix string.
• The next character scanned is '+'. It being an operator, it is pushed to the stack.
End result :
• Infix String : a+b*c-d
• Postfix String : abc*+d-
2. Postfix to Infix: