You are on page 1of 20

//C PROGRAM TO ADD TWO POLYNOMIALS USING LINKED LIST #include <stdio.

h> typedef struct pnode { float coef; int exp; struct pnode *next; }p; p *getnode(); void main() { p *p1,*p2,*p3; p *getpoly(),*add(p*,p*); void display(p*); clrscr(); printf(\n enter first polynomial); p1=getpoly(); printf(\n enter second polynomial); p2=getpoly(); printf(\nthe first polynomial is); display(p1); printf(\nthe second polynomial is); display(p2); p3=add(p1,p2); printf(\naddition of two polynomial is :\n); display(p3); } p *getpoly() { p *temp,*New,*last; int flag,exp; char ans; float coef; temp=NULL; flag=1; printf(\nenter the polynomial in descending order of exponent); do { printf(\nenter the coef & exponent of a term); scanf(%f%d,&coef,&exp); New=getnode(); if(New==NULL) printf(\nmemory cannot be allocated); New->coef=coef;

New->exp=exp; if(flag==1) { temp=New; last=temp; flag=0; } else { last->next=New; last=New; } printf(\ndou want to more terms); ans=getch(); } while(ans==y'); return(temp); } p *getnode() { p *temp; temp=(p*) malloc (sizeof(p)); temp->next=NULL; return(temp); } void display(p*head) { p*temp; temp=head; if(temp==NULL) printf(\npolynomial empty); while(temp->next!=NULL) { printf(%0.1fx^%d+,temp->coef,temp->exp); temp=temp->next; } printf(\n%0.1fx^%d,temp->coef,temp->exp); getch(); } p*add(p*first,p*second) { p *p1,*p2,*temp,*dummy; char ch; float coef;

p *append(int,float,p*); p1=first; p2=second; temp=(p*)malloc(sizeof(p)); if(temp==NULL) printf(\nmemory cannot be allocated); dummy=temp; while(p1!=NULL&&p2!=NULL) { if(p1->exp==p2->exp) { coef=p1->coef+p2->coef; temp=append(p1->exp,coef,temp); p1=p1->next; p2=p2->next; } else if(p1->expexp) { coef=p2->coef; temp=append(p2->exp,coef,temp); p2=p2->next; } else if(p1->exp>p2->exp) { coef=p1->coef; temp=append(p1->exp,coef,temp); p1=p1->next; } } while(p1!=NULL) { temp=append(p1->exp,p1->coef,temp); p1=p1->next; } while(p2!=NULL) { temp=append(p2->exp,p2->coef,temp); p2=p2->next; } temp->next=NULL; temp=dummy->next; free(dummy);

return(temp); } p*append(int Exp,float Coef,p*temp) { p*New,*dum; New=(p*)malloc(sizeof(p)); if(New==NULL) printf(\ncannot be allocated); New->exp=Exp; New->coef=Coef; New->next=NULL; dum=temp; dum->next=New; dum=New; return(dum); }

/C PROGRAM TO IMPLEMENT STACK USING ARRAYS #include<stdio.h> #define size 10 char stack[size][15],ele[20]; int tos; void push();

char pop(); void show(); int isempty(); int isfull(); int main() { int choice; tos=0; do { printf("\tEnter 1 for push,2 for pop,3 to show,and 4 to exit\n"); scanf("%d",&choice); switch(choice) { case 1: if (isfull()) printf("\nThe stack is full"); else { printf("\n Enter element to insert"); scanf("%s",ele); push(ele); } break; case 2: if(isempty()) printf("\n The stack is empty"); else printf("\nThe removed element is:%s",pop()); break; case 3: if(isempty()) printf("\nThe stack is empty"); else show(); break; case 4: exit(1); default: printf("\nEXITING.."); }} while(1); } int isempty() {

return(tos==0); } int isfull() { return(tos==size); } void push(ele) { //stack[tos]=ele; strcpy(stack[tos],ele); tos++; } char pop() { tos--; return(stack[tos]); } void show() { int x=tos; printf("\nThe Stack elements are.....\n"); while(x!=0) printf("\t%s\n",stack[--x]); }

//C PROGRAM TO IMPLEMENT PRIORITY QUEUE USING HEAPS #include<stdio.h> struct heapstruct { int capacity; int size; int *a; }; typedef struct heapstruct *pq; pq initialize(int maxa,int mindata)

{ pq h; h=(struct heapstruct*)malloc(sizeof(struct heapstruct)); if(h==NULL) printf(\nOut of Space); h->a=(int*)malloc((maxa+1)*sizeof(int)); h->capacity=maxa; h->size=0; h->a[0]=mindata; return h; } void insert(int x,pq h) { int i; if(h->size==h->capacity) { printf(\nFull); } else { for(i=++h->size;h->a[i/2]>x;i=i/2) { h->a[i]=h->a[i/2]; } h->a[i]=x; } } int delmin(pq h) { int i,mina,lasta,child; if(h->size==0) { return(h->a[0]); } else { mina=h->a[1]; lasta=h->a[h->size--]; for(i=1;i*2size;i=child) { child=i*2; if(child!=h->size && h->a[child+1]a[child]) child++; if(lasta>h->a[child])

{ h->a[i]=h->a[child]; } else break; } h->a[i]=lasta; return mina; } } void display(pq h) { int i; for(i=1;isize;i++) { printf(\nThe data is: %d,h->a[i]); } } void main() { pq h;int x,y,z,u,v;char ch; clrscr(); printf(\nEnter the maximum number of elements for the Priority Queue:); scanf(%d,&x); printf(\nEnter the minimum element :); scanf(%d,&y); h=initialize(x,y); menu: printf(\nPriority Queue); printf(\n1.Insert\n2.Delete\n3.Display\n4.Exit); scanf(%d,&u); switch(u) { case 1:printf(Enter the Data:); scanf(%d,&z); insert(z,h); break; case 2:v=delmin(h); printf(\nThe deleted element is: %d,v); break; case 3:display(h); break; case 4:exit(0); }

goto menu; }

PROGRAM TO IMPLEMENT QUEUE USING Pointers in c #include < stdio.h> #include < conio.h> #include < malloc.h> #include < process.h> #include < ctype.h> struct linear_queue { int info; struct linear_queue *next; }*front,*rear,*newnode,*ptr; void menu(); void display(); int underflow(); void enqueue(int); void dequeue(); void main() { clrscr(); menu(); } void menu() { int choice,item; printf("MENU"); printf("\n1. Insert into the queue"); printf("\n2. Delete from queue"); printf("\n3. Display"); printf("\n4. Exit"); printf("\nEnter your choice: "); scanf("%d",&choice); switch(choice) { case 1: clrscr(); printf("\nEnter the item tobe inserted: "); scanf("%d",&item); enqueue(item); clrscr(); printf("\nAfter inserting queue is:\n"); display();

getch(); clrscr(); menu(); break; case 2: clrscr(); if(underflow()==1) { dequeue(); if(underflow()==1) { printf("\nAfter deletion queue is:\n"); display(); } } getch(); clrscr(); menu(); break; case 3: clrscr(); if(underflow()==1) { printf("The queue is:\n"); display(); } getch(); clrscr(); menu(); break; case 4: exit(1); default: clrscr(); printf("Your choice is wrong\n\n"); menu(); } } int underflow() { if((front==NULL)&&(rear==NULL)) { printf("\nQueue is empty"); return(0); }

else { return(1); } } void enqueue(int item) { newnode=(struct linear_queue*)malloc(sizeof(struct linear_queue)); newnode->info=item; if((front==NULL)&&(rear==NULL)) { front=newnode; rear=newnode; newnode->next=NULL; } else { rear->next=newnode; newnode->next=NULL; rear=newnode; } } void dequeue() { if(front==rear) { front=NULL; rear=NULL; } else { front=front->next; } } void display() { int i; ptr=front; i=1; while(ptr!=NULL) { printf("\nNode %d : %d",i,ptr->info); ptr=ptr->next;

i++; } }

