Showing posts with label DataStructure. Show all posts
Showing posts with label DataStructure. Show all posts

Saturday, July 6, 2019

Program and Algorithm for POP ,PEEP and PUSH operation on stack (array) in C Data Structures


For many other Assembly Language  Programs related to microprocessor 8086, c++ , datastructures  visit our  BLOG 


Algorithm And Program: -

PUSH :

1.if top==MAX-1 then  print stack is full (overflow)
2.else accept data items from the user
3.set top=top+1
4.stack[top] = item
5.stop

POP:  

1.if top==-1 then print stack is empty (underflow)
2.else   set item= stack[top]
3.set top=top-1
4.stop

Program:

#include<stdio.h>
#include<conio.h>
#define maxsize 10
//---functions--
void pop();
void peep();
void push();
void display();
//------------------
void main()
{
int stack[maxsize] ;
int  top=-1 , i, n ;
clrscr();
printf(“****   OPERATIONS **** \n\n”);
printf(“1.pop  \n2.peep \n3.push \n4.display\n\n\n”);
printf(“enter choice : =  ”);
scanf(“%d”,&n);
do
{
switch(n)
{
case 1 : pop();
break;
case 2: peep();
break;
case 3:push();
break;
case 4: display();
case 5: break;
default:printf(“invalid entry”);
}
}while(c!=5);
getch();
}
void pop()
{
int y;
if(top==-1)
{
printf(“stack is underflow”);
}
else
{
y= stack[top];
top=top-1;
printf(“element deleted ”);
}
}

void peep();
{
if(top== 1)
printf(“stack is full underflow”);
else
printf(“topmost data = %d ”,stack[top]);
}

void display()
{
if(top== 1)
printf(“stack is full underflow”);
else
for(int q=0;q<maxsize-1;q++)
printf(“\n%d”,stack[q]);
}
void push()
{
int x;
if(top==(maxsize-1))
{
printf(“stack is full (overflow)”);
}
else
{
printf(“enter data”);
for(i=0;i<n;i++)
scanf(“%d”,&x);
top=top+1;
stack[top]=x;
}
}


Programs

For many other Assembly Language  Programs related to microprocessor 8086 visit our  BLOG 

There are programs of largest ,smallest ,division ,multiplication,odd/even,block operation ,string operations ,etc


Friday, June 14, 2019

Insertion sort Program in C -Data Structure





( In-place sort and is stable . It has n*(n-1)/2 comparision as in this sort )

#include<stdio.h>
#include<conio.h>
void main()
{
int a[25],temp,n,i,j;
clrscr();
printf(“enter array size”);
scanf(“%d”,&n);
printf(“enter array data”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n-1;i++)
{
j=1;
while(j>0 && a[j-1]>a[j])
{
temp = a[j];
a[j]=a[j-1];
a[j-1]=temp;
j--;
}
}
printf(“result \n\n”);
printf(“\n%d”,a[i]);

 PROGRAMS:-

For many other Language  Programs related to microprocessor 8086, C++ ,  and DataStructures , etc visit our  BLOG

There are programs of largest ,smallest ,division ,multiplication,odd/even,block operation ,string operations ,etc


Friday, May 24, 2019

Linear & Binary Search C Program and Algorithm

For many other Language Programs related to microprocessor 8086  , C++ and Data structure visit our  BLOG 

Linear Search Algorithm: (useful for unsorted data list and fast for small data list)

   1. initialize p =-1
    2. set i=0
    3. repeate step 4 while (i<n)
    4.i f a[i] =val then set p =i print p and go to step 6
    5. else print value not present in array
    6. exit


Binary Search Algorithm: (useful for sorted(ascending) data list and fast for large data list)

    1. start
    2. initialize p =-1 ,beg= 0(lower bound) , end = upper bound
    3. repeate step 4  and step 3 while (beg<= end)
    4.if a[mid] =val  then  srt  end = mid-1     else   set   beg= mid+1  (end if and while loop)
    5. if beg>end  then print value not present in array else print value present in array
    6. exit

    
    Program of Linear and Binary Search:
j   
r    Linear Search Program

#include <stdio.h>
#include<conio.h>
void main()
{
//here n=10   arr[ -->   10 ß ]
int arr[10] ,p= -1,num ,found =0 ,i   ;
printf(“enter elements”);
for(i=0;i<10;i++)
{
scanf(“%d”,&arr[i]);
}
printf(“enter number to search ”);
scanf(“%d”,&num);
for(i=0;i<10;i++)
{
if ( arr[i==num)
{
found=1;
p = I;
printf(“ number found at position =  %d”,i+1);
break;
}
}
if(found==0)
{
printf(“ number not found”);
}
}

     Binary Search Program:

#include <stdio.h>
#include<conio.h>
void main()
{
int arr[10] ,beg,end,mid,val, i   ;
printf(“enter elements”);
for(i=0;i<10;i++)
{
scanf(“%d”,&arr[i]);
}
printf(“enter number to search ”);
scanf(“%d”,&num);
while(beg<=end)
{
mid = (beg+end)/2;
beg=0;end=10-1;
if(a[mid]<val)
{
beg =  mid+1;
}
else if (a[mid]==val)
{
printf(“element found at position %d ”,mid+1);
break;
}
else
(
end =mid-1;
}
mid=(beg +end )/2;
if(beg>end)
{
printf(“ element not found”);
}

For many other Language  Programs related to microprocessor 8086  , C++ and Data structure visit our  BLOG 

There are programs of bubble , selection , linear search , pop, push ,etc for data structures


Wednesday, May 22, 2019

Selection sort C Program and Algorithm -Data Structures


For many other Language  Programs related to microprocessor 8086, data structures  and C++ visit our  BLOG 



Algorithm :(number of comparisions = n*(n-1)/2 and there are n-1 exchanges in this sort)
1. start
2. initialize small = arr[k]
3. initialize pos =k
4. repeate for j=k+1 to n 
5. if(small >arr[j]) then set small =arr[j]  set pos=j
6. stop

Program :

#include<stdio.h>
#include<conio.h>
void main()
{
int a[25],temp,n,i,j,pos;
clrscr();
printf(“enter array size”);
scanf(“%d”,&n);
printf(“enter array data”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n-1;i++)
{
pos=I;
for(j=i+1;j<n;j++)
{
if(a[pos]>a[j])
pos=j;
}
if(pos!=i)
{
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
}
printf(“result\n\n\n”);
for(i=0;i<ni++)
printf(“%d\n”,a[i]);
}

For many other Language Programs related to microprocessor 8086 ,C++, Data Structures visit our  BLOG 

There are programs of bubble , selection , linear search , pop, push ,etc for data structures

Saturday, May 18, 2019

Bubble sort - C Program and Algorithm - Data Structures



For many other Language Programs related to microprocessor 8086, data structures  and C++ visit our  BLOG 


Algorithm:

1. start
2. repeate step 3 for i= 0 to n-1
3. repeate for j=0 to n-1
4. if a[j]= a[j+1] then swap a[j[ and a[j+1]
5. exit

Program :
#include<stdio.h>
#include<conio.h>
void main()
{
int a[25],temp,n,i,j;
clrscr();
printf(“enter array size”);
scanf(“%d”,&n);
printf(“enter array data”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n-1;i++)
{
if(a[j]>a[j+1])
{
temp = a[j];
a[j] =a[j+1];
a[j+1] =temp;
}
}
}
printf(“\n\nafter sort :- ”);
for(i=0;i<n;i++)
printf(“\n %d”,a[i]);
getch();
}
For many other Language Programs related to microprocessor 8086 ,C++ , data structures visit our  BLOG 

There are programs of bubble , selection , linear search , pop, push ,etc for data structures


Friday, May 17, 2019

Program to perform Insertion and Deletion operation on an Linear Queue in C Data Structures

For many other Language  Programs related to microprocessor 8086, data structures  and C++ visit our  BLOG


#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10] ,p ,I ,n ,val ;
printf(“enter elements”);
for(i=0;i<10;i++)
{
scanf(“%d”,&arr[i]);
}
printf(“enter position to delete number”);
scanf(“%d”,&p);
if(p >10)
{
printf(“deletion not possible ”);
}
else
{
for(i=p;i<10;i++)
{
arr[i] = arr[i+1];
}
printf(“\n\nresult after deletion ”);
for(i=0;i<10-1;i++)
printf(“\n%d”,arr[i]);
}

Programs:-

For many other Language  Programs related to microprocessor 8086 visit our  BLOG 

There are programs of bubble , selection , linear search , pop, push ,etc for data structures