PROGRAMMING ASSISSMENT-4
Name: Ujjwal Raj USN: 1NT23CS260
1. In a university database, there are separate lists for
undergraduate and postgraduate students. Due to a recent
merger, the university wants to combine these two lists into a
single student database. Design and implement a C program
to concatenate the undergraduate and postgraduate student
lists. Assume that each node contains the name of the
student. (Note : Use circular linked list)
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
char name[100];
struct Student* next;
};
struct Student* createStudent(char *name) {
struct Student* newStudent = (struct Student*)malloc(sizeof(struct Student));
strcpy(newStudent->name, name);
newStudent->next = newStudent;
return newStudent;
void insertStudent(struct Student** head, char *name) {
struct Student* newStudent = createStudent(name);
if (*head == NULL) {
*head = newStudent;
} else {
struct Student* temp = *head;
while (temp->next != *head) {
temp = temp->next;
temp->next = newStudent;
newStudent->next = *head;
void printList(struct Student* head) {
if (head == NULL) {
printf("The list is empty.\n");
return;
struct Student* temp = head;
do {
printf("%s -> ", temp->name);
temp = temp->next;
} while (temp != head);
printf("(back to start)\n");
void concatenateLists(struct Student** head1, struct Student** head2) {
if (*head1 == NULL) {
*head1 = *head2;
return;
if (*head2 == NULL) {
return;
struct Student* temp1 = *head1;
while (temp1->next != *head1) {
temp1 = temp1->next;
}
struct Student* temp2 = *head2;
while (temp2->next != *head2) {
temp2 = temp2->next;
temp1->next = *head2;
temp2->next = *head1;
*head2 = NULL;
int main() {
struct Student* undergraduateList = NULL;
struct Student* postgraduateList = NULL
insertStudent(&undergraduateList, "Alice");
insertStudent(&undergraduateList, "Bob");
insertStudent(&undergraduateList, "Charlie");
insertStudent(&postgraduateList, "David");
insertStudent(&postgraduateList, "Eve");
printf("Undergraduate Students List:\n");
printList(undergraduateList);
printf("\nPostgraduate Students List:\n");
printList(postgraduateList);
concatenateLists(&undergraduateList, &postgraduateList);
printf("\nCombined Student Database (Undergraduate + Postgraduate):\n");
printList(undergraduateList);
return 0;
2. The hardware of most computers allows integers of only a
specific maximum length. Suppose you are given the task of
representing positive integers of arbitrary length and compute
the sum of two such integers. Propose a solution to this task
using circular linked list and implement the solution.
CODE:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int digit;
struct Node* next;
};
struct Node* createNode(int digit) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->digit = digit;
newNode->next = newNode;
return newNode;
void insertDigit(struct Node** head, int digit) {
struct Node* newNode = createNode(digit);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
temp->next = newNode;
newNode->next = *head;
void printList(struct Node* head) {
if (head == NULL) {
printf("The list is empty.\n");
return;
}
struct Node* temp = head;
do {
printf("%d", temp->digit);
temp = temp->next;
} while (temp != head);
printf("\n");
struct Node* addNumbers(struct Node* num1, struct Node* num2) {
struct Node* result = NULL;
struct Node* temp1 = num1;
struct Node* temp2 = num2;
int carry = 0;
do {
int sum = carry;
if (temp1 != NULL) {
sum += temp1->digit;
temp1 = temp1->next;
if (temp2 != NULL) {
sum += temp2->digit;
temp2 = temp2->next;
carry = sum / 10;
insertDigit(&result, sum % 10);
} while (temp1 != num1 || temp2 != num2 || carry != 0);
return result;
int main() {
struct Node* num1 = NULL;
struct Node* num2 = NULL;
insertDigit(&num1, 3);
insertDigit(&num1, 4);
insertDigit(&num1, 5);
insertDigit(&num2, 7);
insertDigit(&num2, 8);
insertDigit(&num2, 9);
printf("Number 1: ");
printList(num1);
printf("Number 2: ");
printList(num2);
struct Node* result = addNumbers(num1, num2);
printf("Sum: ");
printList(result);
return 0;
3. Design a C program to implement primitive operations on a
stack using circular linked list.
CODE:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Stack {
struct Node* top;
};
void initializeStack(struct Stack* stack) {
stack->top = NULL;
}
int isEmpty(struct Stack* stack) {
return stack->top == NULL;
void push(struct Stack* stack, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
if (stack->top == NULL) {
stack->top = newNode;
newNode->next = newNode;
} else {
struct Node* temp = stack->top;
while (temp->next != stack->top) {
temp = temp->next;
temp->next = newNode;
newNode->next = stack->top;
stack->top = newNode;
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack underflow\n");
return -1;
int data;
if (stack->top->next == stack->top) {
data = stack->top->data;
free(stack->top);
stack->top = NULL;
} else {
struct Node* temp = stack->top;
while (temp->next != stack->top) {
temp = temp->next;
data = stack->top->data;
temp->next = stack->top->next;
free(stack->top);
stack->top = temp->next;
return data;
int peek(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return -1;
return stack->top->data;
void printStack(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return;
struct Node* temp = stack->top;
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != stack->top);
printf("\n");
int main() {
struct Stack stack;
initializeStack(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
push(&stack, 40);
printf("Stack: ");
printStack(&stack);
printf("Popped: %d\n", pop(&stack));
printf("Stack after pop: ");
printStack(&stack);
printf("Peek: %d\n", peek(&stack));
return 0;
4. Design and Implement a C program to concatenate two
doubly linked lists
CODE:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
printf("\n");
void concatenateLists(struct Node** head1, struct Node** head2) {
if (*head1 == NULL) {
*head1 = *head2;
return;
if (*head2 == NULL) {
return;
struct Node* temp = *head1;
while (temp->next != NULL) {
temp = temp->next;
temp->next = *head2;
(*head2)->prev = temp;
int main() {
struct Node* list1 = NULL;
struct Node* list2 = NULL;
insertAtEnd(&list1, 1);
insertAtEnd(&list1, 2);
insertAtEnd(&list1, 3);
insertAtEnd(&list2, 4);
insertAtEnd(&list2, 5);
insertAtEnd(&list2, 6);
printf("List 1: ");
printList(list1);
printf("List 2: ");
printList(list2);
concatenateLists(&list1, &list2);
printf("Concatenated List: ");
printList(list1);
return 0;
5. Assume that you are given two doubly linked lists containing
songs. Design and implement a C Program to Find common
songs from both the lists.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node {
char song[100];
struct Node* next;
struct Node* prev;
};
struct Node* createNode(char *song) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
strcpy(newNode->song, song);
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
void insertAtEnd(struct Node** head, char *song) {
struct Node* newNode = createNode(song);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%s -> ", temp->song);
temp = temp->next;
}
printf("NULL\n");
void findCommonSongs(struct Node* list1, struct Node* list2) {
struct Node* temp1 = list1;
while (temp1 != NULL) {
struct Node* temp2 = list2;
while (temp2 != NULL) {
if (strcmp(temp1->song, temp2->song) == 0) {
printf("Common Song: %s\n", temp1->song);
temp2 = temp2->next;
temp1 = temp1->next;
int main() {
struct Node* list1 = NULL;
struct Node* list2 = NULL;
insertAtEnd(&list1, "Song A");
insertAtEnd(&list1, "Song B");
insertAtEnd(&list1, "Song C");
insertAtEnd(&list2, "Song B");
insertAtEnd(&list2, "Song D");
insertAtEnd(&list2, "Song C")
printf("List 1: ");
printList(list1);
printf("List 2: ");
printList(list2);
findCommonSongs(list1, list2);
return 0;