PROGRAM TO IMPLEMENT BINARY SEARCH USING POINTERS IN C LANGUAGE


#include<stdio.h> void main() { int *a[100],i,no,*srchno,top,bottom,mid,j,*temp; clrscr(); printf("\n Enter the number of elements\n"); scanf("%d",&no); printf("\n Enter %d numbers\n",no); for(i=0;i<no;++i) scanf("%d",&a[i]); printf("Enter the search number\n"); scanf("%d",&srchno); for(i=0;i<no-1;++i) for(j=i+1;j<no;++j) if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } printf("\n Sorted array in ascending order\n"); for(i=0;i<no;++i) printf("%5d",a[i]);

bottom=0; top=no-1; while(top!=bottom+1) { mid=(bottom+top)/2; if (a[mid]<=srchno) bottom=mid; else top=mid; } if(a[bottom]==srchno) printf("\n search number is present"); else printf("\n Search number is not present"); }

PROGRAM TO IMPLEMENT LINEAR SEARCH USING ARRAYS using c programming language #include<stdio.h> int main(){ int a[10],i,n,m,c=0; printf("Enter the size of an array"); scanf("%d",&n); printf("\nEnter the elements of the array"); for(i=0;i<=n-1;i++){ scanf("%d",&a[i]); } printf("\nThe elements of an array are"); for(i=0;i<=n-1;i++){ printf(" %d",a[i]); } printf("\nEnter the number to be search"); scanf("%d",&m); for(i=0;i<=n-1;i++){ if(a[i]==m){ c=1; break; } } if(c==0) printf("\nThe number is not in the list"); else printf("\nThe number is found"); return 0; }

Program for Quick sorting #include<stdio.h> void print(int a[]) { int i; for( i=0;i<=8;i++) { printf("%d ",a[i]); } } int Qsort(int data[], int left, int right) { int mid,tmp,i,j; i = left; j = right; mid = data[(left+right)/2]; do { while (data[i] < mid) i++; while (mid < data[j]) j--; if (i <= j) { tmp = data[i]; data[i] = data[j]; data[j] = tmp; i++; j--; } } while (i <= j); { if (left < j) Qsort(data,left,j); if (i < right) Qsort(data,i,right); } } main() { int array[]={12,99,4,99,12,12,13,10,13}; printf("Before sort:\n\n"); print(array);

Qsort(array,0,8); printf("\n\nAfter sort:\n\n"); print(array); printf(""); }

PROGRAM TO IMPLEMENT Array Implementation.


#include #include # define MAXSIZE 4 int i,a[4],top=-1,data; void push(int data); void pop(); void display(); void main() { int choice; label: printf("enter the choice:\n"); scanf("%d",&choice); printf("1. Push\n"); printf("2. Pop\n"); printf("3. Display\n"); printf("4. Exit\n"); switch(choice) { case 1:printf("enter the data\n"); scanf("%d",&data); push(data); goto label; case 2:pop(data); goto label; case 3:display(); goto label; case 4:exit(0); goto label; default:printf("invalid selection\n"); } } void push(int data) { if(top==(MAXSIZE-1)) printf("the stack is full\n"); else { top=top+1; a[top]=data; }

} void pop() { if(top==-1) printf("the stack is empty\n"); else { data=a[top]; printf("the deleted element is %d",a[top]); top=top-1; } } void display() { if(top==-1) printf("the stack is empty\n"); else { printf("the stack elements are:\n"); for(i=0;i<=top;i++) printf("%d",a[i]); } }

You might also like