0% found this document useful (0 votes)
18 views16 pages

May 22 2_48 PM

The document contains multiple C programming code snippets covering various topics such as finding the maximum of three numbers, searching in an array, handling 2D arrays, linked lists, stacks, and converting infix expressions to postfix. Each code snippet demonstrates specific functionalities like insertion, deletion, display, and reversing of data structures. The document serves as a practical guide for implementing fundamental data structures and algorithms in C.

Uploaded by

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

May 22 2_48 PM

The document contains multiple C programming code snippets covering various topics such as finding the maximum of three numbers, searching in an array, handling 2D arrays, linked lists, stacks, and converting infix expressions to postfix. Each code snippet demonstrates specific functionalities like insertion, deletion, display, and reversing of data structures. The document serves as a practical guide for implementing fundamental data structures and algorithms in C.

Uploaded by

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

1 (1a) :

#include<stdio.h>
void maximum(int*a, int*b,int*c);
void main(){
int n1, n2,n3;
printf("Enter the numbers : ");
scanf("%d%d%d", &n1, &n2, &n3);
maximum(&n1, &n2, &n3);
}
void maximum(int*a,int*b,int*c){
if(*a>*b && *a>*c){
printf("%d is the maximum", *a);
}else if(*b>*a && *b>*c){
printf("%d is the maximum", *b);
}else{
printf("%d is the maximum", *c);
}
}

1 (1b) :
#include<stdio.h>
int main(){
int a[10],n,i,key,*arr=a, flag=0;
printf("Enter the size : ");
scanf("%d", &n);
printf("Enter the elements of array : ");
for(i=0;i<n;i++){
scanf("%d", &a[i]);
}
printf("Enter the element to be searched : ");
scanf("%d",&key);
for(i=0;i<n;i++){
if(*arr==key){
flag=1;
printf("Element found");
break;
}
arr++;
}
if(flag==0){
printf("Element not found");
}
return 0;
}

1 (1c) :
#include<stdio.h>
void max(int (*a)[10],int r, int c){
int i, j, max;
for(i=0;i<r;i++){
max=a[i][0];
for(j=0;j<c;j++){
if(a[i][j]>max){
max=a[i][j];
}
}
printf("Maximum element in row %d is %d\n", i+1, max);
}
}
int main(){
int a[10][10],r,c,i,j;
printf("Enter the size of rows and columns :\n");
scanf("%d%d",&r,&c);
printf("Enter the elements:\n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf("%d", &a[i][j]);
}
}
max(a,r,c);
return 0;
}

2 (2a) :
#include <stdio.h>
#include <stdlib.h>
struct time {
int h, m, s;
struct time *next;
} *t1, *t2;
void display(struct time *t) {
printf("Time 1 : %02d:%02d:%02d\n", t->h, t->m, t->s);
t = t->next;
printf("Time 2 : %02d:%02d:%02d\n", t->h, t->m, t->s);
}
int main() {
t1 = (struct time *)malloc(sizeof(struct time));
t2 = (struct time *)malloc(sizeof(struct time));
printf("Enter time 1 (hour minute second): ");
scanf("%d %d %d", &t1->h, &t1->m, &t1->s);
printf("Enter time 2 (hour minute second): ");
scanf("%d %d %d", &t2->h, &t2->m, &t2->s);
t1->next = t2;
display(t1);
return 0;
}

3 (2b) :
#include <stdio.h>
struct Student {
char name[20];
int usn, m1, m2, m3;
float avg;
} S[30];
int main() {
int n, i, j;
printf("Enter the number of students: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter name: ");
scanf("%s", S[i].name);
printf("Enter USN: ");
scanf("%d", &S[i].usn);
printf("Enter marks of 3 subjects: ");
scanf("%d %d %d", &S[i].m1, &S[i].m2, &S[i].m3);
S[i].avg = (S[i].m1 + S[i].m2 + S[i].m3) / 3.0;
}
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (S[i].avg < S[j].avg) {
struct Student temp = S[i];
S[i] = S[j];
S[j] = temp;
}
}
}
printf("-------Student details-------\n");
for (i = 0; i < n; i++) {
printf("Name: %s\n", S[i].name);
printf("USN: %d\n", S[i].usn);
printf("Average: %.2f\n", S[i].avg);
}
return 0;
}

