0% found this document useful (0 votes)
5 views45 pages

Data Structes Lab Manual Fixed

The document contains various C programming exercises demonstrating different search algorithms (linear and binary), sorting algorithms (bubble, insertion, selection), and data structures (singly linked list and linear queue). Each section includes code snippets, expected outputs, and user interactions for inserting and deleting elements. The exercises cover both searching for elements in arrays and manipulating linked lists and queues.

Uploaded by

balajibalajisci
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views45 pages

Data Structes Lab Manual Fixed

The document contains various C programming exercises demonstrating different search algorithms (linear and binary), sorting algorithms (bubble, insertion, selection), and data structures (singly linked list and linear queue). Each section includes code snippets, expected outputs, and user interactions for inserting and deleting elements. The exercises cover both searching for elements in arrays and manipulating linked lists and queues.

Uploaded by

balajibalajisci
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

lOMoARcPSD|36512938

1a. Given {4,7,3,2,1,7,9,0} find the location of 7 using Linear search and display its first occurance.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,x;
clrscr();
printf("Enter the size of the array : ");
scanf("%d",&n);
printf("\nEnter %d elements to be stored in the array : \n",n);
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("\nEnter the element to be searched : ");
scanf("%d",&x);
for(i=0;i<n;++i)
{
if(a[i]==x)
break;
}
if(i<n)
printf("\nElement is found at position : %d\n",i+1);
else
printf("\nEntered element not found\n");
}

OUTPUT:
Enter the size of the array : 8

Enter 8 elements to be stored in the array :


4
7
3
2
1
7
9
0

Enter the element to be searched : 7

Element is found at position : 2

1b. Given {4,7,3,2,1,7,9,0} find the location of 7 using Binary search and display its first occurance.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,low,high,mid,n,key;

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

clrscr();
printf("Enter the size of array : ");
scanf("%d",&n);
printf("\nEnter %d elements to be stored in the array : \n", n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the element to be searched : ");
scanf("%d",&key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high)
{
if(a[mid] < key)
low = mid + 1;
else if (a[mid] == key)
{
printf("\nElement found at position : %d\n",mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("\nEntered element not found\n");
}

OUTPUT:
Enter the size of array : 8

Enter 8 elements to be stored in the array :


4
7
3
2
1
7
9
0

Enter the element to be searched : 7

Element found at position : 6

2. Given {5,3,1,6,0} order the numbers in ascending order using Bubble sort Algorithm.

#include<stdio.h>
#include<conio.h>
void main()

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

{
int a[20],n,i,j,swap;
clrscr();
printf("Enter the size of array : ");
scanf("%d",&n);
printf("\nEnter %d elements to be stored in the array : \n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j] > a[j+1])
{
swap=a[j];
a[j]=a[j+1];
a[j+1]=swap;
}
}
}
printf("\nSorted Array : ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}

OUTPUT:
Enter the size of array : 5

Enter 5 elements to be stored in the array :


5
1
3
6
0

Sorted Array : 0 1 3 5 6

3a. Perform the Insertion sort on the input {7,5,8,1,16,48,3,7,0} and display the output in
descending order.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,j,size,swap;
clrscr();
printf("Enter the size of the array : ");
scanf("%d",&size);
printf("\nEnter %d elements to be stored in the array : \n",size);

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

for(i=0;i<size;i++)
scanf("%d",&a[i]);
for(i=1;i<(size);i++)
{
j=i;
while(j>0 && a[j]>a[j-1])
{
swap=a[j];
a[j]=a[j-1];
a[j-1]=swap;
j--;
}
}
printf("\nSorted array : ");
for(i=0;i<size;i++)
printf("%d ",a[i]);
printf("\n");
}

OUTPUT:
Enter the size of the array : 9

Enter 9 elements to be stored in the array :


7
5
8
1
16
48
3
7
0

Sorted array : 48 16 8 7 7 5 3 1 0

3b. Perform the Selection sort on the input {7,5,8,1,16,48,3,7,0} and display the output in
descending order.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,j,position,swap;
clrscr();
printf("Enter the size of the array : ");
scanf("%d",&n);
printf("\nEnter %d elements to be stored in the array : \n",n);
for (i=0;i<n;i++)
scanf("%d",&a[i]);

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

for(i=0;i<n-1;i++)
{
position=i;
for(j=i+1;j<n;j++)
{
if(a[position]<a[j])
position=j;
}
if(position!=i)
{
swap=a[i];
a[i]=a[position];
a[position]=swap;
}
}
printf("\nSorted Array : ");
for(i=0;i<n;i++)
printf("%d ", a[i]);
printf("\n");
}

OUTPUT:
Enter the size of the array : 9

Enter 9 elements to be stored in the array :


7
5
8
1
16
48
3
7
0

Sorted array : 48 16 8 7 7 5 3 1 0

4. Program to insert the elements {61,16,8,27} into singly linked list and delete 8,61,27 from the
list . Display your list after each insertion and deletion.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<ctype.h>
struct node
{
int INFO;
struct node * LINK;
};

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

typedef struct node NODE;


NODE * start=NULL;
void create()
{
char ch;
int i=0;
NODE * CURPTR,* NEWNODE;
CURPTR=(NODE*)malloc(sizeof(NODE));
start=CURPTR;
while(1)
{
printf("\nEnter the value to be stored in node %d : ",i+1);
scanf("%d",&CURPTR->INFO);
printf("\nDo you want to add one more element? [y/n]\n");
ch=getch();
if(ch=='y')
{
NEWNODE=(NODE *)malloc(sizeof(NODE));
CURPTR->LINK=NEWNODE;
CURPTR=NEWNODE;
}
else
{
CURPTR->LINK=NULL;
break;
}
i++;
}
}
void insert_beg(int item)
{
NODE * NEWNODE;
NEWNODE=(NODE *)malloc(sizeof(NODE));
NEWNODE->INFO=item;
NEWNODE->LINK=start;
start=NEWNODE;
}
void delete(item)
{
char ch;
NODE * CURPTR=start, * PREVPTR=NULL;
if(start==NULL)
{
printf("\nLinked list is empty\n");
return;

}
else if(start->INFO==item)
{
start=start->LINK;
free(CURPTR);

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

return;

}
else
{
while((CURPTR->INFO!=item)&&(CURPTR!=NULL))
{
PREVPTR=CURPTR;
CURPTR=CURPTR->LINK;
}
if(CURPTR==NULL)
{
printf("\nItem not found\n");
}
else
{
PREVPTR->LINK=CURPTR->LINK;
}
}
}
void display()
{
NODE * CURPTR=start;
if(start==NULL)
printf("\nLinked list is empty\n");
else
{
printf("\nLinked List : ");
while(CURPTR!=NULL)
{
printf("%d",CURPTR->INFO);
printf("->");
CURPTR=CURPTR->LINK;
}
printf("NULL\n");
}
printf("\n");
}
void main()
{
int item;
char ch;
clrscr();
while(1)
{
printf("Create : 1\n");
printf("Insert : 2\n");
printf("Delete : 3\n");
printf("Exit : 4\n");
printf("Enter your choice : ");
scanf("%d", &ch);

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

switch(ch)
{
case 1 :start=NULL;
create();
display();
break;
case 2 :printf("\nEnter the item to be inserted : ");
scanf("%d", &item);
insert_beg(item);
display();
break;
case 3 :printf("\nEnter the item to be deleted : ");
scanf("%d", &item);
delete(item);
display();
break;
case 4 :exit(0);
default:printf("\nINVALID CHOICE\n");
}
}
}

OUTPUT:
Create : 1
Insert : 2
Delete : 3
Exit : 4
Enter your choice : 1

Enter the item to be stored in node 1 : 61

Do you want to add one more element? [y/n]

Enter the item to be stored in node 2 : 16

Do you want to add one more element? [y/n]

Enter the item to be stored in node 3 : 8

Do you want to add one more element? [y/n]

Enter the item to be stored in node 4 : 27

Do you want to add one more element? [y/n]

Linked List : 61→16→8→27→NULL

Create : 1
Insert : 2
Delete : 3
Exit : 4

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

Enter your choice : 3

Enter the item to be deleted : 8

Linked List : 61→16→27→NULL

Create : 1
Insert : 2
Delete : 3
Exit : 4
Enter your choice : 3

Enter the item to be deleted : 61

Linked List : 16→27→NULL

Create : 1
Insert : 2
Delete : 3
Exit : 4
Enter your choice : 3

Enter the item to be deleted : 27

Linked List : 16→NULL

Create : 1
Insert : 2
Delete : 3
Exit : 4
Enter your choice : 4

5. Program to insert the elements {61,16,8,27} into Linear queue and delete three elements from
the list . Display your list after each insertion and deletion.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

{
int choice;
clrscr();
while(choice != 4)
{
printf("\nInsert : 1\n");
printf("Delete : 2\n");
printf("Display the queue : 3\n");
printf("Exit : 4\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:insert();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:exit(0);
break;
default:printf("\nInvaid Choice\n");
}
}
}
void insert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter the item to be inserted : ");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nQueue is Empty\n");
}
else
{
printf("\nQueue : ");
while(ptr != NULL)
{
printf("%d ",ptr -> data);
ptr = ptr -> next;
}
printf("\n");
}
getch();
}

OUTPUT:
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 1

Enter the item to be inserted : 61

Insert : 1
Delete : 2

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

Display the queue : 3


Exit : 4
Enter your choice : 1

Enter the item to be inserted : 16

Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 1

Enter the item to be inserted : 8

Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 1

Enter the item to be inserted : 27

Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 3

Queue : 61 16 8 27

Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 2

Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 2

Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 2

Insert : 1
Delete : 2
Display the queue : 3

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

