0% found this document useful (0 votes)
1 views

Data Structure Code

The document contains multiple C programs demonstrating data structures and algorithms including searching in an array, linked list operations (creation, printing, merging, and sorting), queue operations (enqueue, dequeue, reversing), and stack operations (push, pop, and copying). Each program is structured with functions to perform specific tasks, showcasing fundamental programming concepts. The main function in each program serves as an entry point to execute the defined operations.

Uploaded by

krishnajagave
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Data Structure Code

The document contains multiple C programs demonstrating data structures and algorithms including searching in an array, linked list operations (creation, printing, merging, and sorting), queue operations (enqueue, dequeue, reversing), and stack operations (push, pop, and copying). Each program is structured with functions to perform specific tasks, showcasing fundamental programming concepts. The main function in each program serves as an entry point to execute the defined operations.

Uploaded by

krishnajagave
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

#include <stdio.

h>

#include <stdlib.h>

int main()

int item , i = 0;

int a[5]={34,56,32,78,12};

printf("enter serching item");

scanf ("%d", &item);

while (i<5)

if (a[i] == item)

printf ("item found at %d", i);

exit (0);

++i;

printf("Item nof found");

exit (0);

return 0;

}
#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

return newNode;

void printList(struct Node* head) {

struct Node* temp = head;

while (temp != NULL) {

printf("%d -> ", temp->data);

temp = temp->next;

printf("NULL\n");

struct Node* findMiddle(struct Node* head) {

if (head == NULL) return head;


struct Node* slow = head;

struct Node* fast = head->next;

while (fast != NULL && fast->next != NULL) {

slow = slow->next;

fast = fast->next->next;

return slow;

struct Node* merge(struct Node* left, struct Node* right) {

if (left == NULL) return right;

if (right == NULL) return left;

struct Node* result = NULL;

if (left->data <= right->data) {

result = left;

result->next = merge(left->next, right);

} else {

result = right;

result->next = merge(left, right->next);

return result;

struct Node* mergeSort(struct Node* head) {

if (head == NULL || head->next == NULL) {

return head;

}
struct Node* middle = findMiddle(head);

struct Node* nextOfMiddle = middle->next;

middle->next = NULL;

struct Node* left = mergeSort(head);

struct Node* right = mergeSort(nextOfMiddle);

return merge(left, right);

void insertAtEnd(struct Node** headRef, int data) {

struct Node* newNode = createNode(data);

if (*headRef == NULL) {

*headRef = newNode;

return;

struct Node* temp = *headRef;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

}
int main() {

struct Node* head = NULL;

insertAtEnd(&head, 4);

insertAtEnd(&head, 2);

insertAtEnd(&head, 8);

insertAtEnd(&head, 1);

insertAtEnd(&head, 6);

printf("Original list: ");

printList(head);

head = mergeSort(head);

printf("Sorted list: ");

printList(head);

return 0;

}
#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

return newNode;

void printList(struct Node* head) {

struct Node* temp = head;

while (temp != NULL) {

printf("%d -> ", temp->data);

temp = temp->next;

printf("NULL\n");

struct Node* findMiddle(struct Node* head) {

if (head == NULL) return head;


struct Node* slow = head;

struct Node* fast = head->next;

while (fast != NULL && fast->next != NULL) {

slow = slow->next;

fast = fast->next->next;

return slow;

struct Node* merge(struct Node* left, struct Node* right) {

if (left == NULL) return right;

if (right == NULL) return left;

struct Node* result = NULL;

if (left->data <= right->data) {

result = left;

result->next = merge(left->next, right);

} else {

result = right;

result->next = merge(left, right->next);

return result;

struct Node* mergeSort(struct Node* head) {

if (head == NULL || head->next == NULL) {

return head;
}

struct Node* middle = findMiddle(head);

struct Node* nextOfMiddle = middle->next;

middle->next = NULL;

struct Node* left = mergeSort(head);

struct Node* right = mergeSort(nextOfMiddle);

return merge(left, right);

void insertAtEnd(struct Node** headRef, int data) {

struct Node* newNode = createNode(data);

if (*headRef == NULL) {

*headRef = newNode;

return;

struct Node* temp = *headRef;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

}
int main() {

struct Node* head = NULL;

insertAtEnd(&head, 4);

insertAtEnd(&head, 2);

insertAtEnd(&head, 8);

insertAtEnd(&head, 1);

insertAtEnd(&head, 6);

printf("Original list: ");

printList(head);

head = mergeSort(head);

printf("Sorted list: ");

printList(head);

return 0;

}
#include <stdio.h>

#include <stdlib.h>

#define MAX_SIZE 100

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) {

return q->rear == -1;

int isFull(Queue* q) {

return q->rear == MAX_SIZE - 1;

void enqueue(Queue* q, int value) {

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++;

if (q->front > q->rear) {

q->front = q->rear = -1;

return item;

void reverse_queue(Queue* q) {

int temp[MAX_SIZE];

int top = -1;

while (!isEmpty(q)) {

temp[++top] = dequeue(q);

while (top != -1) {

enqueue(q, temp[top--]);
}

void display(Queue* q) {

if (isEmpty(q)) {

printf("Queue is empty\n");

} else {

for (int i = q->front; i <= q->rear; i++) {

printf("%d ", q->items[i]);

printf("\n");

int main() {

Queue* q = createQueue();

enqueue(q, 1);

enqueue(q, 2);

enqueue(q, 3);

enqueue(q, 4);

enqueue(q, 5);

printf("Queue elements are:\n");

display(q);

printf("Reverse Queue, elements are:\n");

reverse_queue(q);

display(q);

enqueue(q, 100);

enqueue(q, 200);
printf("Add two elements to the said queue:\n");

printf("Queue elements are:\n");

display(q);

printf("Reverse Queue, elements are:\n");

reverse_queue(q);

display(q);

return 0;

}
#include <stdio.h>

#include <stdlib.h>

#define MAX 100 // Maximum size of the stack

// Stack structure definition

typedef struct Stack {

int data[MAX];

int top;

} Stack;

// Stack operations function prototypes

void init(Stack* s);

int isEmpty(Stack* s);

int isFull(Stack* s);

void push(Stack* s, int value);

int pop(Stack* s);

int peek(Stack* s);

// Initialize the stack

void init(Stack* s) {

s->top = -1;

// Check if the stack is empty

int isEmpty(Stack* s) {

return s->top == -1;

// Check if the stack is full

int isFull(Stack* s) {
return s->top == MAX - 1;

// Push an element onto the stack

void push(Stack* s, int value) {

if (isFull(s)) {

printf("Stack overflow\n");

return;

s->data[++(s->top)] = value;

// Pop an element from the stack

int pop(Stack* s) {

if (isEmpty(s)) {

printf("Stack underflow\n");

return -1;

return s->data[(s->top)--];

// Peek at the top element of the stack without popping it

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 value = pop(&temp);

push(source, value); // Restore the original stack

push(dest, value); // Copy to the destination stack

// Main function to demonstrate the program

int main() {

Stack source, dest;

init(&source);

init(&dest);

// Example data for the source stack

push(&source, 10);

push(&source, 20);

push(&source, 30);

// Copy the contents of the source stack to the destination stack

copyStack(&source, &dest);
// Display the contents of the source stack

printf("Source stack after copying:\n");

while (!isEmpty(&source)) {

printf("%d\n", pop(&source));

// Display the contents of the destination stack

printf("Destination stack:\n");

while (!isEmpty(&dest)) {

printf("%d\n", pop(&dest));

return 0;

You might also like