Arrays and Data Structures in C++

23
PROGRAM 1 Write a C++ program to store an  ARRAY  of  10 numbers and implement  LINEAR SEARCH . #include<iostream.h> #include<conio.h> int Linear_Search(int a[],int MAX,int item) { int x=1; for(int i=0;i<MAX;i++ ) if(a[i]==item) return i; return x; } const int MAX=5; void main() { clrscr(); int a[MAX],i,j,x,item; cout<<"Enter the contents of the Array: \n\n"; for(i=0;i<MAX;i++) cin>>a[i];

Transcript of Arrays and Data Structures in C++

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 1/23

PROGRAM 1 Write a C++ program to store an  ARRAY  of  10 numbers and implement   LINEAR SEARCH . 

#include<iostream.h>

#include<conio.h>

int Linear_Search(int a[],int MAX,int item)

{ int x=‐1;

for(int i=0;i<MAX;i++)

if(a[i]==item)

return i;

return x;

}

const int MAX=5;

void main()

{ clrscr();

int a[MAX],i,j,x,item;

cout<<"Enter the contents of the Array: \n\n";

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

cin>>a[i];

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 2/23

  cout<<"\n\nThe contents of the array are\n\n:";

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

cout<<a[i]<<"\t"; cout<<"\n";

cout<<"\n\nEnter the item to be searched:\n";

cin>>item;

x=Linear_Search(a,MAX,item);

if(x>‐1)

cout<<"\n\nThe item is found at "<<x+1;

else

cout<<"\n\nThe Item is not in the array.";

getch();

}

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 3/23

PROGRAM 2 Write a C++ program to store an  ARRAY  of  10 numbers and implement   BINARY  SEARCH . 

#include<iostream.h>

#include<conio.h>

int Binary_Search (int a[], int MAX, int item)

{ int beg=0, last=MAX, mid, x=‐1;

while (beg<last)

{ mid = (beg + last)/2;

If (a[mid]==item)

return mid;

else if (a[mid]<item)

beg = mid+1;

else

last = mid‐1;

}

return x;

}

const int MAX = 10;

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 4/23

void main()

{ clrscr();

int a[MAX], i, j, x, item;

cout<<"Enter the contents of the Array (In ascending Order):\n\n";

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

cin>>a[i];

cout<<"\n\nThe contents of the array are\n\n:";

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

cout<<a[i]<<"\t"; cout<<"\n";

cout<<"\n\nEnter the item to be searched:\n";

cin>>item;

x = Binary_Search(a, MAX, item);

if (x>‐1)

cout<<"\n\nThe item is found at "<<x+1;

else

cout<<"\n\nThe Item is not in the array.";

getch();

}

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 5/23

PROGRAM 3 Write a C++ program to store an  ARRAY  of  10 numbers and implement   BUBBLE SORT . 

#include<iostream.h>

#include<conio.h>

const int MAX=10;

void main()

{ clrscr();

int a[MAX],i,j,x=1,t;

cout<<"Enter the contents of the Array:";

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

cin>>a[i];

cout<<"\n\nThe contents of the array are\n\n:";

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

cout<<a[i]<<"\t"; cout<<"\n";

for(i=1;i<=MAX;i++)

{ for(j=0;j<MAX‐1;j++)

{ if(a[j]>a[j+1])

{ t=a[j];

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 6/23

  a[j]=a[j+1];

a[j+1]=t;

}

cout<<"\n\nStep:"<<x++<<": \t";

for(int m=0;m<MAX;m++)

cout<<a[m]<<"\t";

}

}

cout<<"\n\nThe contents of the array after sorting are\n\n:";

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

cout<<a[i]<<"\t";

getch();

}

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 7/23

PROGRAM 4 Write a C++ program to store an  ARRAY  of  10 numbers and implement   INSERTION SORT . 

#include<iostream.h>

#include<conio.h>

const int MAX=10;

void main()

{ clrscr();

int a[MAX],i,j,x=1,t,M;

cout<<"Enter the contents of the Array:";

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

cin>>a[i];

cout<<"\n\nThe contents of the array are;\n\n";

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

cout<<a[i]<<"\t"; cout<<"\n";

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

{ t=i‐1; M=a[i];

while((a[t]>M)&&(t>=0))

{ a[t+1]=a[t];

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 8/23

  t‐‐;

cout<<"\n\nStep:"<<x++<<": \t";

for(int m=0;m<MAX;m++)

cout<<a[m]<<"\t";

}

a[++t]=M;

}

cout<<"\n\nThe contents of the array after sorting are:\n\n";

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

cout<<a[i]<<"\t";

getch();

}

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 9/23

PROGRAM 5 

Write a C++ program to store an  ARRAY  of  10 numbers and implement   SELECTION SORT . 

#include<iostream.h>

#include<conio.h>

const int MAX=5;

void main()

{ clrscr();

int a[MAX],i,j,pos,t;

cout<<"Enter the contents of the Array:";

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

cin>>a[i];

cout<<"\n\nThe contents of the array are\n\n:";

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

cout<<a[i]<<"\t";

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

{ pos=i;

for(j=i+1;j<MAX;j++)

if(a[j]<a[pos])

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 10/23

  pos=j;

t=a[pos];

a[pos]=a[i];

a[i]=t;

cout<<"\n\nStep:"<<i<<": \t";

for(int m=0;m<MAX;m++)

cout<<a[m]<<"\t";

}

cout<<"\n\nThe contents of the array after sorting are\n\n:";

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

cout<<a[i]<<"\t";

getch();

}

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 11/23

PROGRAM 6 

Write a C++ Program to Input  Two  Arrays of  Size Defined by Userand Display an array merged from these Two  Arrays in  AscendingOrder. 

#include<iostream.h>

#include<conio.h>

void input(int a[],int size)

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

{ cout<<"Enter "<<i+1<<" element:";

cin>>a[i];

}

}

void sort(int a[],int size)

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

{ int t=i‐1,M=a[i];

while((a[t]>M)&&(t>=0))

{ a[t+1]=a[t];

t‐‐;

}

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 12/23

  a[++t]=M;

}

}

void display(int a[],int size)

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

cout<<"\t"<<a[i];

}

void main()

{ clrscr();

int a=0,b=0,c=0,len_a,len_b;

cout<<"Enter the size of Array 1:";

cin>>len_a;

int *A=new int[len_a];

cout<<"\nEnter the elements of Array 1:\n\n";

input(A,len_a);

cout<<"\n\nEnter the size of Array 2:";

cin>>len_b;

int *B=new int[len_b];

cout<<"\nEnter the elements of Array 2:\n\n";

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 13/23

  input(B,len_b);

cout<<"\n\nArray 1:" ; display(A,len_a);

cout<<"\n\nArray 2:" ; display(B,len_b);

sort(A,len_a);sort(B,len_b);

int *C=new int[len_a+len_b];

while((a<len_a)&&(b<len_b))

{ if(A[a]>B[b])

C[c++]=B[b++];

else

C[c++]=A[a++];

}

while(a<len_a)

C[c++]=A[a++];

while(b<len_b)

C[c++]=B[b++];

cout<<"\n\nMerged Array:"; display(C,len_a+len_b);

getch();

}

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 14/23

PROGRAM 7 

Write a menu driven Program to provide the following operations on Queue using Linked List. 

1.  ADD an element  to QUEUE 2. DELETE an element  from QUEUE 3. DISPLAY  QUEUE 

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

struct queue

{ int data;

queue* ptr;

}*FRONT=NULL,*REAR=NULL ;

void add_queue(int data)

{ queue *temp=new queue;

temp‐>data=data;

if(FRONT==NULL)

{ temp‐>ptr=NULL;

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 15/23

  FRONT=REAR=temp;

}

else

{ temp‐>ptr=NULL;

REAR‐>ptr=temp;

REAR=temp;

}

}

void del_queue()

{ queue*temp=FRONT;

if(FRONT!=NULL)

{ FRONT=FRONT‐>ptr;

delete(temp);

}

else

cout<<"Queue is Empty";

}

void display_queue()

{ queue*temp=FRONT;

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 16/23

  while(temp!=NULL)

{ cout<<temp‐>data<<"\t";

temp=temp‐>ptr;

}

}

void main()

{ clrscr();

int chk=1,x;

while(chk)

{ for(int i=0;i<6;i++)

{ cout<<"\n"; }

cout<<"\t\t\tQUEUE";

cout<<"\n\n\n\n\t\t1.\tADD an element to QUEUE";

cout<<"\n\n\t\t2.\tDELETE an element from QUEUE";

cout<<"\n\n\t\t3.\tDISPLAY QUEUE";

cout<<"\n\n\t\t4.\tEXIT";

cout<<"\n\n\n\n\t\tEnter your choice:\t";

cin>>x;

switch (x)

{ case 1: clrscr();

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 17/23

  cout<<"\n\n\n\n\t\tEnter content of the QUEUE:";

cin>>x;

add_queue(x);

cout<<"\n\n\n\n\t\tThe new QUEUE is:\n\n\t\t";

display_queue();

break;

case 2: clrscr();

cout<<"\n\n\n\n\t\tThe old QUEUE is:\n\n\t\t";

display_queue();

del_queue();

cout<<"\n\n\n\n\t\tThe new QUEUE is:\n\n\t\t";

display_queue();

break;

case 3: clrscr();

cout<<"\n\n\n\n\t\tThe QUEUE is:\n\n\t\t";

display_queue();

break;

case 4: exit(0);

break;

default: cout<<"\n\n\t\tINVALID INPUT.";

}

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 18/23

  getch();

clrscr();

}

getch();

}

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 19/23

PROGRAM 8 

Write a menu driven Program to provide the following operations on STACK  using Linked List. 

1.  ADD an element  to STACK  2. DELETE an element  from STACK  3. DISPLAY  STACK  

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

struct stack

{ int data;

stack*ptr;

};

stack*START=NULL;

void push(int a)

{ stack*temp=new stack;

temp‐>data=a;

if(START==NULL)

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 20/23

  { temp‐>ptr=NULL;

START=temp;

}

else

{ temp‐>ptr=START;

START=temp;

}

}

void display()

{ stack*temp=START;

if(START==NULL)

cout<<"The STACK is empty";

while(temp!=NULL)

{ cout<<temp‐>data<<"\t";

temp=temp‐>ptr;

}

}

void pop()

{ stack*temp=START;

if(START!=NULL)

{ START=temp‐>ptr;

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 21/23

  delete temp;

}

}

void main()

{ clrscr();

int chk=1,x;

while(chk)

{ for(int i=0;i<6;i++)

{ cout<<"\n"; }

cout<<"\t\t\tSTACK";

cout<<"\n\n\n\n\t\t1.\tPUSH an element to STACK";

cout<<"\n\n\t\t2.\tPOP an element from STACK";

cout<<"\n\n\t\t3.\tDISPLAY STACK";

cout<<"\n\n\t\t4.\tEXIT";

cout<<"\n\n\n\n\t\tEnter your choice:\t";

cin>>x;

switch (x)

{ case 1: clrscr();

cout<<"\n\n\n\n\t\tEnter content of the STACK:";

cin>>x;

push(x);

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 22/23

  cout<<"\n\n\n\n\t\tThe new STACK is:\n\n\t\t";

display();

break;

case 2: clrscr();

cout<<"\n\n\n\n\t\tThe old STACK is:\n\n\t\t";

display();

pop();

cout<<"\n\n\n\n\t\tThe new STACK is:\n\n\t\t";

display();

break;

case 3: clrscr();

cout<<"\n\n\n\n\t\tThe STACK is:\n\n\t\t";

display();

break;

case 4: exit(0);

break;

default:cout<<"\n\n\t\tINVALID INPUT.";

}

getch();

clrscr();

}

8/14/2019 Arrays and Data Structures in C++

http://slidepdf.com/reader/full/arrays-and-data-structures-in-c 23/23

  getch(); }