Escolar Documentos
Profissional Documentos
Cultura Documentos
ECS-352
Arrays
Implementation of Arrays
#include<stdio.h> #include<conio.h> void searchno(int,int*); void merge(int*,int*); void split_arr(int*,int); int odd_elem(int*); int prime_no(int[]); int armstrong(int*); void main() { int arr[10],a[5],b[5]; int i,choice,no,pos,odd,prime,armstr,temp; int *ptr; clrscr(); do { printf("1. Search a number in an array \n"); printf("2. Merge two arrays \n"); printf("3. Split an array in two \n"); printf("4. Find the number of odd elements in an array \n"); printf("5. Find the number of prime numbers in an array \n"); printf("6. Find the number of armstrong numbers in an array \n"); printf("0. Exit \n"); scanf("%d",&choice); switch(choice) { case 0: exit(0); case 1: printf("Enter the elements in the array - max 10 \n"); for(i=0;i<10;i++) scanf("%d",&arr[i]); printf("Enter number to be searched \n"); scanf("%d",&no); searchno(no,&arr[0]); break; case 2: printf("Enter the elements in first array - max 5 \n"); for(i=0;i<5;i++) scanf("%d",&a[i]); printf("Enter the elements in second array - max 5 \n"); for(i=0;i<5;i++) scanf("%d",&b[i]);
Saumya Raghuvanshi
0806410091
Data Structures in C
Arrays
case 3: printf("Enter the elements in the array - max 10 \n"); for(i=0;i<10;i++) scanf("%d",&arr[i]); printf("Enter position from where array is to be split \n"); scanf("%d",&pos); split_arr(&arr[0],pos); break; case 4: printf("Enter the elements in the array - max 10 \n"); for(i=0;i<10;i++) scanf("%d",&arr[i]); odd = odd_elem(&arr[0]); printf("Number of odd elements = %d \n",odd); break; case 5: printf("Enter the elements in the array - max 10 \n"); for(i=0;i<10;i++) scanf("%d",&arr[i]); prime = prime_no(arr); printf("Number of prime numbers = %d \n",prime); break; case 6: printf("Enter the elements in the array - max 10 \n"); for(i=0;i<10;i++) scanf("%d",&arr[i]); armstr = armstrong(&arr[0]); printf("Number of armstrong numbers = %d \n",armstr); break; default: printf("Wrong choice entered\n"); break; } printf("\n"); } while(choice!=0); getch(); } void searchno(int n,int* ptr) { int i=0,flag=0; while(i<10) {
Saumya Raghuvanshi
0806410091
ECS-352
Arrays
} if(flag>=1) printf("number is present \n"); else printf("Number is not present \n"); } void merge(int* ptr1,int* ptr2) { int arr[10],i,j; for(i=0;i<5;i++) { arr[i]=(*ptr1); ptr1++; } for(j=5;j<10;j++) { arr[j]=(*ptr2); ptr2++; } printf("Array after merging\n"); for(i=0;i<10;i++) printf("%d ",arr[i]); } void split_arr(int* ptr, int position) { int A[10],B[10],i,j,k,l; for(i=0;i<position;i++) { A[i]=(*ptr); ptr++; } for(j=position;j<10;j++) A[j]=0; for(k=0;k<(10-i);k++) { B[k]=(*ptr); ptr++; } for(l=k;l<10;l++) B[l]=0; printf("The splitted arrays are \n"); printf("First array\n");
Saumya Raghuvanshi
0806410091
Data Structures in C for(i=0;i<10;i++) printf("%d ",A[i]); printf("Second array\n"); for(i=0;i<10;i++) printf("%d ",B[i]);
ECS-352
Arrays
} int odd_elem(int* ptr) { int i=0,count=0; while(i<10) { if((*ptr)%2!=0) count++; ptr++; i++; } return(count); } int prime_no(int arr[]) { int i=0,factor,count=0,j; while(i<10) { factor=0; for(j=1;j<=(arr[i]);j++) { if((arr[i])%j==0) factor++; } if(factor==2) count++; i++; } return(count); } int armstrong(int* ptr) { int i=0,j,temp,no,rem,sum=0,count=0; while(i<10) { sum=0; no=temp=*ptr; while(temp!=0) { rem=temp%10; sum+=rem*rem*rem;
Saumya Raghuvanshi
0806410091
ECS-352
Arrays
***
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Arrays
Output
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Arrays
*****
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Saumya Raghuvanshi
0806410091
ECS-352
} if(z==0) printf("The number is not present at any node\n"); } void displaybypos(struct node*q) { int pos,i=1,z=0; printf("Enter the postion of the node to be displayed\n"); scanf("%d",&pos); while(q!=NULL) { if(i==pos) { z=1; printf("The value at specified position is%d\n",q->data); break; } i++; q=q->link; } if(z==0) printf("There are less than %d nodes in linked list\n",pos); } void addatend(struct node*q) { struct node*temp; int num; printf("Enter the number to be added\n"); scanf("%d",&num); temp=q; if(q==NULL) { q=(struct node*)malloc(sizeof(struct node)); temp=q; } else { while(temp->link!=NULL) temp=temp->link; temp->link=(struct node*)malloc(sizeof(struct node)); temp=temp->link; } temp->data=num; temp->link=NULL; }
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
void addatdes(struct node*q) { struct node*temp; int i,num,loc; printf("Enter the number to be added and also the position where it is to be added\n"); scanf("%d%d",&num,&loc); temp=(struct node*)malloc(sizeof(struct node)); temp->data=num; for(i=1;i<(loc-1);i++) { q=q->link; if(q==NULL) printf("position of addition is more than number of nodes in linked list\n"); } temp->link=q->link; q->link=temp; } void maxvalue(struct node*q) { int m=q->data; while(q!=NULL) { if(q->data>m) m=q->data; q=q->link; } printf("Maximum value node is=%d\n",m); } void minvalue(struct node*q) { int m=q->data; while(q!=NULL) { if(q->data<m) m=q->data; q=q->link; } printf("Node with minimum value=%d\n",m); } void deleteall(struct node**q) { struct node *temp; temp=*q; *q=NULL;
Saumya Raghuvanshi
0806410091
ECS-352
} void deletebyvalue(struct node**q) { struct node*temp,*old; int num; printf("Enter value to be deleted\n"); scanf("%d",&num); temp=*q; old=*q; while(temp!=NULL) { if(temp->data==num) { if(temp==*q) { *q=temp->link; free(temp); return; } else { old->link=temp->link; free(temp); return; } } else { old=temp; temp=temp->link; } } printf("The node to be deleted is not present in linked list\n"); } void deletebypos(struct node**q) { struct node*temp,*next; int i,loc; printf("Enter the position from which node is to be deleted\n"); scanf("%d",&loc); temp=*q; for(i=1;i<(loc-1);i++) { temp=temp->link; if(temp==NULL)
Saumya Raghuvanshi
0806410091
Data Structures in C {
ECS-352
printf("Number of nodes are less than given position\n"); return; } } next=temp->link; temp->link=next->link; free(next); printf("The node has been deleted\n"); } void sumall(struct node*q) { int sum=0,z=0; while(q!=NULL) { z=1; sum=sum+q->data; q=q->link; } if(z==0) printf("No node present in the linked list\n"); else printf("Sum of the values at various nodes are=%d\n",sum); } void modifyvalue(struct node**q) { int z=0,v1,v2; struct node*temp; printf("Enter value to be modified and the new value\n"); scanf("%d%d",&v1,&v2); temp=*q; while(temp!=0) { if(temp->data==v1) { temp->data=v2; z=1; break; } temp=temp->link; } if(z==0) printf("Node having input value is not present\n"); else printf("Value has been modified\n"); }
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
void countnode(struct node*q) { int count=0; while(q!=NULL) { count++; q=q->link; } printf("Total number of nodes in linked list are %d\n",count); } void positivenode(struct node*q) { int count=0; while(q!=NULL) { if(q->data>0) { count++; q=q->link; } } printf("Total number of positive nodes in linked list are %d\n",count); } void negnode(struct node*q) { printf("Nodes with negative numbers are\n"); while(q!=0) { if(q->data<0) printf("%d",q->data); else printf("No node with negative number"); q=q->link; } } void delfirst(struct node**q) { struct node*temp,*old; temp=*q; old=*q; temp=temp->link; *q=temp; free(old); printf("First node has been deleted\n"); } void dellast(struct node**q)
Saumya Raghuvanshi
0806410091
Data Structures in C {
ECS-352
struct node*temp,*old; temp=*q; old=*q; while(temp->link!=NULL) { old=temp; temp=temp->link; } old->link=NULL; free(temp); printf("Last node of linked list has been deleted\n"); } void sortlist(struct node*q) { int count=0,i,j,a; struct node *temp,*next,*val; val=q; temp=q; next=temp->link; while(q!=NULL) { count++; =q->link; } for(i=1;i<(count);i++) { temp=val; next=val->link; for(j=1;j<=(count-i);j++) { if(temp->data>next->data) { a=temp->data; temp->data=next->data; next->data=a; } temp=temp->link; next=temp->link; } } printf("Linked list has been sorted"); } void modifypos(struct node **q) { int pos,value,i;
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
struct node *temp; printf("Enter the position to be modified and enter the new value\n"); scanf("%d%d",&pos,&value); temp=*q; for(i=1;i<=(pos-1);i++) temp=temp->link; if(temp==NULL) printf("Position out of the linked list\n"); else temp->data=value; } void main() { int choice; struct node*p; p=NULL; clrscr(); do { printf("\n"); printf("Implementation of Linear Linked List\n"); printf("Enter ur choice\n"); printf("1.Adding a node at beginning\n"); printf("2.Display all nodes\n"); printf("3.Searching a node by value\n"); printf("4.Display by position\n"); printf("5.Adding at the end\n"); printf("6.Adding a node at a particular position\n"); printf("7.Displaying node with maximum value\n"); printf("8.Displaying node with minimum value\n"); printf("9.Deleting all nodes\n"); printf("10.Deleting node by value\n"); printf("11.Deleting node by position\n"); printf("12.Sum of values of all nodes\n"); printf("13.Modify a node by value\n"); printf("14.Count number of nodes in linked list\n"); printf("15.Find total number of positive numbers in linked list\n"); printf("16.Print all negative value nodes in linked list\n"); printf("17.Deleting first node\n"); printf("18.Deleting last node\n"); printf("19.Sorting the linked list\n"); printf("20.Modify a node by position\n"); printf("0.Exit\n"); scanf("%d",&choice); switch(choice) {
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352 case 1: addatbeg(&p); break; case 2: displayall(p); break; case 3: search(p); break; case 4: displaybypos(p); break; case 5: addatend(p); break; case 6: addatdes(p); break; case 7: maxvalue(p); break; case 8: minvalue(p); break; case 9: deleteall(&p); break; case 10: deletebyvalue(&p); break; case 11: deletebypos(&p); break; case 12: sumall(p); break; case 13: modifyvalue(&p); break; case 14: countnode(p); break; case 15:positivenode(p); break; case 16: negnode(p); break; case 17: delfirst(&p); break; case 18: dellast(&p); break; case 19: sortlist(p); break; case 20: modifypos(&p); break; case 0: exit();
} } while(choice!=0); getch(); }
Saumya Raghuvanshi
0806410091
ECS-352
*****
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
splitting(&start,pos); break; case 3: int n,i; printf("\n Enter no. of nodes to be created in second linked list:"); scanf("%d",&n); for(i=1;i<=n;i++) creation(&start1); concatenate(start,start1); break; case 4: duplicate(start); break; case 5: display(start); break; case 6: printf("\n EXIT!"); break; } } while(ch!=6); getch(); } void splitting(struct node **p,int pos) { int i; struct node *temp,*q; temp=*p; for(i=1;i<pos-1;i++) temp=temp->link; q = temp->link; temp->link = NULL; printf("\n After splitting, linked list 1 is:"); display(*p); printf("\n 2nd linked list is:"); display(q); } void concatenate(struct node *p,struct node *q) { struct node *t; t=p; while(p->link!=NULL) p=p->link; p->link=q; printf("\n Concatenated linked list is:"); display(t); }
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
void duplicate(struct node *q) { struct node *p; p=q; } void creation(struct node **q) { int num; struct node *temp,*q1; temp=*q; if((*q)==NULL) { *q=(struct node *)malloc(sizeof(struct node)); temp=*q; } else { while(temp->link!=NULL) temp=temp->link; temp->link=(struct node *)malloc(sizeof(struct node)); temp=temp->link; } printf("\n Enter the number to be inserted:"); scanf("%d",&num); temp->data=num; temp->link=NULL; } void display(struct node *q) { int z=0; while(q!=NULL) { z=1; printf("\n DATA : %d",q->data); q=q->link; } if(z==0) printf("\n There is no node in the linked list"); }
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Output
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
*****
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
switch(ch) { case 0: printf("\n EXIT"); break; case 1: int n; printf("\n Enter the data to be inserted :"); scanf("%d",&n); addatbeg(&start,n); break; case 2: int n; printf("\n Enter the data to be inserted :"); scanf("%d",&n); addatend(&start,n); break; case 3: int n; printf("\n Enter the data to be inserted :"); scanf("%d",&n); addatpos(&start,n); break; case 4: dispall(start); break; case 5: int n; printf("\n Enter the value of the node to be displayed :"); scanf("%d",&n); disp_val(start,n); break; case 6: int pos; printf("\n Enter the position of the node to be displayed: "); scanf("%d",&pos); disp_pos(start,pos); break; case 7: int val; printf("\n Enter the value of the node to be modified :"); scanf("%d",&val); mod_val(start,val); break; case 8: int pos; printf("\n Enter the position of the node to be modified :"); scanf("%d",&pos);
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352 mod_pos(start,pos); break; case 9: count_all(start); break; case 10: delete_all(&start); break;
} while(ch!=0); getch(); } void addatbeg(struct node **q,int num) { struct node *temp; temp=(struct node*)malloc(sizeof(struct node)); temp->data=num; temp->prev=NULL; temp->next=*q; (*q)->prev=temp; *q=temp; } void addatend(struct node **q,int num) { struct node *temp,*r; temp=*q; if(*q==NULL) { printf("\n LIST IS EMPTY"); *q=(struct node*)malloc(sizeof(struct node)); (*q)->prev=NULL; (*q)->data=num; (*q)->next=NULL; } else { while(temp->next!=NULL) temp=temp->next; r = (struct node*)malloc(sizeof(struct node)); r->data=num; r->next=NULL; r->prev=temp; temp->next=r;
Saumya Raghuvanshi
0806410091
Data Structures in C } }
ECS-352
void addatpos(struct node **q,int num) { struct node *temp,*t; int i,pos; printf("\n Enter the position after which the node is to be inserted :"); scanf("%d",&pos); t =*q; for(i=1;i<=pos;i++) t=t->next; if(t = = NULL) printf("\n NODES ARE LESS!"); t = t->prev; temp=(struct node*)malloc(sizeof(struct node)); temp->data=num; temp->prev=t; temp->next=t->next; temp->next->prev=temp; t->next=temp; } void dispall(struct node*q) { printf("\n DISPLAY ALL NODES:"); if(q==NULL) printf("\n THERE ARE NO NODES IN THE LINKED LIST!!"); while(q!=NULL) { printf("\n DATA: %d",q->data); q=q->next; } } void disp_val(struct node *q,int val) { int z=0; while(q!=NULL) { if(q->data==val) { z=1; printf("\n DATA : %d",q->data); break; }
Saumya Raghuvanshi
0806410091
ECS-352
} if(z==0) printf("\n NODE NOT FOUND"); } void disp_pos(struct node *q,int pos) { int z=0; int i=1; while(q!=NULL) { if(i==pos) { z=1; printf("DATA : %d",q->data); break; } q=q->next; i++; } if(z==0) printf("\n NODE NOT FOUND"); } void mod_val(struct node*q,int val) { int z=0,new1; printf("\n Enter the new value :"); scanf("%d",&new1); while(q!=NULL) { if(q->data==val) { z=1; q->data=new1; printf("\n NODE MODIFIED"); break; } q=q->next; } if(z==0) printf("\n NODE NOT FOUND!"); } void mod_pos(struct node *q,int pos)
Saumya Raghuvanshi
0806410091
Data Structures in C {
ECS-352
int i=1; int z=0,new1; printf("\n Enter the new value :"); scanf("%d",&new1); while(q!=NULL) { if(i==pos) { z=1; q->data=new1; printf("\n NODE MODIFIED!"); break; } q=q->next; i++; } if(z==0) printf("\n NODE NOT FOUND"); } void count_all(struct node*q) { int c=0; while(q!=NULL) { c++; q=q->next; } printf("TOTAL NUMBER OF NODES : %d",c); } void delete_all(struct node **q) { *q=NULL; printf("\n ALL NODES DELETED!"); }
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
OUTPUT:
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
*****
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
case 2: printf("\n DISPLAY ALL NODES:"); disp(start); break; case 3: int n; printf("\n Enter the value to be searched :"); scanf("%d",&n); search(start,n); break; case 4: int pos; printf("\n Enter the position of the node to be displayed :"); scanf("%d",&pos); disp_pos(start,pos); break; case 5: int val; printf("\n Enter the value to be modified: "); scanf("%d",&val); modi_val(start,val); break; case 6: int pos; printf("\n Enter the position of the node to be modified: "); scanf("%d",&pos); modi_pos(start,pos); break; case 7: count_all(start); break; case 8: del_all(&start); break; case 9: int n; printf("\n Enter the value of the node to be deleted :"); scanf("%d",&n); del_val(&start,n); break; } } while(ch!=0); getch(); } void add(struct node **q,int n) { struct node *temp; temp=*q; if(*q==NULL) { *q=(struct node*)malloc(sizeof(struct node)); temp=*q; }
Saumya Raghuvanshi
0806410091
ECS-352
while(temp->link!=*q) temp=temp->link; temp->link=(struct node*)malloc(sizeof(struct node)); temp=temp->link; } temp->data=n; temp->link=*q; } void disp(struct node *q) { struct node *ptr=q; if(ptr==NULL) printf("\n List is empty!!"); else { printf("\n DATA: %d",ptr->data); ptr=ptr->link; } while(ptr!=q) { printf("\n DATA: %d",ptr->data); ptr=ptr->link; } } void search(struct node*q,int n) { int z=0,i=1; struct node *ptr; ptr=q; if(ptr->data==n) { z=1; printf("\n Its position is : %d",i); } else { i=2; ptr=ptr->link; while(ptr!=q) { if(ptr->data==n) { z=1; printf("\n NODE FOUND!");
Saumya Raghuvanshi
0806410091
Data Structures in C
} } if(z==0) printf("\n NODE NOT FOUND!"); } void disp_pos(struct node*q,int pos) { int z=0,i=1; struct node *ptr; ptr=q; if(i==pos) { z=1; printf("\n NODE FOUND!"); printf("\n Data at %d is : %d",pos,ptr->data); } else { i=2; ptr=ptr->link; while(ptr!=q) { if(pos==i) { z=1; printf("\n NODE FOUND"); printf("\n Data at %d is : %d",pos,ptr->data); break; } i++; ptr=ptr->link; } } if(z==0) printf("\n NODE NOT FOUND!"); } void modi_val(struct node *q,int val) { int z=0,new1; struct node *ptr; ptr=q;
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
if(ptr->data==val) { z=1; printf("\n NODE FOUND!"); printf("\n Enter the new value: "); scanf("%d",&new1); ptr->data=new1; printf("\n NODE MODIFIED!"); } else { ptr=ptr->link; while(ptr!=q) { if(ptr->data==val) { z=1; printf("\n NODE FOUND!"); printf("\n Enter the new value :"); scanf("%d",&new1); ptr->data=new1; printf("\n NODE MODIFIED!"); break; } ptr=ptr->link; } } if(z==0) printf("\n NODE NOT FOUND!"); } void modi_pos(struct node *q,int pos) { int z=0,i=1,new1; struct node *ptr; ptr=q; if(i==pos) { z=1; printf("\n NODE FOUND!"); printf("\n Enter the new value :"); scanf("%d",&new1); ptr->data=new1; printf("\n NODE MODIFIED!"); } else {
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
i=2; ptr=ptr->link; while(ptr!=q) { if(i==pos) { z=1; printf("\n NODE FOUND!"); printf("\n Enter the new value :"); scanf("%d",&new1); ptr->data=new1; printf("\n NODE MODIFIED!"); break; } i++; ptr=ptr->link; } } if(z==0) printf("\n NODE NOT FOUND!"); } void count_all(struct node *q) { int c=0; struct node *ptr; ptr=q; if(ptr==NULL) c=0; else if(ptr->link==q) c++; else { c=1; ptr=ptr->link; while(ptr!=q) { c++; ptr=ptr->link; } } printf("\n Total number of nodes : %d",c); } void del_all(struct node **q) { *q=NULL; printf("\n ALL NODES DELETED!!");
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
} void del_val(struct node **ptr,int num) { struct node *temp=*ptr,*prv=NULL,*p=*ptr; int a=0; if(temp==NULL) { printf("\nList is Empty"); return; } else if(temp->data==num) { if(temp->link==temp) { *ptr=NULL; free(temp); return; } p=p->link; while(p->link!=temp) p=p->link; *ptr=temp->link; free(temp); p->link=*ptr; return; } else { prv=p; p=p->link; while(p!=temp) { if(p->data==num) { a=1; break; } prv=p; p=p->link; } prv->link=p->link; free(p); } if(a==0) printf("\n Value is not present"); }
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
OUTPUT:
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
*****
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Polynomial Addition
Polynomial Addition
#include<stdio.h> #include<conio.h> #include<malloc.h> void insert(); void add(); void display(); struct node { int data, exp; struct node *link; }; typedef struct node POLY; POLY *start=NULL, *start1=NULL; void main() { int ch; clrscr(); do { printf("1. Enter two polynomials\n"); printf("2. Add the two polynomials\n"); printf("0. Exit\n"); printf("Enter your choice\n"); scanf("%d",&ch); switch(ch) { case 0: exit(0); case 1: insert(); break; case 2: add(); break; default: printf("Wrong Choice Entered\n"); break; } } while(ch != 0); getch(); } void insert() { int i, n, e;
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Polynomial Addition
POLY *temp, *temp1; temp = start; temp1 = start1; printf("Enter first polynomial\n"); do { printf("Enter data\n"); scanf("%d",&n); printf("Enter exponent\n"); scanf("%d",&e); temp=(POLY*)malloc(sizeof(POLY)); temp->data=n; temp->exp=e; temp->link=start; start=temp; printf("Do you want to enter another polynomial in first list (0/1)??"); scanf("%d",&i); } while(i != 0); printf("Enter second polynomial\n"); do { printf("Enter data\n"); scanf("%d",&n); printf("Enter exponent\n"); scanf("%d",&e); temp1=(POLY*)malloc(sizeof(POLY)); temp1->data=n; temp1->exp=e; temp1->link=start1; start1=temp1; printf("Do you want to enter another polynomial in second list (0/1)??"); scanf("%d",&i); } while(i != 0); } void add() { int z=0; POLY *a, *b, *c, *r, *temp1, *temp2; temp1 = a = start; temp2 = b = start1; c = NULL; r = c; while(a != NULL)
Saumya Raghuvanshi
0806410091
Data Structures in C {
ECS-352
Polynomial Addition
while(b != NULL) { if( a->exp==b->exp) { z = 1; c = (POLY*)malloc(sizeof(POLY)); c->data = (a->data + b->data); c->exp = a->exp; c->link = r; r = c; break; } b = b->link; } if(z==0) { c = (POLY*)malloc(sizeof(POLY)); c->data = a->data; c->exp = a->exp; c->link = r; r = c; } z = 0; b = temp2; a = a->link; } a = temp1; b = temp2; z = 0; while(b != NULL) { while(a != NULL) { if( a->exp==b->exp) { z = 1; break; } a = a->link; } if(z==0) { c = (POLY*)malloc(sizeof(POLY)); c->data = b->data; c->exp = b->exp;
Saumya Raghuvanshi
0806410091
ECS-352
Polynomial Addition
} void display(POLY *q) { int z=0; while(q != NULL) { z = 1; printf("Data = %d ",q->data); printf("Exponent = %d ",q->exp); q = q->link; } if(z==0) printf("No polynomial is present\n"); }
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Polynomial Addition
OUTPUT-
*****
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Output
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
*****
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Saumya Raghuvanshi
0806410091
ECS-352
} void push(int info) { STK *ptr; ptr = (STK*)malloc(sizeof(STK)); ptr->data = info; if(top==NULL) ptr->next = NULL; else ptr->next = top; top = ptr; } void pop() { STK *ptr; if(top==NULL) printf("Stack is Empty "Underflow""\n"); else { ptr = top; top = top->next; free(ptr); } } void display() { STK *ptr = top; while(ptr != NULL) { printf("Data = %d\t",ptr->data); ptr=ptr->next; } }
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Output
*****
Saumya Raghuvanshi 0806410091
Data Structures in C
ECS-352
Saumya Raghuvanshi
0806410091
ECS-352
} else if(*r==(n-1)) *r=1; else *r = *r + 1; printf("Enter element\n"); scanf("%d",&item); q[*r] = item; } void display(int q[], int f, int r) { int i; if((f==-1) && (r==-1)) printf("Empty Queue\n"); else { for(i=f;i<=r;i++) printf("%d",q[i]); } } void delete(int q[], int *f, int *r, int n) { int k = q[*f]; if(*f==-1) { printf("Underflow\n"); exit(0); } if(*f==*r) { *f = -1; *r = -1; } else if(*f==(n-1)) *f = 0; else *f = *f + 1; }
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
OUTPUT:
*****
Saumya Raghuvanshi 0806410091
Data Structures in C
ECS-352
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
int k; QUE *fr=front, *re=rear, *ptr=NULL; ptr = (QUE*)malloc(sizeof(QUE)); ptr->data = info; if(ptr==NULL) { printf(Overflow); exit(0); } printf("Enter the element\n"); scanf("%d",&k); ptr->data = k; ptr->link = NULL; if(fr==NULL) fr = re = ptr; else { re->link = ptr; re = ptr; } } void delete() { QUE *fr=front, *re=rear, *temp; int k; if(fr==NULL) { printf("Underflow\n"); exit(0); } temp = fr; k = temp->data; fr = fr->link; free(temp); printf("Element deleted is %d ",k); } void display() { QUE *fr=front, *re=rear; int z =0; while(fr != NULL) { z = 1; printf("Data = %d\t",fr->data); fr=fr->link; }
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
OUTPUT:
*****
Saumya Raghuvanshi 0806410091
Data Structures in C
ECS-352
Recursion
Recursion
#include<stdio.h> #include<conio.h> int factorial(int); void fibonacci(int ); void tower_hanoi(int,char,char,char); void main() { int n, f, choice, term, f0=1, f1=1, s; char beg='a', aux='b', end='c'; clrscr(); do { printf("1. Find Factorial of a number\n"); printf("2. Generate Fibonacci series\n"); printf("3. Tower of Hanoi\n"); printf("0. exit\n"); printf("Enter your choice\n"); scanf("%d",&choice); switch(choice) { case 0: exit(0); case 1: printf("Enter the number whose factorial is to be calculated\n"); scanf("%d",&n); f=factorial(n); printf("Factorial of %d is %d\n",n,f); break; case 2: printf("Enter the number of terms upto which series is to be generated\n"); scanf("%d",&term); if(term>2) { printf("%d\t%d\t",f0,f1); fibonacci(term-2); } else if(term==2)
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352 printf("%d\t%d\t",f0,f1); else if(term==1) printf("%d\t",f0); else printf("Series not possible"); break; case 3: printf("Enter the number of disks\n"); scanf("%d",&n); tower_hanoi(n,beg,aux,end); printf("\n"); break; default: printf("Wrong Choice Entered\n"); break;
Recursion
} } while(choice!=0); getch(); } int factorial(int num) { int fact=1; if(num==1) return 1; else { fact=num*factorial(num-1); return fact; } } void fibonacci(int term) { static int f0=1,f1=1,fib; if(term==0) printf("Series Ends Here\n"); else { fib=f0+f1; f0=f1; f1=fib; printf("%d\t",fib);
Saumya Raghuvanshi
0806410091
ECS-352
Recursion
} void tower_hanoi(int n,char beg,char aux,char end) { if(n==1) printf("\n%c -> %c",beg,end); else { tower_hanoi(n-1,beg,end,aux); printf("\n%c -> %c",beg,end); tower_hanoi(n-1,aux,beg,end); } }
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Output
*****
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Saumya Raghuvanshi
0806410091
ECS-352
if(n<(*tree)->data) insert(&((*tree)->left),n); else insert(&((*tree)->right),n); } } void preorder(struct bst *tree) { if(tree!=NULL) { printf("%d",tree->data); preorder(tree->left); preorder(tree->right); } } void inorder(struct bst *tree) { if(tree!=NULL) { inorder(tree->left); printf("%d",tree->data); inorder(tree->right); } } void postorder(struct bst *tree) { if(tree!=NULL) { postorder(tree->left); postorder(tree->right); printf("%d",tree->data); } }
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Output
*****
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Sorting
Sorting
#include<stdio.h> #include<conio.h> void bubble(int [],int); void insertion(int [],int); void selection(int [],int); void merge(int [],int[],int,int); void main() { int n,arr[25],arr1[25],arr2[25],i,choice,n1,n2; clrscr(); do { printf("1. Sort data by Bubble Sort\n"); printf("2. Sort data by Insertion Sort\n"); printf("3. Sort data by Selection Sort\n"); printf("4. Sort data by Merge Sort\n"); printf("0. exit\n"); printf("Enter your choice\n"); scanf("%d",&choice); switch(choice) { case 0: exit(0); case 1: printf("Enter the number of elements to be entered\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the value\n"); scanf("%d",&arr[i]); } bubble(arr,n); printf("\n"); break; case 2: printf("Enter the number of elements to be entered\n"); scanf("%d",&n); printf("Enter the elements in increasing order\n"); for(i=0;i<n;i++) {
Saumya Raghuvanshi
0806410091
Data Structures in C
Sorting
} insertion(arr,n); printf("\n"); break; case 3: printf("Enter the number of elements to be entered\n"); scanf("%d",&n); printf("Enter the elements in increasing order\n"); for(i=0;i<n;i++) { printf("Enter the value\n"); scanf("%d",&arr[i]); } selection(arr,n); printf("\n"); break; case 4: printf("First list of data\n"); printf("Enter the number of elements to be entered\n"); scanf("%d",&n1); for(i=0;i<n1;i++) { printf("Enter the value\n"); scanf("%d",&arr1[i]); } printf("Second list of data\n"); printf("Enter the number of elements to be entered\n"); scanf("%d",&n2); for(i=0;i<n2;i++) { printf("Enter the value\n"); scanf("%d",&arr2[i]); } merge(arr1,arr2,n1,n2); printf("\n"); break; default: printf("Wrong Choice Entered\n"); break; }
Saumya Raghuvanshi
0806410091
ECS-352
Sorting
} void bubble(int a[],int n) { int i=0,j,temp,swap=1; while(i<=n-1 && swap==1) { j=0; swap=0; while(j<=n-i-1) { if(a[j]>a[j+1]) { swap=1; temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } j++; } i++; } printf("Sorted data is\n"); for(i=0;i<n;i++) printf("%d\t",a[i]); } void insertion(int a[],int n) { int i,j,temp; for(i=0;i<=(n-1);i++) { temp=a[i]; j=i-1; while((temp<a[j]) && (j>=0)) { a[j+1]=a[j]; j=j-1; }
Saumya Raghuvanshi
0806410091
ECS-352
Sorting
} void selection(int a[],int n) { int i,min,pos,j,temp; for(i=0;i<=(n-2);i++) { min=a[i]; pos=i; for(j=i+1;j<=n-1;j++) { if(min>a[j]) { min=a[j]; pos=j; } } if(pos!=i) { temp=a[i]; a[i]=a[pos]; a[pos]=temp; } } printf("Sorted data is\n"); for(i=0;i<n;i++) printf("%d\t",a[i]); } void merge_sort(int a[],int n,int b[],int m) { int c[100],p1=0,p2=0,p3=0; while(p1<n && p2<m) { if(a[p1]<b[p2]) { c[p3]=a[p1]; p1++;
Saumya Raghuvanshi
0806410091
Data Structures in C p3++; } else { c[p3]=b[p2]; p2++; p3++; } } while(p1<n) { c[p3]=a[p1]; p3++; p1++; } while(p2<m) { c[p3]=b[p2]; p3++; p2++; } printf("\n Sorted Array :"); print(c,p3); } printf("Sorted data is\n"); for(l=0;l<n;l++) printf("%d\t",mer[l]); }
ECS-352
Sorting
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Sorting
Output
*****
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Searching
Searching
#include<stdio.h> #include<conio.h> void linear_unsort(int [],int,int); void linear_sort(int [],int,int); void binary(int [],int,int); void main() { int n,arr[25],i,ch,choice; clrscr(); do { printf("1. Perform Linear searching in an unsorted data\n"); printf("2. Perform Linear searching in a sorted data\n"); printf("3. Perform Binary searching in a sorted data\n"); printf("0. exit\n"); printf("Enter your choice\n"); scanf("%d",&choice); switch(choice) { case 0: exit(0); case 1: printf("Enter the number of elements to be entered\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the value\n"); scanf("%d",&arr[i]); } printf("Enter the element to be searched\n"); scanf("%d",&ch); linear_unsort(arr,n,ch); break; case 2: printf("Enter the number of elements to be entered\n"); scanf("%d",&n); printf("Enter the elements in increasing order\n"); for(i=0;i<n;i++) {
Saumya Raghuvanshi
0806410091
Data Structures in C
Searching
} printf("Enter the element to be searched\n"); scanf("%d",&ch); linear_sort(arr,n,ch); break; case 3: printf("Enter the number of elements to be entered\n"); scanf("%d",&n); printf("Enter the elements in increasing order\n"); for(i=0;i<n;i++) { printf("Enter the value\n"); scanf("%d",&arr[i]); } printf("Enter the element to be searched\n"); scanf("%d",&ch); binary(arr,n,ch); break; default: printf("Wrong Choice Entered\n"); break; } } while(choice!=0); getch(); } void linear_unsort(int a[],int n,int num) { int i,flag=0; for(i=0;i<n;i++) { if(a[i]==num) { flag=1; printf("Element found at %d position\n",i+1); } } if(flag==0) printf("Element not found\n"); }
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Searching
void linear_sort(int a[],int n,int num) { int i=0,flag=0; while((a[i]<=num) && (i<n)) { if(a[i]==num) { flag=1; printf("Element found at %d position\n",i+1); break; } i++; } if(flag==0) printf("Element not found\n"); } void binary(int a[],int n,int num) { int first,last,mid,pos; first=0; last=n-1; pos=-1; while((first<=last) && (pos==-1)) { mid=(first+last)/2; if(a[mid]==num) pos=mid; else if(a[mid]<num) first=mid+1; else last=mid-1; } if(pos>-1) { pos=pos+1; printf("Element found at %d position\n",pos); } else printf("Element not found\n"); }
Saumya Raghuvanshi
0806410091
Data Structures in C
ECS-352
Searching
Output
*****
Saumya Raghuvanshi
0806410091
THANK YOU !
________________________