Exit : 4
Enter your choice : 3

Queue : 27

Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 4

6. Program to insert the elements {61,16,8,27} into circular queue and delete four elements from
the list . Display your list after each insertion and deletion.

#include<stdio.h>
#include<conio.h>
#define MAX 5
int cqueue_arr[MAX];
int front = -1;
int rear = -1;
void insert(int item)
{
if((front == 0 && rear == MAX-1) || (front == rear+1))
{
printf("\nQueue Overflow\n");
return;
}
if(front == -1)
{
front = 0;
rear = 0;
}
else
{
if(rear == MAX-1)
rear = 0;
else
rear = rear+1;
}
cqueue_arr[rear] = item ;
}
void deletion()
{
if(front == -1)
{
printf("\nQueue Underflow\n");
return ;
}
printf("\nElement deleted from the queue is : %d",cqueue_arr[front]);

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

printf("\n");
if(front == rear)
{
front = -1;
rear=-1;
}
else
{
if(front == MAX-1)
front = 0;
else
front = front+1;
}
}
void display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("\nQueue is empty\n");
return;
}
printf("\nQueue : ");
if( front_pos <= rear_pos )
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
else
{
while(front_pos <= MAX-1)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
}
printf("\n");
}
int main()
{
int choice,item;
clrscr();
do
{

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

printf("\nInsert : 1\n");
printf("Delete : 2\n");
printf("Display : 3\n");
printf("Quit : 4\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("\nEnter the element to be inserted : ");
scanf("%d", &item);
insert(item);
break;
case 2 :
deletion();
break;
case 3:
display();
break;
case 4:
break;
default:printf("\nInvalid Choice\n");
}
}
while(choice!=4);
return 0;
}

OUTPUT:
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 1

Enter the item to be inserted : 61

Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 1

Enter the item to be inserted : 16

Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 1

Enter the item to be inserted : 8

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 1

Enter the item to be inserted : 27

Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 3

Queue : 61 16 8 27

Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 2

Element deleted from the queue is : 61

Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 2

Element deleted from the queue is : 16

Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 2

Element deleted from the queue is : 8

Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 2

Element deleted from the queue is : 27

Insert : 1
Delete : 2

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

Display the queue : 3


Exit : 4
Enter your choice : 3

Queue is empty

Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 4

7. Program to insert the elements {61,16,8,27} into ordered singly linked list and delete 8,61,27
from the list . Display your list after each insertion and deletion.

#include<stdio.h>
#include<conio.h>
struct node
{
int INFO;
struct node *LINK;
};
typedef struct node NODE;
NODE *start=NULL;
void insertordered(int data)
{
NODE *NEWNODE=(NODE*)malloc(sizeof(NODE));
NEWNODE->INFO=data;
if(start==NULL)
{
start=NEWNODE;
start->LINK=NULL;
}
else if(data<start->INFO)
{
NEWNODE->LINK=start;
start=NEWNODE;
}
else
{
NODE *PREVPTR=start;
NODE *CURRPTR=start->LINK;
while(CURRPTR!=NULL&&data>CURRPTR->INFO)
{
PREVPTR=CURRPTR;
CURRPTR=CURRPTR->LINK;
}
PREVPTR->LINK=NEWNODE;
NEWNODE->LINK=CURRPTR;

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

}
}
void deleteordered(int data)
{
NODE *PREVPTR=start;
NODE *CURRPTR=start->LINK;
if(start==NULL)
printf("\nUnderflow\n");
else if(data==start->INFO)
{
start=CURRPTR;
free(PREVPTR);
}
else
{
while(CURRPTR!=NULL&&CURRPTR->INFO!=data)
{
PREVPTR=CURRPTR;
CURRPTR=CURRPTR->LINK;
}
if(CURRPTR!=NULL)
{
PREVPTR->LINK=CURRPTR->LINK;
free(CURRPTR);
}
else
printf("\nEntered Data Not Found In The List\n");
}
}
void display()
{
NODE *CURRPTR=start;
if(CURRPTR==NULL)
printf("Empty");
else
{
while(CURRPTR!=NULL)
{
printf("%d",CURRPTR->INFO);
printf("->");
CURRPTR=CURRPTR->LINK;
}
printf("NULL");
}
}
void main()
{
int ch,data;
clrscr();
while(1)
{

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

printf("\nInsert : 1");
printf("\nDelete : 2");
printf("\nExit : 3");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter data to be inserted : ");
scanf("%d",&data);
insertordered(data);
printf("\nLinked list after insertion is : ");
display();
printf("\n");
break;
case 2:printf("\nEnter data to be deleted : ");
scanf("%d",&data);
deleteordered(data);
printf("\nLinked list after deletion is : ");
display();
printf("\n");
break;
case 3:exit(0);
}
}
}

OUTPUT:
Insert : 1
Delete : 2
Exit : 3
Enter your choice: 1

Enter data to be inserted: 61

Linked list after insertion is : 61→NULL

