0% found this document useful (0 votes)
22 views14 pages

Ds

Uploaded by

suhasm11111111
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)
22 views14 pages

Ds

Uploaded by

suhasm11111111
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/ 14

1.

#include <stdio.h>
#include <stdlib.h>

#define MAX 5 // Maximum size of the stack

int stack[MAX];
int top = -1;

// Function to add an element to the stack (Push)


void push(int value) {
if (top == MAX - 1) {
printf("Stack Overflow! Unable to push %d\n", value);
} else {
top++;
stack[top] = value;
printf("Pushed %d into the stack.\n", value);
}
}

// Function to remove an element from the stack (Pop)


void pop() {
if (top == -1) {
printf("Stack Underflow! The stack is empty.\n");
} else {
printf("Popped %d from the stack.\n", stack[top]);
top--;
}
}

// Function to display the elements in the stack


void display() {
if (top == -1) {
printf("The stack is empty.\n");
} else {
printf("Stack elements are: ");
for (int i = 0; i <= top; i++) {
printf("%d ", stack[i]);
}
printf("\n");
}
}

int main() {
int choice, value;

while (1) {
printf("\nStack Operations Menu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}

return 0;
}

-----------------------------------------------------------------------------------
-----------------------------------------------
2.
#include <stdio.h>
#include <ctype.h> // For isdigit()
#include <stdlib.h> // For exit()

#define MAX 100 // Maximum size of the stack

int stack[MAX];
int top = -1;

// Function to push an element onto the stack


void push(int value) {
if (top == MAX - 1) {
printf("Stack Overflow!\n");
exit(1);
} else {
stack[++top] = value;
}
}

// Function to pop an element from the stack


int pop() {
if (top == -1) {
printf("Stack Underflow!\n");
exit(1);
} else {
return stack[top--];
}
}

// Function to evaluate a postfix expression


int evaluatePostfix(char* exp) {
int i = 0;
int op1, op2, result;

// Iterate over each character in the expression


while (exp[i] != '\0') {
// If the character is a digit, push it onto the stack
if (isdigit(exp[i])) {
push(exp[i] - '0'); // Convert char to int by subtracting '0'
}
// If the character is an operator, pop two elements and apply the
operation
else {
op2 = pop();
op1 = pop();

switch (exp[i]) {
case '+':
result = op1 + op2;
break;
case '-':
result = op1 - op2;
break;
case '*':
result = op1 * op2;
break;
case '/':
if (op2 == 0) {
printf("Division by zero error!\n");
exit(1);
}
result = op1 / op2;
break;
default:
printf("Invalid operator encountered: %c\n", exp[i]);
exit(1);
}
push(result); // Push the result back onto the stack
}
i++;
}

// The final result is the only element remaining in the stack


return pop();
}

int main() {
char postfix[MAX];

printf("Enter a valid postfix expression (single digit integers and operators


+, -, *, /): ");
scanf("%s", postfix);

int result = evaluatePostfix(postfix);


printf("The result of the postfix expression is: %d\n", result);

return 0;
}

-----------------------------------------------------------------------------------
-------------------------------------------
3.
#include <stdio.h>
#include <ctype.h> // For isdigit() and isalpha()
#include <string.h> // For string functions
#define MAX 100

char stack[MAX];
int top = -1;

// Function to push an element to the stack


void push(char x) {
if (top == MAX - 1) {
printf("Stack Overflow!\n");
} else {
stack[++top] = x;
}
}

// Function to pop an element from the stack


char pop() {
if (top == -1) {
printf("Stack Underflow!\n");
return -1;
} else {
return stack[top--];
}
}

// Function to return the precedence of an operator


int precedence(char x) {
if (x == '+' || x == '-')
return 1;
if (x == '*' || x == '/')
return 2;
return 0;
}

// Function to check if the character is an operator


int isOperator(char x) {
return (x == '+' || x == '-' || x == '*' || x == '/');
}

// Function to convert infix expression to postfix expression


void infixToPostfix(char* infix, char* postfix) {
int i = 0, j = 0;
char x;

while (infix[i] != '\0') {


// If the character is an operand (digit or letter), add it to postfix
if (isalnum(infix[i])) {
postfix[j++] = infix[i];
}
// If the character is '(', push it to the stack
else if (infix[i] == '(') {
push(infix[i]);
}
// If the character is ')', pop and add to postfix until '(' is encountered
else if (infix[i] == ')') {
while ((x = pop()) != '(') {
postfix[j++] = x;
}
}
// If the character is an operator
else if (isOperator(infix[i])) {
while (top != -1 && precedence(stack[top]) >= precedence(infix[i])) {
postfix[j++] = pop();
}
push(infix[i]);
}
i++;
}

// Pop all the remaining operators from the stack


while (top != -1) {
postfix[j++] = pop();
}

postfix[j] = '\0'; // Null terminate the postfix expression


}

int main() {
char infix[MAX], postfix[MAX];

printf("Enter an infix expression: ");


scanf("%s", infix);

infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);

return 0;
}
-----------------------------------------------------------------------------------
---------------------------------
4.
#include <stdio.h>
#include <stdlib.h>

#define MAX 5 // Define the maximum size of the queue

int queue[MAX];
int front = -1, rear = -1;

// Function to check if the queue is full


int isFull() {
return (rear == MAX - 1);
}

// Function to check if the queue is empty


int isEmpty() {
return (front == -1 || front > rear);
}

// Function to enqueue (insert) an element into the queue


void enqueue(int value) {
if (isFull()) {
printf("Queue Overflow! Cannot insert %d\n", value);
} else {
if (front == -1) { // If inserting the first element
front = 0;
}
queue[++rear] = value;
printf("%d enqueued into the queue.\n", value);
}
}

// Function to dequeue (remove) an element from the queue


int dequeue() {
if (isEmpty()) {
printf("Queue Underflow! Cannot dequeue.\n");
return -1;
} else {
int dequeuedValue = queue[front++];
if (front > rear) { // Reset the queue when it becomes empty
front = rear = -1;
}
printf("%d dequeued from the queue.\n", dequeuedValue);
return dequeuedValue;
}
}

// Function to get the element at the front of the queue


int peek() {
if (isEmpty()) {
printf("Queue is empty! No element to peek.\n");
return -1;
} else {
return queue[front];
}
}

// Function to display the elements of the queue


void displayQueue() {
if (isEmpty()) {
printf("Queue is empty!\n");
} else {
printf("Queue elements are: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}

int main() {
int choice, value;

while (1) {
printf("\nQueue Operations:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Peek\n");
printf("4. Display Queue\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
value = peek();
if (value != -1) {
printf("Front element is: %d\n", value);
}
break;
case 4:
displayQueue();
break;
case 5:
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}

return 0;
}
-----------------------------------------------------------------------------------
------------------------------------------
5.
#include <stdio.h>
#include <stdlib.h>

// Define the structure of a node


struct Node {
int data;
struct Node* next;
};

// Head pointer (points to the first node)


struct Node* head = NULL;

// Function to insert a node at the beginning


void insertAtBeginning(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = head;
head = newNode;
printf("%d inserted at the beginning.\n", value);
}

// Function to insert a node at the end


void insertAtEnd(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;

if (head == NULL) { // If the list is empty


head = newNode;
printf("%d inserted at the end (first element).\n", value);
return;
}
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
printf("%d inserted at the end.\n", value);
}

// Function to delete a node with a specific value


void deleteNode(int value) {
struct Node* temp = head;
struct Node* prev = NULL;

// If the head node itself holds the value


if (temp != NULL && temp->data == value) {
head = temp->next; // Change head
free(temp); // Free the old head
printf("%d deleted from the list.\n", value);
return;
}

// Search for the node to be deleted, keep track of the previous node
while (temp != NULL && temp->data != value) {
prev = temp;
temp = temp->next;
}

// If the value was not found


if (temp == NULL) {
printf("%d not found in the list.\n", value);
return;
}

// Unlink the node from the linked list


prev->next = temp->next;
free(temp); // Free memory
printf("%d deleted from the list.\n", value);
}

// Function to search for a value in the linked list


void search(int value) {
struct Node* temp = head;
int pos = 1;
while (temp != NULL) {
if (temp->data == value) {
printf("%d found at position %d.\n", value, pos);
return;
}
temp = temp->next;
pos++;
}
printf("%d not found in the list.\n", value);
}

// Function to display the entire linked list


void displayList() {
struct Node* temp = head;
if (temp == NULL) {
printf("List is empty.\n");
return;
}
printf("Linked List: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
int choice, value;

while (1) {
printf("\nSingly Linked List Operations:\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Delete Node\n");
printf("4. Search\n");
printf("5. Display List\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBeginning(value);
break;
case 2:
printf("Enter value to insert at end: ");
scanf("%d", &value);
insertAtEnd(value);
break;
case 3:
printf("Enter value to delete: ");
scanf("%d", &value);
deleteNode(value);
break;
case 4:
printf("Enter value to search: ");
scanf("%d", &value);
search(value);
break;
case 5:
displayList();
break;
case 6:
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}

return 0;
}
-----------------------------------------------------------------------------------
-------------------------------------
6.
#include <stdio.h>
#include <stdlib.h>

// Define the structure of a node


struct Node {
int data;
struct Node* next;
struct Node* prev;
};

// Head pointer (points to the first node)


struct Node* head = NULL;

// Function to insert at the beginning


void insertAtBeginning(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;

if (head == NULL) { // List is empty


newNode->next = newNode;
newNode->prev = newNode;
head = newNode;
} else {
struct Node* tail = head->prev;
newNode->next = head;
newNode->prev = tail;
tail->next = newNode;
head->prev = newNode;
head = newNode;
}

printf("%d inserted at the beginning.\n", value);


}

// Function to insert at the end


void insertAtEnd(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;

if (head == NULL) { // List is empty


newNode->next = newNode;
newNode->prev = newNode;
head = newNode;
} else {
struct Node* tail = head->prev;
newNode->next = head;
newNode->prev = tail;
tail->next = newNode;
head->prev = newNode;
}

printf("%d inserted at the end.\n", value);


}

// Function to delete a node with a specific value


void deleteNode(int value) {
if (head == NULL) {
printf("List is empty. Cannot delete %d.\n", value);
return;
}

struct Node* temp = head;

// Find the node to be deleted


do {
if (temp->data == value) {
if (temp->next == temp && temp->prev == temp) { // Only one node
head = NULL;
} else {
struct Node* nextNode = temp->next;
struct Node* prevNode = temp->prev;

nextNode->prev = prevNode;
prevNode->next = nextNode;

if (temp == head) {
head = nextNode;
}
}

free(temp);
printf("%d deleted from the list.\n", value);
return;
}
temp = temp->next;
} while (temp != head);

printf("%d not found in the list.\n", value);


}

// Function to display the circular doubly linked list


void displayList() {
if (head == NULL) {
printf("List is empty.\n");
return;
}

struct Node* temp = head;


printf("Circular Doubly Linked List: ");
do {
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != head);
printf("(back to %d)\n", head->data); // Indicating circular nature
}

// Main function to provide a menu-driven interface


int main() {
int choice, value;

while (1) {
printf("\nCircular Doubly Linked List Operations:\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Delete Node\n");
printf("4. Display List\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBeginning(value);
break;
case 2:
printf("Enter value to insert at end: ");
scanf("%d", &value);
insertAtEnd(value);
break;
case 3:
printf("Enter value to delete: ");
scanf("%d", &value);
deleteNode(value);
break;
case 4:
displayList();
break;
case 5:
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}

return 0;
}
-----------------------------------------------------------------------------------
-------------------------------------
7.
#include <stdio.h>
#include <stdlib.h>

#define MAX 100

int tree[MAX]; // Array to represent the binary tree


int size = 0; // Current number of elements in the tree

// Function to insert an element into the binary tree


void insert(int value) {
if (size == MAX) {
printf("Tree is full! Cannot insert more elements.\n");
return;
}
tree[size] = value;
size++;
printf("%d inserted into the tree.\n", value);
}

// Function to delete an element from the binary tree


void delete(int value) {
if (size == 0) {
printf("Tree is empty! Cannot delete elements.\n");
return;
}
// Search for the element to delete
int i;
for (i = 0; i < size; i++) {
if (tree[i] == value) {
break;
}
}

if (i == size) {
printf("Element %d not found in the tree.\n", value);
return;
}

// Replace the element with the last element in the array


tree[i] = tree[size - 1];
size--;

printf("%d deleted from the tree.\n", value);


}

// Function for inorder traversal (Left-Root-Right)


void inorderTraversal(int index) {
if (index >= size) return;

inorderTraversal(2 * index + 1); // Left child


printf("%d ", tree[index]); // Root
inorderTraversal(2 * index + 2); // Right child
}

// Function for preorder traversal (Root-Left-Right)


void preorderTraversal(int index) {
if (index >= size) return;

printf("%d ", tree[index]); // Root


preorderTraversal(2 * index + 1); // Left child
preorderTraversal(2 * index + 2); // Right child
}

// Function for postorder traversal (Left-Right-Root)


void postorderTraversal(int index) {
if (index >= size) return;

postorderTraversal(2 * index + 1); // Left child


postorderTraversal(2 * index + 2); // Right child
printf("%d ", tree[index]); // Root
}

// Function to display the binary tree elements


void displayTree() {
if (size == 0) {
printf("Tree is empty!\n");
return;
}

printf("Binary Tree (array representation): ");


for (int i = 0; i < size; i++) {
printf("%d ", tree[i]);
}
printf("\n");
}

// Main function for user input and menu-driven program


int main() {
int choice, value;

while (1) {
printf("\nBinary Tree Operations Using Array:\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Inorder Traversal\n");
printf("4. Preorder Traversal\n");
printf("5. Postorder Traversal\n");
printf("6. Display Tree\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
insert(value);
break;
case 2:
printf("Enter value to delete: ");
scanf("%d", &value);
delete(value);
break;
case 3:
printf("Inorder Traversal: ");
inorderTraversal(0);
printf("\n");
break;
case 4:
printf("Preorder Traversal: ");
preorderTraversal(0);
printf("\n");
break;
case 5:
printf("Postorder Traversal: ");
postorderTraversal(0);
printf("\n");
break;
case 6:
displayTree();
break;
case 7:
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}

return 0;
}

You might also like