Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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);
}
if (top1 == NULL)
{
printf("Stack is empty");
return;
}
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;
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);
}
rear = temp;
}
count++;
}
void display()
{
front1 = front;
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
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)