Dsafile
Dsafile
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Product
{
char Product_Id;
char Product_Name[50];
int Total_sale;
char Product_Grade;
};
struct Node
{
Product productData;
struct Node *next;
};
struct Queue
{
Node *front;
Node *rear;
};
Queue *createQueue()
{
Queue *queue = (Queue *)malloc(sizeof(Queue));
queue->front = NULL;
queue->rear = NULL;
return queue;
}
int isEmpty(Queue *queue)
{
return queue->front == NULL;
}
if (isEmpty(queue))
{
queue->front = newNode;
queue->rear = newNode;
}
else
{
queue->rear->next = newNode;
queue->rear = newNode;
}
}
queue->front = queue->front->next;
free(temp);
return product;
}
int main()
{
Queue *queue = createQueue();
enqueue(queue, product1);
enqueue(queue, product2);
enqueue(queue, product3);
while (!isEmpty(queue))
{
Product product = dequeue(queue);
printf("Product ID: %c, Name: %s, Total Sale: %d, Grade:
%c\n", product.Product_Id, product.Product_Name,
product.Total_sale, product.Product_Grade);
}
return 0;
}
/* OUTPUT:-
Product ID: A, Name: Product1, Total Sale: 100, Grade: A
Product ID: B, Name: Product2, Total Sale: 150, Grade: B
Product ID: C, Name: Product3, Total Sale: 200, Grade: A */
/*Q2.Let A and B be two structures of type Linked List. Write a
‘C ’ program to create a new
Linked List ‘S’ that contains elements alternately from A and B
beginning with the first
element of A. If you run out of elements in one of the lists,
then append the remaining*/
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct LinkedList {
Node* head;
Node* tail;
};
int main() {
insertAtEnd(&A, 1);
insertAtEnd(&A, 3);
insertAtEnd(&A, 5);
insertAtEnd(&B, 2);
insertAtEnd(&B, 4);
insertAtEnd(&B, 6);
insertAtEnd(&B, 8);
return 0;
}
/*OUTPUT:-
List S: 1 2 3 4 5 6 8
*/
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
if (lessThanV != NULL) {
current = lessThanV;
while (current->next != NULL) {
current = current->next;
}
current->next = greaterThanOrEqualV;
return lessThanV;
} else {
return greaterThanOrEqualV;
}
}
int main() {
Node* head = NULL;
insertAtEnd(&head, 3);
insertAtEnd(&head, 6);
insertAtEnd(&head, 1);
insertAtEnd(&head, 5);
insertAtEnd(&head, 2);
insertAtEnd(&head, 7);
int V = 4;
return 0;
}
/*OUTPUT:-
Original List: 3 6 1 5 2 7
Partitioned List: 3 1 2 6 5 7
*/
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
insertAtEnd(&result, sub);
if (list1 != NULL) {
list1 = list1->next;
}
if (list2 != NULL) {
list2 = list2->next;
}
}
result = reverseList(result);
return result;
}
int main() {
Node* list1 = NULL;
Node* list2 = NULL;
insertAtEnd(&list1, 7);
insertAtEnd(&list1, 9);
insertAtEnd(&list1, 8);
insertAtEnd(&list2, 5);
insertAtEnd(&list2, 4);
insertAtEnd(&list2, 1);
printf("List 1: ");
displayList(list1);
printf("List 2: ");
displayList(list2);
return 0;
}
/*OUTPUT:-
List 1: 7 9 8
List 2: 5 4 1
*/
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
temp = secondHalf->next;
secondHalf->next = current;
secondHalf = temp;
}
}
int main() {
Node* head = NULL;
return 0;
}
/*OUTPUT:-
Original List: 1 2 3 4 5
Rearranged List: 1 5 2 4 3
*/
#include <stdio.h>
#include <stdlib.h>
if (*head == nodeToRemove) {
*head = nodeToRemove->next;
free(nodeToRemove);
return;
}
if (current->next == NULL) {
return;
}
current->next = nodeToRemove->next;
free(nodeToRemove);
}
int main() {
Node* original = NULL;
return 0;
}
/*OUTPUT:-
Original List: -3 5 -9 7 11 -2
Positive List: 5 7 11
Negative List: -3 -9 -2
*/
#include <stdio.h>
#include <stdlib.h>
return root;
}
if (root->right != NULL) {
return 1 + countNodesFromRight(root->right);
}
return countNodesFromRight(root->left);
}
kthLargestUtil(root->right, k, count);
(*count)++;
if (*count == k) {
KthLargest = root->data;
return;
}
kthLargestUtil(root->left, k, count);
}
int main() {
Node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
return 0;
}
/*OUTPUT:-
Key 40 found in the tree
*/
/*Q8.Write a program to add of two polynomials of degree n,
using linked list
For example p1=anx
n+an-1x
n-1 + an-2x
n-2 + …….. a0x
0
P2=bnx
n+bn-1x
n-1 + bn-2x
n-2 + …….. b0x
0
p1 = first polynomial
p2 = second polynomial
Find out p3= p1+p2*/
#include <stdio.h>
#include <stdlib.h>
struct Node {
int coefficient;
int exponent;
struct Node* next;
};
if (*head == NULL) {
*head = newTerm;
return;
}
return result;
}
int main() {
Node* p1 = NULL;
addTerm(&p1, 3, 2);
addTerm(&p1, 2, 1);
addTerm(&p1, 5, 0);
Node* p2 = NULL;
addTerm(&p2, 4, 3);
addTerm(&p2, 1, 1);
addTerm(&p2, 2, 0);
printf("p1: ");
display(p1);
printf("p2: ");
display(p2);
printf("p3: ");
display(p3);
return 0;
}
/*OUTPUT:-
p1: 3x^2 + 2x^1 + 5x^0
p2: 4x^3 + 1x^1 + 2x^0
p3: 4x^3 + 3x^2 + 3x^1 + 7x^0
*/
int main() {
int size;
char sequence[size];
return 0;
}
/*OUTPUT:-
Enter the number of characters in the sequence: 5
Enter the characters: 9
8
2
4
1
Sorted sequence: 1 2 4 8 9 */
/*Q10.Using circular linked list allocate time slots of 10ms for
given processes in time sharing
Environment and then print which process will be completed in
how much time.*/
#include <stdio.h>
#include <stdlib.h>
struct Process {
int processID;
int remainingTime;
struct Process* next;
};
if (*head == NULL) {
*head = newProcess;
newProcess->next = *head;
} else {
Process* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = newProcess;
newProcess->next = *head;
}
}
free(current);
current = temp;
}
} while (current != *head);
}
int main() {
Process* processes = NULL;
int numProcesses, timeSlot;
timeSharing(&processes, timeSlot);
return 0;
}
/*OUTPUT:-
Enter the number of processes: 5
Enter time slot for each process: 10
Processes execution order:
Time slot 10 - Process 1
Process 1 completed in total time 0
*/
#include <stdio.h>
#include <stdlib.h>
int main() {
return 0;
}
/*OUTPUT:-
Enter the number of vertices in the graph: 4
Enter the number of edges: 5
Enter source, destination, and weight for each edge:
0 1 2
0 2 3
1 3 1
2 3 4
3 0 5
Graph representation:
Vertex 0 -> (2, w:3) (1, w:2)
Vertex 1 -> (3, w:1)
Vertex 2 -> (3, w:4)
Vertex 3 -> (0, w:5)
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
while (!isQueueEmpty(queue)) {
int currentVertex = dequeue(queue);
printf("%d ", currentVertex);
int main() {
int numVertices, numEdges, startVertex;
Node* graph[numVertices];
for (int i = 0; i < numVertices; i++) {
graph[i] = createNode(i);
}
return 0;
}
/*OUTPUT:-
Enter the number of vertices: 6
Enter the number of edges: 8
Enter source and destination for each edge:
0 1
0 2
1 3
1 4
2 5
2 6
3 7
4 7
Enter the starting vertex for BFS: 0
BFS Traversal starting from vertex 0: 0 1 2 3 4 5 6 7
*/