PROGRAM 1
#include <stdio.h>
#include <stdlib.h>
int n, i;
struct calender {
char *day;
int date;
Char *desc;
};
Struct Calender x
create ()
{
struct calender *ptr;
ptr = (struct calender *) malloc (n *sizeof (struct calender));
for (i=0; i<n; i++)
ptr [1]. day = (char*) malloc (10* sizeof (char));
ptr[1].desc = (char*) malloc (100* sizeof (char));
}
return ptr;
}
void read (struct calender *p1) {
for (i=0;i<n;i++)
{
printf ("Enter the day:");
scanf ("%s", p1[i].day");
printf ("Enter the date: ");
scarF ("%d", &p1[i].date);
printf ("Enter the description:");
scanf ('%s', p=[5].desc);
Void disp (struct calender, *p1)
{
for (i=0; i<n; i++) {
printf("Day: %s\n", p[i]. day);
printf ("Date: %d\n", p1 [i]. date);
Printf ("Description: %s\n",p1[i].desc);
}
}
Void main()
printf ("Enter no of days:");
scanf (%d", &n);
struct calender *p1;
p1 = Create();
read (p1);
disp (p1);
}
}
PROGRAM 2
#include <stdio.h>
#include <string.h>
char str[100], pat[50], rep[50], ans[100];
int i, n, m, j, c, k, flag = 0;
void stringmatching() {
i = m = c = j = 0;
while (str[c] != '\0') {
if (str[m] == pat[i]) {
i++;
m++;
if (pat[i] == '\0') {
flag = 1;
for (k = 0; rep[k] != '\0'; k++, j++) {
ans[j] = rep[k];
}
i = 0;
c = m;
}
} else {
ans[j] = str[c];
j++;
c++;
m = c;
i = 0;
}
}
ans[j] = '\0';
}
void main() {
printf("Enter the string: ");
scanf("%s", str);
printf("Enter the pattern: ");
scanf("%s", pat);
printf("Enter the replace string: ");
scanf("%s", rep);
stringmatching();
if (flag == 1) {
printf("Pattern found and replaced: %s\n", ans);
} else {
printf("Pattern not found\n");
}
}
PROGRAM 3
#include<stdio.h>
#include<stdlib.h>
# define max 4
int s[max];
int top=-1;
void push(){
if(top==max-1){
printf("stack is full");
} else {
int item;
printf("enter the item :");
scanf("%d",&item);
top=top+1;
s[top]=item;
}
}
void disp(){
int i;
for(i=top;i>=0;i--){
printf("%d\n",s[i]);
}
}
void pop(){
if(top==-1){
printf("stack is empty");
}
else {
top=top-1;
printf("element deleted");
}
}
void pali(){
int n,i,flag=1;
n=top;
for(i=0;i<n;i++){
if(s[i]!=s[n]){
flag=0;
break;
}
n--;
}
if(flag==0){
printf("not palindromne");
}
else{
printf("palendrome");
}
}
void main(){
int ch ;
while(1){
printf("1:PUSH 2:POP 3:DISPLAY 4:PALINDROME 5:EXIT");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch){
case 1: push();
break;
case 2: pop();
break;
case 3: disp();
break;
case 4 : pali();
break;
}}}
PROGRAM 4A
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int s[20], top = -1;
int compute(int op1, int op2, char symbol) {
switch (symbol) {
case '+': return op1 + op2;
break;
case '-': return op1 - op2;
break;
case '*': return op1 * op2;
break;
case '/': return op1 / op2;
break;
}
return 0;
}
void main() {
char symbol;
char postfix[30];
int i, res = 0, op1, op2;
printf("Enter the expression: ");
scanf("%s", postfix);
for (i = 0; i < strlen(postfix); i++) {
symbol = postfix[i];
if (isdigit(symbol)) {
symbol = symbol - '0';
top = top + 1;
s[top] = symbol;
} else {
op2 = s[top--];
op1 = s[top--];
res = compute(op1, op2, symbol);
s[++top] = res;
}
}
printf("%d\n", res);
}
PROGRAM 4B
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
void tower(int n,int s,int t,int d){
if(n==0) return;
tower(n-1,s,d,t);
printf("\n %d from %c to % c",n,s,d);
tower(n-1,t,s,d);
}
void main(){
int n;
printf("enter no of plates");
scanf("%d",&n);
tower(n,'A','B','C');
}
PROGRAM 5
#include <stdio.h>
#include <ctype.h>
char s[30];
int top = -1;
void push(char x) {
top = top + 1;
s[top] = x;
}
char pop() {
return s[top--];
}
int priority(char x) {
if (x == '(') return 0;
if (x == '+' || x == '-') return 1;
if (x == '*' || x == '/') return 2;
if (x == '^') return 3;
return -1;
}
void main() {
char infix[30];
int i = 0;
char x;
printf("Enter the expression: ");
scanf("%s", infix);
while (infix[i] != '\0') {
if (isalnum(infix[i])) {
printf("%c", infix[i]);
} else if (infix[i] == '(') {
push(infix[i]);
} else if (infix[i] == ')') {
printf("%c", x);
}
else {
while (top != -1 && priority(s[top]) >= priority(infix[i])) {
printf("%c", pop());
}
push(infix[i]);
}
i++;
}
while (top != -1) {
printf("%c", pop());
}
printf("\n");
}
PROGRAM 6
#include<stdio.h>
#include<stdlib.h>
# define max 5
char q[max];
int rear=-1,front=0,count=0;
void insert(){
char c;
printf("\nenter the element to be inserted :");
scanf("%s",&c);
if(count==max){
printf("\nqueue is full");
}
else
{
rear=(rear+1)%max;
q[rear]=c;
count++; }
}
void deletee(){
if(count==0){
printf("queue is empty");
}
else {
front=(front+1)%max;
printf("\ndeleted element is :%c",q[front]);
}
}
void display() {
int i;
i = front;
if (count == 0) {
printf("\nQueue is empty");
} else {
while (1) {
printf("%c", q[i]);
if (i == rear) {
break;
}
i = (i + 1) % max;
}
}
}
void main(){
int ch;
while(1){
printf("\n1:INSERT 2:DELETE 3:DISPLAY 4:EXIT");
printf("\n enter your choice : ");
scanf("%d",&ch);
switch(ch){
case 1:insert();
break;
case 2:deletee();
break;
case 3:display();
break;
case 4:exit(0);
}
}
}
PROGRAM 7
#include<stdio.h>
#include<stdlib.h>
struct node{
char usn[20];
char name[20];
char branch[20];
char ph[20];
int sem;
struct node *next;
};
struct node* insertfront(struct node *start)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter the details 1:USN,2:NAME,3:BRANCH,4:PHONE NO,5:SEM");
scanf("%s %s %s %s %d",temp->usn,temp->name,temp->branch,temp->ph,&temp->sem);
temp->next=start;
return temp;
}
void display(struct node *start)
{
struct node *temp;
temp=start;
while(temp!=NULL){
printf("USN:%s\n NAME:%s\n BRANCH:%s\n PHONE NMBER:%s\n SEMISTER: %d\n",temp-
>usn,temp->name,temp->branch,temp->ph,temp->sem);
temp=temp->next;
}}
struct node * deletefront(struct node *start)
{
struct node *temp;
temp=start;
start=start->next;
free(temp);
return start;
}
struct node* insertend(struct node* start) {
struct node* temp;
struct node* last;
temp = (struct node*)malloc(sizeof(struct node));
if (temp == NULL) {
printf("Memory allocation failed\n");
return start;
}
printf("Enter the details: 1:USN, 2:NAME, 3:BRANCH, 4:PHONE NO, 5:SEM\n");
scanf("%19s %19s %19s %19s %d", temp->usn, temp->name, temp->branch, temp->ph,
&temp->sem);
temp->next = NULL;
if (start == NULL) {
return temp;
}
last = start;
while (last->next != NULL) {
last = last->next;
}
last->next = temp;
return start;}
struct node* deleteend(struct node* start) {
struct node* temp;
struct node* prev;
if (start == NULL) {
printf("List is empty.\n");
return start;
}
temp = start;
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
}
prev->next = NULL;
free(temp);
return start;
}
void main()
{
int i,n,ch,f;
struct node *start=NULL;
while(1){
printf("Select your choice: 1:INSERTFRONT 2:DISPLAY 3:DELETEFRONT 4:STRUCT DEMO
5:INSERTEND 6:DELETEEND 7:EXIT \n");
scanf("%d",&ch);
switch(ch){
case 1:printf("enter the no of students:");
scanf("%d",&n);
for(i=0;i<n;i++){
start=insertfront(start);
}
break;
case 2:display(start);
break;
case 3:start=deletefront(start);
break;
case 4:printf("stack demo 1:insertion 2:deletion");
scanf("%d",&f);
switch(f){
case 1:
start=insertfront(start);
break;
case 2: start =deletefront(start);
}
break;
case 5: start=insertend(start);
break;
case 6: start = deleteend(start);
break;
case 7: exit(0)
break;
}}}
PROGRAM 8
#include<stdio.h>
#include<stdlib.h>
struct employee{
char ssn[10];
char name[20];
char dept[20];
char designation[20];
char ph[20];
int salary;
struct employee *prev,*next;
};
struct employee *head=NULL;
struct employee *create()
{
struct employee *newnode;
newnode=(struct employee *)malloc(sizeof(struct employee));
printf("enter the 1:SSN: 2:NAME: 3:DEPARTMENT: 4:DESIGNATION: 5: PHONENO:
6:SALARY");
scanf("%s%s%s%s%s%d",newnode->ssn,newnode->name,newnode->dept,newnode-
>designation,newnode->ph,&newnode->salary);
newnode->prev=NULL;
newnode->next=NULL;
return newnode;
}
void insert_end()
{
struct employee *newnode,*temp;
newnode=create();
if(head==NULL){
head=newnode;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
newnode->prev=temp;
}
}
void display(){
struct employee *temp;
temp=head;
int count =0;
if(head==NULL){
printf("list is empty");
}
else{
while(temp!=NULL)
{
printf("%s%s%s%s%s%d\n",temp->ssn,temp->name,temp->dept,temp->designation,temp-
>ph,temp->salary);
temp=temp->next;
}
count++;
}
printf("%d",count);
}
void insert_front()
{
struct employee *newnode;
newnode=create();
if(head==NULL){
head=newnode;
}
else{
newnode->next=head;
head->prev=newnode;
head=newnode;
}
}
void delete_front()
{
struct employee *newnode;
newnode=head;
head=head->next;
head->prev=NULL;
free(newnode);
}
void delete_end()
{
struct employee *temp,*newnode;
newnode=head;
temp=head;
while(newnode->next!=NULL){
temp=newnode;
newnode=newnode->next;
}
temp->next=NULL;
free(newnode);
}
void demo_queue(){
int f;
printf("enter your choice for queue 1:INSERTFRONT 2:DELETEEND");
scanf("%d",&f);
switch(f){
case 1:insert_end();
break;
case 2:delete_front();
break;
}
}
void main()
{
int ch,n,i;
while(1){
printf("enter your choice 1:INSERTEND 2: DISPLAY 3:INSERTFRONT 4:DELETEFRONT
5:DELETEEND 6:QUEUE 7:EXIT");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("enter no of employees:");
scanf("%d",&n);
for(i=0;i<n;i++){
insert_end();
}
break;
case 2: display();
break;
case 3:
insert_front();
break;
case 4: delete_front();
break;
case 5: delete_end();
break;
case 6: demo_queue();
break;
case 7: exit(0);
}
}
}
PROGRAM 9
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define COMPARE(x, y) ( (x == y) ? 0 : (x > y) ? 1 : -1)
struct node
{
int coef;
int xexp, yexp, zexp;
struct node *link;
};
typedef struct node *NODE;
NODE getnode()
{
NODE x;
x = (NODE) malloc(sizeof(struct node));
if(x == NULL)
{
printf("Running out of memory \n");
return NULL;
}
return x;
}
NODE attach(int coef, int xexp, int yexp, int zexp, NODE head)
{
NODE temp, cur;
temp = getnode();
temp->coef = coef;
temp->xexp = xexp;
temp->yexp = yexp;
temp->zexp = zexp;
cur = head->link;
while(cur->link != head)
{
cur = cur->link;
}
cur->link = temp;
temp->link = head;
return head;
}
NODE read_poly(NODE head)
{
int i, j, coef, xexp, yexp, zexp, n;
printf("\nEnter the no of terms in the polynomial: ");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
printf("\n\tEnter the %d term: ",i);
printf("\n\t\tCoef = ");
scanf("%d", &coef);
printf("\n\t\tEnter Pow(x) Pow(y) and Pow(z): ");
scanf("%d", &xexp);
scanf("%d", &yexp);
scanf("%d", &zexp);
head = attach(coef, xexp, yexp, zexp, head);
}
return head;
}
void display(NODE head)
{
NODE temp;
if(head->link == head)
{
printf("\nPolynomial does not exist.");
return;
}
temp = head->link;
while(temp != head)
{
printf("%dx^%dy^%dz^%d", temp->coef, temp->xexp, temp-
>yexp, temp->zexp);
temp = temp->link;
if(temp != head)
printf(" + ");
}
}
int poly_evaluate(NODE head)
{
int x, y, z, sum = 0;
NODE poly;
printf("\nEnter the value of x,y and z: ");
scanf("%d %d %d", &x, &y, &z);
poly = head->link;
while(poly != head)
{
sum += poly->coef * pow(x,poly->xexp)* pow(y,poly->yexp) *
pow(z,poly->zexp);
poly = poly->link;
}
return sum;
}
NODE poly_sum(NODE head1, NODE head2, NODE head3)
{
NODE a, b;
int coef;
a = head1->link;
b = head2->link;
while(a!=head1 && b!=head2)
{
while(1)
{
if(a->xexp == b->xexp && a->yexp == b->yexp && a->zexp == b->zexp)
{
coef = a->coef + b->coef;
head3 = attach(coef,a->xexp,a->yexp,a->zexp,head3);
a = a->link;
b = b->link;
break;
} //if ends here
if(a->xexp!=0 || b->xexp!=0)
{
switch(COMPARE(a->xexp, b->xexp))
{
case -1 :head3 = attach(b->coef, b->xexp, b->yexp, b->zexp,
head3);
b = b->link;
break;
case 0 : if(a->yexp > b->yexp)
{
head3 = attach(a->coef,a->xexp,a->yexp,a->zexp,head3);
a = a->link;
break;
}
else if(a->yexp < b->yexp)
{
head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3);
b = b->link;
break;
}
else if(a->zexp > b->zexp)
{
head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;
break;
}
else if(a->zexp < b->zexp)
{
head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3);
b = b->link;
break;
}
case 1 :head3 = attach(a->coef,a->xexp,a->yexp,a-
>zexp,head3);
a = a->link;
break;
} //switch ends here
break;
} //if ends here
if(a->yexp!=0 || b->yexp!=0)
{
switch(COMPARE(a->yexp, b->yexp))
{
case -1 :head3 = attach(b->coef, b->xexp, b->yexp, b-
>zexp, head3);
b = b->link;
break;
case 0 :if(a->zexp > b->zexp)
{
head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;
break;
}
else if(a->zexp < b->zexp)
{
head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3);
b = b->link;
break;
}
case 1 :
head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;
break;
}//end switch
break;
}
if(a->zexp!=0 || b->zexp!=0)
{
switch(COMPARE(a->zexp,b->zexp))
{
case -1 : head3 = attach(b->coef,b->xexp,b->yexp,b->zexp,head3);
b = b->link;
break;
case 1 : head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;
break;
}
break;
}
}
}
while(a!= head1)
{
head3 = attach(a->coef,a->xexp,a->yexp,a->zexp,head3);
a = a->link;
}
while(b!= head2)
{
head3 = attach(b->coef,b->xexp,b->yexp,b->zexp,head3);
b = b->link;
}
return head3;
}
void main()
{
NODE head, head1, head2, head3;
int res, ch;
head = getnode(); /* For polynomial evalaution */
head1 = getnode(); /* To hold POLY1 */
head2 = getnode(); /* To hold POLY2 */
head3 = getnode(); /* To hold POLYSUM */
head->link=head;
head1->link=head1;
head2->link=head2;
head3->link= head3;
while(1)
{
printf("\n~~~Menu~~~");
printf("\[Link] and Evaluate a Polynomial P(x,y,z)");
printf("\[Link] the sum of two polynomials POLY1(x,y,z)");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n~~~~Polynomial evaluation P(x,y,z)~~~\n");
head = read_poly(head);
printf("\nRepresentation of Polynomial for evaluation: \n");
display(head);
res = poly_evaluate(head);
printf("\nResult of polynomial evaluation is : %d \n", res);
break;
case 2: printf("\nEnter the POLY1(x,y,z): \n");
head1 = read_poly(head1);
printf("\nPolynomial 1 is: \n");
display(head1);
printf("\nEnter the POLY2(x,y,z): \n");
head2 = read_poly(head2);
printf("\nPolynomial 2 is: \n");
display(head2);
printf("\nPolynomial addition result: \n");
head3 = poly_sum(head1,head2,head3);
display(head3);
break;
case 3: exit(0);
}
}
}
PROGRAM 10
#include<stdio.h>
#include<stdlib.h>
struct node{
struct node *left;
int data;
struct node *right;
};
struct node *create(){
struct node *newnode;
int item;
newnode=(struct node *)malloc(sizeof(struct node));
printf("enter the value of the element:");
scanf("%d",&item);
newnode->data=item;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}
struct node *insert(struct node *root){
struct node *temp;
struct node *cur;
struct node *prev=NULL;
temp=create();
if(root ==NULL){
root=temp;
return root;
}
cur=root;
while(cur!=NULL){
prev=cur;
if(temp->data<cur->data){
cur=cur->left;
}
else{
cur=cur->right;
}}
if(temp->data<prev->data){
prev->left=temp;
}
else{
prev->right=temp;
}
return root;
}
void preorder(struct node *root){
if(root!=NULL){
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}}
void inorder(struct node *root){
if(root!=NULL){
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}}
void postorder(struct node *root){
if(root!=NULL){
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}}
void search(int x, struct node *root) {
struct node *cur = root;
while (cur != NULL) {
if (cur->data == x) {
printf("Element %d found\n", x);
return;
} else if (x < cur->data) {
cur = cur->left;
} else {
cur = cur->right;
}}
printf("Element %d not found\n", x);
}
void main()
{
struct node *root=NULL;
int ch,i,n,x;
while(1){
printf("1:INSERT 2:PREORDER INORDER POSTORDER 3:SEARCH 4:EXIT");
scanf("%d",&ch);
switch(ch){
case 1:printf("enter the no of integers:");
scanf("%d", &n);
for(i=0;i<n;i++){
root=insert(root);
}
break;
case 2:
printf("preorder:\n");
preorder(root);
printf("inorder:\n");
inorder(root);
printf("postorder:\n");
postorder(root);
break;
case 3:
printf("Enter the search element: ");
scanf("%d", &x);
search(x, root);
break;
case 4:
exit(0);
}}}
PROGRAM 11
#include <stdio.h>
int a[10][10], n, m, i, j, source, s[10], b[10];
int visited[10];
void create() {
printf("\nEnter the number of vertices of the digraph: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix of the graph:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
}
void bfs() {
int q[10], u, front = 0, rear = -1;
printf("\nEnter the source vertex to find other nodes reachable or not: ");
scanf("%d", &source);
q[++rear] = source;
visited[source] = 1;
printf("\nThe reachable vertices are: ");
while (front <= rear) {
u = q[front++];
for (i = 1; i <= n; i++) {
if (a[u][i] == 1 && visited[i] == 0) {
q[++rear] = i;
visited[i] = 1;
printf("\n%d", i);
}
}
}
}
void dfs(int source) {
int v, top = -1;
s[++top] = 1;
b[source] = 1;
for (v = 1; v <= n; v++) {
if (a[source][v] == 1 && b[v] == 0) {
printf("\n%d -> %d", source, v);
dfs(v);
}
}
}
void main() {
int ch;
while (1) {
printf("\n1. Create Graph\n2. BFS\n3. Check graph connected or not (DFS)\
n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1: create(); break;
case 2:
bfs();
for (i = 1; i <= n; i++)
if (visited[i] == 0)
printf("\nThe vertex that is not reachable %d", i);
break;
case 3:
printf("\nEnter the source vertex to find the connectivity: ");
scanf("%d", &source);
m = 1;
dfs(source);
for (i = 1; i <= n; i++) {
if (b[i] == 0)
m = 0;
}
if (m == 1)
printf("\nGraph is Connected");
else
printf("\nGraph is not Connected");
break;
default:
exit(0);
}
}
}
OUTPUT OF 11
1. Create Graph
2. BFS
3. Check graph connected or not (DFS)
4. Exit
Enter your choice: 1
Enter the number of vertices of the graph: 5
Enter the adjacency matrix of the graph:
0 1 1 0 0
1 0 1 1 0
1 1 0 1 1
0 1 1 0 1
0 0 1 1 0
1. Create Graph
2. BFS
3. Check graph connected or not (DFS)
4. Exit
Enter your choice: 2
Enter the source vertex to find other nodes reachable or not: 1
The reachable vertices are:
2
3
4
5
1. Create Graph
2. BFS
3. Check graph connected or not (DFS)
4. Exit
Enter your choice: 3
Enter the source vertex to find the connectivity: 1
1 -> 2
2 -> 3
3 -> 4
4 -> 5
Graph is Connected
1. Create Graph
2. BFS
3. Check graph connected or not (DFS)
4. Exit
Enter your choice: 4
PROGRAM 12
#include<stdio.h>
#include<stdlib.h>
int key[20], n, m;
int *ht, index;
int count = 0;
void insert(int key) {
index = key % m;
while (ht[index] != -1) {
index = (index + 1) % m;
}
ht[index] = key;
count++;
}
void display() {
int i;
if (count == 0) {
printf("\nHash Table is empty");
return;
}
printf("\nHash Table contents are:\n ");
for (i = 0; i < m; i++)
printf("\n T[%d] --> %d ", i, ht[i]);
}
void main() {
int i;
printf("\nEnter the number of employee records (N): ");
scanf("%d", &n);
printf("\nEnter the two-digit memory locations (m) for the hash table: ");
scanf("%d", &m);
ht = (int *)malloc(m * sizeof(int));
for (i = 0; i < m; i++)
ht[i] = -1;
printf("\nEnter the four-digit key values (K) for N Employee Records:\n");
for (i = 0; i < n; i++)
scanf("%d", &key[i]);
for (i = 0; i < n; i++) {
if (count == m) {
printf("\n~~~Hash table is full. Cannot insert the record %d key~~~", i
+ 1);
break;
}
insert(key[i]);
}
display();
}