Execute the push and pop operations performed on a stack of size 5 using C program.
1.
#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 5
int stack[MAX_SIZE];
int top = -1;
bool isFull() {
return top == MAX_SIZE - 1;
}
bool isEmpty() {
return top == -1;
}
void push(int item) {
if (isFull()) {
printf("Stack overflow! Cannot push %d\n", item);
}
else {
top++;
stack[top] = item;
printf("Pushed %d onto the stack\n", item);
}
}
void pop() {
if (isEmpty()) {
printf("Stack underflow! Cannot pop from an empty stack\n");
} else {
printf("Popped %d from the stack\n", stack[top]);
top--;
}
}
void display() {
if (isEmpty()) {
printf("Stack is empty\n");
}
else {
printf("Stack elements: ");
for (int i = 0; i <= top; i++) {
printf("%d ", stack[i]);
}
printf("\n");
}
}
int main() {
push(1);
push(2);
push(3);
push(4);
push(5);
display();
pop();
pop();
display();
push(6);
push(7);
display();
return 0;
}
Implement First In First Out order of queue ADT using arrays in C.
2.
#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 5
int queue[MAX_SIZE];
int front = -1, rear = -1;
bool isFull() {
return rear == MAX_SIZE - 1;
}
bool isEmpty() {
return front == -1;
}
void enqueue(int item) {
if (isFull()) {
printf("Queue overflow! Cannot enqueue %d\n", item);
}
else {
if (isEmpty()) {
front = 0;
}
rear++;
queue[rear] = item;
printf("Enqueued %d\n", item);
}
}
void dequeue() {
if (isEmpty()) {
printf("Queue underflow! Cannot dequeue from an empty queue\n");
}
else {
printf("Dequeued %d\n", queue[front]);
if (front == rear) {
front = rear = -1;
}
else {
front++;
}
}
}
void display() {
if (isEmpty()) {
printf("Queue is empty\n");
}
else {
printf("Queue elements: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}
int main() {
enqueue(1);
enqueue(2);
enqueue(3);
display();
dequeue();
dequeue();
display();
enqueue(4);
enqueue(5);
display();
return 0;
}
3. Write a C program to implement a sequential data structure - list ADT using array.
#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 10
struct List {
int arr[MAX_SIZE];
int size;
};
void initList(struct List *list) {
list->size = 0;
}
bool isFull(struct List *list) {
return list->size == MAX_SIZE;
}
bool isEmpty(struct List *list) {
return list->size == 0;
}
void insertAtEnd(struct List *list, int item) {
if (isFull(list)) {
printf("List is full! Cannot insert %d\n", item);
}
else {
list->arr[list->size] = item;
list->size++;
printf("Inserted %d at the end of the list\n", item);
}
}
void removeFromEnd(struct List *list) {
if (isEmpty(list)) {
printf("List is empty! Cannot remove from an empty list\n");
}
else {
printf("Removed %d from the end of the list\n", list->arr[list->size - 1]);
list->size--;
}
}
void display(struct List *list) {
if (isEmpty(list)) {
printf("List is empty\n");
}
else {
printf("List elements: ");
for (int i = 0; i < list->size; i++) {
printf("%d ", list->arr[i]);
}
printf("\n");
}
}
int main() {
struct List myList;
initList(&myList);
insertAtEnd(&myList, 1);
insertAtEnd(&myList, 2);
insertAtEnd(&myList, 3);
display(&myList);
removeFromEnd(&myList);
display(&myList);
insertAtEnd(&myList, 4);
insertAtEnd(&myList, 5);
display(&myList);
return 0;
}
Write a C program to implement stack ADT using linked list implementation
4.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Stack {
struct Node* top;
};
void initializeStack(struct Stack* stack) {
stack->top = NULL;
}
int isEmpty(struct Stack* stack) {
return (stack->top == NULL);
}
void push(struct Stack* stack, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed. Cannot push %d onto the stack\n", data);
return;
}
newNode->data = data;
newNode->next = stack->top;
stack->top = newNode;
printf("Pushed %d onto the stack\n", data);
}
void pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack underflow! Cannot pop from an empty stack\n");
return;
}
struct Node* temp = stack->top;
stack->top = stack->top->next;
printf("Popped %d from the stack\n", temp->data);
free(temp);
}
void display(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return;
}
printf("Stack elements: ");
struct Node* current = stack->top;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
void destroyStack(struct Stack* stack) {
while (!isEmpty(stack)) {
pop(stack);
}
}
int main() {
struct Stack myStack;
initializeStack(&myStack);
push(&myStack, 1);
push(&myStack, 2);
push(&myStack, 3);
display(&myStack);
pop(&myStack);
display(&myStack);
push(&myStack, 4);
push(&myStack, 5);
display(&myStack);
destroyStack(&myStack);
return 0;
}
Write a C program to implement queue ADT using linked list implementation
5.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Queue {
struct Node* front;
struct Node* rear;
};
void initializeQueue(struct Queue* queue) {
queue->front = NULL;
queue->rear = NULL;
}
int isEmpty(struct Queue* queue) {
return (queue->front == NULL);
}
void enqueue(struct Queue* queue, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed. Cannot enqueue %d\n", data);
return;
}
newNode->data = data;
newNode->next = NULL;
if (isEmpty(queue)) {
queue->front = newNode;
queue->rear = newNode;
}
else {
queue->rear->next = newNode;
queue->rear = newNode;
}
printf("Enqueued %d\n", data);
}
void dequeue(struct Queue* queue) {
if (isEmpty(queue)) {
printf("Queue underflow! Cannot dequeue from an empty queue\n");
return;
}
struct Node* temp = queue->front;
queue->front = queue->front->next;
if (queue->front == NULL) {
queue->rear = NULL;
}
printf("Dequeued %d\n", temp->data);
free(temp);
}
void display(struct Queue* queue) {
if (isEmpty(queue)) {
printf("Queue is empty\n");
return;
}
printf("Queue elements: ");
struct Node* current = queue->front;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
void destroyQueue(struct Queue* queue) {
while (!isEmpty(queue)) {
dequeue(queue);
}
}
int main() {
struct Queue myQueue;
initializeQueue(&myQueue);
enqueue(&myQueue, 1);
enqueue(&myQueue, 2);
enqueue(&myQueue, 3);
display(&myQueue);
dequeue(&myQueue);
display(&myQueue);
enqueue(&myQueue, 4);
enqueue(&myQueue, 5);
display(&myQueue);
destroyQueue(&myQueue);
return 0;
}
6. Insert elements in a node with pointers and delete few elements using list ADT in C program.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct List {
struct Node* head;
};
void initializeList(struct List* list) {
list->head = NULL;
}
void insertAtEnd(struct List* list, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed. Cannot insert %d into the list\n", data);
return;
}
newNode->data = data;
newNode->next = NULL;
if (list->head == NULL) {
list->head = newNode;
}
else {
struct Node* current = list->head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
printf("Inserted %d at the end of the list\n", data);
}
void deleteElement(struct List* list, int target) {
if (list->head == NULL) {
printf("List is empty. Cannot delete %d\n", target);
return;
}
if (list->head->data == target) {
struct Node* temp = list->head;
list->head = list->head->next;
printf("Deleted %d from the list\n", target);
free(temp);
return;
}
struct Node* current = list->head;
struct Node* prev = NULL;
while (current != NULL && current->data != target) {
prev = current;
current = current->next;
}
if (current != NULL) {
prev->next = current->next;
printf("Deleted %d from the list\n", target);
free(current);
}
else {
printf("%d not found in the list. Cannot delete\n", target);
}
}
void display(struct List* list) {
if (list->head == NULL) {
printf("List is empty\n");
return;
}
printf("List elements: ");
struct Node* current = list->head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
void destroyList(struct List* list) {
while (list->head != NULL) {
struct Node* temp = list->head;
list->head = list->head->next;
free(temp);
}
}
int main() {
struct List myList;
initializeList(&myList);
insertAtEnd(&myList, 1);
insertAtEnd(&myList, 2);
insertAtEnd(&myList, 3);
insertAtEnd(&myList, 4);
insertAtEnd(&myList, 5);
display(&myList);
deleteElement(&myList, 3);
deleteElement(&myList, 6);
display(&myList);
destroyList(&myList);
return 0;
}
How will you apply stack in converting Infix to postfix using C? Write a program to implement the
7.
same.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_SIZE 100
struct Stack {
char items[MAX_SIZE];
int top;
};
void initializeStack(struct Stack* stack) {
stack->top = -1;
}
bool isEmpty(struct Stack* stack) {
return stack->top == -1;
}
int precedence(char op) {
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return 0;
}
}
void push(struct Stack* stack, char item) {
if (stack->top == MAX_SIZE - 1) {
printf("Stack overflow! Cannot push %c\n", item);
exit(EXIT_FAILURE);
}
stack->items[++stack->top] = item;
}
char pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack underflow! Cannot pop from an empty stack\n");
exit(EXIT_FAILURE);
}
return stack->items[stack->top--];
}
char peek(struct Stack* stack) {
if (isEmpty(stack)) {
return '\0';
}
return stack->items[stack->top];
}
void infixToPostfix(char infix[], char postfix[]) {
struct Stack operatorStack;
initializeStack(&operatorStack);
int i = 0;
int j = 0;
while (infix[i] != '\0') {
char currentSymbol = infix[i];
if (isalnum(currentSymbol)) {
postfix[j++] = currentSymbol;
}
else if (currentSymbol == '(') {
push(&operatorStack, currentSymbol);
}
else if (currentSymbol == ')') {
while (peek(&operatorStack) != '(') {
postfix[j++] = pop(&operatorStack);
}
pop(&operatorStack);
}
else {
while (!isEmpty(&operatorStack) && precedence(currentSymbol) <=
precedence(peek(&operatorStack))) {
postfix[j++] = pop(&operatorStack);
}
push(&operatorStack, currentSymbol);
}
i++;
}
while (!isEmpty(&operatorStack)) {
postfix[j++] = pop(&operatorStack);
}
postfix[j] = '\0';
}
int main() {
char infixExpression[MAX_SIZE], postfixExpression[MAX_SIZE];
printf("Enter an infix expression: ");
scanf("%s", infixExpression);
infixToPostfix(infixExpression, postfixExpression);
printf("Postfix expression: %s\n", postfixExpression);
return 0;
}
8. Extract the coefficient of a given exponent and write a program to perform addition using lists in C.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int coefficient;
int exponent;
struct Node* next;
};
struct Polynomial {
struct Node* head;
};
void initializePolynomial(struct Polynomial* poly) {
poly->head = NULL;
}
void insertTerm(struct Polynomial* poly, int coefficient, int exponent) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed. Cannot insert term\n");
exit(EXIT_FAILURE);
}
newNode->coefficient = coefficient;
newNode->exponent = exponent;
newNode->next = NULL;
if (poly->head == NULL) {
poly->head = newNode;
}
else {
struct Node* current = poly->head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
int getCoefficient(struct Polynomial* poly, int exponent) {
struct Node* current = poly->head;
while (current != NULL) {
if (current->exponent == exponent) {
return current->coefficient;
}
current = current->next;
}
return 0
}
void addPolynomials(struct Polynomial* poly1, struct Polynomial* poly2, struct
Polynomial* result) {
struct Node* current1 = poly1->head;
struct Node* current2 = poly2->head;
while (current1 != NULL || current2 != NULL) {
int coefficient1 = (current1 != NULL) ? current1->coefficient : 0;
int exponent1 = (current1 != NULL) ? current1->exponent : -1;
int coefficient2 = (current2 != NULL) ? current2->coefficient : 0;
int exponent2 = (current2 != NULL) ? current2->exponent : -1;
if (exponent1 == exponent2) {
insertTerm(result, coefficient1 + coefficient2, exponent1);
current1 = current1->next;
current2 = current2->next;
}
else if (exponent1 > exponent2) {
insertTerm(result, coefficient1, exponent1);
current1 = current1->next;
}
else {
insertTerm(result, coefficient2, exponent2);
current2 = current2->next;
}
}
}
void displayPolynomial(struct Polynomial* poly) {
if (poly->head == NULL) {
printf("Polynomial is empty\n");
return;
}
struct Node* current = poly->head;
while (current != NULL) {
printf("%dx^%d", current->coefficient, current->exponent);
if (current->next != NULL) {
printf(" + ");
}
current = current->next;
}
printf("\n");
}
void destroyPolynomial(struct Polynomial* poly) {
while (poly->head != NULL) {
struct Node* temp = poly->head;
poly->head = poly->head->next;
free(temp);
}
}
int main() {
struct Polynomial poly1, poly2, result;
initializePolynomial(&poly1);
initializePolynomial(&poly2);
initializePolynomial(&result);
insertTerm(&poly1, 3, 2);
insertTerm(&poly1, 5, 1);
insertTerm(&poly1, 2, 0);
insertTerm(&poly2, 4, 3);
insertTerm(&poly2, 1, 1);
insertTerm(&poly2, 7, 0);
printf("Polynomial 1: ");
displayPolynomial(&poly1);
printf("Polynomial 2: ");
displayPolynomial(&poly2);
addPolynomials(&poly1, &poly2, &result);
printf("Sum of Polynomials: ");
displayPolynomial(&result);
int exponent = 2;
int coefficient = getCoefficient(&result, exponent);
printf("Coefficient of x^%d: %d\n", exponent, coefficient);
destroyPolynomial(&poly1);
destroyPolynomial(&poly2);
destroyPolynomial(&result);
return 0;
}
9. Write a C program to implement an application of list ADT - Polynomial subtraction.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int coefficient;
int exponent;
struct Node* next;
};
struct Polynomial {
struct Node* head;
};
void initializePolynomial(struct Polynomial* poly) {
poly->head = NULL;
}
void insertTerm(struct Polynomial* poly, int coefficient, int exponent) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed. Cannot insert term\n");
exit(EXIT_FAILURE);
}
newNode->coefficient = coefficient;
newNode->exponent = exponent;
newNode->next = NULL;
if (poly->head == NULL) {
poly->head = newNode;
}
else {
struct Node* current = poly->head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
void subtractPolynomials(struct Polynomial* poly1, struct Polynomial* poly2, struct
Polynomial* result) {
struct Node* current1 = poly1->head;
struct Node* current2 = poly2->head;
while (current1 != NULL || current2 != NULL) {
int coefficient1 = (current1 != NULL) ? current1->coefficient : 0;
int exponent1 = (current1 != NULL) ? current1->exponent : -1;
int coefficient2 = (current2 != NULL) ? current2->coefficient : 0;
int exponent2 = (current2 != NULL) ? current2->exponent : -1;
if (exponent1 == exponent2) {
insertTerm(result, coefficient1 - coefficient2, exponent1);
current1 = current1->next;
current2 = current2->next;
}
else if (exponent1 > exponent2) {
insertTerm(result, coefficient1, exponent1);
current1 = current1->next;
}
else {
insertTerm(result, -coefficient2, exponent2);
current2 = current2->next;
}
}
}
void displayPolynomial(struct Polynomial* poly) {
if (poly->head == NULL) {
printf("Polynomial is empty\n");
return;
}
struct Node* current = poly->head;
while (current != NULL) {
printf("%dx^%d", current->coefficient, current->exponent);
if (current->next != NULL) {
printf(" + ");
}
current = current->next;
}
printf("\n");
}
void destroyPolynomial(struct Polynomial* poly) {
while (poly->head != NULL) {
struct Node* temp = poly->head;
poly->head = poly->head->next;
free(temp);
}
}
int main() {
struct Polynomial poly1, poly2, result;
initializePolynomial(&poly1);
initializePolynomial(&poly2);
initializePolynomial(&result);
insertTerm(&poly1, 3, 2);
insertTerm(&poly1, 5, 1);
insertTerm(&poly1, 2, 0);
insertTerm(&poly2, 4, 3);
insertTerm(&poly2, 1, 1);
insertTerm(&poly2, 7, 0);
printf("Polynomial 1: ");
displayPolynomial(&poly1);
printf("Polynomial 2: ");
displayPolynomial(&poly2);
subtractPolynomials(&poly1, &poly2, &result);
printf("Difference of Polynomials: ");
displayPolynomial(&result);
destroyPolynomial(&poly1);
destroyPolynomial(&poly2);
destroyPolynomial(&result);
return 0;
}
Explain programmatically using C, how to perform the different traversals in a binary tree.
10.
#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));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
void inOrderTraversal(struct Node* root) {
if (root != NULL) {
inOrderTraversal(root->left);
printf("%d ", root->data);
inOrderTraversal(root->right);
}
}
void preOrderTraversal(struct Node* root) {
if (root != NULL) {
printf("%d ", root->data);
preOrderTraversal(root->left);
preOrderTraversal(root->right);
}
}
void postOrderTraversal(struct Node* root) {
if (root != NULL) {
postOrderTraversal(root->left);
postOrderTraversal(root->right);
printf("%d ", root->data);
}
}
struct Node* createSampleTree() {
struct Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->left = createNode(6);
root->right->right = createNode(7);
return root;
}
int main() {
struct Node* root = createSampleTree();
printf("In-Order Traversal: ");
inOrderTraversal(root);
printf("\n");
printf("Pre-Order Traversal: ");
preOrderTraversal(root);
printf("\n");
printf("Post-Order Traversal: ");
postOrderTraversal(root);
printf("\n");
return 0;
}