From 3a568193ae5c7412ce2de5b856557ef89ad9a165 Mon Sep 17 00:00:00 2001 From: Ujjwal Puri Date: Sun, 11 Feb 2024 20:24:39 +0545 Subject: [PATCH 1/6] dynamic memory allocation using malloc in c --- codes/dma/malloc.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 codes/dma/malloc.c diff --git a/codes/dma/malloc.c b/codes/dma/malloc.c new file mode 100644 index 0000000..3f791f3 --- /dev/null +++ b/codes/dma/malloc.c @@ -0,0 +1,21 @@ +#include +#include + +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 Date: Sun, 11 Feb 2024 20:26:14 +0545 Subject: [PATCH 2/6] new folder for dma basics --- codes/dma/malloc.exe | Bin 0 -> 16192 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100755 codes/dma/malloc.exe diff --git a/codes/dma/malloc.exe b/codes/dma/malloc.exe new file mode 100755 index 0000000000000000000000000000000000000000..d5c2a2cc1eb8b088a635feb435a251069802860f GIT binary patch literal 16192 zcmeHOeQX>@6`!-?#BGzqC?vR)iU)<1mN`tA8V0trh7e(~Lo9*s zwPKA}2=oH@gsIt&*GrrR#&Xgt1w^}QtQ5faJG5Zr5h9{pv2tpW)50h!9U1M)uu8HY zYiJyd-0t?LpfMChe!KW2DnMcM+F@))Yh|+-ZJdt-6qi$kY=z;fuO9eXeR9U!|G73oI!vB9AQBlq_Su*10g zyM<5lLF%vAdB{-w87aJCQzG8AtzlClR-1^Ya(%UZJGRwsYf!DUx>*hZ$ECmr=hU8k zhXuG9Lyp2IN(LO6{b5@bwWEIX%df2b#?ceIPyFWO>DM29Xw$k6pZGfJ;JDESb+Dj6 zo+2n?{T}$BjuVeBLCxV1ubIC{A{HwuG~8RDaYYgSE5IuS^fwgIZ!W?gD#AYxyaFG8 z!8jBe=awS;4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V4 LWtGpsCyIXnQ0*B_ literal 0 HcmV?d00001 From c479b9e30877ed089f32dc0506841825005bf7c8 Mon Sep 17 00:00:00 2001 From: Ujjwal Puri Date: Sun, 11 Feb 2024 20:29:42 +0545 Subject: [PATCH 3/6] Dynamic Memory Allocation --- codes/dma/malloc.exe | Bin 16192 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100755 codes/dma/malloc.exe diff --git a/codes/dma/malloc.exe b/codes/dma/malloc.exe deleted file mode 100755 index d5c2a2cc1eb8b088a635feb435a251069802860f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16192 zcmeHOeQX>@6`!-?#BGzqC?vR)iU)<1mN`tA8V0trh7e(~Lo9*s zwPKA}2=oH@gsIt&*GrrR#&Xgt1w^}QtQ5faJG5Zr5h9{pv2tpW)50h!9U1M)uu8HY zYiJyd-0t?LpfMChe!KW2DnMcM+F@))Yh|+-ZJdt-6qi$kY=z;fuO9eXeR9U!|G73oI!vB9AQBlq_Su*10g zyM<5lLF%vAdB{-w87aJCQzG8AtzlClR-1^Ya(%UZJGRwsYf!DUx>*hZ$ECmr=hU8k zhXuG9Lyp2IN(LO6{b5@bwWEIX%df2b#?ceIPyFWO>DM29Xw$k6pZGfJ;JDESb+Dj6 zo+2n?{T}$BjuVeBLCxV1ubIC{A{HwuG~8RDaYYgSE5IuS^fwgIZ!W?gD#AYxyaFG8 z!8jBe=awS;4}n+U<1Zl~*7-@4bv@c==-u&@k%&KRilmW9q@%#Xd5`LrZDegdX~a_^ zld~<+oi$Bpi(BdF&YijyHB#Lmf>!jH9_>A*cN_79$YkRwyIY_GoG07Fo{skB7Jak2 z-K{mawJmei&FWU6w|DN#FvMqnP8sf>$@Eu=dyR6k9&B&rciUj!_%!^I+*tGcT2|kn)UE_ z-EjF9@#q8R2$%C79>W1 z2l8!rZmC#1C%^5J-}K2hee&;p@~^yd{hRGW*FL8Wy`c@>m_E=MsUNGqqK!2E3`Uh- z{yotAOSf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 z5kOdWuDe@}in&a6-A?LJ%vHBQGnUqS66r1@p~viW*3yk!pNOWDnS^PZF}1F+2Ik<$ zb@|txZl>&PKV;h*NmGyIlF5EB@klyo+VfPMw0#(nuD2a*+8fa$`&xC#k#GI66Yuni%P69zPY*MO_pK>G(=!WjgbI;sC)I)5L49<$PVuaS!39)b4#pcRX! z^qghJXg9-rS7s^(F1jp>+{xVy^Y=8k2HQas_8@RHadSe!9S8F_QMl%E=yqLQaLq~O=rxk-S45ga&g##6OR$q)ZAW+Q&Fn-8dk4R z;g4wO!a+Ohw0yzLTJdyho}fcp)=U^^K($Q57OEVk3RSfy4I)-7Gb&Wu>;nb0K7L+b#(~474BPWMnUUAU zpdt&oV^Mq_YPjZPdtPTUaz8wN)?+*Y_PB;+nb)z52dF3i_;~;$P{Ek8J+G@7$4HU; z_aFZgWZz7ByiR8v_1NRF`0bwp4r9Xp-QOYlJ>usVawEseb;Kv(-(>t zpOU@XjY67)I6iOT^T*uJp@Q*e`(pb!S>NCP?|k;tilfQ+Z8jwI_kR<3NG6rnAgb_l zFYovC>$A*w3v4Pp_WT@hwidn3%~wa7liB_@G~nFj=Rsbd^Zs4lr;FDg7PjN_kNY1n zer(Ur#iN^0TZoJU?l;>rE(Bw*y$F+PorByDJ77DO7lSc!+&`};E6Lukk9~Q|GU}hy z-6po*=rT@)?Y+oz$EzA9hkPIY05szIo5#cVMSPy*v3u>S8ysnm&pG$Yy32~#>-fV4 LWtGpsCyIXnQ0*B_ From e3f1cd1ec315218849a2ee00969e069bf280b116 Mon Sep 17 00:00:00 2001 From: Ujjwal Puri Date: Sun, 11 Feb 2024 20:41:59 +0545 Subject: [PATCH 4/6] dma using calloc --- codes/dma/calloc.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 codes/dma/calloc.c diff --git a/codes/dma/calloc.c b/codes/dma/calloc.c new file mode 100644 index 0000000..65edc54 --- /dev/null +++ b/codes/dma/calloc.c @@ -0,0 +1,27 @@ +#include +#include + +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 Date: Sun, 11 Feb 2024 23:13:37 +0545 Subject: [PATCH 5/6] made a priority queue --- codes/queue/priorityqueue.c | 106 ++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 codes/queue/priorityqueue.c diff --git a/codes/queue/priorityqueue.c b/codes/queue/priorityqueue.c new file mode 100644 index 0000000..6739460 --- /dev/null +++ b/codes/queue/priorityqueue.c @@ -0,0 +1,106 @@ +#include +#include + +#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]){ + 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 Date: Mon, 12 Feb 2024 01:03:53 +0545 Subject: [PATCH 6/6] circular queue --- codes/queue/circularqueue.c | 97 +++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 codes/queue/circularqueue.c diff --git a/codes/queue/circularqueue.c b/codes/queue/circularqueue.c new file mode 100644 index 0000000..b118268 --- /dev/null +++ b/codes/queue/circularqueue.c @@ -0,0 +1,97 @@ +// Circular Queue implementation in C + +#include + +#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; +} \ No newline at end of file