Insert : 1
Delete : 2
Exit : 3
Enter your choice: 1

Enter data to be inserted : 16

Linked list after insertion is : 16→61→NULL

Insert : 1
Delete : 2
Exit : 3
Enter your choice: 1

Enter data to be inserted : 8

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

Linked list after insertion is : 8→16→61→NULL

Insert : 1
Delete : 2
Exit : 3
Enter your choice: 1

Enter data to be inserted : 27

Linked list after insertion is : 8→16→27→61→NULL

Insert : 1
Delete : 2
Exit : 3
Enter your choice: 2

Enter data to be deleted : 8

Linked list after deletion is : 16→27→61→NULL

Insert : 1
Delete : 2
Exit : 3
Enter your choice: 2

Enter data to be deleted : 61

Linked list after deletion is : 16→27→NULL

Insert : 1
Delete : 2
Exit : 3
Enter your choice: 2

Enter data to be deleted : 27

Linked list after deletion is : 16→NULL

Insert : 1
Delete : 2
Exit : 3
Enter your choice: 3

8. Program to add 6x3+10x2+0x+5 and 4x2+2x+1 using linked list.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

typedef struct link


{
int coeff;
int pow;
struct link * next;
}
my_poly;
void my_create_poly(my_poly **);
void my_show_poly(my_poly *);
void my_add_poly(my_poly **, my_poly *, my_poly *);
int main(void)
{
int ch;
clrscr();
do
{
my_poly * poly1, * poly2, * poly3;
printf("\nCreate 1st expression : \n");
my_create_poly(&poly1);
printf("\nThe 1st Polynomial Expression is : ");
my_show_poly(poly1);
printf("\n\nCreate 2nd expression : \n");
my_create_poly(&poly2);
printf("\nThe 2nd Polynomial Expression is : ");
my_show_poly(poly2);
my_add_poly(&poly3, poly1, poly2);
my_show_poly(poly3);
printf("\n\nDo you want to create the polynomial expressions from the start?(Y=1/N=0) : ");
scanf("%d", &ch);
}
while (ch);
return 0;
}
void my_create_poly(my_poly ** node)
{
int flag;
int coeff, pow;
my_poly * tmp_node;
tmp_node = (my_poly *) malloc(sizeof(my_poly));
*node = tmp_node;
do
{
printf("\nEnter Coefficient : ");
scanf("%d", &coeff);
tmp_node->coeff = coeff;
printf("\nEnter Power : ");
scanf("%d", &pow);
tmp_node->pow = pow;
tmp_node->next = NULL;
printf("\nDo you want to add more terms to the polynomial list?(Y=1/N=0) : ");
scanf("%d", &flag);

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

if(flag)
{
tmp_node->next = (my_poly *) malloc(sizeof(my_poly));
tmp_node = tmp_node->next;
tmp_node->next = NULL;
}
} while (flag);
}
void my_show_poly(my_poly * node)
{
while(node != NULL)
{
printf("%dx^%d", node->coeff, node->pow);
node = node->next;
if(node != NULL)
{
printf(" + ");
}
}
}
void my_add_poly(my_poly ** result, my_poly * poly1, my_poly * poly2)
{
my_poly * tmp_node;
tmp_node = (my_poly *) malloc(sizeof(my_poly));
tmp_node->next = NULL;
*result = tmp_node;
while(poly1 && poly2)
{
if (poly1->pow > poly2->pow)
{
tmp_node->pow = poly1->pow;
tmp_node->coeff = poly1->coeff;
poly1 = poly1->next;
}
else if (poly1->pow < poly2->pow)
{
tmp_node->pow = poly2->pow;
tmp_node->coeff = poly2->coeff;
poly2 = poly2->next;
}
else
{
tmp_node->pow = poly1->pow;
tmp_node->coeff = poly1->coeff + poly2->coeff;
poly1 = poly1->next;
poly2 = poly2->next;
}
if(poly1 && poly2)
{
tmp_node->next = (my_poly *) malloc(sizeof(my_poly));
tmp_node = tmp_node->next;

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

tmp_node->next = NULL;
}
}
while(poly1 || poly2)
{
tmp_node->next = (my_poly *) malloc(sizeof(my_poly));
tmp_node = tmp_node->next;
tmp_node->next = NULL;
if(poly1)
{
tmp_node->pow = poly1->pow;
tmp_node->coeff = poly1->coeff;
poly1 = poly1->next;
}
if(poly2)
{
tmp_node->pow = poly2->pow;
tmp_node->coeff = poly2->coeff;
poly2 = poly2->next;
}
}
printf("\n\nAddition of the polynomials is : ");
}

OUTPUT:
Create 1st Expression :

Enter Coefficient : 6

Enter Power : 3

Do you want to add more terms to the polynomial list?(Y=1/N=0) : 1

Enter Coefficient : 10

Enter Power : 2

Do you want to add more terms to the polynomial list?(Y=1/N=0) : 1