4 (3a) :
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
} NODE;
NODE* insertfront(NODE* start, int n) {
NODE* newnode = (NODE*)malloc(sizeof(NODE));
newnode->data = n;
newnode->next = start;
start = newnode;
return start;
}
NODE* deletesv(NODE* start, int key) {
NODE* ptr = start;
NODE* temp = NULL;
if (start == NULL) {
printf("List is empty\n");
return start;
}
if (start->data == key) {
start = start->next;
printf("%d is deleted\n", key);
free(ptr);
return start;
}
while (ptr != NULL && ptr->data != key) {
temp = ptr;
ptr = ptr->next;
}
if (ptr == NULL) {
printf("Invalid value\n");
} else {
temp->next = ptr->next;
printf("%d is deleted\n", key);
free(ptr);
}
return start;
}
void display(NODE* start) {
if (start == NULL) {
printf("List is empty\n");
} else {
NODE* ptr = start;
printf("List data: ");
while (ptr != NULL) {
printf("%d->", ptr->data);
ptr = ptr->next;
}
printf("NULL\n");
}
}
void printMenu() {
printf("\nEnter your choice\n");
printf("1. Insert at front\n");
printf("2. Delete a key\n");
printf("3. Display\n");
printf("4. Exit\n");
}
int main() {
NODE* start = NULL;
int n, key, choice;
printMenu();
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to insert: ");
scanf("%d", &n);
start = insertfront(start, n);
break;
case 2:
printf("Enter the key: ");
scanf("%d", &key);
start = deletesv(start, key);
break;
case 3:
display(start);
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}

5 (3b) :
#include<stdio.h>
#include<stdlib.h>
typedef struct node {
int data;
struct node* next;
} NODE;
NODE* insertfront(NODE* start, int n) {
NODE* newnode = (NODE*)malloc(sizeof(NODE));
newnode->data = n;
newnode->next = start;
start = newnode;
return start;
}
NODE* reverse(NODE* start) {
NODE* prev = NULL;
NODE* temp;
NODE* ptr = start;
if (start == NULL) {
printf("List is empty\n");
return start;
}
if (start->next == NULL) {
printf("Only one node %d\n", start->data);
return start;
} else {
while (ptr != NULL) {
temp = ptr->next;
ptr->next = prev;
prev = ptr;
ptr = temp;
}
start = prev;
return start;
}
}
void display(NODE* start) {
NODE* ptr = start;
if (start == NULL) {
printf("List is empty\n");
} else {
printf("List data: ");
while (ptr != NULL) {
printf("%d->", ptr->data);
ptr = ptr->next;
}
printf("NULL\n");
}
}
void printMenu() {
printf("\nEnter your choice\n");
printf("1. Insert\n");
printf("2. Reverse\n");
printf("3. Display\n");
printf("4. Exit\n");
}
int main() {
NODE* start = NULL;
int n, choice;
printMenu();
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value: ");
scanf("%d", &n);
start = insertfront(start, n);
break;
case 2:
printf("Reverse list\n");
start = reverse(start);
display(start);
break;
case 3:
display(start);
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}

6 (4) :
#include<stdio.h>
#include<stdlib.h>
typedef struct node {
int data;
struct node* leftlink;
struct node* rlink;
} NODE;
NODE* insertfront(NODE* start, int n) {
NODE* newnode = (NODE*)malloc(sizeof(NODE));
newnode->data = n;
newnode->leftlink = NULL;
newnode->rlink = start;
if (start != NULL) {
start->leftlink = newnode;
}
start = newnode;
return start;
}
NODE* deletekey(NODE* start, int key) {
NODE* ptr = start;
if (start == NULL) {
printf("List is empty\n");
return start;
}
while (ptr != NULL && ptr->data != key) {
ptr = ptr->rlink;
}
if (ptr == NULL) {
printf("Invalid key\n");
} else {
if (ptr->leftlink != NULL) {
(ptr->leftlink)->rlink = ptr->rlink;
} else {
start = ptr->rlink;
}
if (ptr->rlink != NULL) {
ptr->rlink->leftlink = ptr->leftlink;
}
printf("Item deleted: %d\n", ptr->data);
free(ptr);
}
return start;
}
void display(NODE* start) {
NODE* ptr = start;
if (start == NULL) {
printf("List is empty\n");
} else {
printf("List data: ");
while (ptr != NULL) {
printf("%d->", ptr->data);
ptr = ptr->rlink;
}
printf("NULL\n");
}
}
void printMenu() {
printf("\nEnter your choice\n");
printf("1. Insert\n");
printf("2. Delete key\n");
printf("3. Display\n");
printf("4. Exit\n");
}
int main() {
NODE* start = NULL;
int n, key, choice;
printMenu();
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value: ");
scanf("%d", &n);
start = insertfront(start, n);
break;
case 2:
printf("Enter the key: ");
scanf("%d", &key);
start = deletekey(start, key);
break;
case 3:
display(start);
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}

