Data Structure And Algorithm
1. Dynamic Memory Allocation with calloc and realloc
#include <stdio.h>
#include <stdlib.h>
int main() {
int* ptr;
int n = 5, i;
printf("Enter number of elements: %d\n", n);
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
for (i = 0; i < n; ++i) ptr[i] = i + 1;
printf("Array elements: ");
for (i = 0; i < n; ++i) printf("%d ", ptr[i]);
n = 10;
ptr = (int*)realloc(ptr, n * sizeof(int));
if (ptr == NULL) {
printf("Reallocation failed.\n");
exit(0);
}
for (i = 5; i < n; ++i) ptr[i] = i + 1;
printf("\nAfter realloc: ");
for (i = 0; i < n; ++i) printf("%d ", ptr[i]);
free(ptr);
return 0;
}
2. Dynamic Memory Allocation using malloc and scanf
#include <stdio.h>
#include <stdlib.h>
int main() {
int* ptr;
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
return 1;
}
for (i = 0; i < n; ++i) ptr[i] = i + 1;
printf("Array elements: ");
for (i = 0; i < n; ++i) printf("%d ", ptr[i]);
free(ptr);
return 0;
}
3. malloc vs calloc and freeing memory
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr, *ptr1;
int n = 5;
ptr = (int*)malloc(n * sizeof(int));
ptr1 = (int*)calloc(n, sizeof(int));
if (!ptr || !ptr1) {
printf("Memory allocation failed.\n");
return 1;
}
free(ptr);
printf("Malloc memory freed.\n");
free(ptr1);
printf("Calloc memory freed.\n");
return 0;
}
4. Stack Operations (Push and Pop)
#include <stdio.h>
#define MAX 5
int stack[MAX], top = -1;
void push(int val) {
if (top == MAX - 1)
printf("Stack Overflow\n");
else
stack[++top] = val;
}
int pop() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
}
return stack[top--];
}
int main() {
push(10);
push(20);
push(30);
printf("Popped: %d\n", pop());
printf("Popped: %d\n", pop());
return 0;
}
5. Singly Linked List Operations
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertEnd(struct Node** head, int data) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) *head = newNode;
else {
struct Node* temp = *head;
while (temp->next) temp = temp->next;
temp->next = newNode;
}
}
void display(struct Node* head) {
while (head) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
insertEnd(&head, 1);
insertEnd(&head, 2);
insertEnd(&head, 3);
display(head);
return 0;
}
6. Doubly Linked List Operations
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
void insertFront(struct Node** head, int data) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = *head;
if (*head != NULL)
(*head)->prev = newNode;
*head = newNode;
}
void display(struct Node* head) {
while (head) {
printf("%d <-> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
insertFront(&head, 10);
insertFront(&head, 20);
insertFront(&head, 30);
display(head);
return 0;
}
7. Circular Linked List Operations
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insert(struct Node** last, int data) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = data;
if (*last == NULL) {
newNode->next = newNode;
*last = newNode;
} else {
newNode->next = (*last)->next;
(*last)->next = newNode;
*last = newNode;
}
}
void display(struct Node* last) {
if (!last) return;
struct Node* p = last->next;
do {
printf("%d -> ", p->data);
p = p->next;
} while (p != last->next);
printf("(back to start)\n");
}
int main() {
struct Node* last = NULL;
insert(&last, 1);
insert(&last, 2);
insert(&last, 3);
display(last);
return 0;
}
Very useful thanks ☺️😊
ReplyDeletehttps://drive.google.com/file/d/18wpmBGqvL76PX_7bPhPgZYUlPK3RM67l/view?usp=sharing
ReplyDeletehttps://himal36.github.io/myagdifarmer/
ReplyDelete
ReplyDeletefor update 2082-04-08
update student set stdname = 'Shyamu' where stdid=3;
alter table student drop column symbol;create table hospital ( hid int primary key, name varchar(30), address varchar(30), phone int);
create table covid_patient ( pid int primary key, name varchar(30), address varchar(30), gender varchar(30), age int);
create table admitted ( hid int foreign key (hid) references hospital (hid), pid int foreign key (pid) references covid_patient(pid), admitteddate date, dischargeddate date, remarks varchar(30));
insert into covid_patient(pid, name, address, gender, age) values(01, 'sunil','beni', 'male',23),
(02, 'kundan','beni', 'female', 22),
(03, 'kusal','palpa','male',23),
(04, 'pukar','sunaul', 'male',29);
insert into admitted(hid, pid, admitteddate, dischargeddate, remarks)values(1, 01, '2021 july 7', '2021 july 12', 'discharged'),
(2, 02, '2021 july 18', '2021 july 19', 'discharged'),
(3, 03, '2021 july 6', '2021 july 13', 'discharged'),
(4, 04, '2021 july 9', '2021 july 17', 'discharged');
select covid_patient.name from covid_patient, admitted where covid_patient.pid=admitted.pid and admitted.admitteddate> 2021-07-07;
SELECT Hospital.Name, COUNT(Admitted.PID) AS PatientCount
ReplyDeleteFROM Hospital
JOIN Admitted ON Hospital.HID = Admitted.HID
GROUP BY Hospital.HID, Hospital.Name
HAVING COUNT(Admitted.PID) > 30;
i have installed kali in my winndows 10, so there is two os kali and windows when i open my pc kali is in top and windows boot manager is in 3rd , as kali is in top it open after 5 second if any not touch but i want open windows by default if kali is not selected
ReplyDelete