Skip to content

Instantly share code, notes, and snippets.

@max-programming
Last active February 20, 2023 04:14
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save max-programming/bcb5151c0241f83c14ada9ff555afa7c to your computer and use it in GitHub Desktop.
DFS All Practicals
#include <stdio.h>
int n = 5;
int arr[5];
void print()
{
for (int i = 0; i < n; i++)
{
printf("%d :- %d\n", i, arr[i]);
}
}
void insert()
{
int j, d, i;
printf("Enter Index of number to be Inserted:");
scanf("%d", &j);
printf("Enter Number to be Inserted:");
scanf("%d", &d);
for (i = n - 1; i >= j; i--)
{
arr[i + 1] = arr[i];
}
n++;
arr[j] = d;
print();
}
void delete()
{
int j, i;
printf("Enter Index of number to be deleted:");
scanf("%d", &j);
for (i = j; i < n; i++)
{
arr[i] = arr[i + 1];
}
n--;
print();
}
void search()
{
int d, i, f = 0;
printf("Enter Element to be Searched:");
scanf("%d", &d);
for (i = 0; i < n; i++)
{
if (d == arr[i])
{
printf("The Index No. of Element is %d\n", i);
}
}
if (f == 0)
{
printf("");
}
}
void replace()
{
int j, d;
printf("Enter Index of number to be replaced:");
scanf("%d", &j);
printf("Enter Number to be replaced:");
scanf("%d", &d);
arr[j] = d;
print();
}
void main()
{
int ch, m;
for (int i = 0; i < 5; i++)
{
printf("Enter Element:");
scanf("%d", &arr[i]);
}
while (m != 0)
{
printf("Enter your Choice:- \n 1)Print \n 2)Insert \n 3)Delete \n 4)Search \n 5)Replace \n");
scanf("%d", &ch);
switch (ch)
{
case 1:
print();
break;
case 2:
insert();
break;
case 3:
delete();
break;
case 4:
search();
break;
case 5:
replace();
break;
default:
printf("Invalid Choice");
break;
}
printf("Enter 0 to stop and press any number to continue");
scanf("%d", &m);
}
}
#include <stdio.h>
struct Student
{
char enrollment_number[15], name[20];
float marks1, marks2, marks3, avg;
};
void show_output(struct Student s)
{
printf("Enrollment Number - %s\n", s.enrollment_number);
printf("Name - %s\n", s.name);
printf("Marks 1 - %.2f\n", s.marks1);
printf("Marks 2 - %.2f\n", s.marks2);
printf("Marks 3 - %.2f\n", s.marks3);
printf("Average Marks - %.2f\n", s.avg);
}
void main()
{
struct Student students[3];
for (int i = 0; i < 3; i++)
{
struct Student s = students[i];
printf("\nEnter details for student %d\n", i + 1);
printf("Enter your enrollment number: ");
scanf("%s", &s.enrollment_number);
printf("Enter your name: ");
scanf("%s", &s.name);
printf("Enter marks 1: ");
scanf("%f", &s.marks1);
printf("Enter marks 2: ");
scanf("%f", &s.marks2);
printf("Enter marks 3: ");
scanf("%f", &s.marks3);
s.avg = (s.marks1 + s.marks2 + s.marks3) / 3;
show_output(s);
}
}
#include <stdio.h>
int stack[5];
int top = -1;
int n = 5;
void display()
{
for (int i = 0; i < top + 1; i++)
{
printf("%d\n", stack[i]);
}
}
int pop()
{
if (top == -1)
{
printf("Stack Underflow\n");
return -1;
}
int value = stack[top];
top--;
return value;
}
int peep()
{
if (top == -1)
{
printf("Stack Underflow\n");
return -1;
}
return stack[top];
}
void push(int x)
{
if (top >= n + 1)
{
printf("Stack Overflow\n");
return;
}
top++;
stack[top] = x;
}
void main()
{
int choice, x, removed_element, topmost;
do
{
printf("Enter a number: \n1. Display\n2. Add\n3. Remove Topmost\n4. Display Topmost\n> ");
scanf("%d", &choice);
switch (choice)
{
case 1:
display();
break;
case 2:
printf("Enter element to add: ");
scanf("%d", &x);
push(x);
break;
case 3:
removed_element = pop();
printf("Removed element was %d\n", removed_element);
break;
case 4:
topmost = peep();
printf("Topmost element is %d\n", topmost);
break;
default:
break;
}
} while (choice != 0);
}
#include <stdio.h>
char stack[10];
int top = -1;
int n = 10;
char pop()
{
if (top == -1)
{
// printf("Stack Underflow\n");
return;
}
int value = stack[top];
top--;
return value;
}
void push(char x)
{
if (top >= n + 1)
{
// printf("Stack Overflow\n");
return;
}
top++;
stack[top] = x;
}
void main()
{
char str[10], rev[10];
int i = 0;
printf("Enter some text: ");
gets(str);
while (str[i] != '\0')
{
push(str[i]);
i++;
}
i = 0;
while (top != -1)
{
rev[i] = pop();
i++;
}
printf("Reversed string is: ");
puts(rev);
}
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
} *start = NULL;
void insert_front()
{
int value;
struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
printf("Enter data to front: ");
scanf("%d", &value);
new_node->data = value;
new_node->next = start;
start = new_node;
}
void insert_end()
{
int value;
struct Node *last = start;
struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
printf("Enter data: ");
scanf("%d", &value);
new_node->data = value;
new_node->next = NULL;
if (start == NULL)
{
start = new_node;
}
else
{
while (last->next != NULL)
{
last = last->next;
}
last->next = new_node;
}
}
void delete_front()
{
if (start == NULL)
{
printf("Underflow\n");
return;
}
struct Node *ptr = start;
start = start->next;
free(ptr);
}
void delete_end()
{
if (start == NULL)
{
printf("Underflow\n");
return;
}
struct Node *last = start;
struct Node *pre = start;
while (last->next != NULL)
{
pre = last;
last = last->next;
}
pre->next = NULL;
free(last);
}
void display()
{
struct Node *last = start;
while (last != NULL)
{
printf("%d->", last->data);
last = last->next;
}
printf("NULL\n");
}
void main()
{
int choice;
do
{
printf("Enter a choice. 1. Insert Front, 2. Insert End. 3. Delete Front. 4. Delete End. 5. Display\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert_front();
break;
case 2:
insert_end();
break;
case 3:
delete_front();
break;
case 4:
delete_end();
break;
case 5:
display();
break;
default:
break;
}
} while (choice != 6);
}
@max-programming
Copy link
Author

max-programming commented Feb 20, 2023

Missing practicals are on the repository
https://github.com/max-programming/dfs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment