Sie sind auf Seite 1von 52

Shri Guru Sandipani Institute of Technology & Science

Shri Guru Sandipani Institute of Technology & Science Ujjain

DATA STRUCTURES (CS-305)

LAB MANUAL

Submitted to: Prof.Akanksha Upadhyaya

Submitted by: -

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science

INDEX
S.NO
1.

OBJECTIVE Write a Program to enter 10 numbers in an Array and print their sum. Write a Program for PUSH and POP Operations in Stack Write a Program for INSERT, DELETE and TRAVERSE Operations in Queue. Write a Program for Linear Search in an Array Write a Program for Binary Search in an Array. Write a Program for Different Operations in Linked List. a).Insert b).Delete c).Traverse d).Insert at TOP e).Insert at BOTTOM f).Insert at any Position g).Delete from TOP h). Delete from BOTTOM i). Delete from any Position j).Deletion of any Particular Item. Write a Program for Bubble Sorting in an Array . Write a Program for Insertion Sorting in an Array. Write a Program for Merge Sorting in an Array. Write a Program for Quick Sorting in an Array. Write a Program for different operations in Circular Linked List Write a Program for Addition and Deletion Operation in a Circular Queue.

Date of Practical

Date of Submission

Remark

Signature

2. 3.

4. 5. 6.

7. 8. 9. 10. 11.

12.

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


S.NO
13.

OBJECTIVE Write a Program for Different Operations in Doubly Linked List. a).Insert b).Delete c).Traverse d).Insert at TOP e).Insert at BOTTOM f).Delete from TOP g). Delete from BOTTOM Write a Program that implements the Priority Queue using an Array. Write a Program that implements the De Queue using an Array. Write a Program that implements a Binary Tree using an Array.

Date of Practical

Date of Submission

Remark

Signature

14.

15.

16.

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


1. Write a Program to enter 10 numbers in an Array and print their sum.
DATE:#include<stdio.h> #include<conio.h> void main() { clrscr(); int x[10],SUM,y; printf("enter 10 numbers in an Array"); for(y=0;y<=9;y++) { scanf("%d",&x[y]); } SUM=0; for(y=0;y<=9;y++) { SUM=SUM+x[y]; } printf("SUM of 10 Numbers of Array is %d",SUM); getch(); } INPUT:- enter 10 numbers in an Array 1 2 3 4 5 6 7 8 9 10 OUTPUT: - SUM of 10 Numbers of Array is 55. Signature of Faculty

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


2. Write a Program for PUSH and POP Operations in Stack.
DATE:-

#include<stdio.h> #include<conio.h> #define MAXSIZE 5 void push(); void pop(); void traverse(); int stack[MAXSIZE]; int Top=-1; void main() { int c; clrscr(); do { printf("\n\n ----------------Stack through Static Allocation-------------"); printf("\n1. Push "); printf("\n2. Pop "); printf("\n3. Traverse "); printf("\n4. Exit"); printf("\nEnter your choice"); scanf("%d",&c); switch(c) { case 1: push(); break; case 2: pop(); break; case 3: traverse(); break; case 4: exit(); break; default: printf("\nYou Entered Wrong Choice");

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


} }while(1); } void push() { int item; if(Top==MAXSIZE-1) printf("\nOverflow"); else { printf("\nEnter the element to be inserted"); scanf("%d",&item); Top++; stack[Top] = item; printf("\n Element Inserted"); } } void pop() { int item; if(Top<0) printf("\nUnderflow"); else { item = stack[Top]; printf("\n Deleted Element : %d",item); Top--; } } void traverse() { int i; if(Top<0) printf("\n The Stack is Empty"); else for(i=Top;i>=0;i--) printf("\n%d",stack[i]); printf("\n Element Traversed"); }

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science