Enter Coefficient : 0

Enter Power : 1

Do you want to add more terms to the polynomial list?(Y=1/N=0) : 1

Enter Coefficient : 5

Enter Power : 0

Do you want to add more terms to the polynomial list?(Y=1/N=0) : 0

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

The 1st Polynomial Expression is : 6x^3 + 10x^2 + 0x^1 + 5x^0

Create 2nd Expression :

Enter Coefficient : 4

Enter Power : 2

Do you want to add more terms to the polynomial list?(Y=1/N=0) : 1

Enter Coefficient : 2

Enter Power : 1

Do you want to add more terms to the polynomial list?(Y=1/N=0) : 1

Enter Coefficient : 1

Enter Power : 0

Do you want to add more terms to the polynomial list?(Y=1/N=0) : 0

The 2nd Polynomial Expression is : 4x^2 + 2x^1 + 1x^0

Addition of the polynomials is : 6x^3 + 14x^2 + 2x^1 + 6x^0

Do you want to create the polynomial expressions from the start?(Y=1/N=0) : 0

9. Program to push 5,9,34,17,32 into stack and pop 3 times from the stack , also display the
popped numbers.

#include<stdio.h>
#include<conio.h>
#define size 5
int n=5,top=-1,s[size];
void push()
{
int item;
if(top==(size-1))
{
printf("\nStack Overflow\n");
}
else
{
printf("\nEnter the item to be pushed : ");
scanf("%d",&item);
top=top+1;
s[top]=item;
}

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

}
void pop()
{
if(top==-1)
{
printf("\nStack Underflow\n");
}
else
{
printf("\nPopped Element is : %d\n",s[top]);
top=top-1;
}
}
void display()
{
int i;
if(top==-1)
{
printf("\nStack is empty\n");
}
else
{
printf("\nStack elements are : \n");
for(i=top;i>=0;i--)
{
printf("\n%d\n",s[i]);
}
}
}
void main()
{
char ch;
int item;
clrscr();
while(1)
{
printf("\nPush : 1\n");
printf("Pop : 2\n");
printf("Display : 3\n");
printf("Exit : 4\n");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:exit(0);

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

default:printf("\nINVALID CHOICE\n");
}
}
}

OUTPUT:
Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 1

Enter the item to be pushed : 5

Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 1

Enter the item to be pushed : 9

Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 1

Enter the item to be pushed : 34

Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 1

Enter the item to be pushed : 17

Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 1
Enter the item to pushed : 32

Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 3

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

Stack elements are :

32

17

34

Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 2

Popped Element is : 32

Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 2

Popped Element is : 17

Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 2

Popped Element is : 34

Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 3

Stack Elements are :

Push : 1
Pop : 2
Display : 3

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

Exit : 4
Enter your choice : 4

10. Write a recursive program to find GCD of 4,6,8.

#include<stdio.h>
#include<conio.h>
int gcd(int m,int n)
{
if(n==0)
{
return(m);
}
else if(n>m)
{
return(gcd(n,m));
}
else
{
return(gcd(n,m%n));
}
}
void main()
{
int k,m,n;
clrscr();
printf("Enter three numbers : \n");
scanf("%d %d %d",&k,&m,&n);
printf("\nGCD of(%d,%d,%d) = %d\n",k,m,n,gcd(k,gcd(m,n)));
getch();
}

OUTPUT:
Enter three numbers :
4
6
8

GCD of(4,6,8) = 2

11. Program to insert the elements {5,7,0,6,3,9} into circular queue and delete 3 elements from it
(using linked list implementation).

#include<stdio.h>
#include<stdlib.h>
struct node
{

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

int data;
struct node* next;
};
struct node *front = NULL;
struct node *rear = NULL;
void enqueue(int d)
{
struct node* n;
n = (struct node*)malloc(sizeof(struct node));
n->data = d;
n->next = NULL;
if((rear==NULL)&&(front==NULL))
{
front = rear = n;
rear->next = front;
}
else
{
rear->next = n;
rear = n;
n->next = front;
}
}
void dequeue()
{
struct node* t;
t = front;
if((front==NULL)&&(rear==NULL))
{
printf("\nQueue Underflow\n");
}
else if(front == rear)
{
front = rear = NULL;
free(t);
}
else
{
front = front->next;
rear->next = front;
free(t);
}
}
void print()
{
struct node* t;
t = front;
if((front==NULL)&&(rear==NULL))
{
printf("\nQueue is Empty\n");
}

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

else
{
printf("\nQueue : ");
do
{
printf("%d ",t->data);
t = t->next;
}
while(t != front);
printf("\n");
}
}
int main()
{
int opt,data;
clrscr();
do
{
printf("\nInsert : 1\n");
printf("Delete : 2\n");
printf("Display : 3\n");
printf("Exit : 4\n");
printf("Enter your choice : ");
scanf("%d",&opt);
switch(opt)
{
case 1 : printf("\nEnter the item to be inserted : ");
scanf("%d",&data);
enqueue(data);
break;
case 2:
dequeue();
break;
case 3:
print();
break;
case 4:
break;
default : printf("\nInvalid Choice\n");
}
}
while(opt!=4);
return 0;
}

