Dsa file[1]
Dsa file[1]
void main() {
int n;
printf("enter number of elements in array ");
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++){
printf("enter element %d ",i+1);
scanf("%d",&a[i]);
}
int min=a[0],max=a[0];
for( int i=0;i<n;i++){
if (a[i]>max){
max=a[i];
}
if (a[i]<min){
min=a[i];
}
}
printf("max=%d \nmin=%d \ndifference=%d",max,min,max-min);
}
codes:
b. Initializes an array with ten random integers and then prints four lines of output,
containing: Every element at an even index, Every odd element, All elements in reverse
order, Only the first and last element
void main(){
int a[10];
for(int i=0;i<10;i++){
printf("enter element %d ",i+1);
scanf("%d",&a[i]);
}
printf("elements at even index are ");
for(int i=0;i<10;i+=2){
printf("%d ",a[i]);
}
printf("%c",'\n');
printf("odd elements are ");
for(int i=0;i<10;i++){
if(a[i]%2!=0){
printf("%d ",a[i]);
}
}
printf("%c",'\n');
printf("all elemts in reverse order");
for(int i=9;i>=0;i--){
printf("%d ",a[i]);
}
printf("%c",'\n');
printf("first element= %d\tlast element=%d",a[0],a[9]);
}
codes:
output:
c. Consider an integer array of size 5 and display the following: Sum of all the elements,
Sum of alternate elements in the array, and second highest element in the array
void main(){
int a[5];
for(int i=0;i<5;i++){
printf("enter elemt %d ",i+1);
scanf("%d",&a[i]);
}
int max=a[0],smax=a[0];
int sum=0,altsum=0;
for(int i=0;i<5;i++){
sum+=a[i];
if(i%2==0){
altsum+=a[i];
}
}
printf("sum of all elements =%d",sum);
printf("%c",'\n');
printf("sum of alternative elements =%d",altsum);
for(int i=1;i<5;i++){
if(a[i]>max){
max=a[i];
}
}
for(int i=1;i<5;i++){
if(a[i]>smax &&a[i]<max){
smax=a[i];
}
}
printf("%c",'\n');
printf("second highest number =%d",smax);
}
code:
output:
2. Write a program to create a singly linked list of n nodes and perform:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void main() {
struct Node* head = NULL;
insertAtFirst(&head, 10);
printf("Linked list after inserting the node:10 at the beginning \n");
print(head);
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
struct node* prev;
};
void main() {
struct node *head = NULL;
printf("Linked list after inserting the node:5 at the beggining \n");
ins_beg(&head, 5);
trav(&head);
printf("Linked list after inserting the node:10 at the beggining \n");
ins_beg(&head, 10);
trav(&head);
output:
4. Write a program to create a singly circular and doubly linked list of n nodes and
perform:
a. Insertion at the beginning
b. Insertion at the end
c. Insertion at a specific
d. location
e. Deletion at the beginning
f. Deletion at the end
g. Deletion At a specific location
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
newNode->next = *head;
} else {
struct Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
newNode->next = *head;
temp->next = newNode;
*head = newNode;
}
}
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
newNode->next = *head;
} else {
struct Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = *head;
}
}
void insertAtPosition(struct Node** head, int data, int position) {
struct Node* newNode = createNode(data);
if (position < 1) {
printf("Position should be >= 1\n");
} else if (position == 1) {
insertAtBeginning(head, data);
} else {
struct Node* temp = *head;
for (int i = 1; i < position - 1 && temp->next != *head; i++) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
}
void deleteAtBeginning(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
struct Node* toDelete = *head;
if (temp->next == *head) {
*head = NULL;
free(temp);
} else {
while (temp->next != *head) {
temp = temp->next;
}
*head = (*head)->next;
temp->next = *head;
free(toDelete);
}
}
void deleteAtEnd(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
struct Node* toDelete;
if (temp->next == *head) {
*head = NULL;
free(temp);
} else {
while (temp->next->next != *head) {
temp = temp->next;
}
toDelete = temp->next;
temp->next = *head;
free(toDelete);
}
}
void deleteAtPosition(struct Node** head, int position) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
if (position == 1) {
deleteAtBeginning(head);
return;
}
struct Node* toDelete;
for (int i = 1; i < position - 1 && temp->next != *head; i++) {
temp = temp->next;
}
toDelete = temp->next;
temp->next = temp->next->next;
free(toDelete);
}
insertAtBeginning(&head, 5);
printf("List after inserting at the beginning:\n");
displayList(head);
insertAtEnd(&head, 200);
printf("List after inserting at the end:\n");
displayList(head);
insertAtPosition(&head, 25, 3);
printf("List after inserting at position 3:\n");
displayList(head);
deleteAtBeginning(&head);
printf("List after deleting at the beginning:\n");
displayList(head);
deleteAtEnd(&head);
printf("List after deleting at the end:\n");
displayList(head);
deleteAtPosition(&head, 2);
printf("List after deleting at position 2:\n");
displayList(head);}
codes:
output:
DOUBLE CIRCULAR
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = newNode;
newNode->prev = newNode;
return newNode;
}
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* last = (*head)->prev;
newNode->next = *head;
newNode->prev = last;
last->next = newNode;
(*head)->prev = newNode;
*head = newNode;
}
}
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* last = (*head)->prev;
newNode->next = *head;
newNode->prev = last;
last->next = newNode;
(*head)->prev = newNode;
}
}
void insertAtLocation(struct Node** head, int data, int position) {
if (position == 0) {
insertAtBeginning(head, data);
return;
}
struct Node* newNode = createNode(data);
struct Node* temp = *head;
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
if (temp == *head) {
*head = temp->next;
}
free(temp);
}
void printList(struct Node* head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
deleteFromBeginning(&head);
printList(head);
deleteFromEnd(&head);
printList(head);
deleteFromLocation(&head, 1);
printList(head);
}
codes:
output:
5. Write a program to implement stack using arrays and linked lists.
Using Array
#include <stdio.h>
#include <stdlib.h>
#define size 10
int stack[size];
int TOS = -1;
int main() {
int conti = 1;
while(conti) {
int choice;
printf("1: Push\n");
printf("2: Pop\n");
printf("3: Peek\n");
printf("4: Display\n");
printf("0: Exit\n");
printf("Enter Your Choice : ");
scanf("%d", &choice);
int data;
switch (choice) {
case 1:
printf("Enter the value you want to add : ");
scanf("%d", &data);
push(data);
break;
case 2:
data = pop();
printf("Popped element is : %d \n", data);
break;
case 3:
data = peek();
printf("Element at the top is : %d\n", data);
break;
case 4:
display();
break;
case 0:
printf("Thank you for using stack operator menu");
exit(0);
break;
default:
continue;
}
}
}
int peek() {
if(TOS == -1) {
printf("Stack Underflow\n");
exit(0);
}
return stack[TOS];
}
void display() {
if(TOS == -1) {
printf("Stack Underflow\n");
exit(0);
}
for(int i = TOS; i >= 0; i--) {
printf("%d, ", stack[i]);
}
printf("\n");
}
codes:
output:
Using Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
int conti = 1;
while(conti) {
int choice;
printf("1: Push\n");
printf("2: Pop\n");
printf("3: Peek\n");
printf("4: Display\n");
printf("0: Exit\n");
printf("Enter Your Choice : ");
scanf("%d", &choice);
int data;
switch (choice) {
case 1:
printf("Enter the value you want to add : ");
scanf("%d", &data);
push(data);
break;
case 2:
data = pop();
if(data != -1)
printf("Popped element is : %d \n", data);
break;
case 3:
data = peek();
if(data != -1)
printf("Element at the top is : %d\n", data);
break;
case 4:
display();
break;
case 0:
exit(0);
break;
default:
continue;
}
}
}
int pop() {
if(top == NULL) {
printf("Stack Underflow\n");
return -1;
}
struct Node* temp = top;
int data = top->data;
top = top->next;
free(temp);
return data;
}
int peek() {
if(top == NULL) {
printf("Stack Underflow\n");
return -1;
}
return top->data;
}
void display() {
if(top == NULL) {
printf("Stack Underflow\n");
return;
}
struct Node* temp = top;
while(temp != NULL) {
printf("%d, ", temp->data);
temp = temp->next;
}
printf("\n");
}
codes:
output:
6.Write a program to reverse a sentence/string using stack.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
struct Node {
char data;
struct Node* next;
};
int main() {
char str[MAX];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0';
char pop() {
if (isEmpty()) {
printf("Stack Underflow\n");
return '\0';
}
struct Node* temp = top;
char data = top->data;
top = top->next;
free(temp);
return data;
}
int isEmpty() {
return top == NULL;
}
codes:
output:
7. Write a program to check for balanced parenthesis in a given expression.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Stack structure
typedef struct {
char arr[MAX];
int top;
} Stack;
// Stack operations
void push(Stack* stack, char element) {
if (stack->top == MAX - 1) {
printf("Stack overflow\n");
exit(1);
}
stack->arr[++stack->top] = element;
}
int main() {
char expression[MAX];
if (isBalanced(expression)) {
printf("The parentheses are balanced.\n");
} else {
printf("The parentheses are not balanced.\n");
}
return 0;
}
codes:
output:
8. Write a program to convert infix expression to prefix and postfix
expression.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// Function prototypes
void push(Stack* stack, char element);
char pop(Stack* stack);
char peek(Stack* stack);
int isEmpty(Stack* stack);
int precedence(char operator);
int isOperator(char ch);
void reverseString(char* str);
void infixToPostfix(char* infix, char* postfix);
void infixToPrefix(char* infix, char* prefix);
int main() {
char infix[MAX], postfix[MAX], prefix[MAX];
printf("Enter an infix expression: ");
scanf("%s", infix);
infixToPostfix(infix, postfix);
printf("Postfix Expression: %s\n", postfix);
infixToPrefix(infix, prefix);
printf("Prefix Expression: %s\n", prefix);
return 0;
}
// Reverse a string
void reverseString(char* str) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
while (infix[i]) {
if (isalnum(infix[i])) {
postfix[j++] = infix[i];
} else if (infix[i] == '(') {
push(&stack, infix[i]);
} else if (infix[i] == ')') {
while (!isEmpty(&stack) && peek(&stack) != '(') {
postfix[j++] = pop(&stack);
}
pop(&stack); // Remove '('
} else if (isOperator(infix[i])) {
while (!isEmpty(&stack) && precedence(peek(&stack)) >=
precedence(infix[i])) {
postfix[j++] = pop(&stack);
}
push(&stack, infix[i]);
}
i++;
}
while (!isEmpty(&stack)) {
postfix[j++] = pop(&stack);
}
postfix[j] = '\0';
}
// Reverse infix expression and replace '(' with ')' and vice versa
for (int i = 0; i < len; i++) {
if (infix[len - i - 1] == '(') {
reversedInfix[i] = ')';
} else if (infix[len - i - 1] == ')') {
reversedInfix[i] = '(';
} else {
reversedInfix[i] = infix[len - i - 1];
}
}
reversedInfix[len] = '\0';
struct Queue {
int arr[MAX];
int front;
int rear;
};
int main() {
struct Queue queue;
initQueue(&queue);
int n, data;
printf("Enter the number of elements to enqueue: ");
scanf("%d", &n);
display(&queue);
printf("Front element: %d\n", front(&queue));
printf("Dequeued: %d\n", dequeue(&queue));
display(&queue);
return 0;
}
codes:
output:
Using Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Queue {
struct Node* front;
struct Node* rear;
};
if (isEmpty(queue)) {
queue->front = queue->rear = newNode;
} else {
queue->rear->next = newNode;
queue->rear = newNode;
}
}
int main() {
struct Queue queue;
initQueue(&queue);
int n, data;
printf("Enter the number of elements to enqueue: ");
scanf("%d", &n);
display(&queue);
printf("Front element: %d\n", front(&queue));
printf("Dequeued: %d\n", dequeue(&queue));
display(&queue);
return 0;
}
codes:
output:
10. Write a program to implement Circular Queue using Array and Linked Lists.
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
typedef struct {
int arr[MAX];
int front, rear;
} CircularQueue;
// Enqueue operation
void enqueue(CircularQueue* cq, int value) {
if (isFull(cq)) {
printf("Queue is full!\n");
return;
}
if (isEmpty(cq)) {
cq->front = cq->rear = 0;
} else {
cq->rear = (cq->rear + 1) % MAX;
}
cq->arr[cq->rear] = value;
printf("Inserted %d\n", value);
}
// Dequeue operation
int dequeue(CircularQueue* cq) {
if (isEmpty(cq)) {
printf("Queue is empty!\n");
return -1;
}
int value = cq->arr[cq->front];
if (cq->front == cq->rear) {
cq->front = cq->rear = -1; // Queue is now empty
} else {
cq->front = (cq->front + 1) % MAX;
}
return value;
}
int main() {
CircularQueue cq;
initQueue(&cq);
enqueue(&cq, 10);
enqueue(&cq, 20);
enqueue(&cq, 30);
display(&cq);
enqueue(&cq, 40);
display(&cq);
return 0;
}
codes:
output:
Circular Queue using linked list
#include <stdio.h>
#include <stdlib.h>
// Node structure
int data;
} Node;
typedef struct {
Node* front;
Node* rear;
} CircularQueue;
cq->front = NULL;
cq->rear = NULL;
}
// Check if the queue is empty
// Enqueue operation
if (!newNode) {
return;
newNode->data = value;
if (isEmpty(cq)) {
} else {
newNode->next = cq->front;
cq->rear->next = newNode;
cq->rear = newNode;
}
// Dequeue operation
if (isEmpty(cq)) {
printf("Queue is empty!\n");
return -1;
if (cq->front == cq->rear) {
} else {
cq->front = cq->front->next;
cq->rear->next = cq->front;
free(temp);
return value;
if (isEmpty(cq)) {
printf("Queue is empty!\n");
return;
do {
current = current->next;
printf("\n");
int main() {
CircularQueue cq;
initQueue(&cq);
enqueue(&cq, 10);
enqueue(&cq, 20);
enqueue(&cq, 30);
display(&cq);
display(&cq);
enqueue(&cq, 40);
display(&cq);
return 0;
}
codes:
output:
11.. Write a program to implement Doubly Ended Queue using Array and linked list
Array
#include <stdio.h>
#define MAX 5 // Maximum size of the deque
do {
printf("\n--- Double-Ended Queue Operations ---\n");
printf("1. Insert Front\n");
printf("2. Insert Rear\n");
printf("3. Delete Front\n");
printf("4. Delete Rear\n");
printf("5. Get Front\n");
printf("6. Get Rear\n");
printf("7. Display\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at front: ");
scanf("%d", &value);
insertFront(value);
break;
case 2:
printf("Enter value to insert at rear: ");
scanf("%d", &value);
insertRear(value);
break;
case 3:
value = deleteFront();
if (value != -1) {
printf("Deleted element from front: %d\n", value);
}
break;
case 4:
value = deleteRear();
if (value != -1) {
printf("Deleted element from rear: %d\n", value);
}
break;
case 5:
value = getFront();
if (value != -1) {
printf("Front element: %d\n", value);
}
break;
case 6:
value = getRear();
if (value != -1) {
printf("Rear element: %d\n", value);
}
break;
case 7:
display();
break;
case 8:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 8);
return 0;
}
code:
output:
Linked Lists.
#include <stdio.h>
#include <stdlib.h>
if (isEmpty(dq)) {
dq->front = dq->rear = newNode;
} else {
dq->front->prev = newNode;
dq->front = newNode;
}
printf("Inserted %d at the front.\n", value);
}
if (isEmpty(dq)) {
dq->front = dq->rear = newNode;
} else {
dq->rear->next = newNode;
dq->rear = newNode;
}
printf("Inserted %d at the rear.\n", value);
}
// Delete an element from the front
void deleteFront(Deque* dq) {
if (isEmpty(dq)) {
printf("Deque is empty!\n");
return;
}
Node* temp = dq->front;
printf("Deleted %d from the front.\n", temp->data);
dq->front = dq->front->next;
if (dq->front) {
dq->front->prev = NULL;
} else {
dq->rear = NULL;
}
free(temp);
}
dq->rear = dq->rear->prev;
if (dq->rear) {
dq->rear->next = NULL;
} else {
dq->front = NULL;
}
free(temp);
}
do {
printf("\nMenu:\n");
printf("1. Insert at Front\n");
printf("2. Insert at Rear\n");
printf("3. Delete from Front\n");
printf("4. Delete from Rear\n");
printf("5. Display\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at front: ");
scanf("%d", &value);
insertFront(&dq, value);
break;
case 2:
printf("Enter value to insert at rear: ");
scanf("%d", &value);
insertRear(&dq, value);
break;
case 3:
deleteFront(&dq);
break;
case 4:
deleteRear(&dq);
break;
case 5:
display(&dq);
break;
case 6:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 6);
return 0;
codes:
outputs: