Sunday 2 October 2016

Polynomial Addition C-program w/ Output

Compiled and runned on Visual Studio Express 2013 for Windows Desktop


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct polynode{
float coeff;
int exp;
struct polynode* link;
};

void create_poly(struct polynode** q) //Call by reference to provide address to original pointer
{
float c;
int e, flag;
struct polynode* temp = (struct polynode*) malloc(sizeof(struct polynode));
do{
if (*q == NULL)
{
*q = (struct polynode*) malloc(sizeof(struct polynode));
temp = *q;
}
else{
temp->link = (struct polynode*)malloc(sizeof(struct polynode));
temp = temp->link;
}
printf("\n\tEnter Coeffecient\n\t");
scanf("%f", &c);
temp->coeff = c;
printf("\n\tEnter Exponent\n\t");
scanf("%d", &e);
temp->exp = e;
 printf("\n\tWanna continue Y=1/N=0\n\t");
 scanf("%d", &flag);
} while (flag);
temp->link = NULL;
}

void display(struct polynode *q)
{
if (q==NULL)
{
printf("\n\tNot Found ! Try Again!!!\n\t");
return;
}
while (q != NULL)
{
printf("%fx^%d : ", q->coeff, q->exp);
q = q->link;
}
printf("\b\b\b");// Delete : from last
printf("\n\t");
}

void addpoly(struct polynode* p, struct polynode* q, struct polynode **r)
{
struct polynode *t = (struct polynode*) malloc(sizeof(struct polynode));// Temperary node
if (p==NULL && q==NULL)
{
return;
}
while (p != NULL && q != NULL)
{
if (*r==NULL) //Checking Total node where addition takes place
{
*r = (struct polynode*) malloc(sizeof(struct polynode));
t = *r; //Assigning temp to total node
}
else{
t->link = (struct polynode*) malloc(sizeof(struct polynode));// Creation of nodes at Intermediate Stage
t = t->link;
}
if (p->exp < q->exp)// If expo. of Second is greater then First assigining Second to Total
{
t->coeff = q->coeff;
t->exp = q->exp;
q = q->link;
}
else{
if (p->exp > q->exp)
{
t->coeff = p->coeff;
t->exp = p->exp;
p = p->link;
}
else{
if (p->exp == q->exp)
{
t->coeff = p->coeff+q->coeff;
t->exp = p->exp;
p = p->link;
q = q->link;
}
}//else
}//else
}//while-1

while (p != NULL)//If second list becomes empty or empty
{
if (*r == NULL) //Checking Total node where addition takes place
{
*r = malloc(sizeof(struct polynode));
t = *r; //Assigning temp to total node
}
else{
t->link = malloc(sizeof(struct polynode));// Creation of nodes at Intermediate Stage
t = t->link;
}
t->coeff = p->coeff;
t->exp = p->exp;
p = p->link;
}//while-2


while (q != NULL)//If first list becomes empty or empty
{
if (*r == NULL) //Checking Total node where addition takes place
{
*r = malloc(sizeof(struct polynode));
t = *r; //Assigning temp to total node
}
else{
t->link = malloc(sizeof(struct polynode));// Creation of nodes at Intermediate Stage
t = t->link;
}

t->coeff = q->coeff;
t->exp = q->exp;
q = q->link;
}//while-3

t->link = NULL;  //At last Make total NULL
}//polyadd()

void main()
{
struct polynode *first, *second, *total;
first = second = total = NULL;
int ch;
while(1)
{   
printf("\n\t Enter choice \t 1. First Expression\t2. Second Expression\t3.Addition\t4.exit\n\t");
scanf("%d", &ch);
switch (ch)
{
case 1:printf("\n\tCreate 1st expression\n\t");
create_poly(&first);
printf("\n\tStored the 1st expression\n\t");
display(first);
break;
case 2: printf("\n\tCreate 2nd expression\n\t");
create_poly(&second);
printf("\n\tStored the 2nd expression\n\t");
display(second);
break;
case 3: addpoly(first, second, &total);
display(total);
break;
case 4:exit(0);
}


}

O/p-



Sunday 11 September 2016

Basic operations performed on a Singly Linked List - C language //Easy to understand

//Run & Compiled on MS Visual Studio Express 2013


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

struct node{
int data;
struct node* link;
}*start=NULL;

int count = 0;//Counter to count no. of nodes
void insrtbeg(int num)
{
struct node* newnode = (struct node*)malloc(sizeof(struct node));
if (start==NULL)
{
newnode->data = num;
newnode->link=NULL;
start = newnode;
printf("\n\tNumber inserted Successfully\t\t(:(:(:");
}
else{
newnode->data = num;
newnode->link = start;
start = newnode;
printf("\n\tNumber inserted Successfully\t\t(:(:(:");
}
count++;
}
void delbeg()
{
struct node* p;
if (start == NULL)
{
printf("\n\tDeletion not possible");
exit(0);
}
else{
p = start;
start = p->link;
printf("\n\tDeleted no. is\t %d", p->data);
free(p);
}
count--;
}
static void reverse()
{
struct node* prev = NULL;
struct node* current = start;
struct node* next;
while (current != NULL)
{
next = current->link;
current->link = prev;
prev = current;
current = next;
}
start = prev;

}

void display()

struct node* p;
p = start;
if (start == NULL)
{
printf("\n\tList is empty !!!");
printf("\n\tTry Again ):\n");
return;
}
else{
while (p != NULL)
{
printf("\t%d", p->data);
p = p->link;
}
}

}
void xcount()
{

printf("\n\t No. of NODES are \t %d", count);

}

