Data Structure: Practical File
Data Structure: Practical File
Practical File
Of
DATA STRUCTURE
(using C Programming Language)
(Session 2009-2013)
Submitted To:
VEDPAL SINGH
SINGH Computer engineer
INDEX
Sr.No 1. 2. Practical WAP to Search an element in 2-D Array using linear search. Using Recursion WAP to find the element in Array using Binary Search. WAP to done the operations like Multipication, Addition, Subtraction on a Matrix. Write a Program to Implementation of a Queue. Signature
3.
Write a Program to Implementation Stack WAP for Swapping of two numbers using Call by Reference. WAP for Swapping of two numbers using Call by Value. WAP to perform various String Operations such as Reverse, Concatenation, Length and Copy. WAP to insert the element in a linked list at position of beginning and at end. WAP to Sorting the elements using Bubble Sort technique. WAP to Sort the elements using Selection Sorting Technique. WAP to Sort the list in Array using Insertion Sorting technique.
10.
{ if ( a[i] ==m) c =1; i++; } if ( c ==1) printf ( The entered Number found at position: %d, i1); else printf ( The number is not Found); getch( ); }
Output:
int a[10],I,m,n,beg,end,mid; clrscr( ); printf ( Enter the size of the Array ); scanf ( %d ,& n); printf ( Enter the Elements); for ( i=0; i<n; i++) scanf ( %d ,& a[i] ); printf ( Enter the element is to be Search ); scanf ( %d ,& m); beg = 1; end = n; mid = (beg + end)/2; while(beg<=end && a[mid] !=m) { if (a[mid]<m) beg = mid+1; if (a[mid]>m) end = mid-1; mid = (beg+end)/2; } if (a[mid] == m) printf ( Element found at position: %d ,mid); else printf ( Element is not Found ); getch( ); }
Output:
Program-7
#include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[20],b[20]; int l,c; clrscr(); printf("enter string ~~~a~~"); scanf("%s",&a); printf("enter string ~~~b~~"); scanf("%s",&b); printf("\n 1 for length\n"); printf("\n 2 for concatinate\n"); printf("\n 3 for reverse\n"); printf("\n 4 for copy\n"); printf("\n 5 for exit\n" printf("\n enter your choice\n"); scanf("%d",&c); switch(c) { case 1: { l=strlen(a); printf("length of string a is %d"); l=strlen( b); printf("length of string b is %d"); break; } case 2: { strcat(a,b);
printf("concatinated string is %s\n",a); break; } case 3: { strrev(a); printf("reverse of string a is %s\n",a); strrev(b); printf("reverse of string a is %s\n",b); break; } case 4: { strcpy(b,a); printf("copied string is %s\n",b); break; } case 5: { exit(); } default: { printf("no choice"); } } goto call; getch(); }
Output:
break; } } } insert (int x) { if (r = =(max_size-1)) { printf ( OVERFLOW); return; } else { r = r +1; queue[r] = x; } if (f = =-1) f = 0; } int delete( ) { int x; if (f = = -1) { printf ( UNDERFLOW); return(-1); } x = queue[f]; if (f = = r) f = r = -1; else f = f +1; if (f > max_size) { f = -1; r = -1; } return(x); getch( );
Output:
Case 3: exit(0); default: printf ( Enter correct Choice ); break; } } } push(int x) { if (top ==max_size) { printf ( OVERFLOW ); return; } top=top+1; stack[top] = x; return; } int pop( ) { int x; if (top==NULL) return(NULL); else { x = stack[top]; top=top-1; return(x); getch( ); } }
Output:
Output:
Output:
Program-8
linked list at position of beginning and at end.
#include<stdio.h> #include<conio.h> typedef struct node { int data; struct node *next; }Node; Node *insbeg(Node *,int); Node *insend(Node *,int); void display(Node *f); Node *f; void main() { int ch,x; clrscr(); while(1) { printf("\n\n\t1.INSERT AT THE BEGNING\n\t2.insert at end\n\t3.exit"); printf("\n\t\tEnter Ur Choice :- "); scanf("%d",&ch); switch(ch) { case 1: clrscr();
printf("\n\n\tenter the data u want to insert :- "); scanf("%d",&x); f=insbeg(f,x); display(f); break; case 2: clrscr(); printf("\n\n\tenter the data u want to insert :- "); scanf("%d",&x); f=insend(f,x); display(f); break; case 3: exit(1); default : printf("enter ur choice again"); } } } Node *insbeg(Node *f,int x) { Node *ptr; ptr=(Node *)malloc(sizeof(Node)); ptr->data=x; ptr->next=f; f=ptr; return f; } Node *insend(Node *f,int x) { Node *ptr,*p; ptr=(Node *)malloc(sizeof(Node)); ptr->data=x; ptr->next=NULL; if(f==NULL)
f=ptr; else { for(p=f;p->next!=NULL;p=p->next); p->next=ptr; } return f; } void display(Node *f) { Node *p; if(f==NULL) printf("EMPTY"); else { p=f; printf("LINKED LIST:-\n"); for(p=f;p!=NULL;p=p->next) printf("\t%d",p->data); } }
Output:
Output:
just=a[j]; k=j; } a[k]=a[i]; a[i]=just; } printf("\nTHE SORTED LIST OF ELEMENTS USING SELECTION SORT IS:\n\n"); for(i=0;i<size;i++) { printf("%d\n",a[i]); } getch(); }
Output:
a[j+1]=store; } printf("\nTHE SORTED LIST OF ARRAY USING INSERTION SORT IS\n\n"); for(i=0;i<size;i++) { printf("%d\n",a[i]); } getch(); }
Output:
Program-3
Multipication, Addition, Subtraction on a Matrix.
#include<stdio.h> #include<conio.h> void main() { int a[2][2],b[2][2],c[2][2],i,j,k,ch; clrscr(); printf("enter the element of a"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { scanf("%d",&a[i][j]); } } printf("enter the element of b"); for(i=0;i<2;i++) { for(j=0;j<2;j++)
{ scanf("%d",&b[i][j]); } } CALL: printf("\n 1 for addition\n"); printf("\n 2 for substraction\n"); printf("\n 3 for multiplication\n"); printf("\n 4 for display\n"); printf("\n 5 for exit\n"); printf("\n enter your choice\n"); scanf("%d",&ch); if(ch==1) { for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=a[i][j]+b[i][j]; } } } if(ch==2) { for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=a[i][j]-b[i][j]; } } } if(ch==3) { for(i=0;i<3;i++) { for(j=0;j<3;j++)
c[i][j]=0; for(k=0;k<3;k++) { c[i][j]=c[i][j]+a[i][j]+b[i][j]; } } } if(ch==4) { for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%d",c[i][j]); } } } if(ch==5) { exit(); } goto call; getch(); }
Output:
{ int info; struct node *left; struct node *right; }; typedef struct node tree; tree *root=NULL; int count=0 ; void display(); void insert( ) { int num; tree *p,*temp,*prev; p=(tree *)malloc(sizeof(tree)); printf("\n Enter the number to be inserted: "); scanf("%d",&num); p->info=num; p->left=p->right=NULL; if(root==NULL) { root=p;display(); return; } temp=root; while(temp!=NULL) { if(num>temp->info) { prev=temp; temp=temp->right; } else prev=temp; temp=temp->left; }
} if(num>=prev->info) prev->right=p; else prev->left=p; display(); } void preorder(tree *temp) { if(temp!=NULL) { count=0; printf(" %3d ",temp->info); preorder(temp->left); preorder(temp->right); } } void inorder(tree *temp) { if(temp!=NULL) { inorder(temp->left); printf(" %3d ",temp->info); inorder(temp->right); } } void postorder(tree *temp) { if(temp!=NULL) { postorder(temp->left); postorder(temp->right); count=count+1; printf(" %3d ",temp->info); } } void display()
{ if(root==NULL) printf("\n Empty tree"); else { printf("\nPreorder : "); preorder(root); printf("\ninorder : "); inorder(root); printf("\nPostorder : "); postorder(root); printf("\n\n THE NUMBER OF NODES IN THE BST is :%d", count); } } void main() { int ch=1; root=NULL; while(ch) { printf("\n\t\t\t***********MENU************\n"); printf("\n\t\t\t1:INSERT-INTREE\n\t\t\t2:DISPLAY\n\t\t\t3.QUIT\n"); printf("\nEnter your choice:\n"); scanf("%d",&ch); switch(ch) { case 1:clrscr(); insert(); break; case 2:clrscr(); display(); break case 3:exit(0); }
} } getch();
Output: