Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 55

DATA STRUCTURES

BY: TASHA CHANDOLIA IC-2 516/IC/09

S. No.
1. 2. 3. 4. 5.

Experiment
Factorial of a number using a recursive function. Stack implementation using arrays and linked list. Queue implementation using arrays and linked list. Sequential Search Binary Search Sorting: 1. Bubble Sort 2. Merge Sort 3. Selection Sort 4. Quick Sort 5. Insertion Sort. Implementation of pre-order, inorder, post-order traversal in binary tree. DFS and BFS tree traversal. Evaluation of an expression using Infix, Postfix, and Prefix.

Sign

6.

7. 8.

//Factorial of a number using a recursive function #include<stdio.h> #include<conio.h> #include<process.h> int fact(int); void main() { int n,f; clrscr(); printf("\nEnter the number :"); scanf("%d",&n); f = fact(n); printf("\nThe factorial of %d is %d",n,f); getch(); } int fact( int num) { static int fa; if(num>0) fa= num*fact(num-1); else return 1; return fa; }

OUTPUT: Enter the number :6 The factorial of 6 is 720

//Implementation of stack using arrays #include<stdio.h> #include<conio.h> int stack[50]; int top=-1,n; void push(int); void pop(); void traverse(); void main() { int c,x,i,ch; clrscr(); printf("\nEnter the maximum limit of the stack:"); scanf("%d",&n); for(i=0;i<n;i++) stack[i]=0; while(1) { printf("\nEnter your choice: "); printf("\nEnter 1. to push elements in the stack"); printf("\nEnter 2. to pop the elements out of the stack"); printf("\nEnter 3. to traverse the stack"); scanf("%d",&c); switch(c) { case 1: printf("\nEnter the number to be added in the stack:");

scanf("%d",&x); push(x); break; case 2: pop(); break;

case 3: traverse(); } printf("\nDo you wish to continue? Enter 1 for Yes, 2 for No"); scanf("%d",&ch); if(ch==2) break; } getch(); } void push(int x) { if(top<n) { top++; stack[top]=x; } else printf("\nThe stack is full!"); } void pop()

{ int x; if(top==-1) printf("\nStack is empty!"); else { x = stack[top]; top--; printf("\nThe number popped out of the stack is:%d",x); } } void traverse() { int if(top==-1) printf("\nStack is empty!"); else { printf("\nThe elements of the stack are:\n"); for(i=0;i<=top;i++) printf("%d ",stack[i]); } }

OUTPUT: Enter the maximum limit of the stack:5

Enter your choice: Enter 1. to push elements in the stack Enter 2. to pop the elements out of the stack Enter 3. to traverse the stack 1 Enter the number to be added in the stack:43 Do you wish to continue? Enter 1 for Yes. 2 for No1 Enter your choice: Enter 1. to push elements in the stack Enter 2. to pop the elements out of the stack Enter 3. to traverse the stack 1 Enter the number to be added in the stack:7 Do you wish to continue? Enter 1 for Yes. 2 for No1 Enter your choice: Enter 1. to push elements in the stack Enter 2. to pop the elements out of the stack Enter 3. to traverse the stack 3 The elements of the stack are: 437 Do you wish to continue? Enter 1 for Yes. 2 for No2

//Implementation of stack using linked list #include<stdio.h> #include<conio.h> struct node { int data; struct node *link; } ;

struct node *top; void push(int); void pop(); void display(); void main() { int c,xch; clrscr(); top = NULL; while(1) { printf("\nEnter your choice:"); printf("\nTo add new elements, enter 1."); printf("\nTo delete elements, enter 2."); printf("\nTo traverse the stack, enter 3."); scanf("%d",&c); switch(c) {

case 1: printf("\nEnter the number to be added to the list:"); scanf("%d",&x); push(x); break; case 2: pop(); break; case 3: display(); } printf("\nDo you wish to continue? Enter 1 for Yes, 2 for No"); scanf("%d",&ch); if(ch==2) break; } getch(); }

void push(int x) { struct node *num; num=malloc(sizeof(struct node)); num->data = x; num->link = top; top = num; } void pop() {

int a; if(top==NULL) printf("\nStack is empty!"); else { a=top->data; printf("The value returned is %d ",a); free(top); top=top->link; } }

void display() { int i =0; struct node * temp; temp = top; while(temp!=NULL) { printf("\nItem No. %d : Data %d Link %d ",i++,temp>data,temp->link); temp=temp->link; } }

OUTPUT: Enter your choice:

To add new elements, enter 1. To delete elements, enter 2. To traverse the stack, enter 3.1 Enter the number to be added to the list:5 Do you wish to continue? Enter 1 for Yes, 2 for No1 Enter your choice: To add new elements, enter 1. To delete elements, enter 2. To traverse the stack, enter 3.1 Enter the number to be added to the list:6 Do you wish to continue? Enter 1 for Yes, 2 for No1 Enter your choice: To add new elements, enter 1. To delete elements, enter 2. To traverse the stack, enter 3.3 Item No. 0 : Data 6 Link 2124 Item No. 0 : Data 5 Link 0 Do you wish to continue? Enter 1 for Yes, 2 for No2

//Implementation of queue using arrays #include <stdio.h> #include<conio.h> #include<process.h> int q[50]; int front=0, rear=0,num; void add(int); void del(); void display(); void main() { int c,x,ch,i; clrscr(); printf("\nEnter the number of elements of the queue:"); scanf("%d",&num); for(i=0;i<num;i++) q[i]=0; while(1) { printf("\nEnter your choice for Queue Implementation"); printf("\nEnter 1. to add element to the queue."); printf("\nEnter 2. to delete element from the queue."); printf("\nEnter 3. to traverse the queue."); scanf("%d",&c); switch(c) {

case 1: printf("\nEnter the number to be added to the queue:"); scanf("%d",&x); add(x); break; case 2: del(); break; case 3: display(); } printf("\nDo you wish to continue? Enter 1 for Yes, 2 for No"); scanf("%d",&ch); if(ch==2) break; } getch(); }

void add(int x) {

if(rear>num) printf("Queue is full!"); else { q[rear]=x; rear++;

} }

void del() { int x,i; if(front == rear) printf("Queue is empty!"); else { x=q[front]; printf("\nThe number deleted from the queue is:%d",x); for(i=0;i<rear;i++) q[i]= q[i+1]; rear--;

} } void display() { int i; if(front==rear) printf("\nQueue is empty!"); else { printf("\nThe elements of the queue are:\n");

for(i=0;i<rear;i++) printf("%d ",q[i]); } }

OUTPUT: Enter the number of elements of the queue:4

Enter your choice for Queue Implementation Enter 1. to add element to the queue. Enter 2. to delete element from the queue. Enter 3. to traverse the queue.1 Enter the number to be added to the queue:5

Do you wish to continue? Enter 1 for Yes, 2 for No.1

Enter your choice for Queue Implementation Enter 1. to add element to the queue. Enter 2. to delete element from the queue. Enter 3. to traverse the queue.1

Enter the number to be added to the queue:6

Do you wish to continue? Enter 1 for Yes, 2 for No.1

Enter your choice for Queue Implementation

Enter 1. to add element to the queue. Enter 2. to delete element from the queue. Enter 3. to traverse the queue.2 The number deleted from the queue is:5

Do you wish to continue? Enter 1 for Yes, 2 for No.2

//Implementation of circular queue using arrays #include<stdio.h> #include<conio.h> #include<process.h> int cq[50]; int front=0,rear=0,num; void add(int); void del(); void display(); void main() { int x,ch,c; clrscr(); printf("\nEnter the maximum size of the queue:"); scanf("%d",&num); while(1) { printf("\nEnter your choice for Queue Implementation"); printf("\nEnter 1. to add element to the queue."); printf("\nEnter 2. to delete element from the queue."); printf("\nEnter 3. to traverse the queue."); scanf("%d",&c); switch(c) { case 1: printf("\nEnter the number to be added to the queue:"); scanf("%d",&x);

add(x); break; case 2: del(); break; case 3: display(); } printf("\nDo you wish to continue? Enter 1 for Yes, 2 for No."); scanf("%d",&ch); if(ch==2) break; } getch(); }

void add(int x) { rear++; rear= (rear%num); if(((rear == num)&&(front==0)) || (rear== front-1)) printf("\nCircular Queue is full!"); else cq[rear]=x; }

void del() {

int a; if(front == rear) printf("Circular Queue is empty."); else { a=cq[front]; printf("\nThe number deleted is %d",a); front++; front = front%num;

} } void display() { int i; if(front==rear) printf("\nCircular Queue is empty."); else if(front<rear) { for(i=front;i<rear;i++) printf("%d " , cq[i]); } else { for(i=front;i<num;i++) printf("%d ", cq[i]);

for(i=0;i<rear;i++) printf("%d ", cq[i]);

} }

//Implementation of queue using linked list #include<stdio.h> #include<conio.h> struct node { int data; struct node *link; }; struct node *front, *rear; void add(int); void del(); void display(); void main() { int c,x,ch; clrscr(); front = rear = NULL; while(1) { printf("\nEnter your choice:"); printf("\nTo add new elements, enter 1."); printf("\nTo delete elements, enter 2."); printf("\nTo traverse the queue, enter 3."); printf("\nTo exit, enter 4."); scanf("%d",&c); switch(c)

{ case 1: printf("\nEnter the number to be added to the list:"); scanf("%d",&x); add(x); break; case 2: del(); break; case 3: display(); break; case 4: exit(0) } printf("\nDo you wish to continue? Enter Y/N"); scanf("%d",&ch); } getch(); } void add(int x) { struct node *ptr; ptr=(struct node *)malloc(sizeof(struct node)); ptr->data=x; ptr->link=NULL; if(front ==NULL) { front = rear= ptr; }

else { rear->link=ptr; rear=ptr; } }

void del() { int num; if(front==NULL) printf("Queue is empty."); else { num=front->data; front = front->link; printf("The deleted number is: %d",num); } } void display() { int i; struct node *temp; if(front==NULL) printf("Queue is empty."); else

{ temp=front; while(temp!= NULL) { printf(%d ", temp->data); temp=temp->link; } } free(temp); } } OUTPUT:

Enter your choice: To add new elements, enter 1. To delete elements, enter 2. To traverse the queue, enter 3.1

Enter the number to be added to the list:34

Do you wish to continue? Enter Y/N1

Enter your choice: To add new elements, enter 1. To delete elements, enter 2.

To traverse the queue, enter 3.1

Enter the number to be added to the list:56

Do you wish to continue? Enter Y/N1

Enter your choice: To add new elements, enter 1. To delete elements, enter 2. To traverse the queue, enter 3.3 34 56 Do you wish to continue? Enter Y/N1

Enter your choice: To add new elements, enter 1. To delete elements, enter 2. To traverse the queue, enter 3.2 The number deleted is: 34

Do you wish to continue? Enter Y/N2

//Sequential Search #include<stdio.h> #include<conio.h> void main() { int n,a[20],i,search,f=1,h; clrscr(); printf("\nEnter the number of elements of the array:"); scanf("%d",&n); printf("\nEnter the elements of the array: \n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nThe array is: \n"); for(i=0;i<n;i++) printf("%d ",a[i]); printf("\nEnter the number to be searched: "); scanf("%d",&search); for(i=0;i<n;i++) { if(a[i]==search) { f=0; h=i+1; break; } }

if(f==0) printf("\nSearch successful! The number %d is at position %d .",search,h ); else printf("\nSearch not successful."); getch(); }

OUTPUT:

Enter the number of elements of the array:5

Enter the elements of the array: 1 6 3 12 7

The array is: 1 6 3 12 7 Enter the number to be searched: 5

Search not successful.

//Binary Search #include<stdio.h> #include<conio.h> void main() { int n,a[20],i,j,temp,search,l,u,mid,f=1; clrscr(); printf("\nEnter the number of elements of the array:"); scanf("%d",&n); printf("\nEnter the elements of the array: \n"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } printf("\nThe sorted array is: \n"); for(i=0;i<n;i++) printf("%d ",a[i]);

printf("\nEnter the number to be searched: "); scanf("%d",&search);

l=0; u=n-1; while(l<u) { mid=(l+u)/2; if(a[mid]==search) { f=0; break; } else if(a[mid]>search) u=mid-1; else l=mid+1; } if(f==0) printf("\nSearch successful! The number %d is at position %d .",search, mid+1); else printf("\nSearch not successful."); getch(); }

OUTPUT: Enter the number of elements of the array:5

Enter the elements of the array: 34 6 23 8 9

The sorted array is: 6 8 9 23 34 Enter the number to be searched: 9

Search successful! The number 9 is at position 3 .

//Bubble Sort #include<stdio.h> #include<conio.h> void main() { int n,i,a[20],l,j,temp; clrscr(); printf("\nEnter the number of elements of the array : "); scanf("%d",&n); printf("\nEnter the elements of the array: "); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nThe array is: \n"); for(i=0;i<n;i++) printf("%d",a[i]); l=n; for(i=0;i<n-1;i++) { for(j=0;j<l;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]= a[j+1]; a[j+1]=temp; }

} l--; } printf("\nThe sorted array using Bubble Sort is: \n"); for(i=0;i<n;i++) printf("%d", a[i]); getch(); } OUTPUT: Enter the number of elements of the array : 6

