Professional Documents
Culture Documents
DSA Lab 6
QUESTION 1----> Add and multiply two polynomials
using linked list
#include<stdio.h>
#include<stdlib.h>
struct node
{
float coef;
int expo;
struct node *link;
};
main( )
{
printf("Polynomial 1 is : ");
display(start1);
printf("Polynomial 2 is : ");
display(start2);
poly_add(start1, start2);
poly_mult(start1, start2);
//structure - create
struct node *create(struct node *start)
{
int i,n,ex;
float co;
for(i=1;i<=n;i++)
{
return start;
//structure - insert
struct node *insert_s(struct node *start,float co,int ex)
{
tmp->link=start;
start=tmp;
}
else
{
ptr=start;
return start;
//structure - insert
struct node *insert(struct node *start,float co,int ex)
{
struct node *ptr,*tmp;
tmp->link=start;
start=tmp;
}
else /*Insert at the end of the list*/
{
ptr=start;
while(ptr->link!=NULL)
ptr=ptr->link;
tmp->link=ptr->link;
ptr->link=tmp;
}
return start;
}
//display
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)
{
else if(p1->expo==p2->expo)
{
start3=insert(start3,p1->coef+p2->coef,p1->expo);
p1=p1->link;
p2=p2->link;
}
}
while(p1!=NULL)
{
p2=p2_beg;
while(p2!=NULL)
{
start3=insert_s(start3,p1->coef*p2->coef,p1->expo+p2-
>expo);
p2=p2->link;
}
p1=p1->link;
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
struct Stack {
int top;
unsigned capacity;
int* array;
};
int main()
{
struct Stack* stack = createStack(100);
push(stack, 100);
push(stack, 20);
push(stack, 30);
return 0;
}
OUTPUT
100 pushed to stack
20 pushed to stack
30 pushed to stack
30 popped from stack
QUESTION 3---->
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
}*top = NULL; // Initially the list is empty
void push(int);
void pop();
void display();
int main()
{
int choice, value;
printf("\nIMPLEMENTING STACKS USING LINKED
LISTS\n");
while(1){
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nEnter the value to insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
break;
void pop()
{
if(top == NULL)
printf("\nEMPTY STACK\n");
else{
struct Node *temp = top;
printf("\nPopped Element : %d", temp->data);
printf("\n");
top = temp->next; // After popping, make the next node
as TOP
free(temp);
}}
void display()
{
// Print the stack
if(top == NULL)
printf("\nEMPTY STACK\n");
else
{
printf("The stack is \n");
struct Node *temp = top;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL\n\n",temp->data);
}}
Question 4---->
Write a program to copy the contents of one stack to
another stack in the same order without using any extra
data structure
#include <stdio.h>
int stackA[20],stackB[20],stackC[20];
int sizeA=0,sizeB=0,sizeC=0;
}
void B_to_C(){
while ( sizeB > 0 )
{
int x;
x = Pop( stackB, &sizeB);
Push( stackC, x, &sizeC);
}
}
int main()
{
int size, iterI, item;
printf("Enter the size of the stack ");
scanf("%d",&size);
printf("Now enter the elements ");
for(iterI = 0; iterI<size; iterI++)
{
scanf("%d",&item);
Push( stackA, item, &sizeA);
}
A_to_B();
B_to_C();
for(int i=0;i<size;i++){
printf("%d ",stackB[i]);
}
}
QUESTION 5---->
#include <stdio.h>
int main()
{
int i,j,r,c,h=0,a=0,b=0;
printf("Enter the no of rows: ");
scanf("%d",&r);
printf("Enter the no of cols: ");
scanf("%d",&c);
int mat[r][c];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
printf("Element for (%d,%d): ",i+1,j+1);
scanf("%d",&mat[i][j]);
}
printf("\nThe matrix is:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d ",mat[i][j]);
if(mat[i][j]!=0)
{
if(i>a)
{
a=i;
}
if(j>b)
{
b=j;
}
h++;
}
}
printf("\n");
}
int t1[h+1][3],t2[h+1][3],s[h+1][3],z=1;
t1[0][0]=r;
t1[0][1]=c;
t1[0][2]=h;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
if(mat[i][j]!=0)
{
t1[z][0]=i+1;
t1[z][1]=j+1;
t1[z][2]=mat[i][j];
z++;
}
printf("\n3-tuple form:\n");
for(i=0;i<h+1;i++)
{
for (j=0;j<3;j++)
printf("%d ",t1[i][j]);
printf("\n");
}
t2[0][0]=a+1;
t2[0][1]=b+1;
t2[0][2]=h;
for(i=1;i<h+1;i++)
for(j=0;j<3;j++)
t2[i][j]=t1[i][j];
printf("\nReduced form:\n");
for(i=0;i<h+1;i++)
{
for (j=0;j<3;j++)
printf("%d ",t2[i][j]);
printf("\n");
}
for(i=0;i<h+1;i++)
for(j=0;j<3;j++)
s[i][j]=t1[i][j]+t2[i][j];
printf("\nSum of two 3-tuple matrices is:\n");
for(i=0;i<h+1;i++)
{
for(j=0;j<3;j++)
printf("%d ",s[i][j]);
printf("\n");
}
return 0;
}