
Learning Psychology Notes
21st Century Skills Note
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
ReplyDelete