OUTPUT:
Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 1

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

Enter the item to be inserted : 5

Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 1

Enter the item to be inserted : 7

Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 1

Enter the item to be inserted : 0

Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 1

Enter the item to be inserted : 6

Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 1

Enter the item to be inserted : 3

Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 1

Enter the item to be inserted : 9

Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 3

Queue : 5 7 0 6 3 9

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 2

Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 2

Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 2

Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 3

Queue : 6 3 9

Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 4

12. Program to convert an infix expression x^y/(5*z)+2 to its postfix expression.

#include<stdio.h>
#include<conio.h>
#define max 100
int top=-1, a[max];
void push(char x)
{
a[++top]=x;
}
char pop()
{
if(top==-1)
return -1;
else
return a[top--];
}

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

int prcd(char c)
{
if(c=='(')
return 0;
else if(c=='+'||c=='-')
return 1;
else if(c=='*'||c=='/')
return 2;
}
int infixtopostfix(char infix[max],char postfix[max])
{
char temp,x;
int i=0,j=0;
while(infix[i]!='\0')
{
temp=infix[i];
if(isalnum(temp))
{
postfix[j++]=temp;
}
else if(temp=='(')
push(temp);
else if(temp==')')
{
while((x=pop())!='(')
{
postfix[j++]=x;
}
}
else
{
while(prcd(a[top])>=prcd(temp))
{
postfix[j++]=pop();
}
push(temp);
}
i++;
}
while(top!= -1)
postfix[j++]=pop();
postfix[j]='\0';
return 0;
}
int main()
{
char infix[max],postfix[max];
clrscr();
printf("Enter the infix expression : ");
gets(infix);
infixtopostfix(infix, postfix);

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

printf("\nThe postfix expression is : %s",postfix);


printf("\n");
return 0;
}

OUTPUT:
Enter the infix expression : x^y/(5*z)+2

The postfix expression is : xy^5z*/2+

13. Program to evaluate a postfix expression 5 3 + 8 2 - * .

#include<stdio.h>
#include<conio.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
clrscr();
printf("Enter the expression : ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s is : %d\n",exp,pop());
return 0;
}

OUTPUT:
Enter the expression : 53+82-*

The result of the expression 53+82-* is : 48

14. Program to create binary search tree with the elements {2,5,1,3,9,0,6} and perform inorder ,
preorder and postorder traversal.

#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
void main()
{
void insert(struct node **,int);
void inorder(struct node *);
void postorder(struct node *);
void preorder(struct node *);

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

struct node *ptr = NULL;


int data,choice,no,i,num;
char ch;
clrscr();
do
{
printf("\nInsert a new node in the Binary Tree : 1");
printf("\nDisplay the Binary Tree in Preorder Traversal : 2");
printf("\nDisplay the Binary Tree in Inorder Traversal : 3");
printf("\nDisplay the Binary Tree in Postorder Traversal : 4");
printf("\nExit : 5\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch (choice)
{
case 1 : printf("\nEnter the value to be inserted : ");
scanf("%d",&data);
insert(&ptr,data);
break;
case 2 : printf("\nPreorder Traversal of the Binary Tree : ");
preorder(ptr);
printf("\n");
break;
case 3 : printf("\nInorder Traversal of the Binary Tree : ");
inorder(ptr);
printf("\n");
break;
case 4 : printf("\nPostorder Traversal of the Binary Tree : ");
postorder(ptr);
printf("\n");
break;
case 5 :
break;
default : printf("\nInvalid Choice\n");
}
}
while (choice !=5);
}
void insert(struct node **p,int num)
{
if((*p)==NULL)
{
(*p)=malloc(sizeof(struct node));
(*p)->left = NULL;
(*p)->right = NULL;
(*p)->data = num;
return;
}
else
{
if(num==(*p)->data)

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

{
printf("\nRepeated Entry Error, Value Rejected\n");
return;
}
if(num<(*p)->data)
{
insert(&((*p)->left),num);
}
else
{
insert(&((*p)->right),num);
}
}
return;
}
void inorder(struct node *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("%d ",p->data);
inorder(p->right);
}
else
return;
}
void preorder(struct node *p)
{
if(p!=NULL)
{
printf("%d ",p->data);
preorder(p->left);
preorder(p->right);
}
else
return;
}
void postorder(struct node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("%d ",p->data);
}
else
return;
}

OUTPUT:
Insert a new node in the Binary Tree : 1

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

Display the Binary Tree in Preorder Traversal : 2


Display the Binary Tree in Inorder Traversal : 3
Display the Binary Tree in Postorder Traversal : 4
Exit : 5
Enter your choice : 1

Enter the value to be inserted : 2

Insert a new node in the Binary Tree : 1


Display the Binary Tree in Preorder Traversal : 2
Display the Binary Tree in Inorder Traversal : 3
Display the Binary Tree in Postorder Traversal : 4
Exit : 5
Enter your choice : 1

Enter the value to be inserted : 5

Insert a new node in the Binary Tree : 1


Display the Binary Tree in Preorder Traversal : 2
Display the Binary Tree in Inorder Traversal : 3
Display the Binary Tree in Postorder Traversal : 4
Exit : 5
Enter your choice : 1

Enter the value to be inserted : 1

Insert a new node in the Binary Tree : 1


Display the Binary Tree in Preorder Traversal : 2
Display the Binary Tree in Inorder Traversal : 3
Display the Binary Tree in Postorder Traversal : 4
Exit : 5
Enter your choice : 1

Enter the value to be inserted : 3

Insert a new node in the Binary Tree : 1


Display the Binary Tree in Preorder Traversal : 2
Display the Binary Tree in Inorder Traversal : 3
Display the Binary Tree in Postorder Traversal : 4
Exit : 5
Enter your choice : 1

Enter the value to be inserted : 9

Insert a new node in the Binary Tree : 1


Display the Binary Tree in Preorder Traversal : 2
Display the Binary Tree in Inorder Traversal : 3
Display the Binary Tree in Postorder Traversal : 4
Exit : 5
Enter your choice : 1

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

Enter the value to be inserted : 0

Insert a new node in the Binary Tree : 1


Display the Binary Tree in Preorder Traversal : 2
Display the Binary Tree in Inorder Traversal : 3
Display the Binary Tree in Postorder Traversal : 4
Exit : 5
Enter your choice : 1

Enter the value to be inserted : 6

Insert a new node in the Binary Tree : 1


Display the Binary Tree in Preorder Traversal : 2
Display the Binary Tree in Inorder Traversal : 3
Display the Binary Tree in Postorder Traversal : 4
Exit : 5
Enter your choice : 2

Preorder Traversal of the Binary Tree : 2 1 0 5 3 9 6

Insert a new node in the Binary Tree : 1


Display the Binary Tree in Preorder Traversal : 2
Display the Binary Tree in Inorder Traversal : 3
Display the Binary Tree in Postorder Traversal : 4
Exit : 5
Enter your choice : 3

Inorder Traversal of the Binary Tree : 0 1 2 3 5 6 9

Insert a new node in the Binary Tree : 1


Display the Binary Tree in Preorder Traversal : 2
Display the Binary Tree in Inorder Traversal : 3
Display the Binary Tree in Postorder Traversal : 4
Exit : 5
Enter your choice : 4

Postorder Traversal of the Binary Tree : 0 1 3 6 9 5 2

Insert a new node in the Binary Tree : 1


Display the Binary Tree in Preorder Traversal : 2
Display the Binary Tree in Inorder Traversal : 3
Display the Binary Tree in Postorder Traversal : 4
Exit : 5
Enter your choice : 5

15. Program to sort the following elements using heap sort {9,16,32,8,4,1,5,8,0}.

#include<stdio.h>
#include<conio.h>

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

void Adjust(int Heap_of_Numbers[],int i)


{
int j, copy, Number, Reference = 1;
Number=Heap_of_Numbers[0];
while(2*i<=Number && Reference==1)
{
j=2*i;
if(j+1<=Number && Heap_of_Numbers[j+1] > Heap_of_Numbers[j])
{
j=j+1;
}
if( Heap_of_Numbers[j] < Heap_of_Numbers[i])
{
Reference=0;
}
else
{
copy=Heap_of_Numbers[i];
Heap_of_Numbers[i]=Heap_of_Numbers[j];
Heap_of_Numbers[j]=copy;
i=j;
}
}
}
void Make_Heap(int heap[])
{
int i, Number_of_Elements;
Number_of_Elements=heap[0];
for(i=Number_of_Elements/2;i>=1;i--)
{
Adjust(heap,i);
}
}
int main()
{
int i, heap[30], NumberofElements, LastElement, CopyVariable;
clrscr();
printf("Enter the number of elements present in the unsorted array : ");
scanf("%d",&NumberofElements);
printf("\nEnter the elements of the unsorted array one by one : \n");
for(i=1;i<=NumberofElements;i++)
{
scanf("%d",&heap[i]);
}
heap[0]=NumberofElements;
Make_Heap(heap);
while(heap[0] > 1)
{
LastElement=heap[0];
CopyVariable=heap[1];
heap[1]=heap[LastElement];

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

heap[LastElement]=CopyVariable;
heap[0]--;
Adjust(heap,1);
}
printf("\nSorted Array : ");
for(i=1;i<=NumberofElements;i++)
{
printf("%d ",heap[i]);
}
printf("\n");
return 0;
}

OUTPUT:
Enter the number of elements present in the unsorted array : 9

