0% found this document useful (0 votes)
71 views

Data Structure: Practical File

This document contains 10 programs written in C programming language to demonstrate various data structures concepts. The programs include: 1. Linear search in a 2D array 2. Binary search in an array using recursion 3. Matrix operations like addition, subtraction, multiplication 4. Implementation of a queue 5. Implementation of a stack 6. Swapping numbers using call by reference and value 7. String operations like reverse, concatentation etc. 8. Inserting elements in linked list at beginning and end 9. Sorting arrays using bubble, selection and insertion sort 10. Implementing insertion and deletion in a binary search tree

Uploaded by

Anjali Taliyan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Data Structure: Practical File

This document contains 10 programs written in C programming language to demonstrate various data structures concepts. The programs include: 1. Linear search in a 2D array 2. Binary search in an array using recursion 3. Matrix operations like addition, subtraction, multiplication 4. Implementation of a queue 5. Implementation of a stack 6. Swapping numbers using call by reference and value 7. String operations like reverse, concatentation etc. 8. Inserting elements in linked list at beginning and end 9. Sorting arrays using bubble, selection and insertion sort 10. Implementing insertion and deletion in a binary search tree

Uploaded by

Anjali Taliyan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 39

A

Practical File
Of

DATA STRUCTURE
(using C Programming Language)

(Session 2009-2013)

Submitted To:

Submitted By: JAGBEER CSE-B 2509290

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.

4. 5. 6(a). 6(b) 7. 8. 9(a). 9(b) 9(c)

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.

WAP to Implement Binary Search Tree (Insertion & Deletion).

Program-1 Aim: WAP to Search an element in 2-D


Array using linear search.
# include<stdio.h> # include<conio.h> void main ( ) { int a[10],i,m,n,c =0; clrscr( ); printf ( Enter the Size of Array); scanf( %d , &n); printf ( Enter the Elements of Array ); for ( i=1; i<=n; i++) { scanf ( %d ,& a[i] ); } printf ( The elements of The Array are :); for ( i=1; i<=n; i++) { printf ( %d,a[i] ); } printf ( Enter the number is to be search ); scanf ( %d ,& m); i = 1; while( i<=n && c ==0)

{ 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:

Program-2 Aim: Using Recursion WAP to find the


element in Array using Binary Search.
# include<stdio.h> # include<conio.h> void main( ) {

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

Aim: WAP to perform various String


Operations such as Reverse, Concatenation, Length and Copy.

#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:

Program-4 Aim: Write a Program to Implementation


of a Queue.
# include<stdio.h> # include<conio.h> # define max_size 20 int queue[max_size],f =-1,r =-1; void main( ) { int ch, x ; clrscr( ); printf ( QUEUE OPERATIONS ); printf ( 1. Insert Operation ); printf ( 2. Delete Operation ); printf ( 3. Exit ); while(1) { printf ( Enter your Choice); scanf ( %d , &ch); { Case 1: printf ( Enter the Number to be Inserted); scanf ( %d , &x); Insert(x); Break; Case 2: x = delete( ); if (x != -1) printf ( The Number deleted is: %d, x); else printf ( The Queue is empty); break; Case 3: exit(0); Default: printf ( Enter correct Choice );

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:

Program-5 Aim: Write a Program to Implementation


Stack.
# include<stdio.h> # include<conio.h> # define max_size 20 Int stack[max_size],top= NULL; void main( ) { int ch,x; clrscr( ); printf ( STACK OPERATIONS ); printf ( 1. Push Operation ); printf ( 2. Pop Operation ); printf ( 3. Exit ); while(1) { printf ( Enter your Choice ); scanf ( %d ,& ch); switch(ch) { Case 1: printf ( Enter the number to be Pushed ); scanf ( %d ,& x); push(x); break; Case 2: x = pop( ); if (x !=NULL) printf ( The number Poped is: %d,x); else printf ( The Stack is empty ); break;

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:

Program-6 (a) Aim: WAP for Swapping of two numbers


using Call by Reference.
#include<stdio.h> #include<conio.h> int swapr(int *,int *); void main( ) { int a,b,c; printf("enter a and b"); scanf("%d%d",&a,&b); c=swapr(&a,&b); printf("\n %d %d \n"a,b); getch(); } swapr(int *a,int *b) { int t; t=*a; *a=*b; *b=t; }

Output:

Program-6 (b) Aim: WAP for Swapping of two numbers


using Call by Value.
#include<stdio.h> #include<conio.h> int swapv(int ,int ); void main() { int a,b,c; printf("enter a and b"); scanf("%d%d",&a,&b); c=swapv(a,b); getch(); } swapv(int a,int b) { int t; t=a; a=b; b=t; printf("\n %d %d \n"a,b); }

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();

Aim: WAP to insert the element in a

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:

Program-9 (a) Aim: WAP to Sorting the elements using


Bubble Sort technique.
#include<stdio.h> #include<conio.h> void main( ) { int a[10],t,i,j,n; clrscr( ); printf("Enter the limits:"); scanf("%d",&n); printf("\n Enter the numbers:\n\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++) { for(j=0;j<n-1;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } }

} printf("\n the sorted elements are:\n\n"); for(i=0;i<n;i++) printf("%d",a[i]); getch(); }

Output:

Program-9 (b) Aim: WAP to Sort the elements using


Selection Sorting Technique.
#include<stdio.h> #include<conio.h> #include<math.h> void main() { int i,j,k,just,size,a[50]; clrscr(); printf("\nTO SORT A GIVEN LIST OF ARRAY USING SELECTION SORT\n\n"); printf("\nENTER THE SIZE OF ARRAY\n\n"); scanf("%d",&size); printf("\n enter the elements:\n\n"); for(i=0;i<size;i++) { scanf("%d",&a[i]); } for(i=0;i<size+1;i++) { just=a[i]; k=i; for(j=i+1;j<size;j++) if(just>a[j]) {

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:

Program-9 (c) Aim: WAP to Sort the list in Array using


Insertion Sorting technique.
#include<stdio.h> #include<conio.h> #include<math.h> void main() { int a[50],i,j,size,store; clrscr(); printf("\nTO SORT AN ARRAY USING INSERTION SORT\n\n"); printf("\nENTER THE SIZE OF ARRAY\n\n"); scanf("%d",&size); printf("\nENTER THE ELEMENTS OF ARRAY\n\n"); for(i=0;i<size;i++) { scanf("%d",&a[i]); } for(i=1;i<size;i++) { store=a[i]; for(j=i-1;j>=0&&store<a[j];j--) a[j+1]=a[j];

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++)

Aim: WAP to done the operations like

{ 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:

Program-10 Aim: WAP to Implement Binary Search


Tree (Insertion & Deletion).
#include<stdio.h> #include<conio.h> #include<stdlib.h> struct node

{ 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:

You might also like