Major Assign
Major Assign
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5}; //
int size = sizeof(arr) / sizeof(int);
return 0;
}
OUTPUT
Array elements Are:
1
2
3
4
5
Q.2 Write a program in c that demonstrates insertion of elements in an array.
#include <stdio.h>
int main() {
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int s = 10; // current size of the array
int pos, val;
return 0;
}
OUTPUT
Enter the position where you want to insert an element: 8
Enter the value to insert: 6
Array after insertion:
1 2 3 4 5 6 7 6 8 9 10
--------------------------------
Q.3 Write a program in c that demonstrates deletion of elements from an array.
#include <stdio.h>
int main() {
int arr[100] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int s = 10; // number of elements in the array
int pos;
return 0;
}
OUTPUT
int main()
{
int a[10000],i,s,search;
printf("Enter size of the array : ");
scanf("%d", &s);
printf("Enter elements in array : ");
for(i=0; i<s; i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element you want to search : ");
scanf("%d", &search);
OUTPUT
Enter size of the array : 5
Enter elements in array : 1
2
3
4
5
Enter the element you want to search : 5
element found
--------------------------------
Q.5 Write a program in c that creates and Traverses a linked list.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data; // Data
struct node *next; // Address
}*head;
int main()
{
int n;
crtList(n);
return 0;
}
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
//If memory is not allocated for newNode
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
OUTPUT
Enter the total number of nodes: 5
Enter the data of node 1: 2
Enter the data of node 2: 3
Enter the data of node 3: 4
Enter the data of node 4: 5
Enter the data of node 5: 6
Data in the list
Data = 2
Data = 3
Data = 4
Data = 5
Data = 6
Q.6 Write a program in c that inserts nodes in a linked list.
#include <stdio.h>
#include <stdlib.h>
int main() {
return 0;
}
OUTPUT
Linked list contents: 1 2 3
--------------------------------
Q.7 Write a program in c that deletes nodes from a linked list.
#include <stdio.h>
#include <stdlib.h>
// Function to insert a new node with the given data at the end of the list
void insert(Node** headRef, int data) {
Node* newNode = createNode(data);
if (*headRef == NULL) {
// If the list is empty, make the new node the head
*headRef = newNode;
} else {
Node* curr = *headRef;
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = newNode;
}
}
// Function to delete the first node with the given data from the list
void delete(Node** headRef, int data) {
Node* prev = NULL;
Node* curr = *headRef;
while (curr != NULL && curr->data != data) {
prev = curr;
curr = curr->next;
}
if (curr != NULL) {
if (prev == NULL) {
*headRef = curr->next;
} else {
prev->next = curr->next;
}
free(curr);
}
}
int main() {
// Initialize an empty linked list
Node* head = NULL;
return 0;
}
OUTPUT
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 3);
displayList(head);
return 0;
}
OUTPUT
Doubly Linked List: 1 2 3
Q.9 Write a program in c that inserts an element in using PUSH () in a stack.
#include <stdio.h>
#include <stdlib.h>
struct Stack {
int items[MAX_SIZE];
int top;
};
int main() {
struct Stack stack;
initializeStack(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
return 0;
}
OUTPUT
Element 10 pushed to the stack.
Element 20 pushed to the stack.
Element 30 pushed to the stack.
Q.10 Write a program in C that delete an element in using POP () in a stack .
#include <stdio.h>
#include <stdlib.h>
return data;
}
// Driver function
int main() {
// Create a stack
struct stack *top = NULL;
return 0;
}
OUTPUT
30
20
10
Popped element: 30
20
10
--------------------------------
Q.11 Write a program in c that converts infix to prefix expression using stack.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char stack[MAX_SIZE];
int top = -1;
char pop() {
char item;
if (top == -1) {
printf("Stack Underflow\n");
exit(1);
}
else {
item = stack[top];
top--;
return item;
}
}
push('(');
strcat(infix, ")");
i = 0;
item = infix[i];
while (item != '\0') {
if (item == '(') {
push(item);
}
else if (isdigit(item) || isalpha(item)) {
prefix[j] = item;
j++;
}
else if (is_operator(item) == 1) {
x = pop();
while (is_operator(x) == 1 && precedence(x) >= precedence(item)) {
prefix[j] = x;
j++;
x = pop();
}
push(x);
push(item);
}
else if (item == ')') {
x = pop();
while (x != '(') {
prefix[j] = x;
j++;
x = pop();
}
}
else {
printf("Invalid Expression\n");
exit(1);
}
i++;
item = infix[i];
}
if (top > 0) {
printf("Invalid Expression\n");
exit(1);
}
prefix[j] = '\0';
}
int main() {
char infix[MAX_SIZE], prefix[MAX_SIZE];
printf("Enter Infix Expression: ");
gets(infix);
infix_to_prefix(infix, prefix);
printf("Prefix Expression: ");
puts(prefix);
return 0;
}
OUTPUT
Enter Infix Expression: (a+b)*c-(d-e)*(f+g)
Prefix Expression: -+*abc*def+g
Q.12 Write a program in c that converts prefix to postfix expression using stacks.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// Stack implementation
typedef struct {
char items[MAX_STACK_SIZE];
int top;
} Stack;
int is_operator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}
int precedence(char c) {
switch(c) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
int main() {
char prefix[MAX_STACK_SIZE], postfix[MAX_STACK_SIZE];
printf("Enter the prefix expression: ");
scanf("%s", prefix);
prefix_to_postfix(prefix, postfix);
printf("Postfix expression: %s\n", postfix);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// Stack implementation
typedef struct {
char items[MAX_STACK_SIZE];
int top;
} Stack;
void push(Stack *s, char c) {
if (s->top == MAX_STACK_SIZE - 1) {
printf("Stack overflow!\n");
exit(EXIT_FAILURE);
}
s->items[++s->top] = c;
}
int is_operator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}
int precedence(char c) {
switch(c) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
int main() {
char prefix[MAX_STACK_SIZE], postfix[MAX_STACK_SIZE];
printf("Enter the prefix expression: ");
scanf("%s", prefix);
prefix_to_postfix(prefix, postfix);
printf("Postfix expression: %s\n", postfix);
return 0;
}
OUTPUT
Enter the prefix expression: *+12-34
Postfix expression: 12+34-*
Q.13 Write a program in c that calculates factorial of a number using recursion.
#include <stdio.h>
int factorial(int f) {
if (f == 0) {
return 1;
} else {
return f * factorial(f - 1);
}
}
int main() {
int f;
printf("Enter a number: ");
scanf("%d", &f);
printf("%d! = %d\n", f, factorial(f));
return 0;
}
OUTPUT
Enter a number: 5
5! = 120
--------------------------------
Q.14 Write a program in c that display the Fibonacci Series using recursion.
#include <stdio.h>
int fibonacci(int x) {
if (x <= 1) {
return x;
} else {
return fibonacci(x - 1) + fibonacci(x - 2);
}
}
int main() {
int x, i;
printf("Enter the number of terms in the Fibonacci series: ");
scanf("%d", &x);
printf("The Fibonacci series is:\n");
for (i = 0; i < x; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
return 0;
}
OUTPUT
Enter the number of terms in the Fibonacci series: 5
The Fibonacci series is:
0 1 1 2 3
--------------------------------
Q.15 Write a program in c that inserts an element in a Queue.
#include <stdio.h>
#define MAX_SIZE 100
int queue[MAX_SIZE];
int front = -1;
int rear = -1;
OUTPUT
Enter a value to insert into queue: 5
5 inserted into queue
--------------------------------
Q.16 Write a program in c that deletes an element from a Queue.
#include <stdio.h>
#define MAX_SIZE 100
int queue[MAX_SIZE];
int front = -1;
int rear = -1;
int dequeue() {
if (front == -1 || front > rear) {
printf("Queue underflow\n");
return -1;
} else {
int value = queue[front];
front++;
printf("%d deleted from queue\n", value);
return value;
}
}
int main() {
enqueue(1);
enqueue(2);
enqueue(3);
dequeue();
dequeue();
dequeue();
dequeue();
return 0;
}
OUTPUT
1 inserted into queue
2 inserted into queue
3 inserted into queue
1 deleted from queue
2 deleted from queue
3 deleted from queue
Queue underflow
--------------------------------