Enter the elements of the array: 4 23 78 9 90 12

The array is: 4 23 78 9 90 12 The sorted array using Bubble Sort is: 3 4 9 12 23 78

//Quick Sort #include<stdio.h> #include<conio.h> int a[20]; void q_sort(int [],int,int); void main() { int n,left,right,i; clrscr(); printf("\nEnter the number of elements of the array : "); scanf("%d",&n); printf("\nEnter the elements of the array: "); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nThe array is: \n"); for(i=0;i<n;i++) printf(" %d ", a[i]); left=0; right=n-1; q_sort(a,left,right); printf("\nThe sorted array using Quick Sort is: \n"); for(i=0;i<n;i++) printf(" %d ", a[i]); getch(); } void q_sort(int a[],int left,int right)

{ int i,j,x,y; i= left; j= right; x = a[(left+right)/2]; do { while((a[i]<x) && (i<right)) i++; while((x<a[j]) && (j>left)) j--; if(i<=j) { y = a[i]; a[i]=a[j]; a[j]=y; i++; j--; } }while(i<=j); if(left<j) q_sort(a,left,j); if(i<right) q_sort(a,i,right); }

OUTPUT: Enter the number of elements of the array : 6

Enter the elements of the array: 12 2 67 83 11 89

The array is: 12 2 67 83 11 89 The sorted array using Quick Sort is: 2 11 12 67 83 89

//Selection Sort #include<stdio.h> #include<conio.h> int a[20]; void swap(int,int); void main() { int i,n,j,h,min; clrscr(); printf("\nEnter the number of elements of the array : "); scanf("%d",&n); printf("\nEnter the elements of the array: "); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nThe array is:\n"); for(i=0;i<n;i++) printf(" %d ", a[i]); for(i=0;i<n-1;i++) { min=a[i]; for(j=i+1;j<n;j++) { if(a[j]<min) h=j; } swap(i,h);

} printf("\nThe sorted array using Selection Sort is: \n"); for(i=0;i<n;i++) printf(" %d ", a[i]); getch(); } void swap(int i,int h) { int temp; temp=a[i]; a[i]= a[h]; a[h]=temp; }

OUTPUT: Enter the number of elements of the array : 6

Enter the elements of the array: 4 23 43 91 37 2

The array is: 4 23 43 91 37 2

The sorted array using Bubble Sort is: 2 4 23 37 43 91

//Insertion Sort #include<stdio.h> #include<conio.h> int a[20]; void main() { int n,i,j,p,temp; clrscr(); printf("\nEnter the number of elements of the array : "); scanf("%d",&n); printf("\nEnter the elements of the array: "); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nThe array is: \n"); for(i=0;i<n;i++) printf(" %d ",a[i]); for(p=1;p<n;p++) { j=p; temp=a[p]; while(temp<a[j-1]) { a[j]=a[j-1]; j--; } a[j]=temp;

} printf("\nThe sorted aaray after Insertion Sort is:\n"); for(i=0;i<n;i++) printf(" %d ",a[i]); getch(); }

OUTPUT: Enter the number of elements of the array : 5

Enter the elements of the array: 19 23 6 51 45

The array is: 19 23 6 51 45 The sorted aaray after Insertion Sort is: 6 19 23 45 51

//Merge Sort #include<stdio.h> #include<conio.h> void part(int [],int,int); void sort(int [], int,int,int); void main() { int i,n,a[20]; clrscr(); printf("\nEnter the number of elements of the array : "); scanf("%d",&n); printf("\nEnter the elements of the array: "); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nThe array is:"\n); for(i=0;i<n;i++) printf(" %d ",a[i]); part(a,0,n-1); printf("\nThe array after Merge Sort is:\n"); for(i=0;i<n;i++) printf(" %d ", a[i]); getch(); } void part(int a[],int low,int high) { int mid;

if(low<high) { mid=(low+high)/2; part(a,low,mid); part(a,mid+1,high); sort(arr,low,mid,high); } } void sort(int arr[],int low,int mid,int high) { int i,j,k,l,b[20]; l=low; i=low; j=mid+1; while((l<=mid)&&(j<=high)) { if(arr[l]<=arr[j]) { b[i]=arr[l]; l++; } else { b[i]=arr[j]; j++; }

i++; } if(l>mid) for(k=j;k<=high;k++) { b[i]=arr[k]; i++; } else for(k=l;k<=mid;k++) { b[i]=arr[k]; i++; } for(k=low;k<=high;k++) arr[k]=b[k]; } OUTPUT: Enter the number of elements of the array : 6