void insrtany(int num)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
if (temp == NULL)
{
printf("\n\tOverflow ):):):   \t Aborting!!!");
exit(0);
}
struct node*t =start;//initializing *t
while (t->data != num && t != NULL)
{
t = t->link;
}
if (t==NULL)
{
printf("\n\t\tNUMBER is not available in the list");
exit(0);
}
else{
temp->link = t->link;
temp->data = num;
t->link = temp;

printf("\n\tNumber inserted Successfully\t\t(:(:(:");
}
count++;

}

void main()
{
int ch,num;
while (1)
{
printf("\n\t1. Insertion\n\t2.Deletion\n\t3. Reverse\n\t4. Display\n\t5. No. of NODES\n\t6. Insert at any position\n\t7. Exit\n\t");
scanf("%d", &ch);
switch (ch)
{
case 1: printf("Enter the no. to insert\n\t");
   scanf("%d", &num);
insrtbeg(num);
break;
case 2: delbeg();
break;
case 3:reverse();
break;
case 4:display();
break;
case 5:xcount();
break;
case 6:  printf("Enter the value to insert after the no. you want \n\t");
   scanf("%d", &num);
   insrtany(num);
break;
case 7: exit(0);
}


}
}

Basic operations performed on a Doubly Linked List - C language //Easy to understand

//Run & Compiled on MS Visual Studio Express 2013


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

struct node{
int data;
struct node* prv,* nxt;
}*start = NULL;

int count = 0;//Counter to count no. of nodes
void insrtbeg(int num)
{
struct node* newnode = (struct node*)malloc(sizeof(struct node));
if (start == NULL)
{
newnode->data = num;
newnode->nxt = NULL;
newnode->prv = NULL;
start = newnode;
printf("\n\tNumber inserted Successfully\t\t(:(:(:");
}
else{
newnode->data = num;
newnode->nxt = start;
newnode->prv = NULL;
start->prv = newnode;
start = newnode;
printf("\n\tNumber inserted Successfully\t\t(:(:(:");
}
count++;
}
void delbeg()
{
struct node* p;
if (start == NULL)
{
printf("\n\tDeletion not possible");
exit(0);
}
else{
p = start;
start = p->nxt;
start->prv = NULL;
printf("\n\tDeleted no. is\t %d", p->data);
free(p);
}
count--;
}


void display()
{
struct node* p;
p = start;
if (start == NULL)
{
printf("\n\tList is empty !!!");
printf("\n\tTry Again ):\n");
return;
}
else{
while (p != NULL)
{
printf("\t%d", p->data);
p = p->nxt;
}
}

}
void xcount()
{

printf("\n\t No. of NODES are \t %d", count);

}

void insrtany(int num)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
if (temp == NULL)
{
printf("\n\tOverflow ):):):   \t Aborting!!!");
exit(0);
}
struct node*t = start;//initializing *t
while (t->data != num && t != NULL)
{
t = t->nxt;
}
if (t == NULL)
{
printf("\n\t\tNUMBER is not available in the list");
exit(0);
}
else{
temp->nxt = t->nxt;
temp->prv = t;
temp->data = num;
t->nxt = temp;

printf("\n\tNumber inserted Successfully\t\t(:(:(:");
}
count++;

}

void main()
{
int ch, num;
while (1)
{
printf("\n\t1. Insertion\n\t2. Deletion\n\t3. Display\n\t4. No. of NODES\n\t5. Insert at any position\n\t6. Exit\n\t");
scanf("%d", &ch);
switch (ch)
{
case 1: printf("Enter the no. to insert\n\t");
scanf("%d", &num);
insrtbeg(num);
break;
case 2: delbeg();
break;
case 3:display();
break;
case 4:xcount();
break;
case 5:  printf("Enter the value to insert after the no. you want \n\t");
scanf("%d", &num);
insrtany(num);
break;
case 6: exit(0);
}


}
}

Sister's are an invaluable gift

It has been a year since I realized that, in this highly entropic universe, my sisters are the strongest and most stable place to which I be...