C program Linked List
C program Linked List
write c program to implement a linked list just give me the program i don't any explanation or
definition
#include <stdio.h>
#include <stdlib.h>
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/21
if (temp == NULL) return head;
prev->next = temp->next;
free(temp);
return head;
}
// Main function
int main() {
struct Node* head = NULL;
return 0;
}
write a c program to implement queue using linked list give me the program i don't want any
explanation or definition
#include <stdio.h>
#include <stdlib.h>
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/21
}
// Main function
int main() {
struct Queue* q = createQueue();
enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);
displayQueue(q);
return 0;
}
write a c program to implement a stack using linked list give me only the program i dont want any
explanation or definition
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/21
c
#include <stdio.h>
#include <stdlib.h>
// Main function
int main() {
struct Node* top = NULL;
int poppedData;
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/21
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Main function
int main() {
struct Node* root = NULL;
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/21
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 70);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 60);
root = insert(root, 80);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/21
struct Node* rightRotate(struct Node* y) {
struct Node* x = y->left;
struct Node* T2 = x->right;
x->right = y;
y->left = T2;
return x;
}
y->left = x;
x->right = T2;
return y;
}
return node;
}
// Main function
int main() {
struct Node* root = NULL;
return 0;
}
binary heap
#include <stdio.h>
int heap[MAX_SIZE];
int heapSize = 0;
heap[heapSize] = value;
int current = heapSize;
heapSize++;
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 8/21
int left = 2 * index + 1;
int right = 2 * index + 2;
return root;
}
// Main function
int main() {
insert(10);
insert(20);
insert(15);
insert(30);
insert(5);
return 0;
}
Features:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 9/21
Below is a C program implementing both Min-Heap and Max-Heap operations:
#include <stdio.h>
#include <stdlib.h>
// Heapify Min-Heap
void minHeapify(int index) {
int smallest = index;
int left = 2 * index + 1;
int right = 2 * index + 2;
return root;
}
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 10/21
/*-------------------- MAX HEAP FUNCTIONS --------------------*/
// Heapify Max-Heap
void maxHeapify(int index) {
int largest = index;
int left = 2 * index + 1;
int right = 2 * index + 2;
return root;
}
void displayMinHeap() {
for (int i = 0; i < minHeapSize; i++) {
printf("%d ", minHeap[i]);
}
printf("\n");
}
void displayMaxHeap() {
for (int i = 0; i < maxHeapSize; i++) {
printf("%d ", maxHeap[i]);
}
printf("\n");
}
int main() {
// Min-Heap Operations
printf("Min-Heap:\n");
insertMinHeap(10);
insertMinHeap(20);
insertMinHeap(5);
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 11/21
insertMinHeap(15);
printf("Heap elements: ");
displayMinHeap();
printf("Removed Min: %d\n", removeMin());
printf("Heap after removal: ");
displayMinHeap();
// Max-Heap Operations
printf("\nMax-Heap:\n");
insertMaxHeap(10);
insertMaxHeap(20);
insertMaxHeap(5);
insertMaxHeap(15);
printf("Heap elements: ");
displayMaxHeap();
printf("Removed Max: %d\n", removeMax());
printf("Heap after removal: ");
displayMaxHeap();
return 0;
}
Features:
1. Min-Heap Functions:
`insertMinHeap`:
Insert an element into the Min-Heap.
`removeMin`: Remove the minimum (root) element from the Min-Heap.
`displayMinHeap`: Display elements in the Min-Heap.
2. Max-Heap Functions:
`insertMaxHeap`:Insert an element into the Max-Heap.
`removeMax`:Remove the maximum (root) element from the Max-Heap.
`displayMaxHeap`: Display elements in the Max-Heap.
for every program i ask here afterward dont give any explanation and definiton
Understood.
#include <stdio.h>
#include <stdlib.h>
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 12/21
int exp;
struct Node* next;
};
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 13/21
// Function to subtract two polynomials
struct Node* subtract(struct Node* poly1, struct Node* poly2) {
struct Node* result = NULL;
while (poly1 != NULL && poly2 != NULL) {
if (poly1->exp > poly2->exp) {
insert(&result, poly1->coeff, poly1->exp);
poly1 = poly1->next;
} else if (poly1->exp < poly2->exp) {
insert(&result, -poly2->coeff, poly2->exp);
poly2 = poly2->next;
} else {
int coeff = poly1->coeff - poly2->coeff;
if (coeff != 0) {
insert(&result, coeff, poly1->exp);
}
poly1 = poly1->next;
poly2 = poly2->next;
}
}
while (poly1 != NULL) {
insert(&result, poly1->coeff, poly1->exp);
poly1 = poly1->next;
}
while (poly2 != NULL) {
insert(&result, -poly2->coeff, poly2->exp);
poly2 = poly2->next;
}
return result;
}
// Main function
int main() {
struct Node* poly1 = NULL;
struct Node* poly2 = NULL;
insert(&poly1, 5, 2);
insert(&poly1, 4, 1);
insert(&poly1, 2, 0);
insert(&poly2, 3, 2);
insert(&poly2, 1, 1);
insert(&poly2, 4, 0);
printf("Polynomial 1: ");
display(poly1);
printf("Polynomial 2: ");
display(poly2);
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 14/21
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
// Stack structure
struct Stack {
int top;
char arr[MAX_SIZE];
};
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 15/21
}
// Main function
int main() {
char infix[MAX_SIZE], postfix[MAX_SIZE];
infixToPostfix(infix, postfix);
return 0;
}
Dijkstra's Algorithm
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 16/21
c
#include <stdio.h>
#include <limits.h>
#define V 9
// Main function
int main() {
// Example graph represented as an adjacency matrix
int graph[V][V] = {
{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 14, 10, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 17/21
return 0;
}
#include <stdio.h>
#include <limits.h>
#define V 9
// Print the shortest distance from the source to all other vertices
printf("Vertex\tDistance from Source\n");
for (int i = 0; i < V; i++) {
printf("%d\t%d\n", i, dist[i]);
}
}
// Main function
int main() {
// Example graph represented as an adjacency matrix
int graph[V][V] = {
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 18/21
{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 14, 10, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
return 0;
}
Prim's Algorithm
#include <stdio.h>
#include <limits.h>
#define V 5
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 19/21
parent[v] = u;
key[v] = graph[u][v];
}
}
}
// Main function
int main() {
// Example graph represented as an adjacency matrix
int graph[V][V] = {
{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}
};
primMST(graph);
return 0;
}
evaluate Postfix
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
// Stack structure
struct Stack {
int top;
int arr[MAX_SIZE];
};
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 20/21
// Function to pop an element from the stack
int pop(struct Stack* stack) {
if (!isEmpty(stack)) {
return stack->arr[stack->top--];
}
return -1;
}
switch (current) {
case '+':
push(&stack, operand1 + operand2);
break;
case '-':
push(&stack, operand1 - operand2);
break;
case '*':
push(&stack, operand1 * operand2);
break;
case '/':
push(&stack, operand1 / operand2);
break;
}
}
}
// Main function
int main() {
char expr[MAX_SIZE];
return 0;
}
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 21/21