Q.1 Write a program in c that demonstrates traversing through an array.
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5}; //
int size = sizeof(arr) / sizeof(int);
printf("Array elements Are:\n");
// Traverse through the array and print each element
for (int i = 0; i < size; i++) {
printf("%d\n", arr[i]);
}
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;
printf("Enter the position where you want to insert an element: ");
scanf("%d", &pos);
if (pos < 1 || pos > s + 1) {
printf("Invalid position!\n");
} else {
printf("Enter the value to insert: ");
scanf("%d", &val);
// shift elements to make room for the new element
for (int i = s - 1; i >= pos - 1; i--) {
arr[i + 1] = arr[i];
}
// insert the new element at the specified position
arr[pos - 1] = val;
s++;
// print the updated array
printf("Array after insertion:\n");
for (int i = 0; i < s; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
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;
printf("Enter the position of the element to be deleted: ");
scanf("%d", &pos);
// shift elements to the left starting from the element to be deleted
for (int i = pos-1; i < s-1; i++) {
arr[i] = arr[i+1];
}
// update the number of elements in the array
s--;
// print the updated array
printf("Array after deletion: ");
for (int i = 0; i < s; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
OUTPUT
Enter the position of the element to be deleted: 8
Array after deletion: 1 2 3 4 5 6 7 9 10
Q.4 Write a program in c that performs Search operation in an array.
#include <stdio.h>
#include <conio.h>
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);
for(i=0; i<s; i++)
{
if(a[i]==search)
{
printf("element found ");
return 0;
}
}
printf("element not found");
}
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;
// Functions to create and display list
void crtList(int n);
void travList();
int main()
{
int n;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
crtList(n);
printf("\nData in the list \n");
travList();
return 0;
}
// Create a list of n nodes
void crtList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
// Terminate if memory not allocated
if(head == NULL)
{
printf("Unable to allocate memory.");
exit(0);
}
// Input data of node from the user
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data; // Link data field with data
head->next = NULL; // Link address field to NULL
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);
newNode->data = data; // Link data field of newNode
newNode->next = NULL; // Make sure new node points to NULL
temp->next = newNode; // Link previous node with newNode
temp = temp->next; // Make current node as previous node
}
}
// Display entire list
void travList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
return;
}
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data); // Print data of current node
temp = temp->next; // Move to next node
}
}
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>
typedef struct Node {
int data;
struct Node* next;
} Node;
// Function to create a new node with the given data
Node* createNode(int data) {
Node* newNode = (Node*) malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// 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) {
*headRef = newNode;
} else {
Node* curr = *headRef;
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = newNode;
}
}
// Function to print out the contents of the linked list
void printList(Node* head) {
Node* curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
int main() {
Node* head = NULL;
// Insert some nodes into the linked list
insert(&head, 1);
insert(&head, 2);
insert(&head, 3);
printf("Linked list contents: ");
printList(head);
Node* curr = head;
while (curr != NULL) {
Node* next = curr->next;
free(curr);
curr = next;
}
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>
typedef struct Node {
int data;
struct Node* next;
} Node;
// Function to create a new node with the given data
Node* createNode(int data) {
Node* newNode = (Node*) malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// 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);
}
}
// Function to print out the contents of the linked list
void printList(Node* head) {
Node* curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
int main() {
// Initialize an empty linked list
Node* head = NULL;
// Insert some nodes into the linked list
insert(&head, 1);
insert(&head, 2);
insert(&head, 3);
printf("Linked list contents: ");
printList(head);
printf("Deleting node with data value 2...\n");
delete(&head, 2);
printf("Linked list contents after deletion: ");
printList(head);
Node* curr = head;
while (curr != NULL) {
Node* next = curr->next;
free(curr);
curr = next;
}
return 0;
}
OUTPUT
Linked list contents: 1 2 3
Deleting node with data value 2...
Linked list contents after deletion: 1 3
Q.8 Write a program in c that creates and displayed a double linked list.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
}
void displayList(struct Node* head) {
struct Node* current = head;
printf("Doubly Linked List: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
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>
#define MAX_SIZE 100
struct Stack {
int items[MAX_SIZE];
int top;
};
void initializeStack(struct Stack* stack) {
stack->top = -1;
}
int isFull(struct Stack* stack) {
return stack->top == MAX_SIZE - 1;
}
int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
void push(struct Stack* stack, int value) {
if (isFull(stack)) {
printf("Stack is full. Cannot push element.\n");
} else {
stack->top++;
stack->items[stack->top] = value;
printf("Element %d pushed to the stack.\n", value);
}
}
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>
// Define the stack structure
struct stack {
int data;
struct stack *next;
};
// Function to push an element to the stack
void push(struct stack **top, int data) {
struct stack *new_node = (struct stack *)malloc(sizeof(struct stack));
new_node->data = data;
new_node->next = *top;
*top = new_node;
}
// Function to pop an element from the stack
int pop(struct stack **top) {
if (*top == NULL) {
printf("Stack is empty\n");
return -1;
}
int data = (*top)->data;
struct stack *temp = *top;
*top = (*top)->next;
free(temp);
return data;
}
// Driver function
int main() {
// Create a stack
struct stack *top = NULL;
// Push some elements to the stack
push(&top, 10);
push(&top, 20);
push(&top, 30);
// Print the stack
struct stack *current = top;
while (current != NULL) {
printf("%d\n", current->data);
current = current->next;
}
// Pop an element from the stack
int data = pop(&top);
printf("Popped element: %d\n", data);
// Print the stack again
current = top;
while (current != NULL) {
printf("%d\n", current->data);
current = current->next;
}
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>
#define MAX_SIZE 100
char stack[MAX_SIZE];
int top = -1;
void push(char item) {
if (top >= MAX_SIZE - 1) {
printf("Stack Overflow\n");
}
else {
top++;
stack[top] = item;
}
}
char pop() {
char item;
if (top == -1) {
printf("Stack Underflow\n");
exit(1);
}
else {
item = stack[top];
top--;
return item;
}
}
int is_operator(char symbol) {
if (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/' ||
symbol == '^') {
return 1;
}
else {
return 0;
}
}
int precedence(char symbol) {
if (symbol == '^') {
return 3;
}
else if (symbol == '*' || symbol == '/') {
return 2;
}
else if (symbol == '+' || symbol == '-') {
return 1;
}
else {
return 0;
}
}
void infix_to_prefix(char infix[], char prefix[]) {
int i, j = 0;
char item, x;
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>
#define MAX_STACK_SIZE 100
// 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;
}
char pop(Stack *s) {
if (s->top == -1) {
printf("Stack underflow!\n");
exit(EXIT_FAILURE);
}
return s->items[s->top--];
}
char peek(Stack *s) {
if (s->top == -1) {
printf("Stack underflow!\n");
exit(EXIT_FAILURE);
}
return s->items[s->top];
}
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;
}
}
void prefix_to_postfix(char prefix[], char postfix[]) {
int i, j;
char c;
Stack s;
s.top = -1;
for (i = strlen(prefix) - 1, j = 0; i >= 0; i--) {
c = prefix[i];
if (isdigit(c)) {
postfix[j++] = c;
} else if (is_operator(c)) {
while (!is_operator(peek(&s)) && s.top != -1 &&
precedence(peek(&s)) >= precedence(c)) {
postfix[j++] = pop(&s);
}
push(&s, c);
} else if (c == ')') {
push(&s, c);
} else if (c == '(') {
while (peek(&s) != ')') {
postfix[j++] = pop(&s);
}
pop(&s);
}
}
while (s.top != -1) {
postfix[j++] = pop(&s);
}
postfix[j] = '\0';
strrev(postfix);
}
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>
#define MAX_STACK_SIZE 100
// 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;
}
char pop(Stack *s) {
if (s->top == -1) {
printf("Stack underflow!\n");
exit(EXIT_FAILURE);
}
return s->items[s->top--];
}
char peek(Stack *s) {
if (s->top == -1) {
printf("Stack underflow!\n");
exit(EXIT_FAILURE);
}
return s->items[s->top];
}
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;
}
}
void prefix_to_postfix(char prefix[], char postfix[]) {
int i, j;
char c;
Stack s;
s.top = -1;
for (i = strlen(prefix) - 1, j = 0; i >= 0; i--) {
c = prefix[i];
if (isdigit(c)) {
postfix[j++] = c;
} else if (is_operator(c)) {
while (!is_operator(peek(&s)) && s.top != -1 &&
precedence(peek(&s)) >= precedence(c)) {
postfix[j++] = pop(&s);
}
push(&s, c);
} else if (c == ')') {
push(&s, c);
} else if (c == '(') {
while (peek(&s) != ')') {
postfix[j++] = pop(&s);
}
pop(&s);
}
}
while (s.top != -1) {
postfix[j++] = pop(&s);
}
postfix[j] = '\0';
strrev(postfix);
}
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;
void enqueue(int value) {
if (rear == MAX_SIZE - 1) {
printf("Queue overflow\n");
return;
} else {
if (front == -1) {
front = 0;
}
rear++;
queue[rear] = value;
printf("%d inserted into queue\n", value);
}
}
int main() {
int value;
printf("Enter a value to insert into queue: ");
scanf("%d", &value);
enqueue(value);
return 0;
}
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;
void enqueue(int value) {
if (rear == MAX_SIZE - 1) {
printf("Queue overflow\n");
return;
} else {
if (front == -1) {
front = 0;
}
rear++;
queue[rear] = value;
printf("%d inserted into queue\n", value);
}
}
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
--------------------------------