Array in data structure

Advertisements

Data Structure in C Stack in C

Array in Data Structure

An Array is a collection of similar data type value in a single variable. An array is a derived data type in C, which is constructed from fundamental data type of C language.

array in data structure

Insert element in array at specific position

Insert element in Array

	
#include<stdio.h>
#include<conio.h>

void main()
  {
  int i,a[5],no,pos;
  clrscr();
  printf("Enter element in array: ");
  for(i=0;i<5;i++)
  {
  scanf("%d",&a[i]);
  }
  printf("\nStored element in array: ");
  for(i=0;i<5;i++)
  {
  printf("  %d",a[i]);
  }
  printf("\nEnter position for enter element: ");
  scanf("%d",&pos);
  if(pos>5)
  {
  printf("\nThis is out of range");
  }
  else
  {
  printf("\nEnter new element: ");
  scanf("%d",&no);
  --pos;
  for(i=5;i>=pos;i--)
  {
  a[i+1]=a[i];
  }
  a[pos]=no;
  printf("\nStored data in array: ");
  for(i=0;i<6;i++)
  {
  printf("  %d",a[i]);
  }
  }
  getch();
  }

Output

	
Enter elements in array: 4
4
2
5
7
Stored data in array: 4 2 5 7
Enter position for enter element: 2
Enter new element: 8
Stored data in array: 4 2 5 7 8

Sort array elements

Sort array element

	
#include<stdio.h>
#include<conio.h>

void main()
{
int i,a[10],temp,j;
clrscr();
printf("Enter any 10 num in array: \n");
for(i=0;i<=10;i++)
{
scanf("%d",&a[i]);
}
printf("\nData before sorting: ");
for(j=0;j<10;j++)
{
printf("  %d",a[j]);
}
for(i=0;i<=10;i++)
{
for(j=0;j<=10-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nData after sorting: ");
for(j=0;j<10;j++)
{
printf("  %d", a[j]);
}
getch();
}

Output

	
Enter any 10 num in array:
10
50
30
20
60
80
70
90
100
Data before sorting: 10 50 30 20 60 80 70 90 100
Data after sorting: 10 20 30 40 50 60 70 80 90 100

Delete array element at specific position

Delete element from Array

	
#include<stdio.h>
#include<conio.h>

void main()
 {
 int i,a[5],no,pos;
 clrscr();
 printf("Enter element in array: ");
 for(i=0;i<5;i++)
 {
 scanf("%d",&a[i]);
 }
 printf("\nStored element in array: ");
 for(i=0;i<5;i++)
 {
 printf("  %d",a[i]);
 }
 printf("\nEnter poss. of element to delete: ");
 scanf("%d",&pos);
 if(pos>5)
 {
 printf("\nThis value is out of range ");
 }
 else
 {
 --pos;
 for(i=pos;i<=4;i++)
 {
 a[i]=a[i+1];
 }
 printf("\nAfter deletion elements in array: ");
 for(i=0;i<4;i++)
 {
 printf("  %d",a[i]);
 }
 }
 getch();
 }
 

Output

	
Enter element in array: 10 30 50 20
Stored element in array: 10 30 50 20
Enter poss. of element to delete: 3
After deletion elements in array: 10 30 20

Data Structure in C Stack in Data Structure

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements