SYBSc Computer Slips
SYBSc Computer Slips
Slip1
Q1.
Implement a list library (doublylist.h) for a doubly linked list of integers with the create, display operations. Write a menu driven
program to call these operations. [10]
#include
<stdio.h>
#include
<stdlib.h>
int data;
} Node;
Node* head;
} DoublyLinkedList;
DoublyLinkedList* createList() {
list->head = NULL;
return list;
newNode->data = data;
newNode->next = NULL;
if (list->head == NULL) {
newNode->prev = NULL;
list->head = newNode;
return;
last->next = newNode;
newNode->prev = last;
if (current == NULL) {
printf("List is empty.\n");
return;
current = current->next;
printf("\n");
int main() {
while (1) {
printf("Menu:\n");
printf("3. Exit\n");
switch (choice) {
case 1:
appendNode(list, data);
break;
case 2:
displayList(list);
break;
case 3:
free(list);
exit(0);
default:
return 0;
Q2. Write a program that sorts the elements of linked list using any of sorting technique.
-> #include <stdio.h>
#include <stdlib.h>
// Node structure for the linked list
typedef struct Node {
int data;
struct Node* next;
} Node;
// Function to create a new node
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to append a node to the list
void appendNode(Node** head, int data) {
Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
Node* last = *head;
while (last->next != NULL) {
last = last->next;
}
last->next = newNode;
}
// Function to display the list
void displayList(Node* head) {
Node* current = head;
if (current == NULL) {
printf("List is empty.\n");
return;
}
printf("Linked List: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// Function to sort the linked list using Bubble Sort
void bubbleSort(Node* head) {
if (head == NULL) return;
int swapped;
Node *ptr1;
Node *lptr = NULL;
do {
swapped = 0;
ptr1 = head;
while (ptr1->next != lptr) {
if (ptr1->data > ptr1->next->data) {
// Swap data
int temp = ptr1->data;
ptr1->data = ptr1->next->data;
ptr1->next->data = temp;
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
} while (swapped);
}
// Main function with menu-driven interface
int main() {
Node* head = NULL;
int choice, data;
while (1) {
printf("Menu:\n");
printf("1. Append Node\n");
printf("2. Display List\n");
printf("3. Sort List\n");
printf("4. Exit\n");
printf("Enter your choice:
"); scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter integer to append: ");
scanf("%d", &data);
appendNode(&head, data);
break;
case 2:
displayList(head);
break;
case 3:
bubbleSort(head);
printf("List sorted.\n");
break;
case 4:
// Free the list before exiting
while (head != NULL) {
Node* temp = head;
head = head->next;
free(temp);
}
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Slip 2
Q1.Implement a list library (singlylist.h) for a singly linked list of integer with the operations create, display. Write a menu driven
program to call these operations [10]
#include
<stdio.h>
#include
<stdlib.h>
while (1) {
printf("Menu:\n");
printf("1. Append Node\n");
printf("2. Display List\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter integer to append: ");
scanf("%d", &data);
appendNode(&head, data);
break;
case 2:
displayList(head);
break;
case 3:
// Free the list before exiting
while (head != NULL) {
Node* temp = head;
head = head->next;
free(temp);
}
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Q2. 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).
#include
<stdio.h>
#include
<stdlib.h>
// Function prototypes
void initStack(Stack *s);
int isFull(Stack *s);
int isEmpty(Stack *s);
void push(Stack *s, int value);
int pop(Stack *s);
void copyStack(Stack *source, Stack *destination);
int main() {
Stack stack1, stack2;
initStack(&stack1);
initStack(&stack2);
return 0;
}
// Stack functions
void initStack(Stack *s) {
s->top = -1;
}
Slip3
Sort a random array of n integers (accept the value of n from user) in ascending order by using insertion sort algorithm.
[10]
#include
<stdio.h>
#include
<stdlib.h>
#include
<time.h>
int main() {
int n;
return 0;
}
typedef struct {
int data[MAX];
int top;
} Stack;
// Function prototypes
void initStack(Stack *s);
int isFull(Stack *s);
int isEmpty(Stack *s);
void push(Stack *s, int value);
int pop(Stack *s);
int evaluatePostfix(const char *expression);
int main() {
char expression[MAX];
return 0;
}
// Stack functions
void initStack(Stack *s) {
s->top = -1;
}
Slip4
Read
the ‘n’ numbers from user and sort using bubble sort. [10]
int main() {
int n;
return 0;
}
Q2. Write a program to reverse the elements of a queue using queue library.
Implement basic queue operations init, enqueue, dequeue. [20]
#include
<stdio.h>
#include
<stdlib.h>
typedef struct {
int data[MAX];
int front;
int rear;
} Queue;
// Function prototypes
void initQueue(Queue *q);
int isFull(Queue *q);
int isEmpty(Queue *q);
void enqueue(Queue *q, int value);
int dequeue(Queue *q);
void reverseQueue(Queue *q);
void printQueue(Queue *q);
int main() {
Queue q;
initQueue(&q);
// Enqueue some values
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
enqueue(&q, 40);
enqueue(&q, 50);
printf("Original queue:\n");
printQueue(&q);
printf("Reversed queue:\n");
printQueue(&q);
return 0;
}
// Queue functions
void initQueue(Queue *q) {
q->front = 0;
q->rear = -1;
}
Slip 5
1) Create a random array of n integers. Accept a value x from user and use linear search algorithm to check whether the number is
present in the array or not and output the position if the number is
present. [10]
#include
<stdio.h>
#include
<stdlib.h>
#include
<time.h>
int main() {
int n, x;
return 0;
}
Q2. Implement a priority queue library (PriorityQ.h) of integers using a static implementation of the queue and implement the
below two operations.
typedef struct {
int value; // The value stored in the queue
int priority; // The priority of the value
} Element;
typedef struct {
Element elements[MAX];
int size; // Current number of elements in the priority queue
} PriorityQueue;
// Function prototypes
void initPriorityQueue(PriorityQueue *pq);
int isFull(PriorityQueue *pq);
int isEmpty(PriorityQueue *pq);
void enqueue(PriorityQueue *pq, int value, int priority);
int dequeue(PriorityQueue *pq);
void printPriorityQueue(PriorityQueue *pq);
int main() {
PriorityQueue pq;
initPriorityQueue(&pq);
return 0;
}
Slip6
Sort a random array of n integers (accept the value of n from user) in ascending order by using selection sort algorithm.
[10]
#include
<stdio.h>
#include
<stdlib.h>
#include
<time.h>
int main() {
int n;
return 0;
}
// Queue structure
typedef struct Queue {
Node *front;
Node *rear;
} Queue;
// Function prototypes
void initQueue(Queue *q);
int isEmpty(Queue *q);
void enqueue(Queue *q, int value);
int dequeue(Queue *q);
int peek(Queue *q);
void freeQueue(Queue *q);
int main() {
Queue q;
initQueue(&q);
// Dequeue elements
printf("Dequeued: %d\n", dequeue(&q));
printf("Front element: %d\n", peek(&q));
return 0;
}
if (isEmpty(q)) {
q->front = newNode; // If the queue is empty
q->rear = newNode; // Set front and rear to the new node
} else {
q->rear->next = newNode; // Link the new node to the end of the queue
q->rear = newNode; // Update the rear to the new node
}
}
Slip 7
Sort a random array of n integers (accept the value of n from user) in ascending order by using quick sort algorithm. [10]
-> #include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function prototypes
void quickSort(int arr[], int low, int high);
int partition(int arr[], int low, int high);
void printArray(int arr[], int n);
int main() {
int n;
return 0;
}
// QuickSort Function
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high); // Partitioning index
quickSort(arr, low, pi - 1); // Sort elements before partition
quickSort(arr, pi + 1, high); // Sort elements after partition
}
}
// Partition function
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // Choosing the last element as pivot
int i = (low - 1); // Index of the smaller element
Q2. Write a program that checks whether a string of characters is palindrome or not. The function should use a stack library
(cststack.h) of stack of characters using a static implementation of the stack. [20]
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<ctype.h>
// Function prototypes
void initStack(Stack *s);
int isFull(Stack *s);
int isEmpty(Stack *s);
void push(Stack *s, char item);
char pop(Stack *s);
char peek(Stack *s);
int isPalindrome(const char *str);
int main() {
char str[MAX];
return 0;
}
// Compare characters
for (int i = 0; i < length; i++) {
if (isalnum(str[i])) {
char topChar = pop(&s); // Pop from stack
if (tolower(str[i]) != topChar) {
return 0; // Not a palindrome
}
}
}
return 1; // It is a palindrome
}
Slip 8
1 )Implement a list library (singlylist.h) for a singly linked list of integer With the operations create, delete specific element and
display. Write a menu driven program to call these operations [10]
#include
<stdio.h>
#include
<stdlib.h>
// Function prototypes
Node* createList();
void displayList(Node* head);
Node* deleteElement(Node* head, int value);
void freeList(Node* head);
int main() {
Node* head = NULL;
int choice, value;
do {
printf("\nMenu:\n");
printf("1. Create List\n");
printf("2. Display List\n");
printf("3. Delete Specific Element\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
head = createList();
printf("List created successfully.\n");
break;
case 2:
printf("List contents: ");
displayList(head);
break;
case 3:
printf("Enter the value to delete: ");
scanf("%d", &value);
head = deleteElement(head, value);
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 4);
do {
printf("Enter an integer to add to the list: ");
scanf("%d", &value);
if (head == NULL) {
head = newNode; // First node
tail = newNode;
} else {
tail->next = newNode; // Link new node
tail = newNode; // Update tail
}
return head;
}
Q2. Write a C program to check whether the contents of two stacks are identical. Use stack library to perform basic stack operations.
Neither
stack should be changed. [20]
#include
<stdio.h>
#include
<stdlib.h>
// Stack structure
typedef struct {
int items[MAX];
int top;
} Stack;
// Function prototypes
void initStack(Stack *s);
int isFull(Stack *s);
int isEmpty(Stack *s);
void push(Stack *s, int item);
int pop(Stack *s);
int peek(Stack *s);
int areStacksIdentical(Stack *s1, Stack *s2);
int main() {
Stack stack1, stack2;
// Initialize stacks
initStack(&stack1);
initStack(&stack2);
return 0;
}
// Compare elements
for (int i = 0; i <= s1->top; i++) {
int val1 = s1->items[i];
int val2 = s2->items[i];
-> #include
<stdio.h> #include
<stdlib.h>
#include <ctype.h>
// Stack structure
typedef struct {
char items[MAX];
int top;
} Stack;
// Function prototypes
int main() {
char infix[MAX];
char postfix[MAX];
infixToPostfix(infix, postfix);
return 0;
}
// Initialize the stack
s->top = -1;
if (!isFull(s)) {
s->items[++s->top] = item;
}
// Pop an item from the stack
if (!isEmpty(s)) {
return s->items[s->top--];
if (!isEmpty(s)) {
return s->items[s->top];
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
default:
return 0;
Stack stack;
initStack(&stack);
} else { // Operator
while (!isEmpty(&stack)) {
postfix[j++] = pop(&stack);
} [10]
Q2 Read the data from the ‘employee.txt’ file and sort on age using Counting sort or Quick sort and write the sorted data to another
file 'sortedemponage.txt'.
#include <stdio.h>
-> #include
<stdlib.h>
#include <string.h>
#define NAME_LENGTH 50
// Employee structure
typedef struct {
char name[NAME_LENGTH];
int age;
} Employee;
// Function prototypes
int main() {
Employee employees[MAX_EMPLOYEES];
int count = 0;
// Read employees from the file
if (file == NULL) {
exit(EXIT_FAILURE);
fclose(file);
exit(EXIT_FAILURE);
fclose(file);
}
// Quick Sort function
quickSort(employees, pi + 1, high);
i++;
swap(&employees[i], &employees[j]);
return (i + 1);
}
// Swap function
*a = *b;
*b = temp;
} [20]
Slip 10
Implement a linear queue library (st_queue.h) of integers using a static implementation of the queue and implementing the init(Q),
add(Q) and peek(Q) operations. Write a program that includes queue library and calls different
queue operations [10]
#include
<stdio.h>
#include
<stdlib.h>
#define MAX 100
// Queue structure
typedef struct {
int items[MAX];
int front;
int rear;
} Queue;
// Function prototypes
int main() {
Queue q;
initQueue(&q);
add(&q, 20);
add(&q, 30);
displayQueue(&q);
add(&q, 50);
displayQueue(&q);
return 0;
q->rear = -1;
if (isFull(q)) {
return;
if (isEmpty(q)) {
if (isEmpty(q)) {
if (isEmpty(q)) {
printf("Queue is empty.\n");
return;
printf("\n");
Q2. Read the data from the file “employee.txt” and sort on names in alphabetical order (use strcmp) using bubble sort or selection
sort. [20]
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#define NAME_LENGTH 50
// Employee structure
typedef struct {
char name[NAME_LENGTH];
int age;
} Employee;
// Function prototypes
int main() {
Employee employees[MAX_EMPLOYEES];
int count = 0;
bubbleSort(employees, count);
if (file == NULL) {
exit(EXIT_FAILURE);
(*count)++;
if (*count >= MAX_EMPLOYEES) {
fclose(file);
if (file == NULL) {
fclose(file);
employees[j + 1] = temp;
Slip 11
Accept n values in array from user. Accept a value x from user and use sentinel linear search algorithm to check whether the number is
present in the array or not and output the position if the number is present. [10]
#include <stdio.h>
int main() {
int n, x;
scanf("%d", &n);
int arr[n];
// Accept values in the array
scanf("%d", &arr[i]);
scanf("%d", &x);
if (position != -1) {
} else {
return 0;
}
// Sentinel Linear Search function
int i = 0;
while (arr[i] != x) {
i++;
if (i < n - 1 || arr[n - 1] == x) {
} else {
Q2. Implement a priority queue library (PriorityQ.h) of integers using a static implementation of the queue and implementing the below
two operations. Write a driver program that includes queue library and calls different queue operations.
#include <stdlib.h>
// Function prototypes
void initQueue(PriorityQueue *pq);
int isFull(PriorityQueue *pq);
int isEmpty(PriorityQueue *pq);
void add(PriorityQueue *pq, int data, int priority);
Element delete(PriorityQueue *pq);
void displayQueue(PriorityQueue *pq);
int main() {
PriorityQueue pq;
initQueue(&pq);
return 0;
}
return item;
}
Slip12
Read the data from file 'cities.txt' containing names of cities and their STD codes. Accept a name of the city from user and use linear
search algorithm to check whether the name is present in the file and output the STD code,
otherwise output “city not in the list”. [10]
#include <stdlib.h>
#include <string.h>
#define MAX_CITIES 100
#define NAME_LENGTH 50
#define CODE_LENGTH 10
typedef struct {
char name[NAME_LENGTH];
char std_code[CODE_LENGTH];
} City;
// Function prototypes
int main() {
City cities[MAX_CITIES];
int count = 0;
char city_name[NAME_LENGTH];
if (std_code) {
} else {
}
return 0;
if (file == NULL) {
exit(EXIT_FAILURE);
}
while (fscanf(file, "%49s %9s", cities[*count].name, cities[*count].std_code) != EOF) {
(*count)++;
fclose(file);
if (strcmp(cities[i].name, city_name) == 0) {
Q2. Implement a circular queue library (cir_queue.h) of integers using a dynamic (circular linked list) implementation of the queue and
implementing init(Q), AddQueue(Q) and DeleteQueue(Q) operations. Write a menu driven program that includes queue library and
calls different queue operations. [20]
#include
<stdio.h>
#include
<stdlib.h>
// Node structure for circular linked list
int data;
} Node;
typedef struct {
Node* front;
Node* rear;
} CircularQueue;
// Function prototypes
int main() {
CircularQueue q;
initQueue(&q);
int choice, value;
while (1) {
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
addQueue(&q, value);
break;
case 2:
if (!isEmpty(&q)) {
} else {
}
break;
case 3:
displayQueue(&q);
break;
case 4:
freeQueue(&q);
exit(0);
default:
}
return 0;
void initQueue(CircularQueue* q) {
q->front = NULL;
q->rear = NULL;
int isEmpty(CircularQueue* q) {
newNode->data = data;
newNode->next = NULL;
if (isEmpty(q)) {
q->front = newNode;
q->rear = newNode;
q->rear->next = newNode;
int deleteQueue(CircularQueue* q) {
if (isEmpty(q)) {
if (q->front == q->rear) {
q->front = NULL;
q->rear = NULL;
} else {
q->front = q->front->next;
q->rear->next = q->front; // Update rear to point to new front
free(temp);
return data;
void displayQueue(CircularQueue* q) {
if (isEmpty(q)) {
printf("Queue is empty.\n");
return;
}
do {
current = current->next;
printf("\n");
while (!isEmpty(q)) {
deleteQueue(q);
Slip13
Implement a stack library (ststack.h) of integers using a static implementation of the stack and implementing the operations like init(S),
S=push(S) and S=pop(S). Write a driver program that includes stack library
and calls different stack operations. [10]
#include
<stdio.h>
#include
<stdlib.h>
typedef struct {
int items[MAX];
int top;
} Stack;
// Function prototypes
int main() {
Stack s;
initStack(&s);
while (1) {
printf("\nStack Menu:\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
push(&s, value);
break;
case 2:
if (!isEmpty(&s)) {
} else {
break;
case 3:
displayStack(&s);
break;
case 4:
exit(0);
default:
return 0;
void initStack(Stack* s) {
int isFull(Stack* s) {
int isEmpty(Stack* s) {
}
// Push an element onto the stack
if (isFull(s)) {
return;
int pop(Stack* s) {
if (isEmpty(s)) {
void displayStack(Stack* s) {
if (isEmpty(s)) {
printf("Stack is empty.\n");
return;
}
printf("\n");
Q2. Write a program that sorts the elements of linked list using bubble sort
technique. [20]
#include
<stdio.h>
#include
<stdlib.h>
// Node structure for linked list
int data;
} Node;
// Function prototypes
int n, value;
scanf("%d", &n);
insertEnd(&head, value);
displayList(head);
bubbleSort(&head);
displayList(head);
// Free the allocated memory
freeList(head);
return 0;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
if (*head == NULL) {
*head = newNode;
return;
current->next = newNode;
int swapped;
Node* ptr1;
swapped = 0;
ptr1 = *head;
// Swap data
ptr1->data = ptr1->next->data;
ptr1->next->data = temp;
swapped = 1;
}
ptr1 = ptr1->next;
lptr = ptr1;
} while (swapped);
printf("\n");
Node* next;
next = current->next;
free(current);
current = next;
}
Slip14
Q1. Create a random array of n integers. Accept a value x from user and use linear search algorithm to check whether the number is
present in the array or not and output the position if the number is present. [10]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function prototypes
int main() {
int n, x;
srand(time(0));
// Accept the size of the array from the user
scanf("%d", &n);
int arr[n];
generateRandomArray(arr, n);
}
printf("\n");
scanf("%d", &x);
if (position != -1) {
} else {
}
return 0;
if (arr[i] == x) {
return i; // Return the index if found
Q2. A doubly ended queue allows additions and deletions from both the ends that is front and rear. Initially additions from the front
will not be possible. To avoid this situation, the array can be treated as if it were circular. Implement a queue library
(dstqueue.h) of integers using a static implementation of the circular queue and implementing the following
operations. [20] a. isFull(Q)
b. addFront(Q)
c. getRear(Q)
d. deleteRear(Q)
#include <stdlib.h>
int items[MAX];
int front;
int rear;
} Deque;
// Function prototypes
int main() {
Deque dq;
initDeque(&dq);
while (1) {
printf("\nDeque Menu:\n");
printf("5. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
if (isFull(&dq)) {
} else {
scanf("%d", &value);
addRear(&dq, value);
break;
case 2:
if (dq.rear == dq.front) {
} else {
break;
case 3:
if (dq.rear == dq.front) {
} else {
deleteRear(&dq);
break;
case 4:
displayDeque(&dq);
break;
case 5:
exit(0);
default:
return 0;
dq->front = 0;
dq->rear = 0;
dq->items[dq->rear] = value;
}
// Delete an element from the rear of the deque
if (dq->rear == dq->front) {
printf("Deque is empty.\n");
return;
int i = dq->front;
while (i != dq->rear) {
printf("%d ", dq->items[i]);
i = (i + 1) % MAX;
printf("\n");
Slip15
Q1. Sort a random array of n integers (accept the value of n from user) in ascending order by using selection sort algorithm.
[10]
#include
<stdio.h>
#include
<stdlib.h>
#include
<time.h>
// Function prototypes
int n;
srand(time(0));
scanf("%d", &n);
int arr[n];
displayArray(arr, n);
selectionSort(arr, n);
displayArray(arr, n);
return 0;
int minIndex = i;
minIndex = j;
}
// Swap the found minimum element with the first element
if (minIndex != i) {
arr[i] = arr[minIndex];
arr[minIndex] = temp;
printf("\n");
}
#include <stdio.h>
#include <stdlib.h>
// Queue structure
typedef struct {
Node* front;
Node* rear;
} Queue;
// Function prototypes
void init(Queue* q);
void addQueue(Queue* q, int x);
int peek(Queue* q);
void freeQueue(Queue* q);
// Main function to demonstrate the queue operations
int main() {
Queue q;
init(&q);
while (1) {
printf("\nQueue Menu:\n");
printf("1. Add to Queue\n");
printf("2. Peek\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to add to queue: ");
scanf("%d", &value);
addQueue(&q, value);
printf("Added %d to the queue.\n", value);
break;
case 2:
if (q.front == NULL) {
printf("Queue is empty.\n");
} else {
printf("Front element: %d\n", peek(&q));
}
break;
case 3:
freeQueue(&q);
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
if (q->rear == NULL) {
// Queue is empty
q->front = newNode;
q->rear = newNode;
newNode->next = newNode; // Point to itself
} else {
// Add new node to the end and update rear
q->rear->next = newNode;
q->rear = newNode;
q->rear->next = q->front; // Make it circular
}
}
Slip 16
Q1) Sort a random array of n integers (accept the value of n from user) in ascending order by using Counting sort algorithm.
[10]
-> #include
<stdio.h> #include
<stdlib.h>
#include <time.h>
// Function prototypes
int main() {
int n;
srand(time(0));
scanf("%d", &n);
int arr[n];
generateRandomArray(arr, n);
printf("Generated Array: ");
displayArray(arr, n);
countingSort(arr, n);
displayArray(arr, n);
return 0;
int count[100] = {0}; // Count array for numbers in range [0, 99]
count[arr[i]]++;
output[j++] = i;
count[i]--;
arr[i] = output[i];
printf("\n");
Q2. A postfix expression of the form ab+cd-*ab/ is to be evaluated after accepting the values of a, b, c and d. The value should be
accepted only once and the same value is to be used for repeated occurrence of same symbol in the expression. Formulate the
problem and write a C program to solve the problem
by using stack [20]
#include
<stdio.h>
#include
<stdlib.h>
#include
<ctype.h>
// Stack structure
typedef struct {
int top;
float items[MAX];
} Stack;
// Function prototypes
int main() {
scanf("%f", &a);
scanf("%f", &b);
scanf("%f", &c);
scanf("%f", &d);
printf("The result of the postfix expression '%s' is: %.2f\n", expression, result);
return 0;
void initStack(Stack* s) {
s->top = -1;
int isFull(Stack* s) {
if (!isFull(s)) {
s->items[++(s->top)] = value;
} else {
printf("Stack is full!\n");
float pop(Stack* s) {
if (!isEmpty(s)) {
return s->items[(s->top)--];
} else {
printf("Stack is empty!\n");
Stack stack;
initStack(&stack);
if (isdigit(*p) || isalpha(*p)) {
// Push the corresponding value onto the stack
float value;
switch (*p) {
push(&stack, value);
} else {
switch (*p) {
case '+': push(&stack, operand1 + operand2); break;
case '/':
if (operand2 != 0) {
} else {
break;
default:
}
}
Slip17
Q1 Implement a list library (singlylist.h) for a singly linked list. Create a linked list, reverse it and display reversed linked list. [10]
#include <stdlib.h>
int data;
} Node;
// Function prototypes
int main() {
int n, data;
scanf("%d", &n);
// Create the linked list
scanf("%d", &data);
insertEnd(&head, data);
displayList(head);
reverseList(&head);
freeList(head);
return 0;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert a node at the end of the list
if (*head == NULL) {
*head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
}
// Function to reverse the linked list
current = next;
}
// Function to display the linked list
temp = temp->next;
printf("NULL\n");
Node* temp;
temp = head;
head = head->next;
free(temp);
Q2 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). [20]
#include
<stdio.h>
#include
<stdlib.h>
// Stack structure
typedef struct {
int top;
int items[MAX];
} Stack;
// Function prototypes
int main() {
initStack(&destination);
push(&source, 10);
push(&source, 20);
push(&source, 30);
push(&source, 40);
push(&source, 50);
copyStack(&source, &destination);
printf("%d\n", pop(&destination));
return 0;
void initStack(Stack* s) {
s->top = -1;
int isFull(Stack* s) {
int isEmpty(Stack* s) {
if (!isFull(s)) {
s->items[++(s->top)] = value;
} else {
printf("Stack is full!\n");
}
// Function to pop a value from the stack
int pop(Stack* s) {
if (!isEmpty(s)) {
return s->items[(s->top)--];
} else {
printf("Stack is empty!\n");
int peek(Stack* s) {
if (!isEmpty(s)) {
return s->items[s->top];
} else {
printf("Stack is empty!\n");
Stack temp;
initStack(&temp);
while (!isEmpty(source)) {
push(&temp, pop(source));
}
// Move elements from temp to destination and back to source
while (!isEmpty(&temp)) {
push(destination, value);
Slip18
Sort a random array of n integers (accept the value of n from user) in ascending order by using Selection sort algorithm [10]
#include <stdlib.h>
#include <time.h>
// Function prototypes
void selectionSort(int arr[], int n);
int main() {
int n;
srand(time(0));
scanf("%d", &n);
printf("\n");
selectionSort(arr, n);
printArray(arr, n);
return 0;
int minIndex = i;
minIndex = j;
}
}
if (minIndex != i) {
arr[i] = arr[minIndex];
arr[minIndex] = temp;
printf("\n");
Q2. Write a program that multiply two single variable polynomials. Each polynomial should be represented as a list with linked list
implementation
#include
<stdio.h>
#include
<stdlib.h>
} Node;
// Function prototypes
int main() {
while (1) {
int coeff, exp;
while (1) {
}
// Multiply the polynomials
displayPolynomial(result);
freePolynomial(poly1);
freePolynomial(poly2);
freePolynomial(result);
return 0;
}
newNode->coeff = coeff;
newNode->exp = exp;
newNode->next = NULL;
return newNode;
*head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
free(result);
coeff += nextResult->coeff;
nextResult = nextResult->next;
free(temp);
result = nextResult;
}
return combined;
if (head == NULL) {
printf("0\n");
return;
head = head->next;
if (head != NULL) {
printf(" + ");
printf("\n");
head = head->next;
free(temp);
}
Slip19
Sort a random array of n integers (accept the value of n from user) in ascending order by using selection sort algorithm [10]
#include <stdlib.h>
#include <time.h>
// Function prototypes
int main() {
int n;
// Seed the random number generator
srand(time(0));
scanf("%d", &n);
int arr[n];
printf("\n");
selectionSort(arr, n);
printArray(arr, n);
return 0;
int minIndex = i;
minIndex = j;
}
}
if (minIndex != i) {
arr[i] = arr[minIndex];
arr[minIndex] = temp;
printf("\n");
Q2. There are lists where insertion should ensure the ordering of data elements. Since the elements are in ascending order the search
can terminate once equal or greater element is found. Implement a doubly linked list of ordered integers
(ascending/descending) with insert, search and display operations.
[20]
#include
<stdio.h>
#include
<stdlib.h>
} Node;
// Function prototypes
printf("2. Search\n");
printf("3. Display\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
insert(&head, value);
break;
case 2:
scanf("%d", &value);
if (result != NULL) {
printf("Element %d found in the list.\n", result->data);
} else {
break;
case 3:
display(head);
break;
case 4:
freeList(head);
return 0;
default:
printf("Invalid choice! Please try again.\n");
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
if (*head == NULL) {
*head = newNode;
} else {
current = current->next;
}
if (current == *head) {
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
} else {
if (newNode->prev) {
newNode->prev->next = newNode;
}
if (current) {
newNode->next = current;
current->prev = newNode;
current = current->next;
if (head == NULL) {
return;
current = current->next;
printf("\n");
}
head = head->next;
free(temp);
}
Slip20
Q1) Implement a stack library (ststack.h) of integers using a static implementation of the stack and implementing the operations like
init(S), S=push(S), isFull(S). Write a driver program that includes stack library and
calls different stack operations. [10]
#include
<stdio.h>
#include
<stdlib.h>
#include
<stdbool.h>
// Stack structure
typedef struct {
int items[MAX];
int top;
} Stack;
// Function prototypes
// Main function
int main() {
Stack s;
init(&s);
nMenu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Display\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
if (isFull(&s)) {
} else {
push(&s, value);
printf("%d pushed to stack\n", value);
break;
case 2:
if (isEmpty(&s)) {
} else {
value = pop(&s);
break;
case 3:
if (isEmpty(&s)) {
} else {
value = peek(&s);
break;
case 4:
display(&s);
break;
case 5:
if (isFull(&s)) {
printf("Stack is full.\n");
} else {
break;
case 6:
printf("Exiting...\n");
exit(0);
default:
}
return 0;
void init(Stack* s) {
bool isFull(Stack* s) {
bool isEmpty(Stack* s) {
if (!isFull(s)) {
}
}
int pop(Stack* s) {
if (!isEmpty(s)) {
int peek(Stack* s) {
if (!isEmpty(s)) {
void display(Stack* s) {
if (isEmpty(s)) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements: ");
printf("\n");
Q2. There are lists where new elements are always appended at the end of the list. The list can be implemented as a circular list with
the external pointer pointing to the last element of the list. Implement singly linked circular list of integers with append and display
operations. The operation append(L, n),
appends to the end of the list, n integers either accepted from user or randomly
generated. [20]
#include
<stdio.h>
#include
<stdlib.h>
#include
<time.h>
// Node structure for the circular linked list
int data;
} Node;
// Function prototypes
Node* last = NULL; // Initialize the last node of the circular list
int n, choice;
scanf("%d", &n);
if (choice == 1) {
srand(time(NULL));
append(&last, randomValue);
} else if (choice == 2) {
scanf("%d", &userValue);
append(&last, userValue);
} else {
return 1;
freeList(last);
return 0;
newNode->data = data;
newNode->next = NULL;
return newNode;
if (*last == NULL) {
*last = newNode;
} else {
// If the list is not empty
if (last == NULL) {
return;
}
do {
} while (current != last->next); // Stop when we loop back to the first node
printf("\n");
// Function to free the allocated memory for the circular linked list
Node* nextNode;
do {
} while (current != last->next); // Stop when we loop back to the first node
Slip21
Q1. Write a program that reverses a string of characters. The function should use a stack library (cststack.h). Use a static
implementation of the stack. [10]
#include
<stdio.h>
#include
<string.h>
#include
<stdlib.h>
#include
<stdbool.h>
// Stack structure
typedef struct {
char items[MAX];
int top;
} Stack;
// Function prototypes
int main() {
char str[MAX];
printf("Enter a string: ");
reverseString(str);
return 0;
}
// Function to initialize the stack
void init(Stack* s) {
bool isFull(Stack* s) {
bool isEmpty(Stack* s) {
return s->top == -1;
if (!isFull(s)) {
char pop(Stack* s) {
if (!isEmpty(s)) {
Stack s;
init(&s);
push(&s, str[i]);
// Pop all characters from the stack and put them back into the string
str[i] = pop(&s);
Q2. Read the data from the file “employee.txt” and sort on names in alphabetical order (use strcmp) using insertion sort or selection sort
[20]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAME_LENGTH 50
typedef struct {
char name[NAME_LENGTH];
int age;
} Employee;
// Function prototypes
void readEmployees(const char* filename, Employee employees[], int* count);
int main() {
Employee employees[MAX_EMPLOYEES];
int count = 0;
printEmployees(employees, count);
return 0;
exit(EXIT_FAILURE);
(*count)++;
}
fclose(file);
int j = i - 1;
j--;
employees[j + 1] = key;
printf("%s\n", employees[i].name);
}
Slip 22
Q1. Implement a linear queue library (st_queue.h) of integers using a static implementation of the queue and implementing the
operations like init (Q), AddQueue(Q, x) and X=DeleteQueue(Q). Write a program that includes queue library and calls different queue
operations. [10]
#include
<stdio.h>
#include
<stdlib.h>
#include
<stdbool.h>
// Queue structure
typedef struct {
int items[MAX];
int front;
int rear;
} Queue;
// Function prototypes
int main() {
Queue q;
init(&q);
while (1) {
printf("\nQueue Operations Menu:\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter an integer to add to the queue: ");
scanf("%d", &value);
AddQueue(&q, value);
break;
case 2:
value = DeleteQueue(&q);
if (value != -1) {
break;
case 3:
displayQueue(&q);
break;
case 4:
exit(0);
default:
}
return 0;
void init(Queue* q) {
q->rear = -1;
bool isEmpty(Queue* q) {
if (isFull(q)) {
return;
if (isEmpty(q)) {
int DeleteQueue(Queue* q) {
if (isEmpty(q)) {
}
int deletedValue = q->items[q->front]; // Get front item
if (q->front == q->rear) {
q->rear = -1;
} else {
void displayQueue(Queue* q) {
if (isEmpty(q)) {
printf("Queue is empty.\n");
return;
while (1) {
printf("\n");
Q2. Read the data from file 'cities.txt' containing names of cities and their STD codes. Accept a name of the city from user and use
sentinel linear search algorithm to check whether the name is present in the file and output the STD code, otherwise output “city not
in the list”. [20]
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#define NAME_LENGTH 50
typedef struct {
char name[NAME_LENGTH];
int std_code;
} City;
// Function prototypes
int main() {
City cities[MAX_CITIES];
int count = 0;
char cityName[NAME_LENGTH];
// Read city data from file
scanf("%49s", cityName);
// Output result
if (std_code != -1) {
} else {
return 0;
if (file == NULL) {
exit(EXIT_FAILURE);
++;
}
}
fclose(file);
int i = 0;
i++;
cities[count - 1] = lastCity;
} else {
Slip23
Implement a priority queue library (PriorityQ.h) of integers using a static implementation of the queue and implementing the below
operation [10]
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
int data[MAX_SIZE];
int priority[MAX_SIZE];
int size;
} PriorityQueue;
// Function prototypes
int main() {
PriorityQueue pq;
init(&pq);
while (1) {
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
printf("Enter priority (higher number means higher priority): ");
scanf("%d", &priority);
break;
case 2:
value = removeHighestPriority(&pq);
if (value != -1) {
}
break;
case 3:
displayQueue(&pq);
break;
case 4:
exit(0);
default:
}
}
return 0;
pq->size = 0;
}
// Check if the queue is full
return pq->size == 0;
}
if (isFull(pq)) {
return;
}
int i;
pq->data[i + 1] = pq->data[i];
pq->priority[i + 1] = pq->priority[i];
pq->data[i + 1] = value;
pq->priority[i + 1] = priority;
pq->size++;
}
if (isEmpty(pq)) {
if (isEmpty(pq)) {
return;
}
printf("Priority Queue contents:\n");
Q2. Read the data from file ‘sortedcities.txt’ containing sorted names of cities and their STD codes. Accept a name of the city from user
and use binary search algorithm to check whether the name is present in the file and output the STD code, otherwise output “city not
in the list”. [20]
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#define MAX_CITIES 100
#define NAME_LENGTH 50
typedef struct {
char name[NAME_LENGTH];
int std_code;
} City;
// Function prototypes
int main() {
City cities[MAX_CITIES];
int count = 0;
char cityName[NAME_LENGTH];
scanf("%49s", cityName);
// Output result
if (std_code != -1) {
} else {
return 0;
if (file == NULL) {
exit(EXIT_FAILURE);
}
(*count)++;
fclose(file);
}
// Function to perform binary search
int left = 0;
if (cmp == 0) {
return cities[mid].std_code; // Found the city
} else {
}
Slip 24
Q1) Implement a circular queue library (cir_queue.h) of integers using a static (linked list) implementation of the queue and
implementing the operations like init (Q), AddQueue(Q, x) and X=peek (Q)). Write a menu driven program that includes queue library
and calls different queue operations.
#include
<stdio.h>
#include
<stdlib.h>
int data;
} Node;
typedef struct CircularQueue {
Node* front;
Node* rear;
} CircularQueue;
// Function prototypes
int main() {
CircularQueue q;
init(&q);
int choice, value;
while (1) {
printf("4. Exit\n");
switch (choice) {
case 1:
scanf("%d", &value);
addQueue(&q, value);
break;
case 2:
value = peek(&q);
if (value != -1) {
break;
case 3:
displayQueue(&q);
break;
case 4:
freeQueue(&q);
exit(0);
default:
return 0;
}
// Initialize the circular queue
void init(CircularQueue* q) {
q->front = NULL;
q->rear = NULL;
int isEmpty(CircularQueue* q) {
return (q->front == NULL);
if (!newNode) {
return;
}
newNode->data = x;
newNode->next = NULL;
if (isEmpty(q)) {
q->front = newNode;
q->rear = newNode;
} else {
q->rear->next = newNode;
int peek(CircularQueue* q) {
if (isEmpty(q)) {
printf("Queue is empty! Cannot peek.\n");
return q->front->data;
void displayQueue(CircularQueue* q) {
if (isEmpty(q)) {
printf("Queue is empty.\n");
return;
do {
current = current->next;
} while (current != q->front);
printf("\n");
void freeQueue(CircularQueue* q) {
if (isEmpty(q)) return;
do {
temp = current;
current = current->next;
free(temp);
q->front = NULL;
q->rear = NULL;
}
Q2. Read the data from the file “employee.txt” and sort on names in alphabetical order (use strcmp) using insertion sort or selection
sort. [20]
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#define NAME_LENGTH 50
typedef struct {
char name[NAME_LENGTH];
int id; // Assuming there is an ID field; you can change this as necessary
} Employee;
// Function prototypes
int main() {
Employee employees[MAX_EMPLOYEES];
int count = 0;
insertionSort(employees, count);
displayEmployees(employees, count);
return 0;
if (file == NULL) {
exit(EXIT_FAILURE);
}
while (fscanf(file, "%49s %d", employees[*count].name, &employees[*count].id) == 2) {
(*count)++;
fclose(file);
int j = i - 1;
employees[j + 1] = employees[j];
j--;
employees[j + 1] = key;
}
}
Slip 25
Q1) Read the data from the ‘employee.txt’ file and sort on age using Count sort and write the sorted data to another file
'sortedemponage.txt'.
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#define NAME_LENGTH 50
typedef struct {
char name[NAME_LENGTH];
int age;
} Employee;
// Function prototypes
int main() {
Employee employees[MAX_EMPLOYEES];
int count = 0;
countingSort(employees, count);
return 0;
exit(EXIT_FAILURE);
(*count)++;
}
}
fclose(file);
Employee output[MAX_EMPLOYEES];
// Count occurrences of each age
ageCount[employees[i].age]++;
// Cumulative count
}
// Build the output array
output[ageCount[employees[i].age] - 1] = employees[i];
ageCount[employees[i].age]--;
employees[i] = output[i];
}
}
if (file == NULL) {
exit(EXIT_FAILURE);
}
for (int i = 0; i < count; i++) {
fclose(file);
-> #include
<stdio.h> #include
<stdlib.h>
#include <ctype.h>
#include <string.h>
typedef struct {
char items[MAX_SIZE];
int top;
} Stack;
// Stack functions
void init(Stack* s) {
s->top = -1;
int isFull(Stack* s) {
int isEmpty(Stack* s) {
return s->top == -1;
if (!isFull(s)) {
s->items[++(s->top)] = item;
char pop(Stack* s) {
if (!isEmpty(s)) {
return s->items[(s->top)--];
char peek(Stack* s) {
if (!isEmpty(s)) {
return s->items[s->top];
}
return '\0';
switch (operator) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return 0;
Stack stack;
init(&stack);
postfix[j++] = token;
} else if (token == '(') {
push(&stack, token);
postfix[j++] = pop(&stack);
postfix[j++] = pop(&stack);
}
push(&stack, token);
while (!isEmpty(&stack)) {
postfix[j++] = pop(&stack);
int main() {
char postfix[MAX_SIZE];
infixToPostfix(infix, postfix);
}
Slip26
Q1. Implement a stack library (ststack.h) of integers using a static implementation of the stack and implementing the operations like
init(S), S=Push(S,x) and isEmpty(S). Write a driver program that includes stack library
and calls different stack operations. [10]
#include
<stdio.h>
#include
<stdlib.h>
typedef struct {
int items[MAX_SIZE];
int top;
} Stack;
void init(Stack* s) {
s->top = -1;
int isEmpty(Stack* s) {
}
// Function to push an element onto the stack
s->items[++(s->top)] = item;
return 1; // Success
if (!isEmpty(s)) {
return s->items[(s->top)--];
int peek(Stack* s) {
if (!isEmpty(s)) {
return s->items[s->top];
}
int main() {
Stack s;
init(&s);
printf("Stack operations:\n");
// Push elements onto the stack
if (push(&s, i)) {
} else {
} else {
printf("Stack is empty.\n");
while (!isEmpty(&s)) {
}
// Check if the stack is empty
if (isEmpty(&s)) {
} else {
return 0;
Q2. There are lists where insertion should ensure the ordering of data elements. Since the elements are in ascending order the search
can terminate once equal or greater element is found. Implement a singly linked list of ordered integers(ascending/descending) with
insert, search, and display operations.
#include
<stdio.h>
#include
<stdlib.h>
int data;
} Node;
if (!newNode) {
exit(EXIT_FAILURE);
newNode->data = data;
newNode->next = NULL;
return newNode;
newNode->next = *head;
*head = newNode;
} else {
current = current->next;
newNode->next = current->next;
current->next = newNode;
if (current->data == data) {
}
if (current->data > data) {
current = current->next;
current = current->next;
printf("NULL\n");
int main() {
nMenu:\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
insert(&head, value);
break;
case 2:
scanf("%d", &value);
if (search(head, value)) {
} else {
printf("Element %d not found in the list.\n", value);
break;
case 3:
display(head);
break;
case 4:
exit(0);
default:
}
}
return 0;
}
Slip 27
Q1. Read the data from the file and sort on names in alphabetical order (use strcmp) using Merge sort and write the sorted data to
another file
'sortedemponname.txt' [10]
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
strcpy(names[k], L[i]);
i++;
} else {
strcpy(names[k], R[j]);
j++;
} k+
+;
strcpy(names[k], L[i]);
i++;
k++;
}
strcpy(names[k], R[j]);
j++;
k++;
}
int main() {
char names[MAX_EMPLOYEES][MAX_NAME_LENGTH];
int count = 0;
if (inputFile == NULL) {
return 1;
}
// Read names from the file
names[count][strcspn(names[count], "\n")] = 0;
count++;
fclose(inputFile);
if (outputFile == NULL) {
return 1;
fclose(outputFile);
return 0;
Q2. Write a program that adds two single variable polynomials. Each polynomial should be represented as a list with linked list
implementation.
#include
<stdio.h>
#include
<stdlib.h>
// Node structure for polynomial terms
int coefficient;
int exponent;
} Node;
newTerm->coefficient = coeff;
newTerm->exponent = exp;
newTerm->next = NULL;
return newTerm;
newTerm->next = *poly;
*poly = newTerm;
} else {
current = current->next;
if (current->exponent == exp) {
} else {
newTerm->next = current->next;
current->next = newTerm;
}
poly1 = poly1->next;
}
poly2 = poly2->next;
return result;
current = current->next;
if (current != NULL) {
printf(" + ");
printf("\n");
}
// Main function to demonstrate polynomial addition
int main() {
insertTerm(&poly1, 5, 2);
insertTerm(&poly1, 4, 1);
insertTerm(&poly1, 2, 0);
// Second polynomial: 3x^1 + 7x^0
insertTerm(&poly2, 3, 1);
insertTerm(&poly2, 7, 0);
displayPolynomial(poly1);
displayPolynomial(poly2);
displayPolynomial(result);
return 0;
Slip 28
Q1. Implement a stack library (ststack.h) of integers using a static implementation of the stack and implementing the above six
operations. Write a driver program that includes stack library and calls different stack operations.
#include
<stdio.h>
#include
<stdlib.h>
#define MAX_SIZE 100
// Stack structure
int items[MAX_SIZE];
int top;
} Stack;
s->top = -1;
int isFull(Stack* s) {
if (isFull(s)) {
return;
}
s->items[++(s->top)] = value;
int pop(Stack* s) {
if (isEmpty(s)) {
}
return s->items[(s->top)--];
int peek(Stack* s) {
if (isEmpty(s)) {
return s->items[s->top];
}
void display(Stack* s) {
if (isEmpty(s)) {
printf("Stack is empty!\n");
return;
printf("\n");
// Driver program
int main() {
Stack s;
init(&s);
int choice, value;
while (1) {
printf("\nStack Operations:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
push(&s, value);
break;
case 2:
value = pop(&s);
if (value != -1) {
break;
case 3:
value = peek(&s);
if (value != -1) {
break;
case 4:
if (isEmpty(&s)) {
printf("Stack is empty\n");
} else {
break;
case 5:
if (isFull(&s)) {
printf("Stack is full\n");
} else {
break;
case 6:
display(&s);
break;
case 7:
printf("Exiting...\n");
exit(0);
default:
return 0;
} [10]
Q2. Read the data from the ‘employee.txt’ file and sort on age using Merge sort or Quick sort and write the sorted data to another file
'sortedemponage.txt'.
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#define NAME_LENGTH 50
typedef struct {
char name[NAME_LENGTH];
int age;
} Employee;
*a = *b;
*b = temp;
i++;
swap(&arr[i], &arr[j]);
return i + 1;
}
quickSort(arr, pi + 1, high);
}
// Function to read employee data from file
if (!file) {
return 0;
int count = 0;
count++;
if (count >= MAX_EMPLOYEES) {
fclose(file);
return count;
return;
fclose(file);
}
int main() {
Employee employees[MAX_EMPLOYEES];
if (count == 0) {
return 1;
return 0;
Slip29
Q1. Implement a stack library (ststack.h) of integers using a static implementation of the stack and implementing the operations like
init(S), S=push(S), and X=peek(S). Write a driver program that includes stack library and calls different stack operations. [10]
#include
<stdio.h>
#include
<stdlib.h>
#define MAX_SIZE 100
int items[MAX_SIZE];
int top;
} Stack;
void init(Stack* s) {
s->top = -1; // Stack is initially empty
int isFull(Stack* s) {
int isEmpty(Stack* s) {
if (isFull(s)) {
return;
s->items[++(s->top)] = value;
}
// Function to peek at the top element of the stack
int peek(Stack* s) {
if (isEmpty(s)) {
return s->items[s->top];
// Driver program
int main() {
Stack s;
init(&s);
while (1) {
printf("\nStack Operations:\n");
printf("1. Push\n");
printf("2. Peek\n");
printf("5. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
push(&s, value);
break;
case 2:
value = peek(&s);
if (value != -1) {
break;
case 3:
if (isEmpty(&s)) {
printf("Stack is empty\n");
} else {
break;
case 4:
if (isFull(&s)) {
printf("Stack is full\n");
} else {
break;
case 5:
printf("Exiting...\n");
exit(0);
default:
return 0;
Q2. There are lists where new elements are always appended at the end of the list. The list can be implemented as a circular list with
the external pointer pointing to the last element of the list. Implement singly linked circular list of integers with append and display
operations. The operation append(L, n), appends to the end of the list, n integers accepted from user. [20]
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
Node* last;
} CircularLinkedList;
// Function to initialize the circular linked list
newNode->data = value;
if (list->last == NULL) {
// List is empty
list->last = newNode;
} else {
}
// Function to display the circular linked list
if (list->last == NULL) {
return;
do {
current = current->next;
} while (current != list->last->next);
printf("(back to start)\n");
// Driver program
int main() {
CircularLinkedList list;
initList(&list);
int n, value;
scanf("%d", &value);
append(&list, value);
display(&list);
return 0;
}
Slip30
Q1. Read the data from the file “employee.txt” and sort on names in alphabetical order (use strcmp) using bubble sort [10]
#include <stdlib.h>
#include <string.h>
#define NAME_LENGTH 50
char name[NAME_LENGTH];
int age;
} Employee;
*a = *b;
*b = temp;
}
}
}
if (file == NULL) {
return 0;
int count = 0;
while (fscanf(file, "%49s %d", employees[count].name, &employees[count].age) == 2) {
count++;
fclose(file);
return count;
return;
fclose(file);
}
int main() {
Employee employees[MAX_EMPLOYEES];
if (n == 0) {
return 1;
bubbleSort(employees, n);
// Write sorted data to a new file
return 0;
Q2. Write a program that merges two ordered linked lists into third new list. When two lists are merged the data in the resulting list are
also ordered. The two original lists should be left unchanged. That is merged list should be new
one. Use linked implementation. [20]
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
newNode->next = NULL;
return newNode;
if (*head == NULL) {
*head = newNode;
return;
}
temp = temp->next;
temp->next = newNode;
temp = temp->next;
printf("NULL\n");
*lastPtrRef = createNode(list1->data);
list1 = list1->next;
} else {
*lastPtrRef = createNode(list2->data);
list2 = list2->next;
lastPtrRef = &((*lastPtrRef)->next);
}
// Append remaining elements from list1, if any
*lastPtrRef = createNode(list1->data);
lastPtrRef = &((*lastPtrRef)->next);
list1 = list1->next;
*lastPtrRef = createNode(list2->data);
lastPtrRef = &((*lastPtrRef)->next);
list2 = list2->next;
return mergedList;
// Driver program
int main() {
insertEnd(&list1, 1);
insertEnd(&list1, 3);
insertEnd(&list1, 5);
insertEnd(&list2, 2);
insertEnd(&list2, 4);
insertEnd(&list2, 6);
printf("List 1: ");
display(list1);
printf("List 2: ");
display(list2);
display(mergedList);
// Free allocated memory (optional, but good practice)
return 0;