Sei sulla pagina 1di 13

PREETI SY-C-44

Q1. A) Stack and Queues

Stacks :
#include <stdio.h>
#include <stdlib.h>

struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;

int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();

int count = 0;

void main()
{
int no, ch, e;

printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Dipslay");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");

create();

while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);

switch (ch)
{
case 1:
printf("Enter the value : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Invalid Option! Please enter correct choice ");
break;
}
}
}

void create()
{
top = NULL;
}

void stack_count()
{
printf("\n Number of elements in stack : %d", count);
}

void push(int data)


{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}
void display()
{
top1 = top;

if (top1 == NULL)
{
printf("Stack is empty");
return;
}

while (top1 != NULL)


{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}

void pop()
{
top1 = top;

if (top1 == NULL)
{
printf("\n Pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}

int topelement()
{
return(top->info);
}

void empty()
{
if (top == NULL)
printf("\n Stack is empty");
else
printf("\n Stack is not empty with %d elements", count);
}

void destroy()
{
top1 = top;

while (top1 != NULL)


{
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;

printf("\n All stack elements destroyed");


count = 0;
}
OUTPUT:
1 ­ Push                                                                
                                                                       
 2 ­ Pop                                                                
                                                                        
 3 ­ Top                                                                
                                                                        
 4 ­ Empty                                                              
                                                                        
 5 ­ Exit                                                               
                                                                        
 6 ­ Dipslay                                                            
                                                                        
 7 ­ Stack Count                                                        
                                                                        
 8 ­ Destroy stack                                                      
                                                                        
 Enter choice : 1                                                       
                                                                        
Enter the value : 24                                                    
                                                                        
                                                                        
                                                                        
 Enter choice : 1                                                       
                                                                        
Enter the value : 45                                                    
                                                                        
                                                                        
                                                                        
 Enter choice : 1                                                       
                                                                        
Enter the value : 76                                                    
                                                                        
                                                                        
                                                                        
 Enter choice : 1                                                       
                                                                        
Enter the value : 94                                                    
                                                                        
                                                                        
                                                                        
 Enter choice : 2                                                       
                                                                        
                                                                        
                                                                        
 Popped value : 94                                                      
                                                                        
 Enter choice : 6                                                       
                                                                        
76 45 24     

Queues:
#include <stdio.h>
#include <stdlib.h>

struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;

int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();

int count = 0;

void main()
{
int no, ch, e;

printf("\n 1 - Enque");
printf("\n 2 - Deque");
printf("\n 3 - Front element");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Queue size");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter the value : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
printf("Front element is : %d", e);
else
printf("\n No front element in Queue because queue is
empty");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
printf("Invalid choice! Please enter correct choice ");
break;
}
}
}

void create()
{
front = rear = NULL;
}

void queuesize()
{
printf("\n Queue size is : %d", count);
}

void enq(int data)


{
if (rear == NULL)
{
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
}
else
{
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;

rear = temp;
}
count++;
}
void display()
{
front1 = front;

if ((front1 == NULL) && (rear == NULL))


{
printf("Queue is empty!");
return;
}
while (front1 != rear)
{
printf("%d ", front1->info);
front1 = front1->ptr;
}
if (front1 == rear)
printf("%d", front1->info);
}

void deq()
{
front1 = front;

if (front1 == NULL)
{
printf("\n Display elements from empty queue");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequed value is : %d", front->info);
free(front);
front = front1;
}
else
{
printf("\n Dequed value is : %d", front->info);
free(front);
front = NULL;
rear = NULL;
}
count--;
}

int frontelement()
{
if ((front != NULL) && (rear != NULL))
return(front->info);
else
return 0;
}
void empty()
{
if ((front == NULL) && (rear == NULL))
printf("\n Queue is empty");
else
printf("Queue is not empty");
}
Output:
 1 ­ Enque                                                              
                                                                        
 2 ­ Deque                                                              
                                                                        
 3 ­ Front element                                                      
                                                                        
 4 ­ Empty                                                              
                                                                        
 5 ­ Exit                                                               
                                                                        
 6 ­ Display                                                            
                                                                        
 7 ­ Queue size                                                         
                                                                        
 Enter choice : 1                                                       
                                                                        
Enter the value : 3                                                     
                                                                        
                                                                        
                                                                        
 Enter choice : 1                                                       
                                                                        
Enter the value : 12                                                    
                                                                        
                                                                        
                                                                        
 Enter choice : 1                                                       
                                                                        
Enter the value : 36                                                    
                                                                        
                                                                        
                                                                        
 Enter choice : 1                                                       
                                                                        
Enter the value : 72                                                    
                                                                        
                                                                        
                                                                        
 Enter choice : 2                                                       
                                                                        
                                                                        
                                                                        
 Dequed value is : 3                                                    
                                                                        
 Enter choice : 6                                                       
                                                                        
12 36 72        

B) Polynomial addition and multiplication.


#include<stdio.h>
#include<stdlib.h>
struct node
{
float coef;
int expo;
struct node *link;
};
struct node *create(struct node *);
struct node *insert_s(struct node *,float,int);
struct node *insert(struct node *,float,int);
void display(struct node *ptr);
void poly_add(struct node *,struct node *);
void poly_mult(struct node *,struct node *);
int main( )
{
struct node *start1=NULL,*start2=NULL;
printf("\n\tEnter polynomial 1 :\n");
start1=create(start1);
printf("\n\tEnter polynomial 2 :\n");
start2=create(start2);
printf("\n\tPolynomial 1 is : ");
display(start1);
printf("\n\tPolynomial 2 is : ");
display(start2);
poly_add(start1, start2);
poly_mult(start1, start2);
}
struct node *create(struct node *start)
{
int i,n,ex;
float co;
printf("\nEnter the number of terms : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nEnter coeficient for term %d : ",i);
scanf("%f",&co);
printf("\nEnter exponent for term %d : ",i);
scanf("%d",&ex);
start=insert_s(start,co,ex);
}
return start;
}
struct node *insert_s(struct node *start,float co,int ex)
{
struct node *ptr,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->coef=co;
tmp->expo=ex;
if(start==NULL || ex > start->expo)
{
tmp->link=start;
start=tmp;
}
else
{
ptr=start;
while(ptr->link!=NULL && ptr->link->expo >= ex)
ptr=ptr->link;
tmp->link=ptr->link;
ptr->link=tmp;
}
return start;
}
struct node *insert(struct node *start,float co,int ex)
{
struct node *ptr,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->coef=co;
tmp->expo=ex;

if(start==NULL)
{
tmp->link=start;
start=tmp;
}
else
{
ptr=start;
while(ptr->link!=NULL)
ptr=ptr->link;
tmp->link=ptr->link;
ptr->link=tmp;
}
return start;
}
void display(struct node *ptr)
{
if(ptr==NULL)
{
printf("Zero polynomial\n");
return;
}
while(ptr!=NULL)
{
printf("(%.1fx^%d)", ptr->coef,ptr->expo);
ptr=ptr->link;
if(ptr!=NULL)
printf(" + ");
else
printf("\n");
}
}
void poly_add(struct node *p1,struct node *p2)
{
struct node *start3;
start3=NULL;
while(p1!=NULL && p2!=NULL)
{
if(p1->expo > p2->expo)
{
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
}
else if(p2->expo > p1->expo)
{
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
}
else if(p1->expo==p2->expo)
{
start3=insert(start3,p1->coef+p2->coef,p1->expo);
p1=p1->link;
p2=p2->link;
}
}
while(p1!=NULL)
{
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
}
while(p2!=NULL)
{
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
}
printf("\n\nAddtion of polynomial is : ");
display(start3);
}
void poly_mult(struct node *p1, struct node *p2)
{
struct node *start3;
struct node *p2_beg = p2;
start3=NULL;
if(p1==NULL || p2==NULL)
{
printf("\n\nMultiplied polynomial is zero polynomial\n");
return;
}
while(p1!=NULL)
{
p2=p2_beg;
void poly_add(struct node *p1,struct node *p2)
{
struct node *start3;
start3=NULL;
while(p1!=NULL && p2!=NULL)
{
if(p1->expo > p2->expo)
{
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
}
else if(p2->expo > p1->expo)
{
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
}
else if(p1->expo==p2->expo)
{
start3=insert(start3,p1->coef+p2->coef,p1->expo);
p1=p1->link;
p2=p2->link;
}
}
while(p1!=NULL)
{
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
}
while(p2!=NULL)
{
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
}
printf("\n\nAddtion of polynomial is : ");
display(start3);
}
while(p2!=NULL)
{
start3=insert_s(start3,p1->coef*p2->coef,p1->expo+p2->expo);
p2=p2->link;
}
p1=p1->link;
}
printf("\n\nMultiplication of polynomial is : \n\n");
display(start3);
}

OUTPUT:
                                                                        
                                         
        Enter polynomial 1 :                                            
                                                                        
                                                                        
                                                                        
Enter the number of terms : 2                                           
                                                                        
                                                                        
                                                                        
Enter coeficient for term 1 : 3                                         
                                                                        
                                                                        
                                                                        
Enter exponent for term 1 : 2                                           
                                                                        
                                                                        
                                                                        
Enter coeficient for term 2 : 5                                         
                                                                        
                                                                        
                                                                        
Enter exponent for term 2 : 3                                           
                                                                        
                                                                        
                                                                        
        Enter polynomial 2 :                                            
                                                                        
                                                                        
                                                                        
Enter the number of terms : 3                                           
                                                                        
                                                                        
                                                                        
Enter coeficient for term 1 : 7                                         
                                                                        
                                                                        
                                                                        
Enter exponent for term 1 : 2                                           
                                                                        
                                                                        
                                                                        
Enter coeficient for term 2 : 6                                         
                                                                        
                                                                        
                                                                        
Enter exponent for term 2 : 4                                           
                                                                        
                                                                        
                                                                        
Enter coeficient for term 3 : 9                                         
                                                                        
                                                                        
                                                                        
Enter exponent for term 3 : 3                                           
                                                                        
                                                                        
                                                                        
        Polynomial 1 is : (5.0x^3) + (3.0x^2)                           
                                                                        
                                                                        
                                                                        
        Polynomial 2 is : (6.0x^4) + (9.0x^3) + (7.0x^2)                
                                                                        
                                                                        
                                                                        
                                                                        
                                                                        
Addtion of polynomial is : (6.0x^4) + (14.0x^3) + (10.0x^2)
Multiplication of  polynomial is :                                      
                                                                        
                                                                        
                                                                        
(30.0x^7) + (45.0x^6) + (18.0x^6) + (35.0x^5) + (27.0x^5) + (21.0x^4)