Data structure output 1

23
1. INSERTION AND DELETION PROGRAM: #include<stdio.h> void main() { int n,c,a,value,position,array[50]; clrscr(); printf("Enter the number of elements in array\n"); scanf("%d",&n); printf("Enter the %d elements \n",n); for(c=0;c<n;c++) scanf("%d",&array[c]); printf("1.Insertion\t2.Deletion\n"); scanf("%d",&a); if(a==1) { printf("Enter the location where you wish to insert element\n"); scanf("%d",&position); printf("Enter the value to insert\n"); scanf("%d",&value); for(c=n-1;c>=position-1;c--) array[c+1]=array[c]; array[position-1]=value; printf("Array is"); for(c=0;c<=n;c++) printf("%d\t",array[c]); getch(); } else { printf("Enter the location where you wish to delete element\n"); scanf("%d",&position); if(position>=n+1) printf("Deletion not posible\n"); for(c=position-1;c<n;c++) array[c]=array[c+1]; printf("Array after deletion is\n");

description

its mine

Transcript of Data structure output 1

Page 1: Data structure output 1

1. INSERTION AND DELETION

PROGRAM:

#include<stdio.h>void main(){int n,c,a,value,position,array[50];clrscr();printf("Enter the number of elements in array\n");scanf("%d",&n);printf("Enter the %d elements \n",n);for(c=0;c<n;c++)scanf("%d",&array[c]);printf("1.Insertion\t2.Deletion\n");scanf("%d",&a);if(a==1){printf("Enter the location where you wish to insert element\n");scanf("%d",&position);printf("Enter the value to insert\n");scanf("%d",&value);for(c=n-1;c>=position-1;c--)array[c+1]=array[c];array[position-1]=value;printf("Array is");for(c=0;c<=n;c++)printf("%d\t",array[c]);getch();}else{printf("Enter the location where you wish to delete element\n");scanf("%d",&position);if(position>=n+1)printf("Deletion not posible\n");for(c=position-1;c<n;c++)array[c]=array[c+1];printf("Array after deletion is\n");for(c=0;c<n-1;c++)printf("%d\t",array[c]);getch();}}

Page 2: Data structure output 1

OUTPUT:

Enter the number of elements in array5Enter the 5 elements2468101.Insertion 2.Deletion1Enter the location where you wish to insert element4Enter the value to insert7Array is 2 4 6 7 8 10

Enter the number of elements in array5Enter the 5 elements2468101.Insertion 2.Deletion2Enter the location where you wish to delete element4Array after deletion is2 4 6 10

Page 3: Data structure output 1

2. MERGING TWO SORTED ARRAYSPROGRAM:

#include<stdio.h>void main(){int a[50],b[50],c[100],m,n,i,j,k=0;clrscr();printf("\nEnter size of array A:\n");scanf("%d",&m);printf("\nEnter sorted elements of array A:\n");for(i=0;i<m;i++){scanf("%d",&a[i]);}printf("\nEnter size of array B:\n");scanf("%d",&n);printf("\nEnter sorted elements of array B:\n");for(i=0;i<n;i++){scanf("%d",&b[i]);}i=0;j=0;while(i<m&&j<n){if(a[i]<b[j]){c[k]=a[i];i++;}else{c[k]=b[j];j++;}k++;}if(i>=m){while(j<n){c[k]=b[j];

Page 4: Data structure output 1

j++;k++;}}if(j>=n){while(i<m){c[k]=a[i];i++;k++;}}printf("\nAfter merging:\n");for(i=0;i<m+n;i++){printf("\n%d",c[i]);}getch();}

OUTPUT:Enter sorted elements of array A:2468Enter size of array B:4Enter sorted elements of array B:1357After merging:12345678

Page 5: Data structure output 1

3. SEARCHING AN ELEMENT IN 2-D ARRAY

PROGRAM:

#include<stdio.h>#include<conio.h>void main(){int a[20][20],i,j,m,n,pos,item;char f;int *p=&a[0][0];clrscr();printf("Enter the number of row \n");scanf("%d",&m);printf("Enter the number of column \n");scanf("%d",&n);printf("Enter the data one by one in row wise \n");for(i=0;i<m;i++){for(j=0;j<n;j++)scanf("%d",&a[i][j]);}printf("Enter the search item \n");scanf("%d",&item);for(i=0;i<m;i++){for(j=0;j<n;j++){if(a[i][j]==item){p=p+(i*n+j)*16;f='y';printf("The search item %d is in position [%d][%d]",item,i,j);printf("\nThe address of the search item %d is %u",item,p);break;}}}if(f!='y')printf("\nThe search item %d is not present in the array",item);getch();}

Page 6: Data structure output 1

OUTPUT:

Enter the number of row4Enter the number of column3Enter the data one by one in row wise24681012141618202224Enter the search item12The search item 12 is in position [1][2]The address of the search item 12 is 64876

Page 7: Data structure output 1

4. STACK OPERATION USING ARRAY

PROGRAM:

#include<stdio.h>#include<conio.h>#define n 10struct stack{int top;int data[n];};void main(){struct stack s;int i,opt,item;clrscr();s.top=-1;do{printf("\n1.Push\n");printf("2.Pop\n");printf("3.List\n");printf("4.Exit\n");printf("Enter any one option\n");scanf("%d",&opt);switch(opt){case 1:if(s.top==n-1)printf("Stack Full\n");else{printf("Enter the data to push into the stack\n");scanf("%d",&item);s.top=s.top+1;s.data[s.top]=item;}break;case 2:if(s.top==-1)printf("Stack empty\n");else{printf("The deleted element from stack is %d",s.data[s.top]);

