DATA STRUCTURE CODES Final
DATA STRUCTURE CODES Final
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
// Function prototypes
void push();
void pop();
void display();
int main() {
int choice;
while (1) {
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Quit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
default:
printf("Wrong choice\n");
} /*end of switch */
} /*end of while */
} /*end of main() */
void push() {
int pushed_item;
printf("Stack overflow\n");
} else {
scanf("%d", &pushed_item);
top = top + 1;
stack_arr[top] = pushed_item;
} /*end of push */
void pop() {
if (top == -1) {
printf("Stack underflow\n");
} else {
top = top - 1;
}
void display() {
int i;
if (top == -1) {
printf("Stack is empty\n");
} else {
printf("Stack elements:\n");
printf("%d\n", stack_arr[i]);
} /* end of display() */
INFIX TO POSTFIX
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Stack {
int top;
char items[MAX];
};
if (stack->top == MAX - 1) {
printf("Stack overflow\n");
} else {
stack->items[++stack->top] = item;
if (isEmpty(stack)) {
printf("Stack underflow\n");
return '\0';
} else {
return stack->items[stack->top--];
return 1;
if (op == '*' || op == '/')
return 2;
initialize(&stack);
int i, j;
char postfix[MAX];
i = j = 0;
if (isalnum(token)) {
postfix[j++] = pop(&stack); // Pop and append operators to postfix until an open parenthesis
is encountered
} else {
postfix[j++] = pop(&stack); // Pop and append operators to postfix with higher or equal
precedence
}
i++;
while (!isEmpty(&stack)) {
int main() {
char infix[MAX];
scanf("%s", infix);
infixToPostfix(infix);
return 0;
sp->size = 10;
sp->top = -1;
while (infix[i]!='\0')
{
if(!isOperator(infix[i])){
postfix[j] = infix[i];
j++;
i++;
else{
if(precedence(infix[i])> precedence(stackTop(sp))){
push(sp, infix[i]);
i++;
else{
postfix[j] = pop(sp);
j++;
while (!isEmpty(sp))
postfix[j] = pop(sp);
j++;
postfix[j] = '\0';
return postfix;
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct Stack {
int top;
unsigned capacity;
int* array;
};
stack->capacity = capacity;
stack->top = -1;
return stack;
stack->array[++stack->top] = item;
return stack->array[stack->top--];
if (isdigit(expression[i]))
else {
switch (expression[i]) {
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
return pop(stack);
int main() {
char expression[100];
gets(expression);
int result = evaluatePostfix(expression);
if (result != -1)
else
return 0;
#include <stdlib.h>
struct Queue {
int array[MAX_SIZE];
};
if (!queue) {
queue->front = queue->size = 0;
queue->rear = -1;
return queue;
}
int isFull(struct Queue* queue) {
if (isFull(queue)) {
return;
queue->array[queue->rear] = item;
queue->size++;
if (isEmpty(queue)) {
return -1; // You can choose a different value for an empty queue
queue->size--;
return item;
return -1; // You can choose a different value for an empty queue
return queue->array[queue->front];
int main() {
while (1) {
printf("Choose an operation:\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &item);
enqueue(queue, item);
break;
case 2:
item = dequeue(queue);
if (item != -1) {
break;
case 3:
item = front(queue);
if (item != -1) {
break;
case 4:
free(queue);
exit(0);
default:
return 0;
#include <stdlib.h>
struct CircularQueue {
int data[MAX_SIZE];
};
queue->size = 0;
if (isFull(queue)) {
printf("Queue is full!\n");
exit(1);
if (isEmpty(queue)) {
queue->front = queue->rear = 0;
} else {
queue->data[queue->rear] = value;
queue->size++;
// Remove and return the element from the front of the circular queue
int dequeue(struct CircularQueue* queue) {
if (isEmpty(queue)) {
printf("Queue is empty!\n");
exit(1);
if (queue->front == queue->rear) {
} else {
queue->size--;
return value;
// Get the element at the front of the circular queue without removing it
if (isEmpty(queue)) {
printf("Queue is empty!\n");
exit(1);
return queue->data[queue->front];
int main() {
init(&queue);
enqueue(&queue, 10);
enqueue(&queue, 20);
enqueue(&queue, 30);
printf("Front element: %d\n", front(&queue));
enqueue(&queue, 40);
return 0;
b. Insert at beginning
c. Insert at end
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
if (newNode == NULL) {
exit(1);
scanf("%d", &newNode->data);
newNode->next = NULL;
if (head == NULL) {
head = newNode;
temp = newNode;
} else {
temp->next = newNode;
temp = newNode;
return head;
exit(1);
newNode->data = data;
newNode->next = head;
return newNode;
if (newNode == NULL) {
exit(1);
newNode->data = data;
newNode->next = NULL;
if (head == NULL) {
return newNode;
temp = temp->next;
temp->next = newNode;
return head;
if (newNode == NULL) {
exit(1);
newNode->data = data;
if (head == NULL) {
return newNode;
if (head->data == key) {
newNode->next = head;
return newNode;
prev = current;
current = current->next;
if (current == NULL) {
printf("Node with key %d not found!\n", key);
free(newNode);
return head;
prev->next = newNode;
newNode->next = current;
return head;
if (current == NULL) {
return;
current = current->next;
printf("NULL\n");
current = current->next;
free(temp);
int main() {
scanf("%d", &n);
if (n > 0) {
head = createList(n);
display(head);
scanf("%d", &data);
display(head);
scanf("%d", &data);
display(head);
scanf("%d", &data);
display(head);
} else {
freeList(head);
return 0;
b. Insert at beginning
c. Insert at end
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
if (newNode == NULL) {
exit(1);
scanf("%d", &newNode->data);
newNode->next = NULL;
if (head == NULL) {
head = newNode;
temp = newNode;
} else {
temp->next = newNode;
temp = newNode;
return head;
if (head == NULL) {
} else {
struct Node* temp = head;
head = head->next;
free(temp);
return head;
if (head == NULL) {
free(head);
head = NULL;
} else {
prev = current;
current = current->next;
prev->next = NULL;
free(current);
return head;
if (head == NULL) {
if (head->data == key) {
return head;
prev = current;
current = current->next;
if (current == NULL) {
} else {
if (prev->next == current) {
prev->next = current->next;
free(current);
return head;
return;
current = current->next;
printf("NULL\n");
current = current->next;
free(temp);
int main() {
int n, key;
scanf("%d", &n);
if (n > 0) {
head = createList(n);
display(head);
head = deleteAtBeginning(head);
display(head);
head = deleteAtEnd(head);
display(head);
scanf("%d", &key);
display(head);
} else {
freeList(head);
return 0;
b. Delete at beginning
c. Delete at end
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a circular linked list node
struct Node {
int data;
};
if (newNode == NULL) {
exit(1);
scanf("%d", &newNode->data);
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
temp->next = newNode;
}
temp = newNode;
return head;
if (head == NULL) {
} else {
lastNode = lastNode->next;
lastNode->next = head->next; // Update the last node to point to the new head
free(temp);
return head;
if (head == NULL) {
} else {
struct Node* current = head;
previous = current;
current = current->next;
if (previous != NULL) {
previous->next = head; // Update the previous node to point to the new head
} else {
head = NULL; // If there was only one node, set the head to NULL
free(current);
return head;
if (head == NULL) {
return;
do {
printf("%d -> ", current->data);
current = current->next;
if (head == NULL) {
return;
do {
temp = current;
current = current->next;
free(temp);
int main() {
int n;
scanf("%d", &n);
if (n > 0) {
head = createCircularList(n);
display(head);
head = deleteAtBeginning(head);
display(head);
head = deleteAtEnd(head);
display(head);
} else {
freeCircularList(head);
return 0;
b. Insert at beginning
c. Delete at end
d. Count no of nodes
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
if (newNode == NULL) {
exit(1);
scanf("%d", &newNode->data);
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
temp->next = newNode;
temp = newNode;
return head;
}
if (newNode == NULL) {
exit(1);
newNode->data = data;
if (head == NULL) {
head = newNode;
newNode->next = head;
} else {
temp = temp->next;
newNode->next = head;
temp->next = newNode;
head = newNode;
return head;
return head;
if (head->next == head) {
free(head);
return NULL;
prev = temp;
temp = temp->next;
prev->next = head;
free(temp);
return head;
if (head == NULL) {
return 0;
int count = 0;
struct Node* current = head;
do {
count++;
current = current->next;
return count;
if (head == NULL) {
return;
do {
current = current->next;
if (head == NULL) {
return;
do {
temp = current;
current = current->next;
free(temp);
int main() {
int n;
scanf("%d", &n);
if (n > 0) {
head = createCircularList(n);
display(head);
int dataToInsert;
scanf("%d", &dataToInsert);
display(head);
int nodesToDelete;
printf("Enter the number of nodes to delete at the end: ");
scanf("%d", &nodesToDelete);
head = deleteAtEnd(head);
display(head);
} else {
freeCircularList(head);
return 0;
#include <stdlib.h>
struct Node {
int data;
};
struct Stack {
struct Node* top;
};
if (stack == NULL) {
exit(1);
stack->top = NULL;
return stack;
if (newNode == NULL) {
exit(1);
newNode->data = data;
newNode->next = stack->top;
stack->top = newNode;
}
// Function to pop an element from the stack
if (isEmpty(stack)) {
exit(1);
stack->top = temp->next;
free(temp);
return data;
if (isEmpty(stack)) {
exit(1);
return stack->top->data;
if (isEmpty(stack)) {
printf("Stack is empty.\n");
return;
current = current->next;
printf("\n");
while (!isEmpty(stack)) {
pop(stack);
free(stack);
int main() {
push(stack, 10);
push(stack, 20);
push(stack, 30);
display(stack);
pop(stack);
display(stack);
freeStack(stack);
return 0;
}
#include <stdlib.h>
struct Node {
int data;
};
struct Stack {
};
if (stack == NULL) {
exit(1);
stack->top = NULL;
return stack;
if (newNode == NULL) {
exit(1);
newNode->data = data;
newNode->next = stack->top;
stack->top = newNode;
if (isEmpty(stack)) {
exit(1);
stack->top = temp->next;
free(temp);
return data;
if (isEmpty(stack)) {
printf("Stack is empty. No top element.\n");
exit(1);
return stack->top->data;
if (isEmpty(stack)) {
printf("Stack is empty.\n");
return;
current = current->next;
printf("\n");
while (!isEmpty(stack)) {
pop(stack);
free(stack);
int main() {
push(stack, 20);
push(stack, 30);
display(stack);
pop(stack);
display(stack);
freeStack(stack);
return 0;
b. Searching
c. Traversal (Preorder)
#include <stdlib.h>
struct Node {
int data;
newNode->data = key;
return newNode;
if (root == NULL) {
return createNode(key);
return root;
root = root->left;
return root;
}
// Function to delete a node with a given key from the BST
} else {
if (root->left == NULL) {
free(root);
return temp;
free(root);
return temp;
root->data = temp->data;
return root;
if (root != NULL) {
inOrderTraversal(root->left);
inOrderTraversal(root->right);
if (root != NULL) {
preOrderTraversal(root->left);
preOrderTraversal(root->right);
if (root != NULL) {
postOrderTraversal(root->left);
postOrderTraversal(root->right);
int main() {
struct Node* root = NULL;
while (1) {
printf("Choose an operation:\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &key);
break;
case 2:
scanf("%d", &key);
break;
case 3:
scanf("%d", &key);
} else {
break;
case 4:
inOrderTraversal(root);
printf("\n");
break;
case 5:
preOrderTraversal(root);
printf("\n");
break;
case 6:
postOrderTraversal(root);
printf("\n");
break;
case 7:
free(root);
exit(0);
default:
return 0;
}
16. Implement following operations of Binary Search
Tree using Linked List
a. Insertion
b. Deletion
c. Traversal (Inorder)
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = key;
return newNode;
if (root == NULL) {
return createNode(key);
return root;
root = root->left;
return root;
} else {
if (root->left == NULL) {
free(root);
return temp;
free(root);
return temp;
root->data = temp->data;
return root;
if (root != NULL) {
inOrderTraversal(root->left);
inOrderTraversal(root->right);
int main() {
while (1) {
printf("Choose an operation:\n");
scanf("%d", &choice);
switch (choice) {
case 1:
break;
case 2:
scanf("%d", &key);
break;
case 3:
inOrderTraversal(root);
printf("\n");
break;
case 4:
free(root);
exit(0);
default:
return 0;
b. Traversal ( Postorder)
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int data;
};
if (newNode == NULL) {
exit(1);
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
if (root == NULL) {
return createNode(data);
return root;
if (root == NULL) {
return;
postorderTraversal(root->left);
postorderTraversal(root->right);
return 0;
}
// Function to count the total number of leaf nodes in the BST
if (root == NULL) {
return 0;
return 1;
if (root == NULL) {
return 0;
// Height of the tree is the maximum of left and right subtree heights, plus 1 for the current node.
int main() {
postorderTraversal(root);
printf("\n");
return 0;
#include <stdio.h>
#include <stdlib.h>
// Structure for a BST node
struct Node {
int data;
};
newNode->data = data;
return newNode;
if (root == NULL) {
return createNode(data);
return root;
}
// Function to perform an inorder traversal
if (root != NULL) {
inorderTraversal(root->left);
inorderTraversal(root->right);
if (root != NULL) {
preorderTraversal(root->left);
preorderTraversal(root->right);
if (root != NULL) {
postorderTraversal(root->left);
postorderTraversal(root->right);
if (root == NULL) {
return 0;
}
int main() {
inorderTraversal(root);
printf("\n");
preorderTraversal(root);
printf("\n");
postorderTraversal(root);
printf("\n");
return 0;
#include <stdlib.h>
struct Node {
int data;
};
struct Graph {
int vertices;
int* visited;
};
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to create a new graph with 'v' vertices
graph->vertices = v;
graph->adjacencyList[i] = NULL;
graph->visited[i] = 0;
return graph;
newNode->next = graph->adjacencyList[src];
graph->adjacencyList[src] = newNode;
// For an undirected graph, you would also add the reverse edge
newNode = createNode(src);
newNode->next = graph->adjacencyList[dest];
graph->adjacencyList[dest] = newNode;
graph->visited[vertex] = 1;
if (!graph->visited[adjacentVertex]) {
DFS(graph, adjacentVertex);
current = current->next;
int main() {
int vertices = 5;
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 3);
addEdge(graph, 2, 4);
DFS(graph, 0);
return 0;
#include <stdlib.h>
int data;
};
struct Graph {
int vertices;
int* visited;
};
struct Queue {
unsigned capacity;
int* array;
};
newNode->data = data;
newNode->next = NULL;
return newNode;
graph->vertices = v;
graph->adjacencyList = (struct Node**)malloc(v * sizeof(struct Node*));
graph->adjacencyList[i] = NULL;
graph->visited[i] = 0;
return graph;
newNode->next = graph->adjacencyList[src];
graph->adjacencyList[src] = newNode;
// For an undirected graph, you would also add the reverse edge
newNode = createNode(src);
newNode->next = graph->adjacencyList[dest];
graph->adjacencyList[dest] = newNode;
queue->capacity = capacity;
queue->front = queue->size = 0;
queue->rear = capacity - 1;
return queue;
}
// Function to check if the queue is empty
if (isFull(queue))
return;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
if (isEmpty(queue))
return -1;
queue->size = queue->size - 1;
return item;
graph->visited[startVertex] = 1;
enqueue(queue, startVertex);
while (!isEmpty(queue)) {
if (!graph->visited[adjacentVertex]) {
graph->visited[adjacentVertex] = 1;
enqueue(queue, adjacentVertex);
temp = temp->next;
int main() {
int vertices = 7;
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 5);
addEdge(graph, 2, 6);
BFS(graph, 0);
return 0;
#include <stdlib.h>
#define SIZE 10
struct HashTable {
int keys[SIZE];
int values[SIZE];
};
// Function to insert a key-value pair into the hash table using linear probing
table->keys[index] = key;
table->values[index] = value;
// Function to retrieve a value for a given key from the hash table
if (table->keys[index] == -1) {
return table->values[index];
int main() {
struct HashTable table;
initialize(&table);
while (1) {
printf("Choose an operation:\n");
scanf("%d", &choice);
switch (choice) {
case 1:
break;
case 2:
scanf("%d", &key);
if (value == -1) {
} else {
break;
case 3:
exit(0);
default:
return 0;
#include <stdlib.h>
#define SIZE 10
struct HashTable {
int keys[SIZE];
int values[SIZE];
};
// Function to insert a key-value pair into the hash table using quadratic probing
int i = 0;
i++;
table->keys[index] = key;
table->values[index] = value;
// Function to retrieve a value for a given key from the hash table
int i = 0;
if (table->keys[index] == -1) {
i++;
return table->values[index];
}
int main() {
initialize(&table);
while (1) {
printf("Choose an operation:\n");
scanf("%d", &choice);
switch (choice) {
case 1:
break;
case 2:
scanf("%d", &key);
if (value == -1) {
} else {
break;
case 3:
exit(0);
default:
return 0;