0% found this document useful (0 votes)
8 views10 pages

C Dsa

The document contains multiple C programming examples demonstrating data structures and algorithms, including stack, queue, linked list, and operations like reversing an array, checking for palindromes, and converting between integers and Roman numerals. Each section provides code snippets with explanations of how to implement and utilize these data structures and algorithms. The examples cover basic operations such as push, pop, enqueue, dequeue, and memory management for linked lists.

Uploaded by

s36347032
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views10 pages

C Dsa

The document contains multiple C programming examples demonstrating data structures and algorithms, including stack, queue, linked list, and operations like reversing an array, checking for palindromes, and converting between integers and Roman numerals. Each section provides code snippets with explanations of how to implement and utilize these data structures and algorithms. The examples cover basic operations such as push, pop, enqueue, dequeue, and memory management for linked lists.

Uploaded by

s36347032
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

STACK :--

#include<stdio.h>
#define CAPACITY 3
int stack [CAPACITY];
int top = -1;

void push(int x){


if(top<CAPACITY-1){
top = top+1;
stack[top]=x;
printf("succesfully added item %d\n", x );
}
else
{
printf("exception! no spaces\n");
}

int pop(){
if (top >= 0){
int val = stack[top];
top = top-1;
return val;
}
printf("exception from pop! empty stack\n");

return -1;
}

int peek (){


if(top >= 0){
return stack[top];
}
printf("exception from peek! 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;

for (int i = 0; i < size; i++) {


temp = (struct node *)malloc(sizeof(struct node));
if (!temp) {
printf("Memory allocation failed\n");
return NULL;
}
temp->data = arr[i];
temp->next = NULL;
if (head == NULL) {
head = temp;
current = temp;
} else {
current->next = temp;
current = current->next;
}
}
return head;
}
REVERSE ARRAY

#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]);

for (int i = 0; i < n / 2; i++) {


int temp = arr[i];
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = temp;
}

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;
}

You might also like