Mathematics(IX, X, XI, XII) CBSE & UP Board & CSE(C programming, Python, JAVA, Data Structure, IoT, IT, CBNST etc.)
Showing posts with label Data Structure. Show all posts
Showing posts with label Data Structure. Show all posts
Saturday, December 22, 2018
Monday, December 10, 2018
What is dangling pointer in c?
If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while a pointer is still pointing such memory location. Such a pointer is known as a dangling pointer and this problem is known as the dangling pointer problem.
Example:
Output:
Segmentation fault (core dumped)
Explanation:
Variable x is a local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and the pointer is still pointing ptr is still pointing to that location
In other words, we can say a pointer whose pointing object has been deleted is called a dangling pointer.
The solution to this problem: Make the variable x is as a static variable.
Wednesday, November 14, 2018
Addition of Two Polynomials ( Linked List Implementation)
A polynomial expression can be represented as a linked list where each node contains coefficients and exponents and a link to the next element. For example: Polynomial: ax^2 + bx + c , can be represented in terms of this node:
struct node
{ int coeff;
int exp;
struct node *next;
};
And ax^2 can be stored as node having a and 2 and link to the node, in turn, which has elements b and 1 and link to next node having values c and 0. You are supposed to create list for expressing polynomials and write function to add these two polynomials. For addition function, you may use the following information:
a) Start with pointers to both the polynomials say p and q. If you find that exponents of the visited node are equal, then add their coefficients and store the result in a new node pointed by the result of sum (a new polynomial), say r.
b)If exponents are different, then first add the node of the polynomial whose exponent is higher. Let node of p has higher exponent than add current term of p to r. Keep comparing the exponents in this manner and append the new nodes or nodes from p and q to r.
Ex.
Code:
# include <stdio.h>
# include <malloc.h>
struct node
{
int coeff;
int exp;
struct node *next;
};
struct node *addition(struct node *,struct node *);
struct node *list(struct node *);
struct node *insert(struct node *,int,int);
void display(struct node *);
void main( )
{
struct node *p_start,*q_start,*r_start;
p_start=NULL;
q_start=NULL;
r_start=NULL;
printf("Polynomial 1 :\n");
p_start=list(p_start);
printf("Polynomial 2 :\n");
q_start=list(q_start);
r_start=addition(p_start,q_start);
printf("Polynomial 1 is : ");
display(p_start);
printf("Polynomial 2 is : ");
display(q_start);
printf("Resultant polynomial is : ");
display(r_start);
}
struct node *list(struct node *start)
{
int i,n,ex;
int co;
printf("How many terms u want to enter : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter coefficient for term %d : ",i);
scanf("%d",&co);
printf("Enter exponent for term %d : ",i);
scanf("%d",&ex);
start=insert(start,co,ex);
}
return start;
}
struct node *insert(struct node *start,int co,int ex)
{
struct node *ptr,*tmp;
tmp= (struct node*)malloc(sizeof(struct node));
tmp->coeff=co;
tmp->exp=ex;
if(start==NULL || ex>start->exp)
{
tmp->next=start;
start=tmp;
}
else
{
ptr=start;
while(ptr->next!=NULL && ptr->next->exp>ex)
{
ptr=ptr->next;
}
tmp->next=ptr->next;
ptr->next=tmp;
}
return start;
}
struct node *addition(struct node *p_start,struct node *q_start)
{
struct node *r_start,*tmp,*p;
r_start=NULL;
if(p_start==NULL && q_start==NULL)
return r_start;
else if(p_start!=NULL && q_start==NULL)
{
while(p_start!=NULL)
{
tmp=(struct node *)malloc(sizeof(struct node));
tmp->coeff=p_start->coeff;
tmp->exp=p_start->exp;
if (r_start==NULL)
{
r_start=tmp;
p=tmp;
}
else
{
p->next=tmp;
p=tmp;
}
p_start=p_start->next;
}
}
else if(p_start==NULL && q_start!=NULL)
{
while(q_start!=NULL)
{
tmp=(struct node *)malloc(sizeof(struct node));
tmp->coeff=q_start->coeff;
tmp->exp=q_start->exp;
if (r_start==NULL)
{
r_start=tmp;
p=tmp;
}
else
{
p->next=tmp;
p=tmp;
}
q_start=q_start->next;
}
}
else
{
while(p_start!=NULL && q_start!=NULL )
{
tmp=(struct node*)malloc(sizeof(struct node));
if(r_start==NULL)
{
r_start=tmp;
p=tmp;
}
else
{
p->next=tmp;
p=tmp;
}
if(p_start->exp > q_start->exp)
{
tmp->coeff=p_start->coeff;
tmp->exp=p_start->exp;
p_start=p_start->next;
}
else if(q_start->exp > p_start->exp)
{
tmp->coeff=q_start->coeff;
tmp->exp=q_start->exp;
q_start=q_start->next;
}
else if(p_start->exp == q_start->exp)
{
tmp->coeff=p_start->coeff + q_start->coeff;
tmp->exp=p_start->exp;
p_start=p_start->next;
q_start=q_start->next;
}
}
}
p->next=NULL;
return r_start;
}
void display(struct node *ptr)
{
if(ptr==NULL)
{
printf("Empty\n");
}
while(ptr!=NULL)
{
printf("(%dx^%d) + ", ptr->coeff,ptr->exp);
ptr=ptr->next;
}
printf("\b\b \n");
}
Output:
Polynomial 1 :
How many terms u want to enter : 3
Enter coefficient for term 1 : 5
Enter exponent for term 1 : 3
Enter coefficient for term 2 : 2
Enter exponent for term 2 : 2
Enter coefficient for term 3 : 4
Enter exponent for term 3 : 0
Polynomial 2 :
How many terms u want to enter : 3
Enter coefficient for term 1 : 8
Enter exponent for term 1 : 2
Enter coefficient for term 2 : 10
Enter exponent for term 2 : 1
Enter coefficient for term 3 : 3
Enter exponent for term 3 : 0
Polynomial 1 is : (5x^3) + (2x^2) + (4x^0)
Polynomial 2 is : (8x^2) + (10x^1) + (3x^0)
Resultant polynomial is : (5x^3) + (10x^2) + (10x^1) + (7x^0)
Tuesday, November 13, 2018
C Program for Stack and Queue Implementation
Q:Suppose you purchase 100 shares of stock named ‘X’ in each of January, April and September and sell 100 shares of stocks in each of June and November. The prices per share in these months were:
a. January – INR 10
b. April – INR 30
c. June – INR 20
d. September – INR 50
e. November – INR 30
(i) Show that with FIFO accounting (i.e. you are adding your shares to queue) results in a gain of Rs 1000 (10*100)
(ii) Show that with LIFO accounting (i.e. you are adding your shares to stack) results in a loss of Rs 3000 (30*100)
(iii) In total, you are purchasing 300 shares and selling 200 shares. The 100 shares that you still own do not enter the calculation of loss and profit.
Code:
#include<stdio.h>
#include<stdlib.h>
struct share
{
int amount;
struct share *next;
}*start=NULL;
void purchase_queue_stack();
void sell_queu();
void sell_stack();
void display();
typedef enum abc
{Jan=1,April,June,Sept,Nov} ;
void main()
{ int ch,sp;
char str='y';
printf("\nPrices per Share:\n1:Jan INR 10\n2:April INR 30\n3:June INR 20\n4:Sept INR 50\n5:NOv INR 30\n\nplease Select one from following\n1:Queue(FIFO) Accounting\n2:Stack(LIFO) Accounting");
scanf("%d",&ch);
switch(ch)
{
case 1:
do{
printf("\nDo you want to purchase or sell\n1 for purchase\t 2 for sell");
scanf("%d",&sp);
if(sp==1)
{purchase_queue_stack();
}
if(sp==2)
{
if(start==NULL)
{ printf("\nQueue is empty");
}
else
sell_queu();
}
printf("\n Do you want next operation press y");
scanf(" %c",&str);
}while(str=='y');
break;
case 2:
do{
printf("\nDo you want to purchase of sell\n1 for purchase\t 2 for sell");
scanf("%d",&sp);
if(sp==1)
{
purchase_queue_stack();//insertion in both stack and queue are same
}
if(sp==2)
{
if(start==NULL)
{ printf("\nStack is empty");
}
else
sell_stack();
}
printf("\n Do you want next operation press y");
scanf(" %c",&str);
}while(str=='y');
break;
default:
printf("\nwrong selection");
}
}
void purchase_queue_stack()
{
struct share *node,*ptr;
ptr=start;
int amt=0;
int month;
printf("\nEnter the month no");
scanf("%d",&month);
if(month==1)
amt=10*100;
else if(month==2||month==5)
amt=30*100;
else if(month==3)
amt=20*100;
else if(month==4)
amt=50*100;
else printf("\n price not given ");
node=(struct share*)malloc(sizeof(struct share));
node->amount=amt;
if(start==NULL)
{
node->next=start;
start=node;
}
else
{ while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=node;
node->next=NULL;
}
display();
}
void display()
{
struct share *ptr;
ptr=start;
printf("\n Now List is::\n\n");
while(ptr!=NULL)
{
printf("%d\t",ptr->amount);
ptr=ptr->next;
}
}
void sell_queu()
{ struct share *tmp;
tmp=start;
int amt=0;
int month;
static int profit=0,loss=0;
printf("\nEnter the month no");
scanf("%d",&month);
if(month==1)
amt=10*100;
else if(month==2||month==5)
amt=30*100;
else if(month==3)
amt=20*100;
else if(month==4)
amt=50*100;
else printf("\n price not given ");
if(amt>start->amount)
{
profit=profit+(amt-start->amount);
printf("\n*****************************");
printf("\nPurchase amount=%d\nSell amount of month =%d",start->amount,amt);
printf("\nProfit of this month=%d",amt-start->amount);
printf("\n*****************************");
}
else
{
loss=loss+(start->amount -amt);
printf("\n*****************************");
printf("\nPurchase amount=%d\nSell amount=%d",start->amount,amt);
printf("\nLoss of this month=%d",start->amount -amt);
printf("\n***************************");
}
if(profit>=loss)
printf("\n\nFinal profit=%d",profit-loss);
else
printf("\n\nFinal loss=%d",loss-profit);
printf("\n****************************");
start=start->next;
free(tmp);
display();
}
void sell_stack()
{ struct share *tmp,*ptr;
ptr=start;
int amt=0;
int month;
static int profit=0,loss=0;
printf("\nEnter the month no");
scanf("%d",&month);
if(month==1)
amt=10*100;
else if(month==2||month==5)
amt=30*100;
else if(month==3)
amt=20*100;
else if(month==4)
amt=50*100;
else printf("\n price not given ");
while(ptr->next->next!=NULL)
ptr=ptr->next;
tmp=ptr->next;
ptr->next=NULL;
if(amt>tmp->amount)
{
profit=profit+(amt-tmp->amount);
printf("\n*****************************");
printf("\nPurchase amount=%d\nSell amount of month =%d",tmp->amount,amt);
printf("\nProfit of this month=%d",amt-tmp->amount);
printf("\n*****************************");
}
else
{
loss=loss+(tmp->amount -amt);
printf("\n*****************************");
printf("\nPurchase amount=%d\nSell amount=%d",tmp->amount,amt);
printf("\nLoss of this month=%d",tmp->amount -amt);
printf("\n***************************");
}
if(profit>=loss)
printf("\n\nFinal profit=%d",profit-loss);
else
printf("\n\nFinal loss=%d",loss-profit);
printf("\n****************************");
free(tmp);
display();
}
Output:
FIFO Accounting
Prices per Share:
1:Jan INR 10
2:April INR 30
3:June INR 20
4:Sept INR 50
5:NOv INR 30
please Select one from following
1:Queue(FIFO) Accounting
2:Stack(LIFO) Accounting 1
Do you want to purchase or sell
1 for purchase 2 for sell 1
Enter the month no 1
Now List is::
1000
Do you want next operation press y y
Do you want to purchase or sell
1 for purchase 2 for sell 1
Enter the month no 2
Now List is::
1000 3000
Do you want next operation press y y
Do you want to purchase or sell 1 for purchase 2 for sell 2
Enter the month no 3
*****************************
Purchase amount=1000
Sell amount of month =2000
Profit of this month=1000
*****************************
Final profit=1000
****************************
Now List is::
3000
Do you want next operation press y y
Do you want to purchase or sell 1 for purchase 2 for sell 1
Enter the month no 4
Now List is::
3000 5000
Do you want next operation press y y
Do you want to purchase or sell 1 for purchase 2 for sell 2
Enter the month no 5
*****************************
Purchase amount=3000
Sell amount=3000
Loss of this month=0
***************************
Final profit=1000
****************************
Now List is::
5000
Do you want next operation press y
LIFO Accounting
Prices per Share:
1:Jan INR 10
2:April INR 30
3:June INR 20
4:Sept INR 50
5:NOv INR 30
please Select one from following
1:Queue(FIFO) Accounting
2:Stack(LIFO) Accounting 2
Do you want to purchase or sell
1 for purchase 2 for sell 1
Enter the month no 1
Now List is::
1000
Do you want next operation press y y
Do you want to purchase or sell 1 for purchase 2 for sell 1
Enter the month no 2
Now List is::
1000 3000
Do you want next operation press y y
Do you want to purchase or sell
1 for purchase 2 for sell 2
Enter the month no 3
*****************************
Purchase amount=3000
Sell amount=2000
Loss of this month=1000
***************************
Final loss=1000
****************************
Now List is::
1000
Do you want next operation press y y
Do you want to purchase or sell 1 for purchase 2 for sell 1
Enter the month no 4
Now List is::
1000 5000
Do you want next operation press y y
Do you want to purchase or sell
1 for purchase 2 for sell 2
Enter the month no 5
*****************************
Purchase amount=5000
Sell amount=3000
Loss of this month=2000
***************************
Final loss=3000
****************************
Now List is::
1000 Do you want next operation press y n
Explanation:
Subscribe to:
Posts (Atom)


