0% found this document useful (0 votes)
2 views13 pages

Unit 02

The document covers operations on linear arrays, including traversing, inserting, and deleting elements, as well as sorting and searching techniques. It also discusses sparse matrices, their benefits, and representation, along with an overview of stack data structures and their basic operations. Example code snippets illustrate the insertion and deletion processes in arrays and stack operations.

Uploaded by

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

Unit 02

The document covers operations on linear arrays, including traversing, inserting, and deleting elements, as well as sorting and searching techniques. It also discusses sparse matrices, their benefits, and representation, along with an overview of stack data structures and their basic operations. Example code snippets illustrate the insertion and deletion processes in arrays and stack operations.

Uploaded by

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

Unit-02

Traversing Linear arrays:


Traversing is an operation of array, which is a process of accessing each
and every element of the array exactly once.
Example:
Printing the elements of the array
for (i=0;i<n;i++)
{
printf(“%d”,a[i]);
}
Here at each iteration the elements of the index 0 to n are printed in the
output screen exactly once.

Inserting and deleting elements:


Inserting an element in an array:
Insertion is an operation of array, which is inserting or adding the elements
to the given array.

for (i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}

#include <stdio.h>
int main()
{
int arr[100];
int i, item, pos, n;
printf("Enter the number of elements: ");
scanf(“%d”,&n);
printf (“Enter the elements”);
for (i = 0; i <n; i++)
scanf("%d",&arr[i]);
printf ("Array before insertion: ");
for (i = 0; i < n; i++)
printf ("%d ", arr[i]);
printf("\n");
printf ("Enter the element to be inserted: ");
scanf("%d",&item);
printf ("Enter the position at which the element is to be inserted: ");
scanf("%d",&pos);
size++;

// shift elements forward


for (i = n-1; i >= pos; i--)
arr[i] = arr[i - 1];

// insert item at position


arr[pos - 1] = item;

printf("Array after insertion: ");

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


printf("%d ", arr[i]);
printf("\n");

return 0;
}

Deleting the element in an array:


Deleting is an operation on array, where it is a process of removing or
deleting the element in the array.

#include<stdio.h>

int main()
{
int key, i, pos = -1, size=5;
int arr[5] = {1, 20, 5, 78, 30};

printf("Array before deletion: ");

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


printf("%d ", arr[i]);
printf("\n");
printf("Enter element to delete: ");
scanf("%d",&key);
for(i = 0; i < size; i++)
{
if(arr[i] == key)
{
pos = i;
break;
}
}

if(pos != -1)
{
//shift elements backwards by one position
for(i = pos; i < size - 1; i++)
arr[i] = arr[i+1];

printf("Array after deletion: ");

for(i = 0; i < size - 1; i++)


printf("%d ",arr[i]);
}
else
printf("Element Not Found\n");

return 0;
}

Sorting:
Sorting is an operation of array, where it is a process of arranging the
elements in either ascending or descending order.
Write selection, bubble, insertion, quick, merge sort lab programs
in notes.
Searching:
Searching is an operation of array, where it a process of finding the
element in the array, while searching the element may or may not be
present in the array.
Write linear search and binary search lab programs in notes.
Sparse matrix:
Sparse matrices are those matrices that have the majority of their
elements equal to zero. In other words, the sparse matrix can be defined
as the matrix that has a greater number of zero elements than the non-
zero elements.

There are the following benefits of using the sparse matrix -

Storage - We know that a sparse matrix contains lesser non-zero


elements than zero, so less memory can be used to store elements. It
evaluates only the non-zero elements.

Computing time: In the case of searching in sparse matrix, we need to


traverse only the non-zero elements rather than traversing all the sparse
matrix elements. It saves computing time by logically designing a data
structure traversing non-zero elements.

Representation of sparse matrix:


Array representation of the sparse matrix

Representing a sparse matrix by a 2D array leads to the wastage of lots


of memory. This is because zeroes in the matrix are of no use, so storing
zeroes with non-zero elements is wastage of memory. To avoid such
wastage, we can store only non-zero elements. If we store only non-zero
elements, it reduces the traversal time and the storage space.

In 2D array representation of sparse matrix, there are three fields used


that are named as -

o Row - It is the index of a row where a non-zero element is located


in the matrix.
o Column - It is the index of the column where a non-zero element is
located in the matrix.
o Value - It is the value of the non-zero element that is located at the
index (row, column).
Example -

Let's understand the array representation of sparse matrix with the help
of the example given below -

Consider the sparse matrix -

In the above figure, we can observe a 5x4 sparse matrix containing 7 non-
zero elements and 13 zero elements. The above matrix occupies 5x4 =
20 memory space. Increasing the size of matrix will increase the wastage
space.

Stack:
Stack is a linear data structure based on LIFO(Last In First Out) or
(FILO)First in Last out principle in which the insertion of a new element
and removal of an existing element takes place at the same end
represented as the top of the stack.

Representation of Stack Data Structure:


Stack follows LIFO (Last in First Out) Principle so the element which is
pushed last is popped first.
Basic Operations on Stack:
In order to make manipulations in a stack, there are certain operations
provided to us.
• push() to insert an element into the stack
• pop() to remove an element from the stack
• peek() Returns the top element of the stack.
• isEmpty() returns true if stack is empty else false.
• isFull() returns true if the stack is full else false.

Push Operation in Stack:


Pop Operation in Stack:

Top or Peek Operation in Stack:


isEmpty Operation in Stack:

isFull Operation in Stack:

Program
#include <stdio.h>

#include <stdlib.h>
#define SIZE 4

int top = -1, arr[SIZE];

void push();

void pop();

void show();

int main()

int choice;

while (1)

printf("\nPerform operations on the stack:");

printf("\n1.Push the element\n2.Pop the element\n 3.Show\n


4.End");

printf("\n\nEnter the choice: ");

scanf("%d", &choice);

switch (choice)

case 1:

push();

break;

case 2:

pop();

break;

case 3:
show();

break;

case 4:

exit(0);

default:

printf("\nInvalid choice!!");

void push()

int x;

if (top == SIZE - 1)

printf("\nOverflow!!");

else

printf("\nEnter the element to be added onto the stack: ");

scanf("%d", &x);

top = top + 1;

arr[top] = x;

}
}

void pop()

if (top == -1)

printf("\nUnderflow!!");

else

printf("\nPopped element: %d", arr[top]);

top = top - 1;

void show()

if (top == -1)

printf("\nUnderflow!!");

else

printf("\nElements present in the stack: \n");

for (int i = top; i >= 0; --i)


printf("%d\n", arr[i]);

Assignment Application of stack

You might also like