7 (5) :
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int stack[MAX];
int top = -1;
void push(int element) {
if (top == MAX - 1) {
printf("Stack overflow\n");
} else {
stack[++top] = element;
printf("%d pushed onto stack\n", element);
}
}
void pop() {
if (top == -1) {
printf("Stack underflow\n");
} else {
printf("%d popped from stack\n", stack[top--]);
}
}
void display() {
if (top == -1) {
printf("Stack is empty\n");
} else {
printf("Stack elements: ");
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
}
void printMenu() {
printf("\nEnter your choice\n");
printf("1. Push an element onto stack\n");
printf("2. Pop an element from stack\n");
printf("3. Display the status of stack\n");
printf("4. Exit\n");
}
int main() {
int choice, element;
printMenu();
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the element to push: ");
scanf("%d", &element);
push(element);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}

8 (6a) :
#include <stdio.h>
#include <ctype.h>
void infixtopostfix(char in[50]);
int precedence(char op);
int main() {
char infix[50];
printf("Enter the expression : ");
scanf("%s", &infix);
infixtopostfix(infix);
return 0;
}
int precedence(char op) {
if (op == '^') {
return 2;
} else if (op == '*' || op == '/' || op == '%') {
return 1;
} else if (op == '+' || op == '-') {
return 0;
} else {
return -1;
}
}
void infixtopostfix(char in[50]) {
char stack[50], postfix[50];
int top = -1, i, j = 0;
for (i = 0; in[i] != '\0'; i++) {
if (isalnum(in[i])) {
postfix[j++] = in[i];
} else if (in[i] == '(') {
stack[++top] = in[i];
} else if (in[i] == ')') {
while (top > -1 && stack[top] != '(') {
postfix[j++] = stack[top--];
}
top--;
} else if (in[i] == '+' || in[i] == '-' || in[i] == '*' || in[i] == '/' ||
in[i] == '%' || in[i] == '^') {
while (top > -1 && precedence(stack[top]) >= precedence(in[i])) {
postfix[j++] = stack[top--];
}
stack[++top] = in[i];
}
}
while (top > -1) {
if (stack[top] == '(') {
printf("Invalid expression\n");
return;
}
postfix[j++] = stack[top--];
}
postfix[j] = '\0';
printf("The corresponding postfix expression is %s\n", postfix);
}
9 (6b) :
#include<stdio.h>
#include<string.h>
#define MAX 10
char stack[MAX];
int top = -1;
int isfull() {
return top == MAX - 1;
}
int isempty() {
return top == -1;
}
void push(char ch) {
if (isfull()) {
printf("Stack overflow\n");
} else {
stack[++top] = ch;
}
}
char pop() {
if (isempty()) {
printf("Stack underflow\n");
return '\0';
} else {
return stack[top--];
}
}
int main() {
char str[MAX];
printf("Enter the string: ");
scanf("%s",&str);
for (int i = 0; i < strlen(str); i++) {
push(str[i]);
}
int i = 0, flag = 1;
while (!isempty()) {
char ch = pop();
if (ch != str[i]) {
flag = 0;
break;
}
i++;
}
if (flag) {
printf("It is a palindrome\n");
} else {
printf("It is not a palindrome\n");
}
return 0;
}

9 (6c) :
#include<stdio.h>
int Fibonacci(int n);
int main() {
int n, i = 0, res;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci series: ");
for (i = 0; i < n; i++) {
res = Fibonacci(i);
printf("%d\t", res);
}
printf("\n%d is the last term\n", res);
return 0;
}
int Fibonacci(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return (Fibonacci(n - 1) + Fibonacci(n - 2));
}
}