Enter the elements of the unsorted array one by one :


9
16
32
8
4
1
5
8
0

Sorted array : 0 1 4 5 8 8 9 16 32

16. Given S1={“Flowers”} ; S2={“are beautiful”};


I. Find the length of S1
II. Concatenate S1 and S2
III. Extract the substring “low” from S1
IV. Find “are” in S2 and replace it with “is”

#include<stdio.h>
#include<conio.h>
int LENGTH(char * str)
{
int i=0;
while(str[i]!='\0')
i++;
return i;
}
void CONCAT(char *str1,char *str2)
{
int i=0,j=0;
while(str1[i]!='\0')
i++;

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

while(str2[j]!='\0')
{
str1[i]=str2[j];
i++;
j++;
}
str1[i]='\0';
}
void SUBSTR(char str[],int pos ,int len)
{
char sub[50];
int slen=LENGTH(str);
int p,j,max_ext;
if(pos>slen)
{
printf("\nInvalid Position\n");
return;
}
max_ext=slen-pos+1;
if(len>max_ext)
{
printf("\nInvalid Substring Length\n");
return;
}
p=pos-1;
for(j=0;j<len;j++)
{
sub[j]=str[p];
p++;
}
sub[j]='\0';
printf("\nSubstring : %s",sub);
printf("\n");
}
void REPLACE(char str[],char substr[],char replacestr[])
{
char output[50];
int i=0,j=0,flag=0,start=0;
while(str[i]!='\0')
{
if(str[i]==substr[j])
{
if(!flag)
start=i;
j++;
if(substr[j]=='\0')
break;
flag=1;
}
else
{

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

flag=start=j=0;
}
i++;
}
if(substr[j]=='\0'&&flag)
{
for(i=0;i<start;i++)
output[i]=str[i];
for(j=0;j<LENGTH(replacestr);j++)
{
output[i]=replacestr[j];
i++;
}
for(j=start + LENGTH(substr);j<LENGTH(str);j++)
{
output[i]=str[j];
i++;
}
output[i]='\0';
printf("\nReplaced String : %s",output);
printf("\n");
}
else
printf("%s is not a substring of %s \n",substr,str);
}
void main()
{
char str1[50],str2[50],substr[50],repstr[50];
int len,pos,ch;
clrscr();
while(1)
{
printf("\nFor Length of the String : 1");
printf("\nFor String Concatenation : 2");
printf("\nFor Extracting a Substring : 3");
printf("\nFor Replacing a String : 4");
printf("\nExit : 5");
printf("\nEnter your choice : ");
scanf("%d",&ch);
fflush(stdin);
switch(ch)
{
case 1:printf("\nEnter the string : ");
gets(str1);
printf("\nThe length of the string is : %d",LENGTH(str1));
printf("\n");
break;
case 2 :printf("\nEnter the first string : ");
gets(str1);
printf("\nEnter the second string : ");
gets(str2);

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

CONCAT(str1,str2);
printf("\nConcatenated String : %s",str1);
printf("\n");
break;
case 3:printf("\nEnter the string : ");
gets(str1);
printf("\nEnter the position of substring : ");
scanf("%d",&pos);
printf("\nEnter the length of the substring : ");
scanf("%d",&len);
SUBSTR(str1,pos,len);
break;
case 4:printf("\nEnter the string : ");
gets(str1);
printf("\nEnter the string to be removed : ");
gets(substr);
printf("\nEnter the string to be replaced : ");
gets(repstr);
REPLACE(str1,substr,repstr);
break;
case 5:exit(0);
default:printf("\nInvalid Choice\n");
break;
}
}
}

OUTPUT:
For Length of the String : 1
For String Concatenation : 2
For Extracting a Substring : 3
For Replacing a String : 4
Exit : 5
Enter your choice : 1

Enter the string : Flowers

The length of the string is : 7

For Length of the String : 1


For String Concatenation : 2
For Extracting a Substring : 3
For Replacing a String : 4
Exit : 5
Enter your choice : 2

Enter the first string : Flowers

Enter the second string : are beautiful

Concatenated string : Flowers are beautiful

Downloaded by studocu hi ([email protected])


lOMoARcPSD|36512938

For Length of the String : 1


For String Concatenation : 2
For Extracting a Substring : 3
For Replacing a String : 4
Exit : 5
Enter your choice : 3

Enter the string : Flowers

Enter the position of substring : 2

Enter the length of the substring : 3

Substring : low

For Length of the String : 1


For String Concatenation : 2
For Extracting a Substring : 3
For Replacing a String : 4
Exit : 5
Enter your choice : 4

Enter the string : are beautiful

Enter the string to be removed : are

Enter the string to be replaced : is

Replaced String : is beautiful

For Length of the String : 1


For String Concatenation : 2
For Extracting a Substring : 3
For Replacing a String : 4
Exit : 5
Enter your choice : 5

Downloaded by studocu hi ([email protected])

You might also like