Enter the elements of the array: 2 24 65 33 11 49

The array is: 2 24 65 33 11 49 The array after Merge Sort is: 2 11 24 33 49 65

// Implement inorder, preorder and postorder traversal of tree #include <stdio.h> #include <stdlib.h> #include<conio.h> struct tree { int data; struct node *left,*right; }; struct tree *insert(struct tree *p,int n) { static struct tree *temp1,*temp2; if(p==NULL) { p = (struct node*)malloc(sizeof(struct tree)); p->data = n; p->left = p->right = NULL; } else { temp1 = p; while(temp1 != NULL) { temp2 = temp1; if(n < temp1->data) temp1 = temp1->left; else temp1 = temp1->right; } if(temp2->data > n) { temp2->left = (struct tree*)malloc(sizeof(struct tree)); temp2 = temp2->left; temp2->data = n; temp2->left = temp2->right = NULL; } else { temp2->right = (struct node*)malloc(sizeof(struct tree)); temp2 = temp2->right; temp2->data = n; temp2->left = temp2->right= NULL;

} }

return p; } void inorder(struct tree *p) { if(p != NULL) { inorder(p->left); printf("%d ",p->data); inorder(p->right); } } void preorder(struct tree *p) { if(p != NULL) { printf("%d ",p->data); preorder(p->left); preorder(p->right); } } void postorder(struct tree *p) { if(p != NULL) { postorder(p->left); postorder(p->right); printf("%d ",p->data); } } void main() { int x,y,i; struct tree *root; root = NULL; printf("Enter the no. of nodes in the tree\n"); scanf("%d",&x); while(x > 0) { printf("Enter the data part of the node\n"); scanf("%d",&y); root = insert(root,y); }

printf("\t\tEnter the traversal:\n"); printf("1.Inorder.\n2.Preorder.\n3.Postorder.\n"); scanf("%d",&i); switch(i) { case 1: printf("The inorder traversal is:\n"); inorder(root); break; case 2: printf("The preorder traversal is:\n"); preorder(root); break; case 3: printf("The postorder traversal is:\n"); postorder(root); } getch(); }

// Implement BFS & DFS of a weighted graph of atleast 7 nodes and 13 edges #include <stdio.h> int q[20], top=-1, front=-1, rear=-1, a[20][20], vis[20], stack[20]; void add(int item) { if(rear == 19) printf("QUEUE FULL"); else if(rear == -1) { q[++rear] = item; front++; } else q[++rear] = item; } int del() { int k; if((front > rear) || (front == -1)) return(0); else { k=q[front++]; return k; } } void push(int item) { if(top == 19) printf("Stack overflow "); else stack[++top]=item; } int pop() { int k;

if(top == -1) return(0);

else { k=stack[top--]; return k; } void bfs(int s, int n) { int p,i; add(s); vis[s]=1; p=del(); if(p!=0) printf(" %d",p); while(p!=0) { for(i=1;i<=n;i++) if((a[p][i]!=0)&&(vis[i]==0)) { add(i); vis[i]=1; } p=del(); if(p!=0) printf(" %d ",p); for(i=1;i<=n;i++) if(vis[i]==0) bfs(i,n); } void dfs(int s,int n) { int i,k; push(s); vis[s] = 1;

k = pop(); if(k != 0) printf(" %d ",k); while(k != 0) { for(i=1;i<=n;i++) if((a[k][i] != 0)&&(vis[i] == 0)) { push(i); vis[i]=1; } k=pop(); if(k != 0) printf(" %d ",k); } for(i=1;i<=n;i++) if(vis[i]==0) dfs(i,n); } int main() { int n,i,s,ch,j; char c,dummy; printf("ENTER THE NUMBER VERTICES "); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf("ENTER 1 IF %d HAS A NODE WITH %d ELSE 0 ",i,j); scanf("%d",&a[i][j]); } } printf("THE ADJACENCY MATRIX IS\n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) {

printf(" %d",a[i][j]); } printf("\n"); } do {


for(i=1;i<=n;i++) vis[i]=0; printf("\nMENU"); printf("\n1.B.F.S"); printf("\n2.D.F.S"); printf("\nENTER YOUR CHOICE"); scanf("%d",&ch); printf("ENTER THE SOURCE VERTEX :"); scanf("%d",&s);

switch(ch) { case 1: bfs(s,n); break; case 2:dfs(s,n); }


printf("DO U WANT TO CONTINUE(Y/N) ? "); scanf("%c",&c); } while((c == 'y')||(c == 'Y')); return 0;

You might also like