Ds Programs
Ds Programs
#include <stdio.h>
#include <stdlib.h>
int n, i;
struct calender {
char *day;
int date;
Char *desc;
};
Struct Calender x
create ()
{
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);
p1 = Create();
read (p1);
disp (p1);
}
}
PROGRAM 2
#include <stdio.h>
#include <string.h>
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>
void main() {
char symbol;
char postfix[30];
int i, res = 0, op1, op2;
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);
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;
#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;
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;
#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>
void create() {
printf("\nEnter the number of vertices of the digraph: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix of the graph:\n");
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;
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);
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
1. Create Graph
2. BFS
3. Check graph connected or not (DFS)
4. Exit
Enter your choice: 2
1. Create Graph
2. BFS
3. Check graph connected or not (DFS)
4. Exit
Enter your choice: 3
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 display() {
int i;
if (count == 0) {
printf("\nHash Table is empty");
return;
}
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);
display();
}