INPUT For PUSH Operation ----------------Stack through Static Allocation------------1. Push 2.Pop 3.Traverse 4. Exit Enter your choice 1 OUTPUT Enter the element to be inserted 3 Element Inserted INPUT For Traverse Operation ----------------Stack through Static Allocation------------1. Push 2.Pop 3.Traverse 4. Exit Enter your choice 3 OUTPUT 3 Element Traversed INPUT For POP Operation ----------------Stack through Static Allocation------------1. Push 2.Pop 3.Traverse 4. Exit Enter your choice 2 OUTPUT Deleted Element 3 INPUT

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


For POP Operation ----------------Stack through Static Allocation------------1. Push 2.Pop 3.Traverse 4. Exit Enter your choice 4 OUTPUT:-Exit from Program.

Signature of Faculty

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science

3. Write a Program for INSERT, DELETE and TRAVERSE Operations in Queue.


DATE:-

#include<stdio.h> #include<conio.h> #define MAXSIZE 5 void qinsert(); void qdelete(); void qtraverse(); int f=-1,r=-1,c,q[5]; void main() { clrscr(); do { printf("\n\n--------------Queue through Static Allocation------------"); printf("\n 1 : Insert \n 2 : Delete \n 3 : Traverse \n 4 : Exit"); printf("\n Enter your choice : "); scanf("%d",&c); switch(c) { case 1 : qinsert(); break; case 2 : qdelete(); break; case 3 : qtraverse(); break; case 4 : return; default : printf("\n U've entered Wrong Choice."); } }while(1); } void qinsert()

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


{ if(r==(MAXSIZE-1)) printf("\nOverflow"); else { r++; printf("\n Enter Element to be inserted : "); scanf("%d",&q[r]); if(f==-1) f++; printf(\n Element Inserted); } } void qdelete() { if(f==-1) printf("\n Underflow"); else { printf("\n Deleted Element : %d",q[f]); if(f==r) f=r=-1; else f++; } } void qtraverse() { int i; if(f==-1) printf("\n Queue is Empty."); else { printf("\n Elements of queue :"); for(i=f;i<=r;i++) printf("\n %d",q[i]); } }

INPUT

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


For Insert Operation --------------Queue through Static Allocation-----------1: Insert 2: Delete 3: Traverse 4: Exit Enter your choice 1 Enter Element to be inserted: 5 OUTPUT Element Inserted INPUT For Traverse Operation --------------Queue through Static Allocation-----------1: Insert 2: Delete 3: Traverse 4: Exit Enter your choice 3 OUTPUT Elements of queue: 5 INPUT For Delete Operation --------------Queue through Static Allocation-----------1: Insert 2: Delete 3: Traverse 4: Exit Enter your choice 2 OUTPUT Deleted Element: 5 INPUT --------------Queue through Static Allocation-----------1: Insert 2: Delete 3: Traverse 4: Exit Enter your choice 4

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


OUTPUT Exit from Program Signature of Faculty

4. Write a Program for Linear Search in an Array


DATE:#include<stdio.h> #include<conio.h> void main() { clrscr(); int x[10],y,i,count=0; printf("enter 10 numbers in an Array"); for(y=0;y<=9;y++) { scanf("%d",&x[y]); } printf("enter the number to be search"); scanf("%d", &i); for(y=0;y<=9;y++) { if(x[y]==i) { printf("%d is present at location %d.\n",i,y+1); count ++; } } if(count ==0) { printf("%d is not present in array.\n",i); } else { printf("%d is present %d times in array .\n",i,count); }

getch();

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


}

INPUT:- enter 10 numbers in an Array 1 2 3 4 5 6 7 8 9 10 Enter the number to be searched 5 OUTPUT: 5 is present at location 5 5 is present 1 time in array

Signature of Faculty

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science

5. Write a Program for Binary Search in an Array.


DATE:#include<stdio.h> #include<conio.h> void main() { int c, first, last, middle, n, search, array[100]; clrscr(); printf("Enter number of elements\n"); scanf("%d",&n); printf("Enter %d integers\n", n); for ( c = 0 ; c < n ; c++ ) { scanf("%d",&array[c]); } printf("Enter value to find\n"); scanf("%d",&search); first = 0; last = n - 1; middle = (first+last)/2; while( first <= last ) { if ( array[middle] < search ) first = middle + 1; else if ( array[middle] == search ) { printf("%d found at location %d.\n", search, middle+1); break; } else

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


last = middle - 1; middle = (first + last)/2; } if ( first > last ) printf("Not found! %d is not present in the list.\n", search); getch(); } INPUT Enter Number of Elements 5 Enter 5 Integers 1 2 3 4 5 Enter value to be found 4 OUTPUT 4 found at location 4. Signature of Faculty

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science

6. Write a Program for Different Operations in Linked List. a) Insert b) Delete c)Traverse d)Insert at TOP e)Insert at BOTTOM f)Insert at any Position g) Delete from TOP h) Delete from BOTTOM i) Delete from any Position j) Deletion of any Particular Item.
DATE:#include<stdio.h> #include<conio.h> #include<malloc.h> struct node { int n; struct node *next; }*start=NULL,*temp,*ptr; typedef struct node l; void ins(); void del(); void traverse(); void instop(); void insbot(); void inspos(); void deltop();

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


void delbot(); void delpos(); void delitem(); void main() { int item,i,c; clrscr(); do { printf("\n ---------------------Linked List--------------------------\n"); printf("\n 1 : Insertion at Top "); printf("\n 2 : Insertion at Bottom "); printf("\n 3 : Insertion At any position "); printf("\n 4 : Deletion at Top "); printf("\n 5 : Deletion at Bottom "); printf("\n 6 : Deletion from a Particular position"); printf("\n 7 : Deletion of a Particular element"); printf("\n 8 : Traverse "); printf("\n 9 : Exit "); printf("\n Enter ur choice : "); scanf("%d",&c); switch(c) { case 1: ins(); instop(); break; case 2: insbot(); break; case 3: inspos(); break; case 4: deltop(); break; case 5: delbot(); break; case 6: delpos(); break; case 7: delitem(); break; case 8: traverse(); break; case 9: exit(); getch();

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


default:printf("\n U've entered wrong choice."); } }while(1); } void ins() { temp=(l *)malloc(sizeof(l)); if(temp==NULL) printf("\n Overflow"); else { printf("\n Enter element you want to insert :"); scanf("%d",&temp->n); } } void instop() { temp->next=start; start=temp; } void insbot() { ins(); if(start==NULL) instop(); else { ptr=start; while(ptr->next!=NULL) ptr=ptr->next; temp->next=NULL; ptr->next=temp; } } void inspos() { int i,pos; ins(); printf("\n Enter position at where you want to insert :");

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


scanf("%d",&pos); if(pos==1) instop(); printf(\n Element inserted): else { ptr=start; for(i=1;i<pos-1;i++) { ptr=ptr->next; if(ptr==NULL) { printf("\n Element can't b inserted at given position"); return; } } temp->next=ptr->next; ptr->next=temp; } } void traverse() { if(start==NULL) printf("\n Linked List is empty"); else { temp=start; printf("\n Elements of Linked List :\n"); while(temp!=NULL) { printf("\n %d",temp->n); temp=temp->next; } } } void del() { if(start==NULL) printf("\n Underflow"); ptr=start; }

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


void deltop() { del(); if(start!=NULL) { start=start->next; printf("\n Deleted element : %d",ptr->n); free(ptr); } } void delbot() { l *loc; del(); if(start!=NULL) { while(ptr->next!=NULL) { loc=ptr; ptr=ptr->next; } printf("\n Deleted element : %d",ptr->n); free(ptr); loc->next=NULL; } } void delitem() { l *loc; int item,f=1; del(); if(start!=NULL) { printf("\n Enter element which u want 2 delete :"); scanf("%d",&item); while(ptr!=NULL) { if(ptr->n==item) {

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


if(ptr->n==start->n) { start=start->next; return; } loc->next=ptr->next; free(ptr); f--; return; } else { loc=ptr; ptr=ptr->next; } } if(f) printf("\n Given element is not in Linked List"); } } void delpos() { int i,pos; del(); if(start!=NULL) { printf("\n Enter position from where u want 2 delete :"); scanf("%d",&pos); if(pos==1) deltop(); else { for(i=1;i<pos;i++) { if(i==pos) delbot(); temp=ptr; ptr=ptr->next; if(ptr==NULL) { printf("\n Element can't b deleted from given position"); return; }

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


} temp->next=ptr->next; printf("\n Deleted element : %d",ptr->n); free(ptr); } }

INPUT For Insertion at TOP ..Linked List. 1. Insertion at Top 2. Insertion at Bottom 3. Insertion at any Position 4. Deletion at Top 5. Deletion at Bottom 6. Deletion from a Particular Position 7. Deletion of a Particular Element 8. Traverse 9. Exit Enter your choice 1 Enter element you want to insert 5 OUTPUT Element Inserted at Top.

INPUT For Insertion at BOTTOM ..Linked List. 1. Insertion at Top 2. Insertion at Bottom 3. Insertion at any Position 4. Deletion at Top 5. Deletion at Bottom 6. Deletion from a Particular Position 7. Deletion of a Particular Element

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


8. Traverse 9. Exit Enter your choice 2 Enter element you want to insert 7 OUTPUT Element Inserted at Bottom.

INPUT For Insertion at any Position ..Linked List. 1. Insertion at Top 2. Insertion at Bottom 3. Insertion at any Position 4. Deletion at Top 5. Deletion at Bottom 6. Deletion from a Particular Position 7. Deletion of a Particular Element 8. Traverse 9. Exit Enter your choice 3 Enter element you want to insert 9 Enter the Position where you want to insert the Element 2 OUTPUT Element Inserted at 2nd Position INPUT For Deletion at TOP ..Linked List. 1. Insertion at Top 2. Insertion at Bottom 3. Insertion at any Position 4. Deletion at Top 5. Deletion at Bottom 6. Deletion from a Particular Position

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


7. Deletion of a Particular Element 8. Traverse 9. Exit Enter your choice 4 OUTPUT Delete Element 5

INPUT For Deletion at Bottom ..Linked List. 1. Insertion at Top 2. Insertion at Bottom 3. Insertion at any Position 4. Deletion at Top 5. Deletion at Bottom 6. Deletion from a Particular Position 7. Deletion of a Particular Element 8. Traverse 9. Exit Enter your choice 5

OUTPUT Deleted Element 3

INPUT For Deletion from a Particular Position ..Linked List. 1. Insertion at Top 2. Insertion at Bottom 3. Insertion at any Position 4. Deletion at Top 5. Deletion at Bottom 6. Deletion from a Particular Position 7. Deletion of a Particular Element

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


8. Traverse 9. Exit Enter your choice 6 Enter Position from where you want to delete 2 OUTPUT Deleted Element 23

INPUT For Deletion of a Particular Element ..Linked List. 1. Insertion at Top 2. Insertion at Bottom 3. Insertion at any Position 4. Deletion at Top 5. Deletion at Bottom 6. Deletion from a Particular Position 7. Deletion of a Particular Element 8. Traverse 9. Exit Enter your choice 7 Enter element which you want to delete 9 OUTPUT Element Deleted INPUT For Traversing ..Linked List. 1. Insertion at Top 2. Insertion at Bottom 3. Insertion at any Position 4. Deletion at Top 5. Deletion at Bottom

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


6. Deletion from a Particular Position 7. Deletion of a Particular Element 8. Traverse 9. Exit Enter your choice 8 OUTPUT Element of Linked List are 3 23 45 67

INPUT For Exit ..Linked List. 1. Insertion at Top 2. Insertion at Bottom 3. Insertion at any Position 4. Deletion at Top 5. Deletion at Bottom 6. Deletion from a Particular Position 7. Deletion of a Particular Element 8. Traverse 9. Exit Enter your choice 9 OUTPUT Exit from Program.

Signature of Faculty

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


7. Write a Program for Bubble Sorting in an Array
#include <stdio.h> #include<conio.h> int main() { int array[100], n, c, d, swap; clrscr(); printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); for (c = 0 ; c < ( n - 1 ); c++) { for (d = 0 ; d < n - c - 1; d++) { if (array[d] > array[d+1]) /* For decreasing order use < */ { swap = array[d]; array[d] = array[d+1]; array[d+1] = swap; } } } printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); getch();

DATE:-

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


8. Write a Program for Insertion Sorting in an Array.
DATE:#include<stdio.h> #include<conio.h> void main() { int a[20],n,t,i,j; printf("\nEnter the no. of elements"); scanf("%d",&n); printf("\nEnter the elements of array"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=1;i<n;i++) { t=a[i]; j=i-1; while(t<a[j]&&j>=0) { a[j+1]=a[j]; j=j-1; } a[j+1]=t; } printf("Ascending order of sorted list is:"); for(i=0;i<n;i++) printf("\n%d",a[i]); getch(); }

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science

9. Write a Program for Merge Sorting in an Array.


DATE:#include<stdio.h> #include<conio.h> void merge(int a[],int lb,int mid,int ub) { int i,j,k,b[10]; i=lb; j=mid+1; k=lb; while(i<=mid && j<=ub) { if(a[i]<a[j]) { b[k]=a[i]; i++; } else if(a[i]>a[j]) { b[k]=a[j]; j++; } else{ b[k++]=a[i]; b[k]=a[j]; i++; j++; } k++; } while(i<=mid) { b[k]=a[i]; k++; i++; } while(j<=ub) { b[k]=a[j];

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


j++; k++; } k=lb; while(k<=ub) { a[k]=b[k]; k++; } return; } void divide(int a[],int l,int u) { int m=(l+u)/2; if(l<u) { divide(a,l,m); divide(a,m+1,u); } merge(a,l,m,u); return; } void main() { int a[10],i,no; clrscr(); printf("\n\n Enter no of elements.."); scanf("%d",&no); printf("\n\n Enter the nos.."); for(i=0;i<no;i++) scanf("%d",&a[i]); divide(a,0,no); printf("\n\n Sorted array is.."); for(i=0;i<no;i++) printf("%d\t",a[i]); getch(); }

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


10. Write a Program for Quick Sorting in an Array.
DATE:#include<stdio.h> #include<conio.h> int a[15]; void swap(int x,int y) { int temp; temp=a[x]; a[x]=a[y]; a[y]=temp; } void quick(int low,int high) { int pi,i,j,mov_count=0,cmp_count=0; if(low>high) { return; } i=low+1; j=high; pi=a[low]; while(i<=j) { while((a[i]<=pi) && (i<=high)) { i++; cmp_count++; } cmp_count++; while((a[j]>pi) && (j>=low)) { j--; cmp_count++; } cmp_count++; if(i<j) { swap(i,j); mov_count++; } }

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


if(low<j) { swap(low,j); mov_count++; } quick(low,j-1); quick(j+1,high); } void main() { int n,k; clrscr(); printf("\n\n\t Enter the size of array "); scanf("%d",&n); if(n<1 || n>14) { printf("\n\n\t You can take min. size 1 and max size 14 "); getch(); exit(); } printf("\n\n\t Enter the elements of array "); for(k=0;k<n;k++) { scanf("%d",&a[k]); } quick(0,n-1); printf("\n\n\t Sorted array is "); for(k=0;k<n;k++) { printf(" %d",a[k]); } getch(); }

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


11. Write a Program for different operations in Circular Linked List
#include<stdio.h> #include<conio.h> #include<malloc.h> struct node { int n; struct node *next; }*f=NULL,*l=NULL,*temp; void ins(); void del(); void traverse(); void instop(); void insbot(); void deltop(); void delbot(); void main() { int c; clrscr(); do { printf("\n\n ---------------------Circular Linked List--------------------------\n"); printf("\n 1 : Insertion at Top "); printf("\n 2 : Insertion at Bottom "); printf("\n 3 : Deletion at Top "); printf("\n 4 : Deletion at Bottom "); printf("\n 5 : Traverse "); printf("\n 6 : Exit "); printf("\n Enter ur choice : "); scanf("%d",&c); switch(c) { case 1 : ins(); instop(); DATE:-

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


