0% found this document useful (0 votes)
22 views20 pages

DSA Lab

The document contains multiple C programming code snippets demonstrating various data structures and algorithms, including a calendar management system, string replacement, stack operations, infix to postfix conversion, circular queue implementation, binary search tree operations, and graph traversal using BFS. Each section provides functions for creating, manipulating, and displaying data structures. The code examples illustrate fundamental concepts in computer science such as recursion, data storage, and algorithm efficiency.

Uploaded by

nikilhe318
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)
22 views20 pages

DSA Lab

The document contains multiple C programming code snippets demonstrating various data structures and algorithms, including a calendar management system, string replacement, stack operations, infix to postfix conversion, circular queue implementation, binary search tree operations, and graph traversal using BFS. Each section provides functions for creating, manipulating, and displaying data structures. The code examples illustrate fundamental concepts in computer science such as recursion, data storage, and algorithm efficiency.

Uploaded by

nikilhe318
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/ 20

1.

Calendar
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct day
{
char name[20];
char act[100];
int date;
};
struct day week[7];
void create()
{
for(int i=0;i<7;i++)
{
printf("\nEnter name of day %d: ",i+1);
scanf("%s",week[i].name);
printf("\nEnter date of day %d: ",i+1);
scanf("%d",&week[i].date);
printf("\nEnter activity of day %d: ",i+1);
scanf("%s",week[i].act);
}
}
void read()
{
create();
}
void display()
{
printf("\nDay\tDate\tActivity\n");
for(int i=0;i<7;i++)
{
printf("\n%s\t%d\t%s",week[i].name,week[i].date,week[i].act);
}
}
int main()
{
printf("Creating the calendar: ");
create();
printf("Printing the Calendar: ");
display();
return 0;
}
2.String Replacement
#include<stdio.h>
char str[50], pat[20], rep[20], ans[50];
int c=0, m=0, i=0, j=0, k, flag=0;
void stringmatch()
{
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("\nEnter the main string:");
scanf(“%[^\n]”,str);
printf("\nEnter the pattern string:");
scanf(“%s”,pat);
printf("\nEnter the replace string:");
scanf(“%s”,rep);
stringmatch();
if(flag == 1)
printf("\nResultant string is %s", ans);
else
printf("\nPattern string is not found");
}
3.Stack Operations
#include<stdio.h>
#include<stdlib.h>
int top=-1,a[10];
void push()
{
int item;
if(top>=20)
{
printf("Stack overflow");
}
else
{
printf("Enter element: ");
scanf("%d",&item);
a[++top]=item;
}
}
void pop()
{
int item;
if(top<0)
{
printf("Stack underflow");
}
else
{
item=a[top--];
printf("The deleted item is %d",item);
}
}
void display()
{
int i;
if(top<0)
{
printf("Stack underflow");
}
else
{
for(i=0;i<=top;i++)
{
printf("\n%d",a[i]);
}
}
}
void palindrome()
{
int i,temp=top;
if(top!=-1)
{
for(i=0;i<=top/2;i++)
if(a[i]!=a[top-1])
{
printf("Stack is not palindrome");
return;
}
printf("Stack is palindrome");
}
return;
}
void main()
{
int ch;
while(1)
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Palindrome\n5.Exit\nEnter ur choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
palindrome();
break;
case 5: exit(0);
}
}
}
4.Infix to Pos ix
#include<stdio.h>
#include<ctype.h>

char stack[100];
int top = -1;

void push(char x)
{
stack[++top] = x;
}

char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}

