Successfully reported this slideshow.
Upcoming SlideShare
×

# Data structure new lab manual

1,764 views

Published on

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

### Data structure new lab manual

1. 1. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath EXPERIMENT NO-1 a) WRITE A C PROGRAM FOR INSERT AN ELEMENT AT DESIRED POSITION IN AN ARRAY. Program: #include<stdio.h> #include<conio.h> void main() { int array[100], position, i, n, value; clrscr(); printf("Enter number of elements in arrayn"); scanf("%d", &n); printf("Enter %d elementsn", n); for (i = 0; i< n; i++) scanf("%d", &array[i]); printf("Enter the location where you wish to insert an elementn"); scanf("%d", &position); printf("Enter the value to insertn"); scanf("%d", &value); for (i = n - 1; i >= position - 1; i--) array[i+1] = array[i]; array[position-1] = value; printf("Resultant array isn"); for (i = 0; i <= n; i++) printf("%dn", array[i]); getch(); } b) WRITE A C PROGRAM FOR DELETE AN ELEMENT AT DESIRED POSITION IN AN ARRAY. Program: #include<stdio.h> #include<conio.h> void main() { int a[100], pos, i, n, item; clrscr(); printf("Enter number of elements in arrayn"); scanf("%d", &n); printf("Enter %d elementsn", n); for (i = 0; i< n; i++)
2. 2. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath scanf("%d", &a[i]); printf("Enter the location where you wish to delete an elementn"); scanf("%d", &pos); for (i = pos-1;i<n;i++) a[i] = a[i+1]; printf("Resultant array isn"); for (i = 0; i <n-1; i++) printf("%dn", a[i]); getch(); } c) WRITE A C PROGRAM FOR SEARCHING AN ELEMENT IN AN ARRAY. Program: #include<stdio.h> #include<conio.h> void main() { int a[10],i,item,n; printf("Enter The Rangen"); scanf("%d",&n); printf("Enter The Datan"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter The Item To Be Searchn"); scanf("%d",&item); for(i=0;i<n;i++) { if(a[i]==item) { printf("Item Present in Position:%dt",i+1); } } getch(); }
3. 3. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath EXPERIMENT NO-2 WRITE A C PROGRAM TO GENERATE SPARSE MATRIX Program: #include<stdio.h> #include<conio.h> void main() { int A[10][10],B[10][3],m,n,s=0,i,j; clrscr(); printf("nEnter the order m x n of the sparse matrixn"); scanf("%d%d",&m,&n); printf("nEnter the elements in the sparse matrix(mostly zeroes)n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("n%d row and %d column: ",i,j); scanf("%d",&A[i][j]); } } printf("The given matrix is:n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%d ",A[i][j]); } printf("n"); } for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(A[i][j]!=0) { B[s][0]=i; B[s][1]=j; B[s][2]=A[i][j]; s++; } }
4. 4. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath } printf("nThe sparse matrix is given by"); printf("n"); for(i=0;i<s;i++) { for(j=0;j<3;j++) { printf("%d ",B[i][j]); } printf("n"); } getch(); }
5. 5. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath EXPERIMENT NO-3 WRITE A C-PROGRAM FOR MATRIX MULTIPLICATION Program: #include<stdio.h> Void main(){ int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p; printf("nEnter the row and column of first matrix"); scanf("%d %d",&m,&n); printf("nEnter the row and column of second matrix"); scanf("%d %d",&o,&p); printf("nEnter the First matrix->"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("nEnter the Second matrix->"); for(i=0;i<o;i++) for(j=0;j<p;j++) scanf("%d",&b[i][j]); printf("nThe First matrix isn"); for(i=0;i<m;i++){ printf("n"); for(j=0;j<n;j++){ printf("%dt",a[i][j]); } } printf("nThe Second matrix isn"); for(i=0;i<o;i++){ printf("n"); for(j=0;j<p;j++){ printf("%dt",b[i][j]); } } for(i=0;i<m;i++) for(j=0;j<p;j++) c[i][j]=0; for(i=0;i<m;i++){ //row of first matrix for(j=0;j<p;j++){ //column of second matrix
6. 6. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath sum=0; for(k=0;k<n;k++) sum=sum+a[i][k]*b[k][j]; c[i][j]=sum; } } } printf("nThe multiplication of two matrix isn"); for(i=0;i<m;i++){ printf("n"); for(j=0;j<p;j++){ printf("%dt",c[i][j]); } } getch(); }
7. 7. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath Experiment 4: WRITE A C PROGRAMME TO CREATE A STACK USING AN ARRAY AND PERFORM (I) PUSH OPERATION (II) POP OPERATION Program: #include<stdio.h> #include<conio.h> #define MAX 5 void push(int[],int); void pop(int[]); void display(int[]); int top=-1; int item; void main() { int stack[MAX],ch; clrscr(); while(ch!=4) { printf("n****Menu****n"); printf("n1-INSERTIONn"); printf("n2-DELETIONn"); printf("n3-DISPLAYn"); printf("n4-EXITn"); printf("n Enter Your Choice:n"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter Your Item:"); scanf("%d",&item); push(stack,item); break; case 2: printf("Popped Item is:"); pop(stack);
8. 8. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath break; case 3: display(stack); break; case 4: exit(); } } getch(); } void push(int stack[MAX],int item) { if(top==MAX-1) { printf("Overflow"); } else { top++; stack[top]=item; } } void pop(int stack[MAX]) { int item; if(top==-1) { printf("Underflow"); } else { item=stack[top]; top--; printf("Popped Item is%d",item); } } void display(int stack[MAX]) { int i; for(i=top;i>=0;i--) { printf("%dt",stack[i]); } }
9. 9. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath Experiment 5: WRITE A C PROGRAMME TO CREATE A LINEAR QUEUE AND PERFORM (I)PUSH (II) POP (III) TRAVERSAL. Program: #include<stdio.h> #include<conio.h> #define MAX 5 void insert(int[],int); void del(int[]); void display(int[]); int f=-1,r=-1; int item; void main() { int q[MAX],ch; clrscr(); while(ch!=4) { printf("n****Menu****n"); printf("1-INSERTIONn"); printf("2-DELETIONn"); printf("3-DISPLAYn"); printf("4-EXITn"); printf("Enter Your Choice:n"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter Your Item:");
10. 10. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath scanf("%d",&item); insert(q,item); break; case 2: del(q); break; case 3: display(q); break; case 4: exit(); } } getch(); } void insert(int q[MAX],int item) { if(r==MAX-1) { printf("Overflow"); } if(f==-1||r==-1) { f=0; r=0; } else { r++; } q[r]=item; } void del(int q[MAX]) { int item; if(f==-1||r==-1) { printf("Underflow"); } item=q[f]; if(f==r) { f=-1; r=-1; } else {
11. 11. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath f++; } printf("Deleted Item is %d:",item); } void display(int q[MAX]) { int i; for(i=f;i<=r;i++) { printf("%dt",q[i]); } } Experiment 6: WRITE A C PROGRAMME TO CREATE A LINEAR QUEUE AND PERFORM (I)PUSH (II) POP (III) TRAVERSAL #include<stdio.h> #include<conio.h> #define MAX 5 void insert(int[],int); void del(int[]); void display(int[]); int f=-1,r=-1; int item; void main() { int q[MAX],ch; clrscr(); while(ch!=4) { printf("n****Menu****n"); printf("1-INSERTIONn"); printf("2-DELETIONn"); printf("3-DISPLAYn"); printf("4-EXITn"); printf("Enter Your Choice:n"); scanf("%d",&ch); switch(ch)
12. 12. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath { case 1: printf("Enter Your Item:"); scanf("%d",&item); insert(q,item); break; case 2: del(q); break; case 3: display(q); break; case 4: exit(); } } getch(); } void insert(int q[MAX],int item) { if(r==MAX-1) { printf("Overflow"); } if(f==-1||r==-1) { f=0; r=0; } else { r=(r+1)%MAX; } q[r]=item; } void del(int q[MAX]) { int item; if(f==-1||r==-1) { printf("Underflow"); } item=q[f]; if(f==r) { f=-1; r=-1;
13. 13. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath } else { f=(f+1)%MAX; } printf("Deleted Item is %d:",item); } void display(int q[MAX]) { int i; for(i=f;i<=r;i++) { printf("%dt",q[i]); } } Experiment 7: WRITE A C PROGRAMME TO CREATE A DOUBLE ENDED QUEUE AND PERFORM (I)PUSH (II) POP (III) TRAVERSAL. #include<stdio.h> #include<conio.h> #define MAX 5 void insert_r(int[],int); void insert_f(int[],int); void del_f(int[]); void del_r(int[]); void display(int[]); int f=-1,r=-1; int item; void main() { int q[MAX],ch; clrscr(); while(ch!=4) { printf("n****Menu****n"); printf("1-INSERTION AT REAR ENDn");
14. 14. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath printf("2-INSERTION AT FRONT ENDn"); printf("3-DELETION AT FRONT ENDn"); printf("4-DELETION AT REAR ENDn"); printf("5-DISPLAYn"); printf("6-EXITn"); printf("Enter Your Choice:n"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter Your Item:"); scanf("%d",&item); insert_r(q,item); break; case 2: printf("Enter Your Item:"); scanf("%d",&item); insert_f(q,item); break; case 3: del_f(q); break; case 4: del_r(q); break; case 5: display(q); break; case 6: exit(); } } getch(); } void insert_r(int q[MAX],int item) { if(r==MAX-1) { printf("Overflow"); } if(f==-1||r==-1) { f=0; r=0; } else { r++; } q[r]=item;
15. 15. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath } void insert_f(int q[MAX],int item) { if(f==0) { printf("Overflow"); } if(f==-1||r==-1) { f=0; r=0; } else { f--; } q[f]=item; } void del_f(int q[MAX]) { int item; if(f==-1||r==-1) { printf("Underflow"); } item=q[f]; if(f==r) { f=-1; r=-1; } else { f++; } printf("Deleted Item is %d:",item); } void del_r(int q[MAX]) { int item; if(f==-1||r==-1) { printf("Underflow"); }
16. 16. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath item=q[r]; if(f==r) { f=-1; r=-1; } else { r--; } printf("Deleted Item is %d:",item); } void display(int q[MAX]) { int i; for(i=f;i<=r;i++) { printf("%dt",q[i]); } } Experiment 8: WRITE A C PROGRAM SEARCH AN ELEMENT FROM LIST OF INTEGERS USING LINEAR SEARCH. Program: #include<stdio.h> #include<conio.h> void search(int[],int,int); void main() { int a[10],item,n,i; clrscr(); printf("enter the range"); scanf("%d",&n); printf("enter the data"); for(i=0;i<n;i++)
17. 17. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath { scanf("%d",&a[i]); } printf("Enter the item to be search"); scanf("%d",&item); search(a,n,item); getch(); } void search(int a[],int n,int item) { int k=0,flag=0; while(k<=n) { if(a[k]==item) { flag=1; printf("Item found at %d",k); break; } k++; } if(flag==0) { printf("Item is not found"); } } Experiment 9: WRITE A C PROGRAM SEARCH AN ELEMENT FROM LIST OF INTEGERS USING BINARY SEARCH. Program: Experiment 6: WRITE A C PROGRAM TO INFIX TO POSTFIX AND EVALUATION OF POSTFIX Program: #include<stdio.h> #include<string.h> #include<math.h>
18. 18. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath #define MAX 40 long int pop (); long int post_eval(); char infix[MAX], postfix[MAX]; long int stack[MAX]; int top; main() { top = 0; printf("Enter infix expression: "); gets(infix); infix_postfix(); printf("Postfix : %sn",postfix); printf("Value of expression is %ldn",post_eval()); }// end of main() infix_postfix() { int i,p=0,precedence; char next ; stack[top]='#'; infix[strlen(infix)]='#'; i=0; while(infix[i]!='#') { if(!space(infix[i])) { switch(infix[i]) { case '(': push(infix[i]); break; case ')': while((next = pop()) != '(') postfix[p++] = next; break; case '+': case '-': case '*': case '/': case '%': case '^': precedence = find_prec(infix[i]); while(stack[top]!='#' && precedence<= find_prec(stack[top])) postfix[p++] = pop();
19. 19. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath push(infix[i]); break; default: //if an operand comes postfix[p++] = infix[i]; }//End of switch }//End of if i++; }//End of while while(stack[top]!='#') postfix[p++] = pop(); postfix[p] = '0' ; //To make post fix array to string }//End of infix_postfix() find_prec(char symbol ) { switch(symbol) { case '(': return 0; case '+': case '-': return 1; case '*': case '/': case '%': return 2; case '^': return 3; } }//End of prec() push(long int symbol) { if(top > MAX) { printf("Stack overflow(Stack is full)n"); exit(1); } else { top=top+1; stack[top] = symbol; } }//End of push() long int pop() {
20. 20. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath if (top == -1 ) { printf("Stack underflow(stack is empty)n"); exit(1); } else return (stack[top--]); }//End of pop space(char symbol) { if( symbol == ' ' || symbol == 't' || symbol == '0') return 1; else return 0; }//End of space() long int post_eval() { long int x,y,val,result,len; int i; len=strlen(postfix); postfix[len]='#'; for(i=0;postfix[i]!='#';i++) { if(postfix[i]<='9' && postfix[i]>='0') push( postfix[i]-48 ); else { x=pop(); y=pop(); switch(postfix[i]) { case '+': val=x+y; break; case '-': val=x-y; break; case '*': val=x*y;break; case '/': val=x/y;break; case '%':
21. 21. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath val=x%y;break; case '^': val=pow(x,y); } push(val); } } return pop(); }//End of eval_post
22. 22. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath Experiment 7: WRITE A C PROGRAMME STACK IMPLEMENTATION USING LINKED LIST #include<stdio.h> #include<conio.h> #include<alloc.h> #include<stdlib.h> /* Node decleration */ struct node { int data; struct node *link; //to maintain the link other nodes }; struct node *top,*temp; void create(); void push(); void pop(); void display(); /* create function create the head node */ void create() { printf("nENTER THE FIRST ELEMENT: "); top=(struct node *)malloc(sizeof(struct node)); scanf("%d",&top->data); top->link=NULL; temp=top; } /* display function visit the linked list from top to end */ void display() { top=temp; // bring the top to top position printf("n"); while(top!=NULL) { printf("%dn",top->data); top=top->link; // Now top points the previous node in the list } } void push() { printf("nENTER THE NEXT ELEMENT: "); top=(struct node *)malloc(sizeof(struct node)); scanf("%d",&top->data);
23. 23. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath top->link=temp; temp=top; } void pop() { if(temp==NULL) { printf("nSTACK IS EMPTYn"); } else { top=temp; printf("nDELETED ELEMENT IS %dn",temp->data); temp=temp->link; free(top); } } void main() { int ch; clrscr(); while(1) { printf("nn 1.CREATE n 2.PUSH n 3.POP n 4.EXIT n"); printf("n ENTER YOUR CHOICE : "); scanf("%d",&ch); switch(ch) { case 1: create(); display(); break; case 2: push(); display(); break; case 3: pop(); display(); break; case 4: exit(0); }
24. 24. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath } Experiment 8: WRITE A C PROGRAMME TO PERFORM THE FOLLOWING OPERATION ON A SINGLE LINKED LIST (I) CREATION (II) INSERTION (III) TRAVERSE Program: #include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int info; struct node *link; }; struct node *head,*ptr,*temp; void insert_beg(); void insert_end(); void insert_any(); void display(); void main() { int item,ch,c=1; clrscr(); head->info=NULL; head->link=NULL; while(c==1) { printf("nDISPLAY MENUESn"); printf("n1.INSERT_BEGn"); printf("n2.INSERT_ENDn"); printf("n3.INSERT_ANYn"); printf("n4.DISPLAYn"); printf("nEnter Your Choicen"); scanf("%d",&ch); switch(ch) { case 1: insert_beg(); break; case 2: insert_end(); break; case 3: insert_any();
25. 25. DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath break; case 4: display(); break; } printf("nDo You Want To Continue(1/0)"); scanf("%d",&c); } getch(); } /************INSERT AT BEGING***************/ void insert_beg() { int item; printf("nEnter The Itemn"); scanf("%d",&item); temp=(struct node*)malloc(sizeof(struct node)); temp->info=item; temp->link=head; head=temp; } /************INSERT AT END***************/ void insert_end() { int item; printf("nEnter The Itemn"); scanf("%d",&item); temp=(struct node*)malloc(sizeof(struct node)); ptr=head; while(ptr->link!=NULL) { ptr=ptr->link; } temp->info=item; temp->link=ptr->link; ptr->link=temp; } /************INSERT AT ANY POSITION***************/ void insert_any() { int item,key; printf("nEnter The Itemn"); scanf("%d",&item); printf("nEnter The Key Element:n"); scanf("%d",&key);