0% found this document useful (0 votes)
12 views28 pages

Programs

ds programs

Uploaded by

shettykavan006
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)
12 views28 pages

Programs

ds programs

Uploaded by

shettykavan006
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/ 28

Program 1

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
char* name;
int date;
char* description;
}Day;
Day* create()
{
Day* calender=(Day*)malloc(sizeof(Day)*7);
if(calender==NULL)
{
printf("|nError allocating memory for calender");
exit(1);
}
return calender;
}
void read(Day* calendar)
{
for (int i = 0; i < 7; i++)
{
printf("\nEnter the name of the day %d: ", i + 1);
calendar[i].name = (char*)malloc(sizeof(char) * 100);
if (calendar[i].name == NULL)
{
printf("\nError allocating memory for name");
exit(1);
}
gets(calendar[i].name);
printf("Enter the date of the day %d: ", i + 1);
scanf("%d", &calendar[i].date);
getchar();
printf("Enter the description of activity for day %d: ", i + 1);
calendar[i].description = (char*)malloc(sizeof(char) * 100);
if (calendar[i].description == NULL)
{
printf("\nError allocating memory for description");
exit(1);
}
gets(calendar[i].description);
}
}void display(Day* calender)
{
printf("\n\nWeek activity details report\n");
printf("================================\n");
for(int i=0;i<7;i++)
{
printf("%s
%d:%s\n",calender[i].name,calender[i].date,calender[i].descripti
on);
}
}
int main()
{
Day* calender=create();
read(calender);
display(calender);
for(int i=0;i<7;i++)
{
free(calender[i].name);
free(calender[i].description);
}
free(calender);
return 0;
}
Program 2

#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:");
gets(str);
printf("\nEnter the pat string:");
gets(pat);
printf("\nEnter the replace string:");
gets(rep);
stringmatch();
if(flag == 1)
printf("\nResultant string is %s", ans);
else
printf("\nPattern string is not found");
}
Program 3
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int s[MAX];
int top = -1;
void push(int item);
int pop();
void palindrome();
void display();
void main()
{
int choice, item;
while(1)
{
printf("\n\n\n\n~~~~~~Menu~~~~~~ : ");
printf("\n=>1.Push an Element to Stack and Overflow
demo ");
printf("\n=>2.Pop an Element from Stack and
Underflow demo");
printf("\n=>3.Palindrome demo ");
printf("\n=>4.Display ");
printf("\n=>5.Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\nEnter an element to be pushed: ");
scanf("%d", &item);
push(item);
break;
case 2:
item = pop();
if(item != -1)
printf("\nElement popped is: %d", item);
break;
case 3:
palindrome();
break;
case 4:
display();
break;
case 5:
exit(1);
default:
printf("\nPlease enter valid choice ") ;
break;
}
}
}
void push(int item)
{
if(top == MAX-1)
{
printf("\n~~~~Stack overflow~~~~");
return;
}
top = top + 1 ;
s[top] = item;
}
int pop()
{
int item;
if(top == -1)
{
printf("\n~~~~Stack underflow~~~~");
return -1;
}
item = s[top];
top = top - 1;
return item;
}
void display()
{
int i;
if(top == -1)
{
printf("\n~~~~Stack is empty~~~~");
return;
}
printf("\nStack elements are:\n ");
for(i=top; i>=0 ; i--)
printf("| %d |\n", s[i]);
}
void palindrome()
{
int flag=1,i;
printf("\nStack content are:\n");
for(i=top; i>=0 ; i--)
printf("| %d |\n", s[i]);
printf("\nReverse of stack content are:\n");
for(i=0; i<=top; i++)
printf("| %d |\n", s[i]);
for(i=0; i<=top/2; i++)
{
if( s[i] != s[top-i] )
{
flag = 0;
break;
}
}
if(flag == 1)
{
printf("\nIt is palindrome number");
}
else
{
printf("\nIt is not a palindrome number");
}
}
Program 4
#include<stdio.h>
#include<stdlib.h>
void evaluate();
void push(char);
char pop();
int prec(char);
char infix[30], postfix[30], stack[30];
int top = -1;
void main()
{
printf("\nEnter the valid infix expression:\t");
scanf("%s", infix);
evaluate();
printf("\nThe entered infix expression is :\n %s \n",
infix);
printf("\nThe corresponding postfix expression is :\n %s
\n", postfix);
}
void evaluate()
{
int i = 0, j = 0;
char symb, temp;
push('#');
for(i=0; infix[i] != '\0'; i++)
{
symb = infix[i];
switch(symb)
{
case '(' :
push(symb);
break;
case ')' :
temp = pop();
while(temp != '(' )
{
postfix[j] = temp;
j++;
temp = pop();
}
break;
case '+' :
case '-' :
case '*' :
case '/' :
case '%' :
case '^' :
case '$' :
while( prec(stack[top]) >= prec(symb) )
{
temp = pop();
postfix[j] = temp;
j++;
}
push(symb);
break;
default:
postfix[j] = symb;
j++;
}
}
while(top > 0)
{
temp = pop();
postfix[j] = temp;
j++;
}
postfix[j] = '\0';
}
void push(char item)
{
top = top+1;
stack[top] = item;
}
char pop()
{
char item;
item = stack[top];
top = top-1;
return item;
}
int prec(char symb)
{
int p;
switch(symb)
{
case '#' :
p = -1;
break;
case '(' :
case ')' :
p = 0;
break;
case '+' :
case '-' :
p = 1;
break;
case '*' :
case '/' :
case '%' :
p = 2;
break;
case '^' :
case '$' :
p = 3;
break;
}
return p;
}
Program 5a
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
int i, top = -1;
int op1, op2, res, s[20];
char postfix[90], symb;
void push(int item)
{
top = top+1;
s[top] = item;
}
int pop()
{
int item;
item = s[top];
top = top-1;
return item;
}
void main()
{
printf("\nEnter a valid postfix expression:\n");
scanf("%s", postfix);
for(i=0; postfix[i]!='\0'; i++)
{
symb = postfix[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;
case '$':
case '^':
push(pow(op1, op2));
break;
default : push(0);
}
}
}
res = pop();
printf("\n Result = %d", res);
}
Program 5b

#include <stdio.h>
void tower(int n, int source, int temp,int destination)
{
if(n == 0)
return;
tower(n-1, source, destination, temp);
printf("\nMove disc %d from %c to %c", n, source,
destination);
tower(n-1, temp, source, destination);
}
void main()
{
int n;
printf("\nEnter the number of discs: \n");
scanf("%d", &n);
tower(n, 'A', 'B', 'C');
printf("\n\nTotal Number of moves are: %d", (int)pow(2,n)-
1);
}
Program 6

#include <stdio.h>
#include<stdlib.h>
#include<stdio_ext.h>
#define MAX 3
char cq[MAX];
int front = -1, rear = -1;
void insert(char);
void delete();
void display();
void main()
{
int ch;
char item;
while(1)
{
printf("\n\n~~Main Menu~~");
printf("\n==> 1. Insertion and Overflow Demo");
printf("\n==> 2. Deletion and Underflow Demo");
printf("\n==> 3. Display");
printf("\n==> 4. Exit");
printf("\nEnter Your Choice: ");
scanf("%d", &ch);
__fpurge(stdin);
switch(ch)
{
case 1:
printf("\n\nEnter the element to be inserted: ");
scanf("%c", &item);
insert(item);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\nPlease enter a valid choice");
}
}
}
void insert(char item)
{
if(front == (rear+1)%MAX)
{
printf("\n\n~~Circular Queue Overflow~~");
}
Else
{
if(front == -1)
front = rear = 0;
else
rear = (rear+1)%MAX;
cq[rear] = item;
}
}
void delete()
{
char item;
if(front == -1)
{
printf("\n\n~~Circular Queue Underflow~~");
}
else
{
item = cq[front];
printf("\n\nDeleted element from the queue is: %c ",item );
if(front == rear) //only one element
front = rear = -1;
else
front = (front+1)%MAX;
}
}
void display ()
{
int i ;
if(front == -1)
{
printf("\n\nCircular Queue Empty");
}
else
{
printf("\nCircular Queue contents are:\n");
printf("Front[%d]-> ", front);
for(i = front; i != rear ; i = (i+1)%MAX)
{
printf(" %c", cq[i]);
}
printf(" %c", cq[i]);
printf(" <-[%d]Rear", rear);
}
}
Program 11

#include<stdio.h>
#include<stdlib.h>
int a[50][50], n, visited[50];
int q[20], front = -1,rear = -1;
int s[20], top = -1, count=0;
void bfs(int v)
{
int i, cur;
visited[v] = 1;
q[++rear] = v;
while(front!=rear)
{
cur = q[++front];
for(i=1;i<=n;i++)
{
if((a[cur][i]==1)&&(visited[i]==0))
{
q[++rear] = i;
visited[i] = 1;
printf("->%d ", i);
}
}
}
}
void dfs(int v)
{int i;
visited[v]=1;
s[++top] = v;
for(i=1;i<=n;i++)
{
if(a[v][i] == 1&& visited[i] == 0 )
{
printf("->%d ", i);
dfs(i);
}
}
}
int main()
{
int ch, start, i,j;
printf("\nEnter the number of vertices in graph: ");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=1; i<=n; i++)
{
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
}
for(i=1;i<=n;i++)
visited[i]=0;
printf("\nEnter the starting vertex: ");
scanf("%d",&start);
while(1)
{printf("\n==>1. BFS: Print all nodes reachable from a
given
starting node");
printf("\n==>2. DFS: Print all nodes reachable from a
given
starting node");
printf("\n==>3:Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nNodes reachable from starting vertex
%d
are: ", start);
bfs(start);
for(i=1;i<=n;i++)
{
if(visited[i]==0)
printf("\nThe vertex that is not reachable is %d" ,i);
}
break;
case 2: printf("\nNodes reachable from starting vertex
%d
are:\n",start);
dfs(start);
break;
case 3: exit(0);
default: printf("\nPlease enter valid choice:");
}
}
}
Program 8

#include<stdio.h>
#include<stdlib.h>
struct node
{
char ssn[25],name[25],dept[20],designation[25];
int sal;
long int phone;
struct node *llink;
struct node *rlink;
};
typedef struct node* NODE;
NODE first = NULL;
int count=0;
NODE create()
{
NODE enode;
enode = (NODE)malloc(sizeof(struct node));
if( enode== NULL)
{
printf("\nRunning out of memory");
exit(0);
}
printf("\nEnter the
ssn,Name,Department,Designation,Salary,PhoneNo of the
employee: \n");
scanf("%s %s %s %s %d %ld", enode->ssn, enode->name,
enode->dept, enode->designation,
&enode->sal, &enode->phone);
enode->llink=NULL;
enode->rlink=NULL;
count++;
return enode;
}
NODE insertfront()
{
NODE temp;
temp = create();
if(first == NULL)
{
return temp;
}
temp->rlink = first;
first->llink = temp;
return temp;
}
void display()
{
NODE cur;
int nodeno=1;
cur = first;
if(cur == NULL)
printf("\nNo Contents to display in DLL");
while(cur!=NULL)
{
printf("\nENode:%d||SSN:%s|Name:%s|Department:%s|Desig
nation:%s|Salary:%d|Phone no:%ld",
nodeno, cur->ssn, cur->name,cur->dept, cur->designation,
cur->sal, cur->phone);
cur = cur->rlink;
nodeno++;
}
printf("\nNo of employee nodes is %d",count);
}
NODE deletefront()
{
NODE temp;
if(first == NULL)
{
printf("\nDoubly Linked List is empty");
return NULL;
}
if(first->rlink== NULL)
{
printf("\nThe employee node with the ssn:%s is deleted", first-
>ssn);
free(first);
count--;
return NULL;
}
temp = first;
first = first->rlink;
temp->rlink = NULL;
first->llink = NULL;
printf("\nThe employee node with the ssn:%s is
deleted",temp->ssn);
free(temp);
count--;
return first;
}
NODE insertend()
{
NODE cur, temp;
temp = create();
if(first == NULL)
{
return temp;
}
cur= first;
while(cur->rlink!=NULL)
{
cur = cur->rlink;
}
cur->rlink = temp;
temp->llink = cur;
return first;
}
NODE deleteend()
{
NODE prev,cur;
if(first == NULL)
{
printf("\nDoubly Linked List is empty");
return NULL;
}
if(first->rlink == NULL)
{
printf("\nThe employee node with the ssn:%s is deleted",first-
>ssn);
free(first);
count--;
return NULL;
}
prev=NULL;
cur=first;
while(cur->rlink!=NULL)
{
prev=cur;
cur = cur->rlink;
}
cur->llink = NULL;
printf("\nThe employee node with the ssn:%s is deleted",cur-
>ssn);
free(cur);
prev->rlink = NULL;
count--;
return first;
}
void deqdemo()
{
int ch;
while(1)
{
printf("\nDemo Double Ended Queue Operation");
printf("\n1:InsertQueueFront\n 2: DeleteQueueFront\n
3:InsertQueueRear\n 4:DeleteQueueRear\n
5:DisplayStatus\n 6: Exit \n");
scanf("%d", &ch);
switch(ch)
{
case 1: first=insertfront();
break;
case 2: first=deletefront();
break;
case 3: first=insertend();
break;
case 4: first=deleteend();
break;
case 5: display();
break;
default : return;
}
}
}
void main()
{
int ch,i,n;
while(1)
{
printf("\n\n~~~Menu~~~");
printf("\n1:Create DLL of Employee Nodes");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtEnd");
printf("\n4:DeleteAtEnd");
printf("\n5:InsertAtFront");
printf("\n6:DeleteAtFront");
printf("\n7:Double Ended Queue Demo using DLL");
printf("\n8:Exit \n");
printf("\nPlease enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\nEnter the no of Employees: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
first = insertend();
break;
case 2: display();
break;
case 3: first = insertend();
break;
case 4: first = deleteend();
break;
case 5: first = insertfront();
break;
case 6: first = deletefront();
break;
case 7: deqdemo();
break;
case 8 : exit(0);
default: printf("\nPlease Enter the valid choice");
}
}
}

You might also like