int infix(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
if(x == '%' || x == '^')
return 3;
return 0;

int main()
{
char exp[100];
char *e, x;
prin ("Enter the expression : ");
scanf("%s",exp);
e = exp;

while(*e != '\0')
{
if(isalnum(*e))
prin ("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
prin ("%c ", x);
}
else
{
while(infix(stack[top]) >= infix(*e))
prin ("%c ",pop());
push(*e);
}
e++;
}

while(top != -1)
{
prin ("%c ",pop());
}
return 0;
}
5.Stack Applications
#include<stdio.h>
#include<math.h>
#include<ctype.h>
#include<stdlib.h>
int top=-1;
int op1,op2,res,s[20];
char pf[90],symb;
void push(int item)
{
s[++top]=item;
}
int pop()
{
int item;
item=s[top--];
return item;
}
int main()
{
double val=0;
printf("\nEnter a valid expression:");
scanf("%s",pf);
for(int i=0;pf[i]!='\0';i++)
{
symb=pf[i];
if(isdigit(symb))
{
push(symb-'0');
}
else
{
op2=pop();
op1=pop();
switch(symb)
{
case'+':push(op1+op2);
break;
case'-':push(op1-op2);
break;
case'*':push(op1*op2);
break;
case'/':push(op1/op2);
break;
case'%':push(op1%op2);
break;
default:push(0);
}
}
}
res=pop();
printf("Result=%d",res);
return 0;
}
6.Circular Queue
#include<stdio.h>
#include<stdlib.h>
#define size 5
int q[size],i,r=-1,f=0,ch,count=0,j;
int main()
{
for(;;)
{
printf("\n1.Insert\n2.Delete\n3.Display\n4.Exit");
printf("\nEnter ur choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(count==size)
printf("\nQueue Overflow");
else
{
r=(r+1)%size;
printf("\nEnter the element: ");
scanf("%d",&q[r]);
count++;
}
break;
case 2:
if(count==0)
printf("\nQueue Underflow");
else
{
printf("\nDeleted element is: %d",q[f]);
count--;
f=(f+1)%size;
}
break;
case 3:
if(count==0)
printf("\nQueue is Empty");
else
{
i=f;
for(j=0;j<count;j++)
{
printf("%d\t\n",q[i]);
i=(i+1)%size;
}
}
break;
default:exit(0);
}
}
}
10.Binary Search Tree
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node *left, *right;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

struct Node* insertNode(struct Node* root, int data) {


if (!root)
return createNode(data);
if (data < root->data)
root->left = insertNode(root->left, data);
else if (data > root->data)
root->right = insertNode(root->right, data);
return root;
}

void inorder(struct Node* root) {


if (root)
{
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

void preorder(struct Node* root) {


if (root)
{
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}

void postorder(struct Node* root) {


if (root)
{
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}

struct Node* search(struct Node* root, int key) {


if (!root || root->data == key)
return root;
return key < root->data ? search(root->left,key):search(root->right,key);
}

int main() {
struct Node* root = NULL;
int choice, key, data, n;

while (1) {
printf("\n1.Create BST\n2.Tree Traversal\n3.Search\n4.Exit\nChoice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++)
{
scanf("%d", &data);
root = insertNode(root, data);
}
break;
case 2:
printf("Inorder: ");
inorder(root);
printf("\nPreorder: ");
preorder(root);
printf("\nPostorder: ");
postorder(root);
printf("\n");
break;
case 3:
printf("Key to search: ");
scanf("%d", &key);
printf(search(root, key) ? "Found\n" : "Not Found\n");
break;
case 4:
return 0;
default:
printf("Invalid choice\n");
}
}
}
11.Graph of cities
#include<stdio.h>
#include<stdlib.h>
int n,a[10][10],i,j,source,s[10],choice,count;
void bfs(int n,int a[10][10],int source,int s[])
{
int q[10],u;
int front=1,rear=1;
s[source]=1;
q[rear]=source;
while(front<=rear)
{
u=q[front++];
for(i=1;i<=n;i++)
if(a[u][i]==1&&s[i]==0)
{
rear=rear+1;
q[rear]=i;
s[i]=1;
}
}
}
int main()
{
printf("Enter no of nodes: ");
scanf("%d",&n);
printf("Enter the adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the source: ");
scanf("%d",&source);
for(i=1;i<=n;i++)
s[i]=0;
bfs(n,a,source,s);
for(i=1;i<=n;i++)
{
if(s[i]!=0)
printf("\nThe node %d is reachable",i);
else
printf("\nThe node %d is not reachable",i);
}
}

You might also like