Data Structure Code
Data Structure Code
h>
#include <stdlib.h>
int main()
int item , i = 0;
int a[5]={34,56,32,78,12};
while (i<5)
if (a[i] == item)
exit (0);
++i;
exit (0);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = data;
newNode->next = NULL;
return newNode;
temp = temp->next;
printf("NULL\n");
slow = slow->next;
fast = fast->next->next;
return slow;
result = left;
} else {
result = right;
return result;
return head;
}
struct Node* middle = findMiddle(head);
middle->next = NULL;
if (*headRef == NULL) {
*headRef = newNode;
return;
temp = temp->next;
temp->next = newNode;
}
int main() {
insertAtEnd(&head, 4);
insertAtEnd(&head, 2);
insertAtEnd(&head, 8);
insertAtEnd(&head, 1);
insertAtEnd(&head, 6);
printList(head);
head = mergeSort(head);
printList(head);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = data;
newNode->next = NULL;
return newNode;
temp = temp->next;
printf("NULL\n");
slow = slow->next;
fast = fast->next->next;
return slow;
result = left;
} else {
result = right;
return result;
return head;
}
middle->next = NULL;
if (*headRef == NULL) {
*headRef = newNode;
return;
temp = temp->next;
temp->next = newNode;
}
int main() {
insertAtEnd(&head, 4);
insertAtEnd(&head, 2);
insertAtEnd(&head, 8);
insertAtEnd(&head, 1);
insertAtEnd(&head, 6);
printList(head);
head = mergeSort(head);
printList(head);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int items[MAX_SIZE];
int front;
int rear;
} Queue;
Queue* createQueue() {
Queue* q = (Queue*)malloc(sizeof(Queue));
q->front = -1;
q->rear = -1;
return q;
int isEmpty(Queue* q) {
int isFull(Queue* q) {
if (isFull(q)) {
printf("Queue is full\n");
} else {
if (q->front == -1) {
q->front = 0;
q->rear++;
q->items[q->rear] = value;
int dequeue(Queue* q) {
int item;
if (isEmpty(q)) {
printf("Queue is empty\n");
item = -1;
} else {
item = q->items[q->front];
q->front++;
return item;
void reverse_queue(Queue* q) {
int temp[MAX_SIZE];
while (!isEmpty(q)) {
temp[++top] = dequeue(q);
enqueue(q, temp[top--]);
}
void display(Queue* q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
} else {
printf("\n");
int main() {
Queue* q = createQueue();
enqueue(q, 1);
enqueue(q, 2);
enqueue(q, 3);
enqueue(q, 4);
enqueue(q, 5);
display(q);
reverse_queue(q);
display(q);
enqueue(q, 100);
enqueue(q, 200);
printf("Add two elements to the said queue:\n");
display(q);
reverse_queue(q);
display(q);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int data[MAX];
int top;
} Stack;
void init(Stack* s) {
s->top = -1;
int isEmpty(Stack* s) {
int isFull(Stack* s) {
return s->top == MAX - 1;
if (isFull(s)) {
printf("Stack overflow\n");
return;
s->data[++(s->top)] = value;
int pop(Stack* s) {
if (isEmpty(s)) {
printf("Stack underflow\n");
return -1;
return s->data[(s->top)--];
int peek(Stack* s) {
if (isEmpty(s)) {
printf("Stack is empty\n");
return -1;
return s->data[s->top];
// Function to copy the contents of one stack into another, preserving order
void copyStack(Stack* source, Stack* dest) {
Stack temp;
init(&temp);
// Step 1: Pop elements from the source stack and push them into a temporary stack
while (!isEmpty(source)) {
push(&temp, pop(source));
// Step 2: Pop elements from the temporary stack, push them back into source and into the
destination stack
while (!isEmpty(&temp)) {
int main() {
init(&source);
init(&dest);
push(&source, 10);
push(&source, 20);
push(&source, 30);
copyStack(&source, &dest);
// Display the contents of the source stack
while (!isEmpty(&source)) {
printf("%d\n", pop(&source));
printf("Destination stack:\n");
while (!isEmpty(&dest)) {
printf("%d\n", pop(&dest));
return 0;