Sybsc cs 3rd sem Journal
Q.1. Implement a list library for a doubly linked list of integers with the create, display
operations. Write a menu driven program to call these operations.
Ans:
#include <stdio.h>
#include <stdlib.h>
// Node structure for a doubly linked list
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
// Doubly linked list structure
struct DoublyLinkedList {
struct Node* head;
};
// Function to create a new node in the doubly linked list
void create(struct DoublyLinkedList* list, int data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = data;
new_node->prev = NULL;
new_node->next = NULL;
if (list->head == NULL) {
list->head = new_node;
} else {
struct Node* current = list->head;
while (current->next != NULL) {
current = current->next;
current->next = new_node;
new_node->prev = current;
printf("Node inserted successfully.\n");
// Function to display the doubly linked list
void display(struct DoublyLinkedList* list) {
struct Node* current = list->head;
printf("Doubly Linked List: ");
while (current != NULL) {
printf("%d <-> ", current->data);
current = current->next;
}
printf("NULL\n");
int main() {
struct DoublyLinkedList dll;
dll.head = NULL;
int choice, data;
while (1) {
printf("\nDoubly Linked List Operations:\n");
printf("1. Create\n");
printf("2. Display\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
create(&dll, data);
break;
case 2:
display(&dll);
break;
case 3:
printf("Exiting the program.\n");
exit(0);
default:
printf("Invalid choice. Please enter a valid option.\n");
return 0;
===============Output
Q. 2. Write a program that sorts the elements of linked list using any of sorting technique.
Ans:
#include <stdio.h>
#include <stdlib.h>
// Node structure for a linked list
struct Node {
int data;
struct Node* next;
};
// Function to create a new node in the linked list
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to display the linked list
void display(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
printf("NULL\n");
// Function to perform bubble sort on the linked list
void bubbleSort(struct Node* head) {
int swapped, temp;
struct Node* current;
struct Node* lastSorted = NULL;
// If the list is empty or has only one element, it is already sorted
if (head == NULL || head->next == NULL) {
return;
do {
swapped = 0;
current = head;
while (current->next != lastSorted) {
if (current->data > current->next->data) {
// Swap data of the current and next nodes
temp = current->data;
current->data = current->next->data;
current->next->data = temp;
swapped = 1;
current = current->next;
// Mark the last sorted node
lastSorted = current;
} while (swapped);
int main() {
// Creating a sample linked list
struct Node* head = createNode(5);
head->next = createNode(2);
head->next->next = createNode(9);
head->next->next->next = createNode(1);
head->next->next->next->next = createNode(7);
printf("Original Linked List: ");
display(head);
// Sorting the linked list using bubble sort
bubbleSort(head);
printf("Linked List after Bubble Sort: ");
display(head);
// Freeing the allocated memory
struct Node* current = head;
while (current != NULL) {
struct Node* nextNode = current->next;
free(current);
current = nextNode;
return 0;
Output
Q-1. Implement a list library for a singly linked list of integer with the operations create,
display. Write a menu driven program to call these operations.
Ans:
#include <stdio.h>
#include <stdlib.h>
// Node structure for a singly linked list
struct Node {
int data;
struct Node* next;
};
// Singly linked list structure
struct SinglyLinkedList {
struct Node* head;
};
// Function to create a new node in the singly linked listvoid
create(struct SinglyLinkedList* list, int data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = data;
new_node->next = NULL;
if (list->head == NULL) { list-
>head = new_node;
} else {
struct Node* current = list->head;
while (current->next != NULL) {
current = current->next;
}
current->next = new_node;
printf("Node inserted successfully.\n");
// Function to display the singly linked list
void display(struct SinglyLinkedList* list) {
struct Node* current = list->head;
printf("Singly Linked List: ");
while (current != NULL) { printf("%d
-> ", current->data);current =
current->next;
}
printf("NULL\n");
int main() {
struct SinglyLinkedList sll;
sll.head = NULL;
int choice, data;
while (1) {
printf("\nSingly Linked List Operations:\n");
printf("1. Create\n");
printf("2. Display\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data); create(&sll,
data);
break;
case 2:
display(&sll);
break;
case 3:
printf("Exiting the program.\n");
exit(0);
default:
printf("Invalid choice. Please enter a valid option.\n");
return 0;
==============output
Q. 2. Write a program that copies the contents of one stack into another. Use stack library to
perform basic stack operations. The order of two stacks must be identical.(Hint: Use a temporary
stack to preserve the order).
Ans:
#include <stdio.h>
#include <stdlib.h>
// Define the maximum size of the stack
#define MAX_SIZE 100
// Stack structure
struct Stack {
int arr[MAX_SIZE];
int top;
};
// Function to initialize a stack void
initialize(struct Stack* stack) {
stack->top = -1;
// Function to check if the stack is emptyint
isEmpty(struct Stack* stack) {
return stack->top == -1;
// Function to check if the stack is fullint
isFull(struct Stack* stack) {
return stack->top == MAX_SIZE - 1;
// Function to push an element onto the stack
void push(struct Stack* stack, int value) {
if (isFull(stack)) {
printf("Stack overflow. Cannot push element %d.\n", value);
return;
}
stack->arr[++stack->top] = value;
// Function to pop an element from the stackint
pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack underflow. Cannot pop element.\n");
exit(1); // Exit the program if stack underflow occurs
}
return stack->arr[stack->top--];
}
// Function to copy the contents of one stack into another while preserving ordervoid
copyStack(struct Stack* source, struct Stack* destination) {
struct Stack tempStack;
initialize(&tempStack);
// Copy elements from the source stack to the destination stack
while (!isEmpty(source)) {
int element = pop(source);
push(&tempStack, element);
}
while (!isEmpty(&tempStack)) {
int element = pop(&tempStack);
push(destination, element);
push(source, element); // Restore the source stack
// Function to display the elements of a stack
void display(struct Stack* stack) {
printf("Stack: ");
for (int i = 0; i <= stack->top; i++) {
printf("%d ", stack->arr[i]);
}
printf("\n");
}
int main() {
struct Stack stack1, stack2;
initialize(&stack1);
initialize(&stack2);
// Push some elements onto the original stack (stack1)
push(&stack1, 1);
push(&stack1, 2);
push(&stack1, 3);
push(&stack1, 4);
// Display the original stack
printf("Original ");
display(&stack1);
// Copy the contents of stack1 to stack2
copyStack(&stack1, &stack2);
// Display the copied stack (stack2)
printf("Copied ");
display(&stack2);
return 0;
==============output
Q. 1. Sort a random array of n integers (accept the value of n from user) in ascending
order by using insertion sort algorithm.
Ans:
#include <stdio.h> #include
<stdlib.h>#include <time.h>
// Function to perform insertion sort on an arrayvoid insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {key = arr[i];
j = i - 1;
// Move elements of arr[0..i-1] that are greaterthan key
// to one position ahead of their current positionwhile (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];j = j - 1;
}
arr[j + 1] = key;
// Function to display an array void displayArray(int arr[], int n)
{
printf("Sorted Array: ");for (int i = 0; i < n;
i++) { printf("%d ", arr[i]);
printf("\n");
int main() {int n;
// Accept the value of n from the user printf("Enter the number of elements in the array:
");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid input. Please enter a positiveinteger for the number of
elements.\n");
return 1; // Exit with an error code
int arr[n];
// Seed the random number generator with thecurrent time
srand(time(NULL));
// Fill the array with random integersfor (int i = 0; i < n; i++) {
arr[i] = rand() % 100; // Generate randomintegers between 0 and 99
// Display the original array
printf("Original Array: ");for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
printf("\n");
// Perform insertion sortinsertionSort(arr, n);
// Display the sorted arraydisplayArray(arr, n);
return 0;
Output
Q. 2. Write a C program to evaluate postfix expression.
Ans:
#include <stdio.h> #include
<stdlib.h>#include <ctype.h>
// Stack structurestruct Stack {
int top;
int capacity;int* array;
};
// Function to create a stack
struct Stack* createStack(int capacity) {struct Stack* stack = (struct
Stack*)malloc(sizeof(struct Stack));
stack->top = -1;
stack->capacity = capacity;
stack->array = (int*)malloc(stack->capacity *sizeof(int));
return stack;
// Function to check if the stack is emptyint isEmpty(struct Stack* stack) {
return stack->top == -1;
// Function to push an element onto the stackvoid push(struct Stack* stack, int value)
{
stack->array[++stack->top] = value;
}
// Function to pop an element from the stackint pop(struct Stack* stack) {
return stack->array[stack->top--];
// Function to evaluate a postfix expressionint evaluatePostfix(char* expression)
{
struct Stack* stack = createStack(strlen(expression));
int i;
// Traverse the expression for (i = 0; expression[i]; ++i)
{
// If the current character is a digit, push it ontothe stack
if (isdigit(expression[i])) { push(stack, expression[i] - '0');
} else {
// If the current character is an operator, poptwo operands from the stack
int operand2 = pop(stack);
int operand1 = pop(stack);
// Perform the operation and push the resultback onto the stack
switch (expression[i]) {case '+':
push(stack, operand1 + operand2);break;
case '-':
push(stack, operand1 - operand2);break;
case '*':
push(stack, operand1 * operand2);break;
case '/':
push(stack, operand1 / operand2);break;
}
// The final result is on the top of the stackint result = pop(stack);
// Clean up free(stack->array);
free(stack);
return result;
int main() {
char expression[100];
// Accept the postfix expression from the userprintf("Enter a postfix expression: ");
scanf("%s", expression);
// Evaluate the postfix expression
int result = evaluatePostfix(expression);
// Display the result printf("Result: %d\n", result);
return 0;
=============output
Q. 1. Read the ‘n’ numbers from user and sort using bubble sort.
Ans:
#include <stdio.h>
// Function to perform bubble sort on an arrayvoid bubbleSort(int arr[], int n)
{
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1] if they are in the wrong
order
}
}
= arr[j]; arr[j] = arr[j + 1];arr[j +
1] = temp;
t
e
m
p
// Function to display an array
void displayArray(int arr[], int n) {printf("Sorted Array:
");
for (int i = 0; i < n; i++) {printf("%d ",
arr[i]);
}
printf("\n");
int main() {int n;
// Accept the value of n from the user printf("Enter the number of
elements: ");scanf("%d", &n);
if (n <= 0) {
printf("Invalid input. Please enter a positive integer forthe number of elements.\n");
return 1; // Exit with an error code
int arr[n];
// Read 'n' numbers from the user printf("Enter %d
numbers:\n", n);for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
// Display the original arrayprintf("Original
Array: "); for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
printf("\n");
// Perform bubble sortbubbleSort(arr,
n);
// Display the sorted arraydisplayArray(arr,
n);
return 0;
}
Output
Q.2. Write a program to reverse the elements of a queue using queue library. Implement
basic queue operations init, enqueue, dequeue.
Ans:
#include <stdio.h> #include
<stdlib.h>
// Queue structure struct
Queue {
int front, rear, size; unsigned
capacity; int* array;
};
// Function to initialize a queue
struct Queue* init(unsigned capacity) {struct Queue* queue =
(struct
Queue*)malloc(sizeof(struct Queue));queue->capacity =
capacity;
queue->front = queue->size = 0;
queue->rear = capacity - 1; // Important for circular queuequeue->array = (int*)malloc(queue-
>capacity *
sizeof(int)); return queue;
}
// Function to check if the queue is fullint isFull(struct Queue*
queue) {
return (queue->size == queue->capacity);
// Function to check if the queue is emptyint isEmpty(struct Queue*
queue) {
return (queue->size == 0);
}
// Function to enqueue an element to the queuevoid enqueue(struct Queue* queue,
int item) {
if (isFull(queue)) {
printf("Queue is full. Cannot enqueue %d.\n", item);return;
}
queue->rear = (queue->rear + 1) % queue->capacity;queue->array[queue->rear] =
item;
queue->size += 1;
// Function to dequeue an element from the queueint dequeue(struct Queue* queue) {
if (isEmpty(queue)) {
printf("Queue is empty. Cannot dequeue.\n"); exit(1); // Exit the program if queue
underflow occurs
}
int item = queue->array[queue->front];
queue->front = (queue->front + 1) % queue->capacity;queue->size -= 1;
return item;
}
// Function to reverse the elements of a queuevoid reverseQueue(struct Queue*
queue) {
if (isEmpty(queue)) {
printf("Queue is empty. Cannot reverse.\n");return;
int* tempArray = (int*)malloc(queue->size * sizeof(int));int i = 0;
// Dequeue elements and store them in a temporaryarray
while (!isEmpty(queue)) { tempArray[i] =
dequeue(queue);i++;
}
// Enqueue elements back to the queue in reverse orderfor (int j = i - 1; j >= 0; j--) {
enqueue(queue, tempArray[j]);
}
free(tempArray);
// Function to display the elements of a queuevoid displayQueue(struct
Queue* queue) {
if (isEmpty(queue)) { printf("Queue is empty.\n");
return;
}
printf("Queue: "); int i = queue-
>front;do {
printf("%d ", queue->array[i]);i = (i + 1) % queue-
>capacity;
} while (i != (queue->rear + 1) % queue->capacity);printf("\n");
}
int main() {
unsigned capacity;
printf("Enter the capacity of the queue: ");scanf("%u", &capacity);
struct Queue* myQueue = init(capacity);
// Enqueue elements to the queueenqueue(myQueue, 1);
enqueue(myQueue, 2);
enqueue(myQueue, 3);
enqueue(myQueue, 4);
enqueue(myQueue, 5);
// Display the original queueprintf("Original ");
displayQueue(myQueue);
// Reverse the elements of the queue
reverseQueue(myQueue);
// Display the reversed queueprintf("Reversed ");
displayQueue(myQueue);
// Free allocated memoryfree(myQueue-
>array); free(myQueue);
return 0;
Output