10 (7a) :
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int queue[MAX], front = -1, rear = -1;
void enqueue(int num) {
if (rear == MAX - 1) {
printf("Overflow\n");
return;
} else if (front == -1 && rear == -1) {
front = rear = 0;
} else {
rear++;
}
queue[rear] = num;
}
void dequeue() {
if (front == -1 || front > rear) {
printf("Underflow\n");
return;
} else {
printf("Deleted item: %d\n", queue[front]);
front++;
if (front > rear) {
front = rear = -1;
}
}
}
void display() {
if (front == -1 || front > rear) {
printf("Queue is empty\n");
} else {
printf("Queue data: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}
void printMenu() {
printf("\nEnter your choice\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
}
int main() {
int choice, num;
printMenu();
while(1) {
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter the value: ");
scanf("%d", &num);
enqueue(num);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}

11 (7b) :
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int num;
struct node* next;
} NODE;
NODE* front = NULL;
NODE* rear = NULL;
void insertrear(int n) {
NODE* newnode = (NODE*)malloc(sizeof(NODE));
newnode->num = n;
newnode->next = NULL;
if (front == NULL && rear == NULL) {
front = rear = newnode;
rear->next = front;
} else {
rear->next = newnode;
newnode->next = front;
rear = newnode;
}
}
void deletefront() {
NODE* ptr;
if (front == NULL && rear == NULL) {
printf("Queue is empty\n");
} else if (front == rear) {
printf("Item deleted: %d\n", front->num);
free(front);
front = rear = NULL;
} else {
ptr = front;
front = front->next;
rear->next = front;
printf("Item deleted: %d\n", ptr->num);
free(ptr);
}
}
void display() {
NODE* ptr = front;
if (ptr == NULL) {
printf("Queue is empty\n");
} else {
printf("The queue data are:\n");
do {
printf("%d\t", ptr->num);
ptr = ptr->next;
} while (ptr != front);
printf("\n");
}
}
void printMenu() {
printf("\nEnter your choice\n");
printf("1. Insert at rear\n");
printf("2. Delete at front\n");
printf("3. Display\n");
printf("4. Exit\n");
}
int main() {
int num, choice;
printMenu();
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the data to insert: ");
scanf("%d", &num);
insertrear(num);
break;
case 2:
deletefront();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
12 (8a):
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *left,
struct node *right;
};
struct node* createNode(int data) {
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
struct node* insert(struct node* node, int data) {
if (node == NULL){
return createNode(data);
}
if (data < node->data){
node->left = insert(node->left, data);
} else if (data > node->data){
node->right = insert(node->right, data);
}
return node;
}
void inorder(struct node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
void preorder(struct node* root) {
if (root != NULL) {
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct node* root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}
int main() {
struct node* root = NULL;
int n, data;
printf("Enter the number of nodes: ");
scanf("%d", &n);
printf("Enter node value: ");
for (int i = 0; i < n; i++) {
scanf("%d", &data);
root = insert(root, data);
}
printf("Inorder traversal: ");
inorder(root);
printf("\n");
printf("Preorder traversal: ");
preorder(root);
printf("\n");
printf("Postorder traversal: ");
postorder(root);
printf("\n");
return 0;
}

13 (8b) :
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *left,
struct node *right;
};
struct node* createNode(int data) {
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
struct node* insert(struct node* node, int data) {
if (node == NULL){
return createNode(data);
}
if (data < node->data){
node->left = insert(node->left, data);
}else if (data > node->data){
node->right = insert(node->right, data);
}
return node;
}
int totalLeafNodes(struct node* node) {
if (node == NULL){
return 0;
}
if (node->left == NULL && node->right == NULL) {
printf("%d ", node->data);
return 1;
}
return totalLeafNodes(node->left) + totalLeafNodes(node->right);
}
int main() {
struct node* root = NULL;
int n, data;
printf("Enter the number of nodes: ");
scanf("%d", &n);
printf("Enter node value: ");
for (int i = 0; i < n; i++) {
scanf("%d", &data);
root = insert(root, data);
}
printf("Leaf nodes: ");
int leafCount = totalLeafNodes(root);
printf("\nNumber of leaf nodes: %d\n", leafCount);
return 0;
}

You might also like