Professional Documents
Culture Documents
Output
Output
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct treenode
{
int data;
struct treenode *left;
struct treenode *right;
}tnode;
tnode *insertion(int,tnode*);
void preorder(tnode *);
void inorder(tnode *);
void postorder(tnode *);
void main()
{
tnode *T=NULL;
int ch1,n;
char ch2;
do
{
clrscr();
printf("\n\t\t****Operation With Tree****");
printf("\n\t1.Insertion");
printf("\n\t2.Inorder Traversal");
printf("\n\t3.Preorder Traversal");
printf("\n\t4.Postorder Traversal");
printf("\n\tEnter Your Choice :");
scanf("%d",&ch1);
switch(ch1)
{case 1:
printf("\n\nenter the element to be inserted :");
scanf("%d",&n);
T=insertion(n,T);
break;
case 2:
inorder(T);
break;
case 3:
preorder(T);
break;
case 4:
postorder(T);
break;
default:
printf("\n\nInvalid Option");
break;}
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<alloc.h>
struct tree
{
int data;
struct tree *lchild;
struct tree *rchild;
}*t,*temp;
int element;
void inorder(struct tree *);
void preorder(struct tree *);
void postorder(struct tree *);
struct tree * create(struct tree *, int);
struct tree * find(struct tree *, int);
struct tree * insert(struct tree *, int);
struct tree * del(struct tree *, int);
struct tree * findmin(struct tree *);
struct tree * findmax(struct tree *);
void main()
{
int ch;
do
{
printf("\n\t\t\tBINARY SEARCH TREE");
printf("\n\t\t\t****** ****** ****");
printf("\nMain Menu\n");
{
printf("\n1.Create\n2.Insert\n3.Delete\n4.Find\n5.FindMin\n6.FindMax");
printf("\n7.Inorder\n8.Preorder\n9.Postorder\n10.Exit\n");
printf("\nEnter ur choice :");
scanf("%d",&ch);
switch(ch)
case 1:
printf("\nEnter the data:");
scanf("%d",&element);
t=create(t,element);
inorder(t);
break;
case 2:
printf("\nEnter the data:");
scanf("%d",&element);
t=insert(t,element);
inorder(t);
break;
case 3:
printf("\nEnter the data:");
scanf("%d",&element);
t=del(t,element);
inorder(t);
break;
case 4:
printf("\nEnter the data:");
scanf("%d",&element);
temp=find(t,element);
if(temp->data==element)
printf("\nElement %d is at %d",element,temp);
else
printf("\nElement is not found");
break;
case 5:
temp=findmin(t);
printf("\nMin element=%d",temp->data);
break;
case 6:
temp=findmax(t);
printf("\nMax element=%d",temp->data);
break;
case 7:
inorder(t);
break;
case 8:
preorder(t);
break;
case 9:
postorder(t);
break;
case 10:
exit(0);
}
}while(ch<=10);
}
printf("%c",stack[top]);
top--;
}
top--;
}
else
{
while(isp(stack[top])>=icp(exp[i]))
{
printf("%c",stack[top]);
top--;
}
top++;
stack[top]=exp[i];
}
}
else
printf("%c",exp[i]);
i++;
}
while(top>0)
{
printf("%c",stack[top]);
top--;
}
getch();
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define n 5
void main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
//clrscr();
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\n Queue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
getch();
}
OUTPUT:
PROGRAM:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is
%d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{ printf("\n The STACK is empty");
}
}
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define maxsize 10
int list[maxsize],n;
void Create();
void Insert();
void Delete();
void Display();
void Search();
void main()
{
int choice;
do
{
printf("\nArray implementation of List\n");
printf("1.create 2.insert 3.delete\n");
printf("4.search 5.exit\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
Create();
break;
case 2:
Insert();
break;
case 3:
Delete();
break;
case 4:
Search();
break;
case 5:
exit(1);
default:
printf("Enter option between 1-6\n");
break;
}
}while(choice<7);
}
void Create()
{
int i;
printf("\nEnter the element to be added: ");
scanf("%d",&n);
printf("\nEnter the array elements:");
for(i=0;i<n;i++)
scanf("%d",&list[i]);
Display();
}
void Insert()
{
int i,data,pos;
printf("\nEnter the data to be inserted:" );
scanf("%d",&data);
printf("\nEnter position at which element to be inserted: ");
scanf("%d",&pos);
for(i=n-1;i>=pos;i--)
list[i+1]=list[i];
list[pos-1]=data;
n+=1;
Display();
}
void Delete()
{
int i,pos;
printf("\nEnter the position to be deleted:");
scanf("%d",&pos);
printf("\nThe data deleted is : %d",list[pos-1]);
for(i=pos-1;i<n-1;i++)
list[i]=list[i+1];
n=n-1;
Display();
}
void Display()
{
int i;
printf("\n********** Elements in array **********\n");
for(i=0;i<n;i++)
printf("%d",list[i]);
}
void Search()
{
int search,i,count=0;
printf("\nEnter the element to be searched :");
scanf("%d",&search);
for(i=0;i<=n;i++)
{
if(search==list[i])
count++;
}
if(count==0)
printf("Element not present in the list");
else
printf("Element present in the list");
}
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#define NULL 0
struct info
{
int data;
struct info *next;
};
struct info *head,*temp,*disp;
void additem();
void delitem();
void display();
int size();
void search();
void main()
{
int choice;
clrscr();
while(1)
{
printf("\n1.Add records");
printf("\n2.Delete records");
printf("\n3.Display records");
printf("\n4.Count no. of items in the list");
printf("\n5.Searching an item in the list");
printf("\n6.Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
fflush(stdin);
switch(choice)
{
case 1:
additem();
break;
case 2:
delitem();
break;
case 3:
display();
break;
case 4:
printf("\nThe size of the list is %d",size());
break;
case 5:
search();
break;
case 6:
exit(0);
}
}
}
void additem()
{
struct info *add;
char proceed='y';
while(toupper(proceed)=='Y')
{
add=(struct info*)malloc(sizeof(struct info));
printf("Enter data:");
scanf("%d",&add->data);
fflush(stdin);
if(head==NULL)
{
head=add;
add->next=NULL;
temp=add;
}
else
{
temp->next=add;
add->next=NULL;
temp=add;
}
printf("\nWant to proceed y/n");
proceed=getchar();
fflush(stdin);
}
}
void delitem()
{
struct info *curr,*prev;
int tdata;
if(head==NULL)
{
printf("\nNo records to delete");
return;
}
printf("\nEnter the data to delete");
scanf("%d",&tdata);
fflush(stdin);
prev=curr=head;
while((curr!=NULL)&&(curr->data!=tdata))
{
prev=curr;
curr=curr->next;
}
if(curr==NULL)
{
printf("\nData not found");
return;
}
if(curr==head)
head=head->next;
else
{
/*for inbetween element deletion*/
prev->next=curr->next;
/*for the last element deletion*/
if(curr->next==NULL)
temp=prev;
}
free(curr);
}
void display()
{
if(head==NULL)
{
printf("\nNo data to display");
return;
}
for(disp=head;disp!=NULL;disp=disp->next)
{
printf("Data->%d",disp->data);
}
}
int size()
{
int count=0;
if(head==NULL)
return count;
for(disp=head;disp!=NULL;disp=disp->next)
count++;
return count;
}
void search()
{
int titem,found=0;
if(head==NULL)
{
printf("\nNo data in the list");
return;
}
printf("\Enter the no. to search:");
scanf("%d",&titem);
for(disp=head;disp!=NULL&&found==0;disp=disp->next)
{
if(disp->data==titem)
found=1;
}
if(found==0)
printf("\nSearch no. is not present in the list");
else
printf("\nSearch no. is present in the list");
return;
}
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define NULL 0
struct info
{
int data;
struct info *next;
struct info *prev;
};
struct info *head,*temp,*disp;
void additem();
void delitem();
void display();
int size();
void search();
void main()
{
int choice;
clrscr();
while(1)
{
printf("\n1.Add records");
printf("\n2.Delete records");
printf("\n3.Display records");
printf("\n4.Count no. of items in the list");
printf("\n5.Searching an item in the list");
printf("\n6.Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
fflush(stdin);
switch(choice)
{
case 1:
additem();
break;
case 2:
delitem();
break;
case 3:
display();
break;
case 4:
printf("\nThe size of the list is %d",size());
break;
case 5:
search();
break;
case 6:
exit(0);
}
}
}
void additem()
{
struct info *add;
char proceed='y';
while(toupper(proceed)=='Y')
{
add=(struct info*)malloc(sizeof(struct info));
printf("Enter data:");
scanf("%d",&add->data);
fflush(stdin);
if(head==NULL)
{
head=add;
add->next=NULL;
add->prev=NULL;
temp=add;
}
else
{
temp->next=add;
add->prev=temp;
add->next=NULL;
temp=add;
}
printf("\nWant to proceed y/n");
proceed=getchar();
fflush(stdin);
}
}
void delitem()
{
int x;
struct info *p;;
if(head==NULL)
{
printf("\nNo items in the list");
return;
}
printf("\nEnter the data to delete");
scanf("%d",&x);
//fflush(stdin);
p=(struct info *)malloc(sizeof(struct info));
p=head->next;
if(head->data==x)
{
head=head->next;
return;
}
while(p)
{
if(p->data==x)
{
p->prev->next=p->next;
if(p->next!=NULL)
p->next->prev=p->prev;
else
temp=p->prev;
return;
}
else
{
p=p->next;
}
}
printf("\nInvalid input");
}
void display()
{
if(head==NULL)
{
printf("\nNo data to display");
return;
}
printf("\nFrom forward direction\n");
for(disp=head;disp!=NULL;disp=disp->next)
{
printf("Data->%d",disp->data);
}
printf("\nFrom backward direction\n");
for(disp=temp;disp!=NULL;disp=disp->prev)
{
printf("Data->%d",disp->data);
}
}
int size()
{
int count=0;
if(head==NULL)
return count;
for(disp=head;disp!=NULL;disp=disp->next)
count++;
return count;
}
void search()
{
int titem,found=0;
if(head==NULL)
{
printf("\nNo data in the list");
return;
}
printf("\Enter the no. to search:");
scanf("%d",&titem);
for(disp=head;disp!=NULL&&found==0;disp=disp->next)
{
if(disp->data==titem)
found=1;
}
}
if(found==0)
printf("\nSearch no. is not present in the list");
else
printf("\nSearch no. is present in the list");
return;}
OUTPUT:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
if (exp1 == exp2) {
sumExp = exp1; // Exponents are equal
current1 = current1->next;
current2 = current2->next;
} else if (exp1 > exp2) {
current1 = current1->next;
} else {
sumExp = exp2;
current2 = current2->next;
}
printf("Polynomial 2: ");
displayPolynomial(poly2);
return 0;
}
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
OUTPUT:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
void insert();
void del();
void display();
struct node {
int priority;
int info;
struct node *next;
};
int main() {
int ch;
do {
printf("\n[1] INSERTION\t[2] DELETION\t[3] DISPLAY [4]
EXIT\t:");
scanf("%d", &ch);
switch (ch) {
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
break;
}
} while (ch < 4);
return 0;
}
void insert() {
int item, itprio;
new = (N *)malloc(sizeof(N));
new->info = item;
new->priority = itprio;
new->next = NULL;
if (start == NULL) {
start = new;
} else if (start != NULL && itprio <= start->priority) {
new->next = start;
start = new;
} else {
q = start;
while (q->next != NULL && q->next->priority <= itprio) {
q = q->next;
}
new->next = q->next;
q->next = new;
}
}
void del() {
if (start == NULL) {
printf("\nQUEUE UNDERFLOW\n");
} else {
new = start;
printf("\nDELETED ITEM IS %d\n",
new->info);
start = start->next;
free(new);
}
}
void display() {
temp = start;
if (start == NULL) {
printf("QUEUE IS EMPTY\n");
} else {
printf("QUEUE IS:\n");
for (temp = start; temp != NULL; temp
= temp->next) {
printf("\n%d priority = %d\n", temp-
>info, temp->priority);
}
}
}
OUTPUT:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
void DFS(int);
int G[10][10], visited[10], n;
int main() {
int i, j;
printf("Enter the number of vertices: ");
scanf("%d", &n);
printf("\nEnter adjacency matrix of the
graph:\n");
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
scanf("%d", &G[i][j]);
for (i = 0; i < n; i++)
visited[i] = 0;
DFS(0);
return 0;
}
void DFS(int i) {
int j;
printf("\n%d", i);
visited[i] = 1;
#include <stdio.h>
void bfs(int v) {
for (i = 1; i <= n; i++)
if (adj[v][i] && !visited[i])
queue[++rear] = i;
if (front <= rear) {
visited[queue[front]] = 1;
bfs(queue[front++]);
}
}
int main() {
int v;
printf("Enter the number of vertices: ");
scanf("%d", &n);
bfs(v);
return 0;
}
OUTPUT:
PROGRAM:
#include <stdio.h>
#define INFINITY 9999
#define MAX 10
void Dijkstra(int Graph[MAX][MAX], int n, int start);
void Dijkstra(int Graph[MAX][MAX], int n, int start) {
int cost[MAX][MAX], distance[MAX], pred[MAX];
int visited[MAX], count, mindistance, nextnode, i, j;
distance[start] = 0;
visited[start] = 1;
count = 1;
visited[nextnode] = 1;
count++;
}
int main() {
int Graph[MAX][MAX], i, j, n;
n = 4;
Graph[0][0] = 0;
Graph[0][1] = 2;
Graph[0][2] = 0;
Graph[0][3] = 1;
Graph[1][0] = 0;
Graph[1][1] = 0;
Graph[1][2] = 0;
Graph[1][3] = 2;
Graph[2][0] = 3;
Graph[2][1] = 0;
Graph[2][2] = 0;
Graph[2][3] = 0;
Graph[3][0] = 0;
Graph[3][1] = 0;
Graph[3][2] = 1;
Graph[3][3] = 0;
int start = 0;
Dijkstra(Graph, n, start);
return 0;
}
OUTPUT:
PROGRAM:
#include <stdio.h>
int main() {
int c, first, last, middle, n, search, array[100];
first = 0;
last = n - 1;
middle = (first + last) / 2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle + 1);
break;
} else {
last = middle - 1;
middle = (first + last) / 2;
}
}
#include<stdio.h>
#include<conio.h>
int main()
{
int data[100],n,temp,i,j;
struct data {
int key;
int value;
};
struct hashtable_item {
int flag; // 0: data not present, 1: data present, 2: data was present but deleted
struct data *item;
};
array[index].item = new_item;
array[index].flag = 1;
size++;
printf("\nKey (%d) has been inserted\n", key);
}
int size_of_hashtable() {
return size;
}
void display() {
int i;
for (i = 0; i < max; i++) {
if (array[i].flag != 1) {
printf("\nArray[%d] has no elements\n", i);
} else {
printf("\nArray[%d] has elements\nKey (%d) and Value (%d)\n", i,
array[i].item->key, array[i].item->value);
}
}
}
void init_array() {
int i;
for (i = 0; i < max; i++) {
array[i].item = NULL;
array[i].flag = 0;
}
prime = get_prime();
}
int get_prime() {
int i, j;
for (i = max - 1; i >= 1; i--) {
int flag = 0;
for (j = 2; j <= (int)sqrt(i); j++) {
if (i % j == 0) {
flag++;
}
}
if (flag == 0) {
return i;
}
}
return 3;
}
int main() {
int choice, key, value, n, c;
clrscr();
switch (choice) {
case 1:
printf("Inserting element in Hash Table\nEnter key and value: ");
scanf("%d %d", &key, &value);
insert(key, value);
break;
case 2:
printf("Deleting in Hash Table\nEnter the key to delete: ");
scanf("%d", &key);
remove_element(key);
break;
case 3:
n = size_of_hashtable();
printf("Size of Hash Table is: %d\n", n);
break;
case 4:
display();
break;
default:
printf("Wrong Input\n");
}
printf("\nDo you want to continue? (press 1
for yes): ");
scanf("%d", &c);
} while (c == 1);
getch();
return 0;
}
OUTPUT: