Posts

Showing posts from November, 2021

Simulation of a basic memory allocator and garbage collector using doubly linked list

#include<stdio.h> #include<stdlib.h> struct node { int data; int avail; struct node *llink,*rlink; }*first=NULL,*last=NULL,*temp=NULL,*t=NULL; void create() { temp=(struct node*)malloc(sizeof(struct node)); printf("Enter data:"); scanf("%d",&temp->data); temp->avail=1; temp->llink=NULL; temp->rlink=NULL; if(first==NULL) { first=temp; last=temp; } else { last->rlink=temp; temp->llink=last; last=temp; } } void display() { temp=first; printf("NULL"); while(temp!=NULL) { printf(" %d:%d<---->",temp->data,temp->avail); temp=temp->rlink; } printf(" NULL\n"); } void allocate() { int size,newdata; printf("Enter size\n"); scanf("%d",&size); temp=first; while(temp!=NULL) { if(temp->data>=size && temp->avail==1)    break; temp=temp->rlink; } if( temp==NULL)   printf("cannot allocate\n"); else if (temp->data==size)   temp->avail=0;  else    {

Polynomial Addition using Arrays

Learn Theory -  Polynomials /****************************************************************/ #include<stdio.h> void create(int poly[],int degree) {     int i=0;          printf("Enter the coeffecients for:\n");     for(i=degree;i>=0;i--)     {         printf("Exp_%d: ",i);         scanf("%d",&poly[i]);     } } void display(int poly[],int degree) {     int i;     for(i=degree;i>=0;i--)     {                  if(i!=degree && poly[i]>0)         {              printf("+");          }         printf("%dx^%d",poly[i],i);     } } void main() {     int poly1[100]={0},degree1,poly2[100]={0},degree2,degreeresult,polyresult[100],i=0;          printf("Enter the degree of first polynomial");      scanf("%d",&degree1);     create(poly1,degree1);     printf("Enter the degree of second polynomial");      scanf("%d",&degree2);     create(poly2,degree2);     if(degree1>degree