Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions codes/dma/calloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include<stdio.h>
#include<stdlib.h>

int main(){
int number;
float *p;
printf("Enter the number of blocks that you want to allocate:");
scanf("%d",&number);

p=(float*)calloc(number,sizeof(float));
//here it allocates with respect to the number

if(p==NULL){
printf("No space has been allocated");
}
for(int i=0;i<number;++i){
printf("p[%d]=",i);
scanf("%f",p+i);
//didnt use & because p+i it is directly referencing to the memory
}
//displaying all the elements in the dynmaically allocated array
for(int i=0;i<number;++i){
printf("%f\t",*(p+i));
}
free(p);
return 0;
}
21 changes: 21 additions & 0 deletions codes/dma/malloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include<stdio.h>
#include<stdlib.h>

int main(){
int i, sum=0, *p;
int n;
printf("Enter the number of int spaces that you want to dynamically allocate:\n");
scanf("%d",&n);
p= (int*)malloc(n*sizeof(int));
if(p==NULL){
printf("No space was dynamically allocated");
}
for(int i=0;i<n;++i){
printf("p[%d]=\n",i);
scanf("%d",p+i);
sum+=*(p+i);
}
printf("\nNow the sum of all the items of the array is %d",sum);
free(p);
return 0;
}
97 changes: 97 additions & 0 deletions codes/queue/circularqueue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Circular Queue implementation in C

#include <stdio.h>

#define SIZE 5

int items[SIZE];
int front = -1, rear = -1;

// Check if the queue is full
int isFull() {
if ((front == rear + 1) || (front == 0 && rear == SIZE - 1)) return 1;
return 0;
}

// Check if the queue is empty
int isEmpty() {
if (front == -1) return 1;
return 0;
}

// Adding an element
void enQueue(int element) {
if (isFull())
printf("\n Queue is full!! \n");
else {
if (front == -1) front = 0;
rear = (rear + 1) % SIZE;
items[rear] = element;
printf("\n Inserted -> %d", element);
}
}

// Removing an element
int deQueue() {
int element;
if (isEmpty()) {
printf("\n Queue is empty !! \n");
return (-1);
} else {
element = items[front];
if (front == rear) {
front = -1;
rear = -1;
}
// Q has only one element, so we reset the
// queue after dequeing it. ?
else {
front = (front + 1) % SIZE;
}
printf("\n Deleted element -> %d \n", element);
return (element);
}
}

// Display the queue
void display() {
int i;
if (isEmpty())
printf(" \n Empty Queue\n");
else {
printf("\n Front -> %d ", front);
printf("\n Items -> ");
for (i = front; i != rear; i = (i + 1) % SIZE) {
printf("%d ", items[i]);
}
printf("%d ", items[i]);
printf("\n Rear -> %d \n", rear);
}
}

int main() {
// Fails because front = -1
deQueue();

enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);

// Fails to enqueue because front == 0 && rear == SIZE - 1
enQueue(6);

display();
deQueue();

display();

enQueue(7);
display();

// Fails to enqueue because front == rear + 1
enQueue(8);

return 0;
}
106 changes: 106 additions & 0 deletions codes/queue/priorityqueue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include <stdio.h>
#include <stdlib.h>

#define size 3

void enqueue(int item);
void dequeue();
void show();
int front = -1;
int arr[size];
int priority[size];
int largest;
int ans;

int main()
{
do
{

int choice, item;
printf("1)Enqueue\n2)Dequeue\n3)Show");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter the item that you want to enqueue");
scanf("%d", &item);
enqueue(item);
break;
case 2:
dequeue();
break;
case 3:
show();
break;

default:
printf("Choose a correct input");
break;
}
printf("Do you want to continue?\t press 0 for yes and 1 for no\n");
scanf("%d",&ans);
} while (ans == 0);
}
void enqueue(int item)
{
if (front == size - 1)
{
printf("not enough space\n");
}
else
{
front++;
arr[front] = item;
printf("Enter the priority of it:\n");
scanf("%d", &priority[front]);
}
}
void dequeue()
{
int item;
if (front == -1)
{
printf("there is no element");
}
else
{
for (int i = 1; i < size; i++)
{
if (priority[i] > priority[i - 1])
{
if(priority[i]>priority[i+1]){
largest=i;
}
}
else if (priority[i]<priority[i-1])
{
if(priority[i-1]>priority[i+1]){
largest=i-1;
}
else{
largest=i+1;
}
}

}
front--;
item=arr[largest];
printf("The element with the highest priority is %d",item);
printf("The item %d has been dequeued",item);
for(int i=0;i<size;i++){
if(item == arr[i]){
arr[i]=0;
priority[i]=0;
}
}
}
}

void show()
{
for (int i = 0; i < size; i++)
{
printf("%d\t", arr[i]);
}
}