break; case 2 : insbot(); break; case 3 : deltop(); break; case 4 : delbot(); break; case 5 : traverse(); break; case 6 : exit(); getch(); default: printf("\n U've entered wrong choice."); } }while(1); } void ins() { temp=(struct node *)malloc(sizeof(struct node)); if(temp==NULL) printf("\n Overflow"); else { printf("\n Enter element u want 2 insert :"); scanf("%d",&temp->n); if(f==NULL) { f=temp; temp->next=f; l=f; } } } void instop() { if(f!=NULL) { temp->next=f; f=temp; l->next=f; } }

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


void insbot() { ins(); if(f!=NULL) { l->next=temp; l=temp; l->next=f; } } void traverse() { struct node *p=NULL,*q=f; if(q==NULL) printf("\n Linked List is empty"); else { printf("\n Elements of Linked List :\n"); while(q!=p) { printf("\n %d",q->n); q=q->next; p=f; } } } void del() { if(f==NULL) { printf("\n Underflow"); return; } if(f==l) { printf("\n Deleted element : %d",f->n); f=l=NULL; } temp=f; } void deltop()

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


{ del(); if((f!=NULL)&&(f!=l)) { f=temp->next; l->next=f; printf("\n Deleted element : %d",temp->n); free(temp); } } void delbot() { struct node *t; t=l; del(); if((f!=NULL)&&(f!=l)) { while(temp->next!=l) temp=temp->next; l=temp; temp->next=f; printf("\n Deleted element : %d",t->n); free(t); } }

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


12. Write a Program for Addition and Deletion Operation in a Circular Queue.
#include<stdio.h> #include<conio.h> #define MAX 10 void addq(int *,int,int *,int *); int delq(int *,int *,int *); void display(int *); void main() { int arr[MAX]; int I,front,rear; clrscr(); front=rear=-1; for(i=0;i<MAX;i++) arr[i]=0; addq(arr,14,&front,&rear); addq(arr,22,&front,&rear); addq(arr,13,&front,&rear); addq(arr,-6,&front,&rear); addq(arr,25,&front,&rear); printf(\n Elements in the circular queue); display(arr); i= delq(arr,&front,&rear); printf(item deleted: %d,i); i=delq(arr,&front,&rear); printf(\n Item Deleted: %d, i); printf(\n Elements in the circular queue after deletion:); display(arr); addq(arr,21,&front,&rear); addq(arr,17,&front,&rear); addq(arr,18,&front,&rear); DATE:-

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


addq(arr,9,&front,&rear); addq(arr,20,&front,&rear); printf(Elements in the circular queue after addition); display(arr); addq(arr,32,&front,&rear); printf(Elements in the circular array after addition: ); display(arr); getch(); } void addq(int *arr,int item,int*pfront,int*prear) { if((*prear==MAX-1&& *pfront==0)|| (*prear+1= =*pfront)) { Printf(\n queue is full); return; } if (*prear= = MAX -1) *prear=0; else ( *prear)++; arr[*prear]=item; if(*pfront= = -1) *pfront=0; } int delq(int *arr,int *pfront, int *prear) { int data; if(*pfront= = -1) { printf(\n Queue is Empty); return Null; } data= arr[ *pfront]; arr[*pfront]=0; if (*pfront==*prear)

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


{ *pfront= -1; *prear= -1; } else { if (*pfront== MAX-1) *pfront=0; else (*pfront)++; } return data; } void display(int *arr) { int i; printf(\n); for(i=0;i<MAX;i++) printf(%d\t,arr[i]); printf(\n); }

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science

13. Write a Program for Different Operations in Doubly Linked List. a).Insert b).Delete c).Traverse d).Insert at TOP e).Insert at BOTTOM f).Delete from TOP g). Delete from BOTTOM
#include<stdio.h> #include<conio.h> #include<malloc.h> struct node { int n; struct node *l,*r; }*start=NULL,*temp; void ins(); void del(); void traverse(); void instop(); void insbot(); void deltop(); void delbot(); void main() { int c; clrscr(); do { printf("\n ---------------------Doubly Linked List--------------------------\n"); printf("\n 1 : Insertion at Top "); printf("\n 2 : Insertion at Bottom "); printf("\n 3 : Deletion at Top "); printf("\n 4 : Deletion at Bottom "); printf("\n 5 : Traverse "); printf("\n 6 : Exit "); DATE:-

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


printf("\n Enter ur choice : "); scanf("%d",&c); switch(c) { case 1 : ins(); instop(); break; case 2 : insbot(); break; case 3 : deltop(); break; case 4 : delbot(); break; case 5 : traverse(); break; case 6 : exit(); getch(); default : printf("\n U've entered wrong choice."); } }while(1); } void ins() { temp=(struct node *)malloc(sizeof(struct node)); if(temp==NULL) printf("\n Overflow"); else { printf("\n Enter element u want 2 insert :"); scanf("%d",&temp->n); if(start==NULL) { temp->l=NULL; temp->r=NULL; start=temp; } } } void instop() { if(start!=NULL) {

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


temp->l=NULL; temp->r=start; start->l=temp; start=temp; } } void insbot() { struct node *ptr; ptr=start; ins(); if(start!=NULL) { while(ptr->r!=NULL) ptr=ptr->r; ptr->r=temp; temp->l=ptr; temp->r=NULL; } } void traverse() { struct node *ptr; if(start==NULL) printf("\n Doubly Linked List is empty"); else { ptr=start; printf("\n Elements of Doubly Linked List :\n"); while(ptr) { printf("\n %d",ptr->n); ptr=ptr->r; } } } void del() { if(start==NULL) {

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


printf("\n Underflow"); return; } else temp=start; } void deltop() { del(); if(start!=NULL) { start=start->r; start->l=NULL; printf("\n Deleted element : %d",temp->n); free(temp); } } void delbot() { struct node *t,*ptr; del(); if(start!=NULL) { if(temp->r==NULL) start=NULL; else { while(temp->r!=NULL) { t=temp; temp=temp->r; } t->r=NULL; } printf("\n Deleted element : %d",temp->n); free(temp); } }

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science

14. Write a Program that implements the Priority Queue using an Array.
#include<stdio.h> #include<conio.h> #define MAX 5 struct data { char job[MAX]; int prno; int ord; }; struct pque { struct data d[MAX]; int front; int rear; }; void initpque(struct pque *); void add(struct pque *,struct data); struct data delete(stuct pque *); void main() { struct pque q; struct data dt,temp; int i,j=0; clrscr(); initpque(&q); printf(Enter Job Description(max 4 chars) and its priority\n); printf(lower the priority number,higher the priority\n); printf(job priority\n); for(i=0;i<MAX;i++) DATE:-

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


{ scanf(%s %d,&dt.job,&dt.prno); dt.ord=j++; add(&q,dt) ; } printf(\n); printf(process jobs prioritywise\n); printf(job\tpriority\n); for(i=0;i<MAX;i++) { temp=delete(&q); printf(%s\t%d\n,temp.job,temp.prno); } printf(\n); getch(); } void initpque(struct pque *pq) { int i; pq->front=pq->rear=-1; for(i=0;i<MAX;i++) { strcpy(pq-> d[i].job,\0); pq->d[i].prno=pq->d[i].ord=0; } } void add(struct pque *pq,struct data dt) { struct data temp; int i,j; if(pq->rear==MAX-1) { printf (\nQueue is full); return; } pq->rear ++; pq->d[pq->rear]=dt; if(pq->front= =-1) pq->front=0; for(i=pq->front;i<=pq->rear;i++)

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


{ for(j=j+1;j<=pq->rear;j++) { if( pq->d[i].prno>pq->d[j].prno) { temp=pq->d[i]; pq->d[i]=pq->d[j]; pq->d[j]=temp; } else { if(pq->d[i].prno==pq->d[j].prno) { if(pq->d[i].ord>pq->d[j].ord) { temp =pq->d[i]; pq->d[i]=pq->d[j]; pq->d[j]=temp; } } } } } } struct data delete(struct pque *pq) { struct data t; strcpy(t.job, ); t.prno=0; t.ord=0; if(pq->front==-1) { printf( \n queue is empty.\n); return t; } T=pq->d[pq->front]; Pq->d[pq->front]=t; if(pq->front==pq->rear) pq->front=pq->rear= -1; else

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


pq->front ++; return t; }

15. Write a Program that implements the De Queue using an Array.


#include<stdio.h> #include<conio.h> int q[5],front=-1,rear=-1,max=5; void print() { int i; if(front==-1) printf("QUEUE EMPTY"); else { if(front<=rear) for(i=front;i<=rear;i++) printf(" %d ",q[i]); else { for(i=0;i<=rear;i++) printf(" %d ",q[i]); for(i=front;i<max;i++) printf(" %d ",q[i]); } } } void insertrear() { if((front==rear+1)||((front==0)&&(rear==max-1))) printf("QUEUE IS FULL"); else DATE:-

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


{ if(rear==max-1) rear=0; else rear++; if(front==-1) front++;

printf("ENTER THE ELEMENT TO BE INSERTED :"); scanf("%d",&q[rear]); printf("\ QUEUE AFTER INSERTION :"); print(); } } void delbeg() { if(front==-1) printf("QUEUE EMPTY"); else { printf("ELEMENT DELETED : %d",q[front]); if(front==rear) { front=-1; rear=-1; } else if(front==max-1) front=0; else front++; printf("\ QUEUE AFTER DELETION :"); print(); } } void insertbeg() { if((front==rear+1)||((front==0)&&(rear==max-1))) printf("QUEUE IS FULL"); else {

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


if(front==-1) { front++; rear++; } else if(front==0) front=max-1; else front--;

printf("ENTER THE ELEMENT TO BE INSERTED :"); scanf("%d",&q[front]); printf("\ QUEUE AFTER INSERTION :"); print(); } } void delrear() { if(front==-1) printf("QUEUE EMPTY"); else { printf("ELEMENT DELETED : %d",q[rear]); if(front==rear) { front=-1; rear=-1; } else if(rear==0) rear=max-1; else rear--; printf("\ QUEUE AFTER DELETION :"); print(); } } void main() { int ch;

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


clrscr(); do { printf("\n1) INSERTION AT FRONT \n2)INSERTION AT REAR\n3.DELETEION AT FRONT\n4.DELETION AT REAR\n "); printf("\n ENTER CHOICE "); scanf("%d",&ch); switch(ch) { case 1:insertbeg(); break; case 2:insertrear(); break; case 3:delbeg(); break; case 4:delrear(); break; default :printf("WRONG CHOICE"); break; } }while(ch>=1&&ch<=4); getch(); }

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science

16. Write a Program that implements a Binary Tree using an Array.


#include<stdio.h> #include<conio.h> #include<alloc.h> struct node { struct node *left; char data; struct node *right; }; struct node * buildtree(int); void inorder (struct node*); char arr[ ]={A,B,C,D,E,F,G,\0,\0,H}; int lc[ ]={1,3,5,-1,9,-1,-1,-1,-1,-1}; int rc[ ]={2,4,6,-1,-1,-1,-1,-1,-1,-1}; void main( ) { struct node *root; clrscr(); root=buildtree(0); printf(In-order Traversal:\n); inorder (root); getch(); } DATE:-

Department of Computer Science & Engineering

Shri Guru Sandipani Institute of Technology & Science


struct node *buildtree(int index) { struct node *temp=NULL; if(index !=-1) { temp =(struct node *)malloc (sizeof (struct node)); temp->left=buildtree(lc[index]); temp->data=arr[index]; temp->right=buildtree(rc [index]); } return temp; } void inorder (struct node *root) { if(root != NULL) { inorder(root->left); printf(%c\t,root->data); inorder(root->right); } }

Department of Computer Science & Engineering

Das könnte Ihnen auch gefallen