C Dsa
C Dsa
#include<stdio.h>
#define CAPACITY 3
int stack [CAPACITY];
int top = -1;
int pop(){
if (top >= 0){
int val = stack[top];
top = top-1;
return val;
}
printf("exception from pop! empty stack\n");
return -1;
}
int main(){
printf("Implementing my stack in c.\n");
peek();
push(10);
push(20);
push(30);
printf("Pop item : %d\n",pop());
push(40);
printf("top of the stack : %d\n" , peek());
return 0;
}
QUEUE:--
#include<stdio.h>
#include<stdbool.h>
#define CAPACITY 5
int ourQueue[CAPACITY];
int front = 0, rear = -1, totalItem = 0;
bool isFull(){
if(totalItem == CAPACITY){
return true;
}
return false;
}
bool isEmpty(){
if(totalItem == 0){
return true;
}
return false;
}
void enqueue(int item){
if(isFull()){
printf("Sorry, the Queue is full.\n");
return;
}
rear = (rear + 1) % CAPACITY;
ourQueue[rear] = item;
totalItem++;
}
int dequeue(){
if(isEmpty()){
printf("Sorry, the Queue is empty.\n");
return -1;
}
int frontItem = ourQueue[front];
ourQueue[front] = -1;
front = (front + 1) % CAPACITY;
totalItem--;
return frontItem;
}
void printQueue(){
int i;
printf("Queue: ");
for (i = 0; i < CAPACITY; i++){
printf("%d ", ourQueue[i]);
}
printf("\n");
}
int main(){
printf("\n***********\nLet's implement our Queue.\n\n");
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
printQueue();
enqueue(50);
printQueue();
enqueue(60);
printf("Deque: %d\n", dequeue());
printQueue();
enqueue(60);
printQueue();
return 0;
}
LINKED LIST :--
#include<stdio.h>
#include<stdlib.h>
struct node {
int data ;
struct node *next;
};
int main(){
struct node *a = NULL;
struct node *b = NULL;
struct node *c = NULL;
a = (struct node* )malloc(sizeof(struct node));
b = (struct node* )malloc(sizeof(struct node));
c = (struct node* )malloc(sizeof(struct node));
a->data = 10;
b->data = 20;
c->data = 30;
a->next = b;
b->next = c;
c->next = NULL;
while (a != NULL){
printf("%d -> ",a->data);
a = a->next;
}
return 0;
}
ARRAY TO LINKED LIST :--
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *createlinkedlist(int arr[], int size);
int main() {
int a[] = {5, 10, 30};
struct node *head;
head = createlinkedlist(a, 3);
struct node *current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
return 0;
}
struct node *createlinkedlist(int arr[], int size) {
struct node *head = NULL, *temp = NULL, *current = NULL;
#include <stdio.h>
int main() {
int n, arr[100];
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements:\n");
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
printf("Reversed array:\n");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n");
return 0;
}
SUM OF TWO NUMBER IN ARRAY
#include <stdio.h>
int main() {
int arr[100];
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int m;
printf("Enter the target sum: ");
scanf("%d", &m);
int i = 0;
int j = n - 1;
while (i < j) {
int sum = arr[i] + arr[j];
if (sum == m) {
printf("The result: %d + %d = %d\n", arr[i], arr[j], sum);
i++;
j--;
} else if (sum < m) {
i++;
} else {
j--;
}
}
return 0;
}
PALINDROME
#include <stdio.h>
int main() {
int x, original, reversed = 0;
printf("Enter an integer: ");
scanf("%d", &x);
original = x;
if (x == 0) {
printf("The number is a palindrome.\n");
return 0;
}
while (x != 0) {
reversed = reversed * 10 + (x % 10);
x /= 10;
}
if (original == reversed) {
printf("true");
} else {
printf("false");
}
return 0;
}
INTEGER TO ROMAN
#include <stdio.h>
#include <string.h>
void int_to_roman(int num, char *result) {
int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10,
9, 5, 4, 1};
const char *symbols[] = {"M", "CM", "D", "CD", "C", "XC", "
L", "XL", "X", "IX", "V", "IV", "I"};
result[0] = '\0';
for (int i = 0; i < 13; i++) {
while (num >= values[i]) {
num -= values[i];
strcat(result, symbols[i]);
}
}
}
int main() {
int number ;
printf("enter number :");
scanf(" %d",&number);
char roman[20];
int_to_roman(number, roman);
printf("The Roman numeral for %d is %s\n", number,
roman);
return 0;
}
ROMAN TO INTEGER
#include <stdio.h>
#include <string.h>
int roman_to_int(const char *roman) {
int total = 0;
int prev_value = 0;
const struct {
char symbol;
int value;
} roman_map[] = {
{'M', 1000},
{'D', 500},
{'C', 100},
{'L', 50},
{'X', 10},
{'V', 5},
{'I', 1}
};
for (int i = strlen(roman) - 1; i >= 0; i--) {
char current_symbol = roman[i];
int current_value = 0;
for (int j = 0; j < 7; j++) {
if (roman_map[j].symbol == current_symbol) {
current_value = roman_map[j].value;
break;
}
}
if (current_value < prev_value) {
total -= current_value;
} else {
total += current_value;
}
prev_value = current_value;
}
return total;
}
int main() {
char roman[20];
printf("Enter Roman numeral: ");
scanf("%s", roman);
int result = roman_to_int(roman);
printf("The integer value for %s is %d\n", roman, result);
return 0;
}
LINK LIST
#include<stdio.h>
#include<stdlib.h>
struct y {
int x ;
struct y *z;
};
int main(){
struct y *a = NULL;
struct y *b = NULL;
struct y *c = NULL;
a = (struct y* )malloc(sizeof(struct y));
b = (struct y* )malloc(sizeof(struct y));
c = (struct y* )malloc(sizeof(struct y));
a->x = 10;
b->x = 20;
c->x= 30;
a->z = b;
b->z = c;
c->z = NULL;
while (a != NULL){
printf("%d ",a->x);
a = a->z;
}
return 0;
}