Page 8: Data structure output 1

s.top--;}break;case 3:if(s.top==-1)printf("\n stack empty");else{printf("The elements in the stack are\n");for(i=s.top;i>=0;i--)printf("\n%d\n",s.data[i]);}break;}}while(opt!=4);getch();}

Page 9: Data structure output 1

OUTPUT:

1.Push2.Pop3.List4.ExitEnter any one option1Enter the data to push into the stack10

1.Push2.Pop3.List4.ExitEnter any one option1Enter the data to push into the stack20

1.Push2.Pop3.List4.ExitEnter any one option1Enter the data to push into the stack30

1.Push2.Pop3.List4.ExitEnter any one option3The elements in the stack are

30

20

10

Page 10: Data structure output 1

1.Push2.Pop3.List4.ExitEnter any one option2The deleted element from stack is 301.Push2.Pop3.List4.ExitEnter any one option2The deleted element from stack is 301.Push2.Pop3.List4.ExitEnter any one option3The elements in the stack are

20

10

1.Push2.Pop3.List4.ExitEnter any one option4

Page 11: Data structure output 1

5. CONVERTING INFIX EXPRESSION TO POSTFIX EXPRESSION

PROGRAM:

#include<stdio.h>#include<conio.h>struct stack{int data[50];int top;};struct stack s;char infix[50],post[50];void postfix();void push(int);char pop();void main(){clrscr();printf("Enter the infix expression\n");scanf("%s",infix);postfix();getch();}void postfix(){int i,j=0;for(i=0;infix[i]!='\0';i++){switch(infix[i]){case'+':

while(s.data[s.top]>=i)post[j++]=pop();push(1);break;

case'-':while(s.data[s.top]>=1)post[j++]=pop();push(2);break;

case'*':while(s.data[s.top]>=3)

Page 12: Data structure output 1

post[j++]=pop();push(3);break;

case'/':while(s.data[s.top]>=4)post[j++]=pop();push(4);break;

case'^':while(s.data[s.top]>=4)post[j++]=pop();push(5);break;

case'(':push(0);break;

case')':while(s.data[s.top]!=0)post[j++]=pop();s.top--;break;

default:post[j++]=infix[i];

}}while(s.top>0)post[j++]=pop();printf("\n\tThe post fix expression is \t%s",post);}void push(int v){s.top++;s.data[s.top]=v;}char pop(){int a;char c;a=s.data[s.top];s.top--;switch(a){case 1:

Page 13: Data structure output 1

c='+';break;

case 2:c='-';break;

case 3:c='*';break;

case 4:c='/';break;

case 5:c='^';break;

}return(c);}

OUTPUT:

Enter the infix expression(a+b*c-d/e)

The post fix expression is abc*+de/-

Page 14: Data structure output 1

6. EVALUATING A POSTFIX EXPRESSION

PROGRAM:

#include<stdio.h>#include<conio.h>#include<math.h>#include<ctype.h>struct stack{int top;float a[20];}s;void main(){int i,n;float opt1,opt2,opt3;char ex[20];clrscr();s.top=-1;printf("\n Enter the postfix expression:");gets(ex);n=strlen(ex);for (i=0;i<n;i++){if (isdigit(ex[i])){s.top++;s.a[s.top]=ex[i]-'0';}else{opt2=s.a[s.top--];opt1=s.a[s.top--];switch(ex[i]){case'+':

opt3=opt1+opt2;break;

case'-':opt3=opt1-opt2;break;

case'*':

Page 15: Data structure output 1

opt3=opt1*opt2;break;

case'/':opt3=opt1/opt2;break;

case'^':opt3=pow(opt1,opt2);break;

}s.a[++s.top]=opt3;}}printf("\nThe Result %s is %.2f",ex,s.a[s.top]);getch();}

OUTPUT:

Enter the postfix expression:853*+94/-

The Result 853*+94/- is 20.75

Page 16: Data structure output 1

7. QUEUE OPERATIONS USING ARRAY

PROGRAM:

#include<stdio.h>#include<conio.h>struct queue{int data[50];int front;int rear;};int i,opt,item;int x;void main(){struct queue q;clrscr();q.front=-1;q.rear=-1;do{printf("Select any one option \n");printf("1.Create \n");printf("2.List \n");printf("3.Insert \n");printf("4.Delete \n");printf("5.Exit \n");scanf("%d",&opt);switch(opt){case 1:

printf("Enter 10 data one by one \n");for(i=0;i<10;i++)scanf("%d",&q.data[++q.rear]);break;

case 2:printf("The data in the queue are \n");for(i=q.front+1;i<=q.rear;i++)printf("%d\n",q.data[i]);break;

case 3:printf("Enter the data to insert \n");

Page 17: Data structure output 1

scanf("%d",&item);q.data[++q.rear]=item;break;

case 4:q.front++;x=q.data[q.front];printf("\nThe deleated item is \t%d\n",x);break;

}}while(opt!=5);getch();}

Page 18: Data structure output 1

OUTPUT:

Select any one option1.Create2.List3.Insert4.Delete5.Exit1Enter 10 data one by one12345678910

Select any one option1.Create2.List3.Insert4.Delete5.Exit3Enter the data to insert11

1.Create2.List3.Insert4.Delete5.Exit2The data in the queue are12345

Page 19: Data structure output 1

67891011

Select any one option1.Create2.List3.Insert4.Delete5.Exit4The deleated item is 1

Select any one option1.Create2.List3.Insert4.Delete5.Exit4The deleated item is 2

Select any one option1.Create2.List3.Insert4.Delete5.Exit5

Page 20: Data structure output 1