Q:1- WRITE A PROGRAM TO SEARCH AN ELEMENT IN AN
ARRAY.(LINEAR SEARCH)
#include
<stdio.h>
int
search();
int
main(void)
{
int
i, arr[10], n;
printf("\n
Enter the size of array :");
scanf("%d",&n);
printf("\n
Enter the array :");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
int x;
printf("\n
Enter the element which do u want to search :");
scanf("%d",&x);
int
result=search(arr,n,x);
(result==-1)?printf("element
is not present in array"):printf("element is present at index
%d",result+1);
return 0;
}
int
search(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
return i ;
return -1;
}
Q:2- WRITE A PROGRAM TO SEARCH AN ELEMENT IN AN ARRAY ( BINARY SEARCH).
#include
<stdio.h>
#include<math.h>
int binarysearch();
int main(void)
{
int
i, arr[10],n;
printf("\n
Enter the size of array :");
scanf("%d",&n);
printf("\n
Enter the array :");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
int
x;
printf("\n
Enter the element which do u want to search :");
scanf("%d",&x);
int
result=binarysearch(arr,0,n-1,x);
(result==-1)?printf("Element
is not present in array"):printf("Element is present at index : %d",result+1);
return
0;
}
int binarysearch(int arr[], int f, int l, int x)
{
while
(f <= l)
{
int
m = floor((f+1) / 2);
if
(arr[m] == x) // Check if x is present at mid
return
m;
if
(arr[m] < x) // If x greater, ignore left half
f
= m + 1;
else // If x is smaller, ignore right half
l
= m - 1;
}
return
-1; // if we reach here, then element was not
present
}
Q:3- WRITE A PROGRAM TO STORE SPARSE MATRIX USING AN
ARRAY.(SPARSE MATRIX)
#include <stdio.h>
int
main()
{
int a[10][10], b[10][3], m, n, i , j, s=0 ;
printf("Enter the size of matrix : ");
scanf("%d %d", &m , &n);
printf("\n Enter the matrix :");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("\n The matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]!=0)
{
b[s][0]=i;
b[s][1]=j;
b[s][2]=a[i][j];
s++;
}
}
}
printf(" \nThe sparse matrix is :\n");
printf("\n\t ROW\tCOL\tVAL\n");
for(i=0;i<s;i++)
{
for(j=0;j<3;j++)
{
printf("\t%d",b[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
Q:4- WRITE A PROGRAM
FOR STATIC IMPLEMENTATIOMN OF STACK USING ARRAY.( STACK)
#include <stdio.h>
#define MAXSIZE 10
void
push();
void
pop();
void
display();
int top = -1,stack[MAXSIZE],item;
int
main()
{
int choice;
char ch;
do
{
printf("\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. DISPLAY");
printf("\n ENTER YOURE
CHOICE:");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
default: printf("\nentered wrong
choice");
break;
}
printf("\n Do You Want To Continue :");
scanf("%c",&ch);
scanf("%c",&ch);
}
while(ch =='y');
return 0;
}
int
isempty()
{
if(top == -1)
return 1;
else
return 0;
}
int
isfull()
{
if(top == MAXSIZE-1)
return 1;
else
return 0;
}
void
push()
{
printf("Enter the item : ");
scanf("%d",&item);
if(!isfull())
{
top = top + 1; //Incrementing top position
stack[top] = item; //Inserting element on incremented
position
}
else
{
printf("Could not insert data, Stack is
Overflow.\n");
}
}
void
pop()
{
if(!isempty())
{
top = top - 1;
}
else
{
printf("Stack is empty. Underflow condition!\n");
}
}
void
display()
{
int i;
for(i=top; i>=0; i--)
{
printf("%d", stack[i]);
}
}
Q:5- WRITE A PROGRAM FOR STATIC IMPLEMENTATION OF QUEUE
USING ARRAY.( QUEUE)
#include <stdio.h>
#define MAXSIZE 10
void enqueue();
void dequeue();
void display();
int front= -1,rear=-1,queue[MAXSIZE],item;
int main()
{
int
choice;
char ch;
do
{
printf("\n 1. ENQUEUE");
printf("\n 2. DEQUEUE");
printf("\n 3. DISPLAY");
printf("\n ENTER YOURE
CHOICE:");
scanf("%d",&choice);
switch(choice)
{
case 1:enqueue();
break;
case 2:dequeue();
break;
case 3:display();
break;
default: printf("\nentered wrong choice");
break;
}
printf("\n Do You Want To Continue");
scanf("%c",&ch);
scanf("%c",&ch);
}
while(ch =='y');
return 0;
}
void enqueue()
{
int
item;
printf("Insert the element in queue: ");
scanf("%d",&item);
if(rear==MAXSIZE-1)
printf("Queue Overflow \n");
else if(front==-1&&rear==-1)
{
front=rear=0;
queue[rear]=item;
}
else
{
rear=rear+1;
queue[rear]=item;
}
}
void dequeue()
{
if
(front == - 1)
{
printf("Queue Underflow \n");
return ;
}
else
if(front==rear)
{
front = rear = -1;
}
else
{
printf("Deleted Element is : %d\n", queue[front]);
front = front + 1;
}
}
void display()
{
int
i;
if(front==-1
&& rear==-1)
{
printf("The queue is Empty\n");
}
else
{
for(i= front; i<=rear;i++)
{
printf("%d
",queue[i]);
}
}
}
Q:6- WRITE A PROGRAM
OF STATIC IMPLEMENTATION OF QUEUE USING ARRAY.(
CIRCULAR QUEUE)
#include<stdio.h>
#define MAXSIZE 3
int front=-1, rear=-1,cqueue[MAXSIZE], item;
void enqueue();
void dequeue();
void display();
int main()
{
int
choice;
char ch;
do{
printf("\n 1. ENQUEUE");
printf("\n 2. DEQUEUE");
printf("\n 3. DISPLAY");
printf("\n ENTER YOUR CHOICE:");
scanf("%d", &choice);
switch(choice){
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3: display();
break;
default: printf("\n Wrong Choice.");
break;
}
printf("\nDo you want to continue:");
scanf("%c",&ch);
scanf("%c",&ch);
}
while(ch=='y');
return 0;
}
void enqueue()
{
int item;
printf("Insert the element in queue :
");
scanf("%d", &item);
if (front==(rear+1)%MAXSIZE)
printf("circular Queue Overflow \n");
else if (front == -1 && rear == -1 )
//If queue is initially empty
{
front = rear = 0;
cqueue[rear] = item;
}
else
{
rear = (rear + 1)%MAXSIZE;
cqueue[rear] = item;
}
}
void dequeue()
{
if
(front == - 1)
{
printf("circular Queue Underflow \n");
}
else
if(front==rear)
{
front = rear = -1;
}
else
{
printf("Deleted Element is : %d\n", cqueue[front]);
front = (front + 1)%MAXSIZE;
}
}
void display()
{
int
i;
if(front==-1
&& rear==-1)
{
printf("The queue is Empty\n");
}
else
{
for(i= front; i!=rear;i=(i+1)%MAXSIZE)
{
printf("%d
",cqueue[i]);
}
printf("%d ",cqueue[i]);
}
}
Q:7- WRITE A PROGRAM OF( SINGLY LINKED
LIST).
#include<stdio.h>
#include<malloc.h>
struct node
{
int
data;
struct node *next;
};
struct node *start=NULL;
void create();
void display();
void insert_begin();
void insert_end();
void insert_after_specific();
void delete_first();
void delete_last();
void delete_specific();
int length();
void reverse();
void exit();
void main()
{
int
ch;
char choice;
while(1)
{
printf("\n1. Create List
\n");
printf("2. Display List
\n");
printf("3. Insert at Beginning
\n");
printf("4. Insert after Specific
\n");
printf("5. Insert at Last
\n");
printf("6. Delete First
\n");
printf("7. Delete Last
\n");
printf("8. Delete Specific
\n");
printf("9.
Length \n");
printf("10.Reverse \n");
printf("11. Exit \n");
printf("Enter Your Choice :");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert_begin();
break;
case 4: insert_after_specific();
break;
case 5: insert_end();
break;
case 6: delete_first();
break;
case 7: delete_last();
break;
case 8: delete_specific();
break;
case 9: length();
break;
case 10: reverse();
break;
case 11:exit(1);
default:printf("Invalid Input!\)n");
}
}
}
void create()
{
char ch;
struct node *temp=(struct node*)malloc(sizeof(struct node*));
printf("Enter node data :");
scanf("%d",&temp->data);
temp->next=NULL;
start=temp;
do
{
struct node *temp1=(struct node*)malloc(sizeof(struct node*));
printf("Enter next node data :");
scanf("%d",&temp1->data);
temp->next=temp1;
temp=temp1;
printf("Press y for more node :\n");
scanf("%c",&ch);
scanf("%c",&ch);
}
while(ch == 'y');
temp->next=NULL;
}
void insert_begin()
{
struct node *temp=(struct node*)malloc(sizeof(struct node*));
printf("Enter node data :");
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
temp->next=start;
start=temp;
}
}
void insert_end()
{
struct node *temp=(struct node*)malloc(sizeof(struct node*));
printf("Enter node data :");
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
struct node *loc=start;
while(loc->next!=NULL)
{
loc=loc->next;
}
loc->next=temp;
}
}
void insert_after_specific()
{
int
x;
struct node *temp1=(struct node*)malloc(sizeof(struct node*));
printf("Enter the Data Which Do You Want To Insert:");
scanf("%d",&temp1->data);
printf("Enter the Node After Which Do You Want To Insert:");
scanf("%d",&x);
struct node *loc=start;
while(loc->data!=x)
{
loc=loc->next;
}
temp1->next=loc->next;
loc->next=temp1;
}
void delete_first()
{
struct node *temp=start;
if(start==NULL)
{
printf("List is Empty\n");
}
else
{
start=start->next;
free(temp);
}
}
void delete_last()
{
if(start==NULL)
{
printf("List is Empty\n");
}
else
{
struct node *loc=start;
struct node *temp;
while(loc->next!=NULL)
{
temp=loc;
loc=loc->next;
}
temp->next=NULL;
free(loc);
}
}
void delete_specific()
{
int
x;
struct node *temp1;
struct node *temp=start;
printf("Enter The Node Do You Want To Delete");
scanf("%d",&x);
while(temp->next!=NULL)
{
if(temp->next->data==x)
{
temp1=temp->next;
temp->next=temp1->next;
free(temp1);
}
else
{
temp=temp->next;
}
}
}
void display()
{
struct node *temp=start;
if(start==NULL)
{
printf("No Nodes In The List");
}
else
{
while(temp!=NULL)
{
printf("%d ->",temp -> data);
temp=temp->next;
}
}
}
int length()
{
struct node *temp=start;
if(start==NULL)
{
printf("\n No nodes in the Linked list\n");
}
else
{
int count=0;
while(temp!=NULL)
{
count++;
temp=temp->next;
}
printf("%d\n",count);
}
}
void reverse()
{
struct node *current=start;
struct node *prev=NULL,*next=NULL;
while(current!=NULL)
{
next=current->next;
current->next=prev;
prev=current;
current=next;
}
start=prev;
}
4.INSERT AFTER
SPECIFIC

5.INSERT AT LAST

6.DELETE FIRST

7.DELETE LAST

8.DELETE SPECIFIC

9.LENGTH (TOTAL NO.
OF NODES)

10.REVERSE
Q:8- WRITE A PROGRAM OF( CIRCULAR
LINKED LIST).
#include<stdio.h>
#include<malloc.h>
struct node
{
int
data;
struct node *next;
};
struct node *start=NULL;
void create();
void display();
void insert_begin();
void insert_end();
void insert_after_specific();
void delete_first();
void delete_last();
void delete_specific();
int length();
void reverse();
void exit();
void main()
{
int
ch;
char choice;
while(1)
{
printf("\n1. Create List
\n");
printf("2. Display List
\n");
printf("3. Insert at Beginning
\n");
printf("4. Insert after Specific
\n");
printf("5. Insert at Last
\n");
printf("6. Delete First
\n");
printf("7. Delete Last
\n");
printf("8. Delete Specific
\n");
printf("9. Length \n");
printf("10.Reverse \n");
printf("11. Exit \n");
printf("Enter Your Choice :");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert_begin();
break;
case 4: insert_after_specific();
break;
case 5: insert_end();
break;
case 6: delete_first();
break;
case 7: delete_last();
break;
case 8: delete_specific();
break;
case 9: length();
break;
case 10: reverse();
break;
case 11:exit(1);
default:printf("Invalid Input!\n");
}
}
}
void create()
{
char ch;
struct node *temp=(struct node*)malloc(sizeof(struct node*));
printf("Enter node data :");
scanf("%d",&temp->data);
temp->next=temp;
start=temp;
do
{
struct node *temp1=(struct node*)malloc(sizeof(struct node*));
printf("Enter next node data :");
scanf("%d",&temp1->data);
temp->next=temp1;
temp=temp1;
printf("Press y for more node :");
scanf("%c",&ch);
scanf("%c",&ch);
}
while(ch == 'y');
temp->next=start;
}
void insert_begin()
{
struct node *last;
struct node *temp=(struct node*)malloc(sizeof(struct node*));
printf("Enter node data :");
scanf("%d",&temp->data);
if(start==NULL)
{
temp->next=temp;
start=temp;
}
else
{
last=start;
while(last->next!=start)
{
last=last->next;
}
temp->next=start;
start=temp;
last->next=start;
}
}
void insert_end()
{
struct node *temp=(struct node*)malloc(sizeof(struct node*));
printf("Enter node data :");
scanf("%d",&temp->data);
if(start==NULL)
{
temp->next=temp;
start=temp;
}
else
{
struct node *last=start;
while(last->next!=start)
{
last=last->next;
}
last->next=temp;
temp->next=start;
}
}
void insert_after_specific()
{
int
x;
struct node *temp1=(struct node*)malloc(sizeof(struct node*));
printf("Enter the Data Which Do You Want To Insert:");
scanf("%d",&temp1->data);
printf("Enter the Node After Which Do You Want To Insert:");
scanf("%d",&x);
struct node *loc=start;
while(loc->data!=x)
{
loc=loc->next;
}
temp1->next=loc->next;
loc->next=temp1;
}
void delete_first()
{
struct node *temp=start;
if(start==NULL)
{
printf("List is Empty\n");
}
else
{
struct node *temp=start;
struct node*last=start;
while(last->next!=start)
{
last=last->next;
}
start=start->next;
free(temp);
last->next=start;
}
}
void delete_last()
{
if(start==NULL)
{
printf("List is Empty\n");
}
else
{
struct node *last=start;
struct node *temp;
while(last->next!=start)
{
temp=last;
last=last->next;
}
temp->next=start;
free(last);
}
}
void delete_specific()
{
int
x;
struct node *temp1;
struct node *temp=start;
printf("Enter The Node Do You Want To Delete");
scanf("%d",&x);
while(temp->next!=NULL)
{
if(temp->next->data==x)
{
temp1=temp->next;
temp->next=temp1->next;
free(temp1);
}
else
{
temp=temp->next;
}
}
}
void display()
{
struct node *temp=start;
if(start==NULL)
{
printf("No Nodes In The List");
}
else
{
while(temp->next!=start)
{
printf("%d ->",temp -> data);
temp=temp->next;
}
printf("%d ->",temp -> data);
}
}
int length()
{
struct node *temp=start;
if(start==NULL)
{
printf("\n No nodes in the Linked list\n");
}
else
{
int count=1;
while(temp->next!=start)
{
count++;
temp=temp->next;
}
printf("%d\n",count);
}
}
void reverse()
{
struct node *current=start;
struct node *prev=start,*next=start;
do
{
next=current->next;
current->next=prev;
prev=current;
current=next;
}
while(current!=start);
start=prev;
current->next=start;
}
Q:9- WRITE A PROGRAM OF DOUBLY
LINKED LIST.
#include<stdio.h>
#include<malloc.h>
struct node
{
int
data;
struct node *prev;
struct node *next;
};
struct node *start=NULL;
struct node *last=NULL;
void create();
void forward_display();
void backward_display();
void insert_begin();
void insert_end();
void insert_after_specific();
void delete_first();
void delete_last();
void delete_specific();
void exit();
void main()
{
int
ch;
char choice;
while(1)
{
printf("\n1. Create List
\n");
printf("2. Display List
\n");
printf("3. Insert at Beginning
\n");
printf("4. Insert after Specific
\n");
printf("5. Insert at Last
\n");
printf("6. Delete First \n");
printf("7. Delete Specific
\n");
printf("8. Delete Last
\n");
printf("9. Reverse \n");
printf("10. Exit \n");
printf("Enter Your Choice :");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: forward_display();
break;
case 3: insert_begin();
break;
case 4: insert_after_specific();
break;
case 5: insert_end();
break;
case 6: delete_first();
break;
case 7: delete_specific;
break;
case 8: delete_last();
break;
case 9: backward_display();
break;
case 10:exit(1);
default:printf("Invalid Input!\n");
}
}
}
void create()
{
char ch;
struct node *temp=(struct node*)malloc(sizeof(struct node*));
printf("Enter node data :");
scanf("%d",&temp->data);
temp->prev=temp->next=NULL;
start=last=temp;
do
{
struct node *temp=(struct node*)malloc(sizeof(struct node*));
printf("Enter next node data :");
scanf("%d",&temp->data);
last->next=temp;
temp->prev=last;
last=temp;
printf("Press y for more node :");
scanf("%c",&ch);
scanf("%c",&ch);
}
while(ch=='y');
last->next=NULL;
}
void insert_begin()
{
struct node *last;
struct node *temp=(struct node*)malloc(sizeof(struct node*));
printf("Enter node data :");
scanf("%d",&temp->data);
if(start==NULL)
{
temp->prev=temp->next=NULL;
start=last=temp;
}
else
{
start->prev=temp;
temp->next=start;
temp->prev=NULL;
start=temp;
}
}
void insert_end()
{
struct node *temp=(struct node*)malloc(sizeof(struct node*));
printf("Enter node data :");
scanf("%d",&temp->data);
if(start==NULL)
{
temp->prev=temp->next=NULL;
start=last=temp;
}
else
{
last->next=temp;
temp->prev=last;
temp->next=NULL;
last=temp;
}
}
void insert_after_specific()
{
int
x;
struct node *temp=start;
struct node *temp1=(struct node*)malloc(sizeof(struct node*));
printf("Enter the Data Which Do You Want To Insert:");
scanf("%d",&temp1->data);
printf("Enter the Node After Which Do You Want To Insert:");
scanf("%d",&x);
while(temp->data!=x)
{
temp=temp->next;
}
temp1->next=temp->next;
temp->next->prev=temp1;
temp->next=temp1;
temp1->prev=temp;
}
void delete_first()
{
struct node *temp=start;
if(start==NULL)
{
printf("List is Empty\n");
}
else if(start->next==NULL)
start=last=NULL;
else
{
start=start->next;
start->prev=NULL;
}
free(temp);
}
void delete_last()
{
struct node *temp=last;
if(start==NULL)
{
printf("List is Empty\n");
}
else if(start->next==NULL)
start=last=NULL;
else
{
last=last->prev;
last->next=NULL;
}
free(temp);
}
void delete_specific()
{
int
x;
struct node *temp=start;
printf("Enter The Node Do You Want To Delete :");
scanf("%d",&x);
while(temp->data!=x)
{
temp=temp->next;
}
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
}
void forward_display()
{
struct node *temp=start;
if(start==NULL)
{
printf("No Nodes In The List");
}
else
{
while(temp!=NULL)
{
printf("%d ->",temp -> data);
temp=temp->next;
}
}
}
void backward_display()
{
struct node *temp=last;
if(start==NULL)
{
printf("No Nodes In The List");
}
else
{
while(temp!=NULL)
{
printf("%d ->",temp -> data);
temp=temp->prev;
}
}
}
Q:10- WRITE A PROGRAM
TO ADD, SUBTRCAT AND MULTIPLY THE POLYNOMIAL USING LINKED LIST.
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct
node
{
int coeff;
int pow;
struct node *next;
};
struct
node *poly1=NULL, *poly2=NULL, *poly=NULL;
void polyadd (struct node*, struct node*, struct node*);
void polysub (struct node*, struct node*, struct node* );
void polymul (struct node*, struct node*, struct node* );
void display (struct node*);
void create (struct node*);
void
exit();
// Main Function
void main()
{
poly1=(struct node*)malloc(sizeof(struct
node*));
poly2=(struct node*)malloc(sizeof(struct
node*));
printf("Enter First polynomial :\n
");
create (poly1);
printf("Enter Second polynomial :\n
");
create (poly2);
printf("\n1st equation : ");
display(poly1);
printf("\n2nd equation :");
display(poly2);
poly=(struct node*)malloc(sizeof(struct
node*));
int ch;
char choice;
while(1)
{
printf("\n\n1.ADDITION\n");
printf("2.SUBTRACTION\n");
printf("3.MULTIPLICATION\n");
printf("4.EXIT\n");
printf("Enter Your Choice
:");
scanf("%d",&ch);
switch(ch)
{
case 1: polyadd(poly1,poly2,poly);
printf("\nResult is: ");
display (poly);
break;
case 2: polysub(poly1,poly2,poly);
printf("\nResult is: ");
display (poly);
break;
case 3: polymul(poly1,poly2,poly);
printf("\nResult is: ");
display (poly);
break;
case 4:exit(1);
default:printf("Invalid
Input!\n");
}
}
}
// Create Function
void create(struct node *temp)
{
char ch;
do
{
printf("Enter Coefficient :");
scanf("%d",&temp->coeff);
printf("Enter Power :");
scanf("%d",&temp->pow);
temp->next=(struct
node*)malloc(sizeof(struct node*));
temp=temp->next;
temp->next=NULL;
printf("\nCONTINUE ?? (y/n)
:");
scanf("%c", &ch);
scanf("%c", &ch);
}
while(ch=='y');
}
// Display Linked List
void display(struct node *temp)
{
while(temp->next!=NULL)
{
printf("%dx^%d",temp->coeff,temp->pow);
temp=temp->next;
if(temp->next!=NULL)
printf("+");
}
}
// Function Adding Two Polynomial Numbers
void polyadd(struct node *poly1,struct node *poly2,struct node
*poly)
{
while(poly1->next!=NULL &&
poly2->next!=NULL)
{
//If power
of 1st Polynomial is greater than 2nd
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
//If power
of 2nd Polynomial is greater than 1st
else if(poly2->pow>poly1->pow)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly->next;
}
//If power
of both Polynomials are same
else
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff+poly2->coeff;
poly1=poly1->next;
poly2=poly2->next;
}
// Dynamically create new node
poly->next=(struct
node*)malloc(sizeof(struct node*));
poly=poly->next;
poly->next=NULL;
}
while(poly1->next!=NULL ||
poly2->next!=NULL)
{
if(poly1->next!=NULL)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
if(poly2->next!=NULL)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly->next;
}
poly->next=(struct
node*)malloc(sizeof(struct node*));
poly=poly->next;
poly->next=NULL;
}
}
// Function Subtracting Two Polynomial Numbers
void polysub(struct node *poly1,struct node *poly2,struct node
*poly)
{
while(poly1->next!=NULL &&
poly2->next!=NULL)
{
//If power
of 1st Polynomial is greater than 2nd
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
//If power
of 2nd Polynomial is greater than 1st
else if(poly2->pow>poly1->pow)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly->next;
}
//If power
of both Polynomials are same
else
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff-poly2->coeff;
poly1=poly1->next;
poly2=poly2->next;
}
// Dynamically create new node
poly->next=(struct
node*)malloc(sizeof(struct node*));
poly=poly->next;
poly->next=NULL;
}
while(poly1->next!=NULL ||
poly2->next!=NULL)
{
if(poly1->next!=NULL)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
if(poly2->next!=NULL)
{
poly->pow= poly2->pow;
poly->coeff= poly2->coeff;
poly2=poly->next;
}
poly->next=(struct
node*)malloc(sizeof(struct node*));
poly=poly->next;
poly->next=NULL;
}
}
// Function multiplying Two Polynomial Numbers
void polymul(struct node *poly1,struct node *poly2,struct node
*poly)
{
while(poly1->next!=NULL &&
poly2->next!=NULL)
{
//If power
of 1st Polynomial is greater than 2nd
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
//If power
of 2nd Polynomial is greater than 1st
else if(poly2->pow>poly1->pow)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly->next;
}
//If power
of both Polynomials are same
else
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff *
poly2->coeff;
poly1=poly1->next;
poly2=poly2->next;
}
// Dynamically create new node
poly->next=(struct
node*)malloc(sizeof(struct node*));
poly=poly->next;
poly->next=NULL;
}
while(poly1->next!=NULL ||
poly2->next!=NULL)
{
if(poly1->next!=NULL)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
if(poly2->next!=NULL)
{
poly->pow= poly2->pow;
poly->coeff= poly2->coeff;
poly2=poly->next;
}
poly->next=(struct
node*)malloc(sizeof(struct node*));
poly=poly->next;
poly->next=NULL;
}
}
Q:11- WRITE A PROGRAM
FOR DYNAMIC IMPLEMENTATION OF STACK USING LINKED LIST.(STACK)
#include <stdio.h>
#include<malloc.h>
void push();
void pop();
void display();
struct node
{
int
data;
struct
node *next;
};
struct node *top=NULL;
void main()
{
int
choice;
char ch;
do{
printf("\n 1.push");
printf("\n 2.pop");
printf("\n 3.display");
printf("\n enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
}
printf("\n do u want to continue");
scanf("%c",&ch);
scanf("%c",&ch);
}
while(ch=='y');
}
void push()
{
struct node *temp=(struct
node*)malloc(sizeof(struct node*));
if (temp==NULL)
{
printf("overflow");
}
else
{
printf("enter the node");
scanf("%d",&temp->data);
temp->next = top; // put top pointer
reference into temp link
top = temp; //
make temp as top of stack
}
}
void pop()
{
struct node* temp;
if (top == NULL) // check for stack underflow
{
printf("\nStack Underflow");
}
else
{
temp = top; // top assign into
temp
top = top->next; // assign second node to top
free(temp); // release memory of
top node
}
}
void display()
{
struct node *temp=top;
if (top == NULL)
{
printf("\n Stack is empty");
}
else
{
while(temp!=NULL)
{
printf("\n %d",temp->data);
temp=temp->next;
}
}
}
Q:12- WRITE A PROGRAM
FOR DYNAMIC IMPLEMENTATIOMN OF QUEUE USING LINKED LIST.(QUEUE)
#include
<stdio.h>
#include<malloc.h>
void enqueue();
void dequeue();
void display();
struct
node
{
int data;
struct node *next;
};
struct
node *front=NULL;
struct
node *rear=NULL;
int main()
{
int choice;
char ch;
do
{
printf("\n 1. ENQUEUE");
printf("\n 2. DEQUEUE");
printf("\n 3. DISPLAY");
printf("\n ENTER YOUR
CHOICE:");
scanf("%d", &choice);
switch(choice){
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3: display();
break;
default: printf("\n Wrong
Choice.");
break;
}
printf("\nDo you want to
continue:");
scanf("%c",&ch);
scanf("%c",&ch);
}
while(ch=='y');
return 0;
}
void enqueue()
{
struct node *temp=(struct
node*)malloc(sizeof(struct node*));
printf("Enter the node :");
scanf("%d",&temp->data);
temp->next=NULL;
if(temp==NULL)
{
printf("OVERFLOW");
}
else if(front==NULL)
{
front=rear=temp;
}
else
{
rear->next=temp;
rear=temp;
}
}
void dequeue()
{
struct node *temp;
if(front==NULL)
{
printf("\nQueue is Empty");
}
else if(front==rear)
{
front=rear=NULL;
}
else
{
temp=front;
front=front->next;
}
free(temp);
}
void display()
{
struct node *temp=front;
if(front==NULL)
{
printf("\nQueue is Empty");
}
else
{
while(temp!=NULL)
{
printf("\n%d",temp->data);
temp=temp->next;
}
}
}
Q:13- WRITE A PROGRAM
FOR DYNAMIC IMPLEMENTATION OF QUEUE USING LINKED LIST. (CIRCULAR QUEUE).
#include<stdio.h>
#include<malloc.h>
void insert();
void delete();
void display();
void
exit();
struct
node
{
int data;
struct node *next;
};
struct
node *front=NULL;
struct
node *rear=NULL;
void main()
{
int ch;
while(1)
{
printf("\n 1. Insert");
printf("\n 2. Display");
printf("\n 3. Delete");
printf("\n 4. Exit");
printf("\n Enter your choice :
");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: display();
break;
case 3: delete();
break;
case 4: exit(1);
default: printf("\n Invalid
choice!! ");
}
}
}
void insert()
{
struct node *temp=(struct
node*)malloc(sizeof(struct node*));
printf("\n Enter node data :");
scanf("%d",&temp->data);
if(temp==NULL)
printf("\n Queue is
overflow") ;
else
if(rear==NULL)
{
front = rear =temp;
}
else
{
rear->next=temp;
rear=temp;
temp->next=front;
}
}
void display()
{
struct node *temp=front;
if(front==NULL)
printf("\n Empty Queue
");
else
{
while(temp!=rear)
{
printf(" %d ->
",temp ->data);
temp = temp ->next;
}
printf(" %d ",temp -> data);
}
}
void delete()
{
struct node *temp=front;
if(front==NULL)
printf("\n Empty Queue");
else
if(front==rear)
front=rear=NULL;
else
{
front=front->next;
rear->next=front;
}
free(temp);
}
Q:14- WRITE A PROGRAM
TO IMPLEMENT SELECTION SORT.(CALL BY REFERENCE)
#include
<stdio.h>
void swap();
void selection_sort(int arr[], int n)
{
int i, j,smallest ;
for (i = 0; i < n-1; i++)
{
smallest = i;
for (j = i+1; j < n; j++)
{
if (arr[j] < arr[smallest])
{
smallest = j;
}
}
swap(&arr[smallest], &arr[i]);
}
}
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void show(int arr[], int n)
{
int i;
for (i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[10],n,i;
printf(" Enter the size :");
scanf("%d",&n);
printf(" Enter the array :");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
selection_sort(arr, n);
printf(" Sorted array: \n");
show(arr, n);
return 0;
}
Q:15- WRITE A PROGRAM
TO IMPLEMENT SELECTION SORT.(CALL BY VALUE)
#include
<stdio.h>
void selection_sort(int arr[], int n)
{
int i, j,smallest;
for (i = 0; i < n-1; i++)
{
smallest = i;
for (j = i+1; j < n; j++)
{
if (arr[j] < arr[smallest])
{
smallest = j;
}
}
int temp = arr[i];
arr[i]= arr[smallest];
arr[smallest]= temp;
}
for (i=0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int arr[10],n,i;
printf(" Enter the size :");
scanf("%d",&n);
printf(" Enter the array :");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf(" Sorted array: \n");
selection_sort(arr, n);
return 0;
}
Q:16- WRITE A PROGRAM
TO IMPLEMENT BUBBLE SORT.(CALL BY REFERENCE)
#include <stdio.h>
void swap();
void bubble_sort(int arr[], int n)
{
int
i, j,smallest;
for
(i = 0; i < n-1; i++)
{
for (j = 0; j < n-1-i; j++)
{
if (arr[j] > arr[j+1])
{
swap(&arr[j], &arr[j+1]);
}
}
}
}
void swap(int *x, int *y)
{
int
temp = *x;
*x
= *y;
*y
= temp;
}
void show(int arr[], int n)
{
int
i;
for
(i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int
arr[10],n,i;
printf(" Enter the size :");
scanf("%d",&n);
printf(" Enter the array :");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
bubble_sort(arr, n);
printf(" Sorted array: \n");
show(arr, n);
return 0;
}
Q:17- WRITE A PROGRAM
TO IMPLEMENT BUBBLE SORT.(CALL BY VALUE)
#include <stdio.h>
void bubble_sort(int arr[], int n)
{
int
i, j,smallest;
for
(i = 0; i < n-1; i++)
{
for (j = 0; j < n-1-i; j++)
{
if (arr[j] > arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for
(i=0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int
arr[10],n,i;
printf(" Enter the size :");
scanf("%d",&n);
printf(" Enter the array :");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf(" Sorted array: \n");
bubble_sort(arr, n);
return 0;
}
Q:18- WRITE A PROGRAM
TO IMPLEMENT INSERTION SORT.
#include<stdio.h>
#include<conio.h>
void insertion_sort(int arr[], int );
int main()
{
int
arr[30],i,n;;
printf(" Enter Size of Array : ");
scanf("%d",&n);
printf(" Enter the elements : ");
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
insertion_sort(arr,n);
printf(" Sorted elements :");
for(i=0; i<n; i++)
{
printf(" %d",arr[i]);
}
return
0;
}
void insertion_sort(int arr[], int n)
{
int
i,j,key;
for(i=0; i<n; i++)
{
for(j=i-1; j>=0; j--)
{
if(arr[j]>arr[j+1])
{
key=arr[j];
arr[j]=arr[j+1];
arr[j+1]=key;
}
}
}
}
Q:19- WRITE A PROGRAM
TO IMPLEMENT QUICK SORT.
#include<stdio.h>
void swap(int *x, int *y)
{
int
temp = *x;
*x
= *y;
*y
= temp;
}
int partition(int arr[],int p,int r )
{
int
pivot=arr[r];
int
i=p-1;
for(int j=p;j<=r-1;j++)
{
if(arr[j]<pivot)
{
i=i+1;
swap(&arr[i],&arr[j]);
}
}
swap(&arr[i+1],&arr[r]);
return (i+1);
}
void quick_sort(int arr[],int p,int r )
{
if(p<r)
{
int q=partition(arr,p,r);
quick_sort(arr,p,q-1);
quick_sort(arr,q+1,r);
}
}
void show(int arr[], int n)
{
int
i;
for
(i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int
arr[10],n,i;
printf(" Enter the size :");
scanf("%d",&n);
printf(" Enter the array :");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
quick_sort(arr,0, n-1);
printf(" Sorted array: \n");
show( arr, n);
return 0;
}
OUTPUT:-

Q:20- WRITE A PROGRAM
TO IMPLEMENT RADIX SORT.
#include <stdio.h>
int largest(int array[], int n)
{
int
large = array[0];
for
(int i = 1; i < n; i++)
if
(array[i] > large)
large = array[i];
return large;
}
void countingSort(int array[], int n, int place)
{
int
output[n + 1];
int
large = (array[0] / place) % 10;
for
(int i = 1; i < n; i++)
{
if
(((array[i] / place) % 10) > large)
large = array[i];
}
int
count[large + 1];
for
(int i = 0; i < large; ++i)
count[i] = 0;
for
(int i = 0; i < n; i++)
count[(array[i] / place) % 10]++;
for
(int i = 1; i < 10; i++)
count[i] += count[i - 1];
for
(int i = n - 1; i >= 0; i--)
{
output[count[(array[i]
/ place) % 10] - 1] = array[i];
count[(array[i] / place) % 10]--;
}
for
(int i = 0; i < n; i++)
array[i] = output[i];
}
void radixsort(int array[], int n)
{
int
large = largest(array, n);
for
(int place = 1; large / place > 0; place *= 10)
countingSort(array, n, place);
}
void show(int arr[], int n)
{
int
i;
for
(i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int
arr[10],n,i;
printf(" Enter the size :");
scanf("%d",&n);
printf(" Enter the array :");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
radixsort(arr,n);
printf(" Sorted array: \n");
show( arr, n);
return 0;
}
Q:21- WRITE A PROGRAM
TO IMPLEMENT MERGE SORT.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void merge(int arr[], int p, int q, int r)
{
int
i, j, k;
int
n1 = q - p + 1;
int
n2 = r - q;
int
L[n1], R[n2];
for
(i = 0; i < n1; i++)
L[i] = arr[p + i];
for
(j = 0; j < n2; j++)
R[j] = arr[q + 1 + j];
i =
0;
j =
0;
k =
p;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int p, int r)
{
if
(p < r)
{
int q = floor((p+r)/2);
mergeSort(arr, p, q);
mergeSort(arr, q+ 1, r);
merge(arr, p,q,r);
}
}
void show(int arr[], int n)
{
int
i;
for
(i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int
arr[10],n,i;
printf(" Enter the size :");
scanf("%d",&n);
printf(" Enter the array :");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
mergeSort(arr,0, n-1);
printf(" Sorted array: \n");
show( arr, n);
return 0;
}
OUTPUT:-
0 Comments