Saturday, June 20, 2026

NEP-SYBCS-DBMS-I Assignment-I

Assignment no.1 Data Definition Query 

(Create simple tables with all constraints) 

SET A

1. Create a table with following details

Table Name:- Player

Columns Column Name         Column  Data Type             Constraints

1             player_id                 integer                                 Primary key

2             Name                       varchar (50)

3             Birth_date                 date

4             Birth_place             varchar(100)


Table level constraint Name should not be NULL

Answer:-

CREATE TABLE Player

(

    player_id INT PRIMARY KEY,

    Name VARCHAR(50) NOT NULL,

    Birth_date DATE,

    Birth_place VARCHAR(100)

);


2. Create a table with following details

Table Name :-Student

Columns     Column Name         Column Data Type         Constraints

1                 Roll_no                     integer

2                 Class                         varchar (20)

3                 Weight                         numeric (6,2)

4                 Height                         numeric (6,2)

Table level constraint Roll_no and class as primary key

Answer:-

CREATE TABLE Student

(

    Roll_no INT,

    Class VARCHAR(20),

    Weight NUMERIC(6,2),

    Height NUMERIC(6,2),

    CONSTRAINT pk_student PRIMARY KEY (Roll_no, Class)

);


Set B

1. Create a table with details as given below

Table Name:- Machine

Columns         Column Name             Column Data Type             Constraints

1                     Machine_id                 integer                                 primary key

2                     Machine_name            Varchar (50)                         NOT NULL, uppercase

3                     Machine_type              varchar(10)                         Type in ( ‘drilling’, ‘milling’, ‘lathe’,

                                                                                                                ‘turning’, ‘grinding’)

4                 Machine_price                 float                                 Greater than zero

5                 Machine_cost                 float

Table level constraint Machine_cost less than Machine_price

Answer:-

CREATE TABLE Machine

(

    Machine_id INT PRIMARY KEY,

    Machine_name VARCHAR(50) NOT NULL

        CHECK (Machine_name = UPPER(Machine_name)),

    Machine_type VARCHAR(10)

        CHECK (Machine_type IN ('drilling', 'milling', 'lathe', 'turning', 'grinding')),

    Machine_price FLOAT

        CHECK (Machine_price > 0),

    Machine_cost FLOAT,

    CONSTRAINT chk_cost_price

    CHECK (Machine_cost < Machine_price)

);


2. Create a table with details as given below

Table Name:- Employee

Columns         Column Name             Column Data Type                 Constraints

1                     Employee_id                 integer                               Primary key

2                     Employee_name            varchar (50)                      NOT NULL, uppercase

3                     Employee_desg             varchar(10)                        Designation in ( ‘Manager’, ‘staff’,                                                                                                                                                     ‘worker’)

4                     Employee_sal                 float                                  Greater than Zero

5                     Employee_uid                 text                                    Unique

Table level constraint Employee_uid not equal to Employee_id

Answer:-

CREATE TABLE Employee

(

    Employee_id INT PRIMARY KEY,

    Employee_name VARCHAR(50) NOT NULL

        CHECK (Employee_name = UPPER(Employee_name)),

    Employee_desg VARCHAR(10)

        CHECK (Employee_desg IN ('Manager', 'staff', 'worker')),

    Employee_sal FLOAT

        CHECK (Employee_sal > 0),

    Employee_uid TEXT UNIQUE,

    CONSTRAINT chk_uid_not_equal

    CHECK (Employee_uid <> CAST(Employee_id AS TEXT))

);


Set C

1. Create a table with details as given below

Table Name:- Student

Columns         Column Name             Column Data Type             Constraints

1                     Stud_id                             integer                                 Primary key

2                     Stud _name                      varchar (50)                         NOT NULL, uppercase

3                     Stud _Class                      varchar(10)                         Classin ( ‘FY’, ‘SY’, ‘TY’)

4                     Stud _Marks                        float                                 Greater than Zero

5                     Stud _uid                          text                                      Unique

Table level constraint Stud_uid not equal to Stud_id

CREATE TABLE Student

(

    Stud_id INT PRIMARY KEY,

    Stud_name VARCHAR(50) NOT NULL

        CHECK (Stud_name = UPPER(Stud_name)),

    Stud_Class VARCHAR(10)

        CHECK (Stud_Class IN ('FY', 'SY', 'TY')),

    Stud_Marks FLOAT

        CHECK (Stud_Marks > 0),

    Stud_uid TEXT UNIQUE,

    CONSTRAINT chk_uid_not_equal

    CHECK (Stud_uid <> CAST(Stud_id AS TEXT))

);



NEP-SYBCS-DS-I Assignment-6

Assignment 6 Queue  


Set A

a) Perform the following operations using static implementation of Queue

i. Create a queue of n integers.

ii. Delete the element from the queue and display it.

#include <stdio.h>

#define MAX 100


int queue[MAX];

int front = -1, rear = -1;


// Enqueue Operation

void enqueue(int value)

{

    if(rear == MAX - 1)

    {

        printf("Queue Overflow\n");

        return;

    }


    if(front == -1)

        front = 0;


    queue[++rear] = value;

}


// Dequeue Operation

int dequeue()

{

    int value;


    if(front == -1 || front > rear)

    {

        printf("Queue Underflow\n");

        return -1;

    }


    value = queue[front];


    if(front == rear)

        front = rear = -1;

    else

        front++;


    return value;

}


// Display Queue

void display()

{

    int i;


    if(front == -1)

    {

        printf("Queue is Empty\n");

        return;

    }


    printf("Queue Elements:\n");

    for(i = front; i <= rear; i++)

        printf("%d ", queue[i]);


    printf("\n");

}


int main()

{

    int n, i, value, deleted;


    printf("Enter number of elements: ");

    scanf("%d", &n);


    printf("Enter %d integers:\n", n);


    for(i = 0; i < n; i++)

    {

        scanf("%d", &value);

        enqueue(value);

    }


    printf("\nQueue after insertion:\n");

    display();


    deleted = dequeue();


    if(deleted != -1)

        printf("\nDeleted Element = %d\n", deleted);


    printf("\nQueue after deletion:\n");

    display();


    return 0;

}


b) Perform the following operations using dynamic implementation of Queue

i. Insert n elements in a queue.

ii. Delete the element from the queue and display it.

#include <stdio.h>

#include <stdlib.h>


// Node structure

struct Node

{

    int data;

    struct Node *next;

};


struct Node *front = NULL;

struct Node *rear = NULL;


// Enqueue Operation

void enqueue(int value)

{

    struct Node *newNode;


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


    if(newNode == NULL)

    {

        printf("Queue Overflow\n");

        return;

    }


    newNode->data = value;

    newNode->next = NULL;


    if(front == NULL)

    {

        front = rear = newNode;

    }

    else

    {

        rear->next = newNode;

        rear = newNode;

    }

}


// Dequeue Operation

int dequeue()

{

    struct Node *temp;

    int value;


    if(front == NULL)

    {

        printf("Queue Underflow\n");

        return -1;

    }


    temp = front;

    value = temp->data;


    front = front->next;


    if(front == NULL)

        rear = NULL;


    free(temp);


    return value;

}


// Display Queue

void display()

{

    struct Node *temp;


    if(front == NULL)

    {

        printf("Queue is Empty\n");

        return;

    }


    temp = front;


    printf("Queue Elements:\n");

    while(temp != NULL)

    {

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

        temp = temp->next;

    }

    printf("\n");

}


int main()

{

    int n, i, value, deleted;


    printf("Enter number of elements: ");

    scanf("%d", &n);


    printf("Enter %d integers:\n", n);


    for(i = 0; i < n; i++)

    {

        scanf("%d", &value);

        enqueue(value);

    }


    printf("\nQueue after insertion:\n");

    display();


    deleted = dequeue();


    if(deleted != -1)

        printf("\nDeleted Element = %d\n", deleted);


    printf("\nQueue after deletion:\n");

    display();


    return 0;

}


Set B

a) Perform the following operations on circular Queue (static or dynamic implementation)

i. Create a queue of n integers.

ii. Delete the element from the queue and display it.

#include <stdio.h>

#define MAX 5

int queue[MAX];

int front = -1, rear = -1;

// Insert element

void enqueue(int value)

{

    if ((front == 0 && rear == MAX - 1) || (front == rear + 1))

    {

        printf("Queue Overflow\n");

        return;

    }

    if (front == -1)

    {

        front = rear = 0;

    }

    else if (rear == MAX - 1)

    {

        rear = 0;

    }

    else

    {

        rear++;

    }

    queue[rear] = value;

}

// Delete element

int dequeue()

{

    int value;

    if (front == -1)

    {

        printf("Queue Underflow\n");

        return -1;

    }

    value = queue[front];

    if (front == rear)

    {

        front = rear = -1;

    }

    else if (front == MAX - 1)

    {

        front = 0;

    }

    else

    {

        front++;

    }

    return value;

}

// Display queue

void display()

{

    int i;

    if (front == -1)

    {

        printf("Queue is Empty\n");

        return;

    }


    printf("Queue Elements: ");


    if (front <= rear)

    {

        for (i = front; i <= rear; i++)

            printf("%d ", queue[i]);

    }

    else

    {

        for (i = front; i < MAX; i++)

            printf("%d ", queue[i]);

        for (i = 0; i <= rear; i++)

            printf("%d ", queue[i]);

    }

    printf("\n");

}

int main()

{

    int n, i, value, deleted;

    printf("Enter number of elements (Max %d): ", MAX);

    scanf("%d", &n);

    if (n > MAX)

    {

        printf("Queue size exceeds maximum capacity.\n");

        return 0;

    }

    printf("Enter %d integers:\n", n);

    for (i = 0; i < n; i++)

    {

        scanf("%d", &value);

        enqueue(value);

    }

    printf("\nQueue after insertion:\n");

    display();

    deleted = dequeue();

   if (deleted != -1)

        printf("\nDeleted Element = %d\n", deleted);

    printf("\nQueue after deletion:\n");

    display();

    return 0;

}


b) Write a program to create a Priority Queue and display it.

i. Add an element with its priority into the queue.

ii. Delete an element from queue according to its priority.

#include <stdio.h>

#define MAX 10

struct PriorityQueue

{

    int data;

    int priority;

};

struct PriorityQueue pq[MAX];

int size = 0;

// Insert element

void enqueue(int value, int priority)

{

    if (size == MAX)

    {

        printf("Priority Queue Overflow\n");

        return;

    }

    int i = size - 1;

    // Arrange elements according to priority

    while (i >= 0 && pq[i].priority > priority)

    {

        pq[i + 1] = pq[i];

        i--;

    }

    pq[i + 1].data = value;

    pq[i + 1].priority = priority;

    size++;

}

// Delete highest priority element

void dequeue()

{

    int i;

    if (size == 0)

    {

        printf("Priority Queue Underflow\n");

        return;

    }

    printf("Deleted Element = %d\n", pq[0].data);


    for (i = 0; i < size - 1; i++)

    {

        pq[i] = pq[i + 1];

    }


    size--;

}

// Display queue

void display()

{

    int i;


    if (size == 0)

    {

        printf("Priority Queue is Empty\n");

        return;

    }

    printf("\nElement\tPriority\n");

    for (i = 0; i < size; i++)

    {

        printf("%d\t%d\n", pq[i].data, pq[i].priority);

    }

}

int main()

{

    int n, i, value, priority;


    printf("Enter number of elements: ");

    scanf("%d", &n);


    for (i = 0; i < n; i++)

    {

        printf("\nEnter Element: ");

        scanf("%d", &value);


        printf("Enter Priority (Smaller number = Higher Priority): ");

        scanf("%d", &priority);


        enqueue(value, priority);

    }

    printf("\nPriority Queue:\n");

    display();

    printf("\nDeleting Highest Priority Element...\n");

    dequeue();

    printf("\nPriority Queue after Deletion:\n");

    display();


    return 0;

}

NEP-SYBCS-DS-I Assignment 5

 Assignment 5 Stack

Set A

a) Perform the following operations using static implementation of stack

i. Accept n integers from user and push it into the stack.

ii. Pop the element from the stack and display it (Use isEmpty())

#include <stdio.h>

#include <stdlib.h>

#define MAX 100

int stack[MAX];

int top = -1;

// Check if stack is full

int isFull()

{

    return (top == MAX - 1);

}

// Check if stack is empty

int isEmpty()

{

    return (top == -1);

}

// Push operation

void push(int value)

{

    if(isFull())

    {

        printf("Stack Overflow\n");

        return;

    }

    stack[++top] = value;

}


// Pop operation

int pop()

{

    if(isEmpty())

    {

        printf("Stack Underflow\n");

        return -1;

    }

    return stack[top--];

}


// Display stack

void display()

{

    int i;

    if(isEmpty())

    {

        printf("Stack is Empty\n");

        return;

    }

    printf("Stack Elements:\n");

    for(i = top; i >= 0; i--)

        printf("%d\n", stack[i]);

}

int main()

{

    int n, i, value;

    printf("Enter number of elements: ");

    scanf("%d", &n);

   printf("Enter %d integers:\n", n);

    for(i = 0; i < n; i++)

    {

        scanf("%d", &value);

        push(value);

    }

    printf("\nStack after Push:\n");

    display();

    value = pop();

    if(value != -1)

        printf("\nPopped Element = %d\n", value);

    printf("\nStack after Pop:\n");

    display();

    return 0;

}


b) Perform the following operations using dynamic implementation of stack

i. Accept n integers from user and push it into the stack.

ii. Pop the element from the stack and display it.

#include <stdio.h>

#include <stdlib.h>


struct node

{

    int data;

    struct node *next;

};


struct node *top = NULL;


// Push operation

void push(int value)

{

    struct node *newnode;


    newnode = (struct node *)malloc(sizeof(struct node));


    if(newnode == NULL)

    {

        printf("Stack Overflow\n");

        return;

    }


    newnode->data = value;

    newnode->next = top;

    top = newnode;

}


// Pop operation

int pop()

{

    struct node *temp;

    int value;


    if(top == NULL)

    {

        printf("Stack Underflow\n");

        return -1;

    }


    temp = top;

    value = temp->data;

    top = top->next;


    free(temp);


    return value;

}


// Display stack

void display()

{

    struct node *temp = top;


    if(top == NULL)

    {

        printf("Stack is Empty\n");

        return;

    }


    printf("Stack Elements:\n");


    while(temp != NULL)

    {

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

        temp = temp->next;

    }

}


int main()

{

    int n, i, value;


    printf("Enter number of elements: ");

    scanf("%d", &n);


    printf("Enter %d integers:\n", n);


    for(i = 0; i < n; i++)

    {

        scanf("%d", &value);

        push(value);

    }


    printf("\nStack after Push:\n");

    display();


    value = pop();


    if(value != -1)

        printf("\nPopped Element = %d\n", value);


    printf("\nStack after Pop:\n");

    display();


    return 0;

}


Set B

a) Perform the following operations using implementation of stack (Static or dynamic)

i. Accept n integers from user and push it into the stack.

ii. Reverses a string of characters using stack and check weather given string is palindrome or not.

#include <stdio.h>

#include <string.h>


#define MAX 100


// Integer Stack

int stack[MAX];

int top = -1;


// Character Stack

char cstack[MAX];

int ctop = -1;


// Push integer

void push(int value)

{

    if(top == MAX - 1)

    {

        printf("Stack Overflow\n");

        return;

    }

    stack[++top] = value;

}


// Display integer stack

void displayStack()

{

    int i;

    printf("Stack Elements:\n");

    for(i = top; i >= 0; i--)

        printf("%d\n", stack[i]);

}


// Push character

void pushChar(char ch)

{

    cstack[++ctop] = ch;

}


// Pop character

char popChar()

{

    return cstack[ctop--];

}


int main()

{

    int n, i, value;

    char str[MAX], rev[MAX];


    // Push integers

    printf("Enter number of integers: ");

    scanf("%d", &n);


    printf("Enter %d integers:\n", n);

    for(i = 0; i < n; i++)

    {

        scanf("%d", &value);

        push(value);

    }


    printf("\nStack after Push:\n");

    displayStack();


    // Clear input buffer

    getchar();


    // Reverse string

    printf("\nEnter a string: ");

    gets(str);


    int len = strlen(str);


    for(i = 0; i < len; i++)

        pushChar(str[i]);


    for(i = 0; i < len; i++)

        rev[i] = popChar();


    rev[len] = '\0';


    printf("Reversed String: %s\n", rev);


    if(strcmp(str, rev) == 0)

        printf("The given string is a Palindrome.\n");

    else

        printf("The given string is NOT a Palindrome.\n");


    return 0;

}

b) Write a menu driven program to perform the following operations on stack

i. Convert an infix expression of the form (a*(b-c*d)/((a+d)/b)) into its equivalent postfix notation.

ii. Evaluation of postfix expression

#include <stdio.h>

#include <ctype.h>

#include <string.h>


#define MAX 100


char stack[MAX];

int top = -1;


// Stack operations for characters

void push(char x)

{

    stack[++top] = x;

}


char pop()

{

    return stack[top--];

}


char peek()

{

    return stack[top];

}


int precedence(char ch)

{

    switch(ch)

    {

        case '+':

        case '-':

            return 1;


        case '*':

        case '/':

            return 2;


        case '^':

            return 3;

    }

    return 0;

}


// Infix to Postfix

void infixToPostfix(char infix[], char postfix[])

{

    int i = 0, j = 0;

    char ch;


    while(infix[i] != '\0')

    {

        ch = infix[i];


        if(isalnum(ch))

            postfix[j++] = ch;


        else if(ch == '(')

            push(ch);


        else if(ch == ')')

        {

            while(peek() != '(')

                postfix[j++] = pop();

            pop();

        }


        else

        {

            while(top != -1 && precedence(peek()) >= precedence(ch))

                postfix[j++] = pop();

            push(ch);

        }

        i++;

    }


    while(top != -1)

        postfix[j++] = pop();


    postfix[j] = '\0';

}


// Evaluate Postfix

int evaluatePostfix(char postfix[])

{

    int st[MAX];

    int t = -1;

    int i, a, b;


    for(i = 0; postfix[i] != '\0'; i++)

    {

        if(isdigit(postfix[i]))

            st[++t] = postfix[i] - '0';


        else

        {

            b = st[t--];

            a = st[t--];


            switch(postfix[i])

            {

                case '+':

                    st[++t] = a + b;

                    break;


                case '-':

                    st[++t] = a - b;

                    break;


                case '*':

                    st[++t] = a * b;

                    break;


                case '/':

                    st[++t] = a / b;

                    break;

            }

        }

    }

    return st[t];

}

int main()

{

    int choice;

    char infix[MAX], postfix[MAX];

    do

    {

        printf("\n----- MENU -----\n");

        printf("1. Infix to Postfix\n");

        printf("2. Evaluate Postfix\n");

        printf("3. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);


        switch(choice)

        {

            case 1:

                top = -1;

                printf("Enter Infix Expression: ");

                scanf("%s", infix);

                infixToPostfix(infix, postfix);

               printf("Postfix Expression: %s\n", postfix);

                break;

            case 2:

                printf("Enter Postfix Expression: ");

                scanf("%s", postfix);


                printf("Result = %d\n", evaluatePostfix(postfix));

                break;

            case 3:

                printf("Program Ended.\n");

                break;

            default:

                printf("Invalid Choice!\n");

        }

    } while(choice != 3);


    return 0;

}


NEP-SYBCS-DS-I Assignment -4

 Assignment 4 Singly Linked List

Set A
a) Write a menu driven program for the following operations.
i. Insert an element in a linked list in a particular position
ii. Search an element in a linked list.
iii. Delete a particular element from a linked list.
 #include <stdio.h>

#include <stdlib.h>


struct node

{

    int data;

    struct node *next;

};


struct node *head = NULL;


// Insert at a particular position

void insert()

{

    struct node *newnode, *temp;

    int data, pos, i;


    newnode = (struct node *)malloc(sizeof(struct node));


    printf("Enter data: ");

    scanf("%d", &data);

    newnode->data = data;

    newnode->next = NULL;


    printf("Enter position: ");

    scanf("%d", &pos);


    if (pos == 1)

    {

        newnode->next = head;

        head = newnode;

        return;

    }


    temp = head;

    for (i = 1; i < pos - 1 && temp != NULL; i++)

        temp = temp->next;


    if (temp == NULL)

    {

        printf("Invalid Position\n");

        free(newnode);

        return;

    }


    newnode->next = temp->next;

    temp->next = newnode;


    printf("Node Inserted Successfully.\n");

}


// Search an element

void search()

{

    struct node *temp = head;

    int key, pos = 1;


    printf("Enter element to search: ");

    scanf("%d", &key);


    while (temp != NULL)

    {

        if (temp->data == key)

        {

            printf("Element found at position %d\n", pos);

            return;

        }

        temp = temp->next;

        pos++;

    }


    printf("Element not found.\n");

}


// Delete a particular element

void deleteNode()

{

    struct node *temp = head, *prev = NULL;

    int key;


    printf("Enter element to delete: ");

    scanf("%d", &key);


    while (temp != NULL && temp->data != key)

    {

        prev = temp;

        temp = temp->next;

    }


    if (temp == NULL)

    {

        printf("Element not found.\n");

        return;

    }


    if (prev == NULL)

        head = temp->next;

    else

        prev->next = temp->next;


    free(temp);


    printf("Element deleted successfully.\n");

}


// Display linked list

void display()

{

    struct node *temp = head;


    if (head == NULL)

    {

        printf("List is Empty.\n");

        return;

    }


    printf("Linked List: ");


    while (temp != NULL)

    {

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

        temp = temp->next;

    }


    printf("NULL\n");

}


int main()

{

    int choice;


    while (1)

    {

        printf("\n----- MENU -----\n");

        printf("1. Insert at Particular Position\n");

        printf("2. Search Element\n");

        printf("3. Delete Element\n");

        printf("4. Display List\n");

        printf("5. Exit\n");


        printf("Enter your choice: ");

        scanf("%d", &choice);


        switch (choice)

        {

            case 1:

                insert();

                break;


            case 2:

                search();

                break;


            case 3:

                deleteNode();

                break;


            case 4:

                display();

                break;


            case 5:

                exit(0);


            default:

                printf("Invalid Choice!\n");

        }

    }

    return 0;

}



b) Write a menu driven program for the following operations.
i. Reverse a linked list and display it.
ii. Accept an element from user and Concatenate it with a created link list.
iii. Merge two linked list and display it.

#include <stdio.h>

#include <stdlib.h>


struct node

{

    int data;

    struct node *next;

};


struct node *head1 = NULL, *head2 = NULL;


// Create a linked list

void create(struct node **head)

{

    struct node *newnode, *temp;

    int n, i;


    printf("Enter number of nodes: ");

    scanf("%d", &n);


    for(i = 0; i < n; i++)

    {

        newnode = (struct node *)malloc(sizeof(struct node));


        printf("Enter data: ");

        scanf("%d", &newnode->data);


        newnode->next = NULL;


        if(*head == NULL)

        {

            *head = newnode;

        }

        else

        {

            temp = *head;

            while(temp->next != NULL)

                temp = temp->next;


            temp->next = newnode;

        }

    }

}


// Display linked list

void display(struct node *head)

{

    while(head != NULL)

    {

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

        head = head->next;

    }

    printf("NULL\n");

}


// Reverse linked list

void reverse()

{

    struct node *prev = NULL, *curr = head1, *next;


    while(curr != NULL)

    {

        next = curr->next;

        curr->next = prev;

        prev = curr;

        curr = next;

    }


    head1 = prev;


    printf("Reversed Linked List:\n");

    display(head1);

}


// Concatenate one element

void concatenate()

{

    struct node *newnode, *temp;


    newnode = (struct node *)malloc(sizeof(struct node));


    printf("Enter element: ");

    scanf("%d", &newnode->data);


    newnode->next = NULL;


    if(head1 == NULL)

    {

        head1 = newnode;

    }

    else

    {

        temp = head1;

        while(temp->next != NULL)

            temp = temp->next;


        temp->next = newnode;

    }


    printf("After Concatenation:\n");

    display(head1);

}


// Merge two linked lists

void merge()

{

    struct node *temp;


    if(head1 == NULL)

    {

        head1 = head2;

    }

    else

    {

        temp = head1;


        while(temp->next != NULL)

            temp = temp->next;


        temp->next = head2;

    }


    printf("Merged Linked List:\n");

    display(head1);

}


int main()

{

    int ch;


    printf("Create First Linked List\n");

    create(&head1);


    while(1)

    {

        printf("\n----- MENU -----\n");

        printf("1. Reverse Linked List\n");

        printf("2. Concatenate Element\n");

        printf("3. Merge Two Linked Lists\n");

        printf("4. Display First List\n");

        printf("5. Exit\n");


        printf("Enter your choice: ");

        scanf("%d", &ch);


        switch(ch)

        {

            case 1:

                reverse();

                break;


            case 2:

                concatenate();

                break;


            case 3:

                printf("Create Second Linked List\n");

                create(&head2);

                merge();

                break;


            case 4:

                display(head1);

                break;


            case 5:

                exit(0);


            default:

                printf("Invalid Choice\n");

        }

    }


    return 0;

}



Set B
Implement a list library (doublylist.h) for a doubly linked list with Append, Insert, Search and Delete operations.
a) Write a menu driven program for the following operations.
i. Insert an element in a linked list in a particular position
ii. Search an element in a linked list.
iii. Delete a particular element from a linked list.

1. Header File: doublylist.h

#ifndef DOUBLYLIST_H
#define DOUBLYLIST_H

#include <stdio.h>
#include <stdlib.h>

struct node
{
int data;
struct node *prev;
struct node *next;
};

struct node *head = NULL;

// Append node
void append(int value)
{
struct node *newnode, *temp;

newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = value;
newnode->next = NULL;
newnode->prev = NULL;

if(head == NULL)
{
head = newnode;
}
else
{
temp = head;
while(temp->next != NULL)
temp = temp->next;

temp->next = newnode;
newnode->prev = temp;
}
}

// Display list
void display()
{
struct node *temp = head;

if(head == NULL)
{
printf("List is Empty\n");
return;
}

while(temp != NULL)
{
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Insert at position
void insert(int value, int pos)
{
struct node *newnode, *temp;
int i;

newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = value;
newnode->next = NULL;
newnode->prev = NULL;

if(pos == 1)
{
newnode->next = head;

if(head != NULL)
head->prev = newnode;

head = newnode;
return;
}

temp = head;

for(i = 1; i < pos - 1 && temp != NULL; i++)
temp = temp->next;

if(temp == NULL)
{
printf("Invalid Position\n");
free(newnode);
return;
}

newnode->next = temp->next;
newnode->prev = temp;

if(temp->next != NULL)
temp->next->prev = newnode;

temp->next = newnode;
}

// Search element
void search(int key)
{
struct node *temp = head;
int pos = 1;

while(temp != NULL)
{
if(temp->data == key)
{
printf("Element found at position %d\n", pos);
return;
}
temp = temp->next;
pos++;
}

printf("Element not found\n");
}

// Delete element
void deleteElement(int key)
{
struct node *temp = head;

while(temp != NULL && temp->data != key)
temp = temp->next;

if(temp == NULL)
{
printf("Element not found\n");
return;
}

if(temp == head)
{
head = temp->next;

if(head != NULL)
head->prev = NULL;
}
else
{
temp->prev->next = temp->next;

if(temp->next != NULL)
temp->next->prev = temp->prev;
}

free(temp);
printf("Element deleted successfully\n");
}

#endif

2. Main Program: main.c

#include "doublylist.h"

int main()
{
int ch, value, pos;

while(1)
{
printf("\n------ MENU ------\n");
printf("1. Append Element\n");
printf("2. Insert at Position\n");
printf("3. Search Element\n");
printf("4. Delete Element\n");
printf("5. Display List\n");
printf("6. Exit\n");

printf("Enter your choice: ");
scanf("%d", &ch);

switch(ch)
{
case 1:
printf("Enter element: ");
scanf("%d", &value);
append(value);
break;

case 2:
printf("Enter element and position: ");
scanf("%d%d", &value, &pos);
insert(value, pos);
break;

case 3:
printf("Enter element to search: ");
scanf("%d", &value);
search(value);
break;

case 4:
printf("Enter element to delete: ");
scanf("%d", &value);
deleteElement(value);
break;

case 5:
display();
break;

case 6:
exit(0);

default:
printf("Invalid Choice\n");
}
}

return 0;
}


b) Write a menu driven program for the following operations.
i. Reverse a linked list and display it.
ii. Accept an element from user and Concatenate it with a created link list.
iii. Merge two linked list and display it.

#include <stdio.h>

#include <stdlib.h>


struct node

{

    int data;

    struct node *next;

};


struct node *head1 = NULL, *head2 = NULL;


// Create a linked list

void create(struct node **head)

{

    struct node *newnode, *temp;

    int n, i;


    printf("Enter number of nodes: ");

    scanf("%d", &n);


    for(i = 0; i < n; i++)

    {

        newnode = (struct node *)malloc(sizeof(struct node));


        printf("Enter data: ");

        scanf("%d", &newnode->data);


        newnode->next = NULL;


        if(*head == NULL)

        {

            *head = newnode;

        }

        else

        {

            temp = *head;

            while(temp->next != NULL)

                temp = temp->next;


            temp->next = newnode;

        }

    }

}


// Display linked list

void display(struct node *head)

{

    if(head == NULL)

    {

        printf("List is Empty\n");

        return;

    }


    while(head != NULL)

    {

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

        head = head->next;

    }

    printf("NULL\n");

}


// Reverse linked list

void reverse()

{

    struct node *prev = NULL, *curr = head1, *next;


    while(curr != NULL)

    {

        next = curr->next;

        curr->next = prev;

        prev = curr;

        curr = next;

    }


    head1 = prev;


    printf("Reversed Linked List:\n");

    display(head1);

}


// Concatenate an element

void concatenate()

{

    struct node *newnode, *temp;


    newnode = (struct node *)malloc(sizeof(struct node));


    printf("Enter element to concatenate: ");

    scanf("%d", &newnode->data);


    newnode->next = NULL;


    if(head1 == NULL)

    {

        head1 = newnode;

    }

    else

    {

        temp = head1;


        while(temp->next != NULL)

            temp = temp->next;


        temp->next = newnode;

    }


    printf("After Concatenation:\n");

    display(head1);

}


// Merge two linked lists

void merge()

{

    struct node *temp;


    printf("Create Second Linked List\n");

    create(&head2);


    if(head1 == NULL)

    {

        head1 = head2;

    }

    else

    {

        temp = head1;


        while(temp->next != NULL)

            temp = temp->next;


        temp->next = head2;

    }


    printf("Merged Linked List:\n");

    display(head1);

}


int main()

{

    int ch;


    printf("Create First Linked List\n");

    create(&head1);


    while(1)

    {

        printf("\n----- MENU -----\n");

        printf("1. Reverse Linked List\n");

        printf("2. Concatenate Element\n");

        printf("3. Merge Two Linked Lists\n");

        printf("4. Display Linked List\n");

        printf("5. Exit\n");


        printf("Enter your choice: ");

        scanf("%d", &ch);


        switch(ch)

        {

            case 1:

                reverse();

                break;

            case 2:

                concatenate();

                break;

            case 3:

                merge();

                break;

            case 4:

                display(head1);

                break;

            case 5:

                exit(0);

            default:

                printf("Invalid Choice\n");

        }

    }

    return 0;

}



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

struct node
{
    int coeff;
    int exp;
    struct node *next;
};

// Create polynomial
void create(struct node **head)
{
    struct node *newnode, *temp;
    int n, i;

    printf("Enter number of terms: ");
    scanf("%d", &n);

    for(i = 0; i < n; i++)
    {
        newnode = (struct node *)malloc(sizeof(struct node));

        printf("Enter coefficient and exponent: ");
        scanf("%d%d", &newnode->coeff, &newnode->exp);

        newnode->next = NULL;

        if(*head == NULL)
        {
            *head = newnode;
        }
        else
        {
            temp = *head;
            while(temp->next != NULL)
                temp = temp->next;

            temp->next = newnode;
        }
    }
}

// Display polynomial
void display(struct node *head)
{
    while(head != NULL)
    {
        printf("%dx^%d", head->coeff, head->exp);

        if(head->next != NULL)
            printf(" + ");

        head = head->next;
    }
    printf("\n");
}

// Add two polynomials
struct node* add(struct node *p1, struct node *p2)
{
    struct node *result = NULL, *temp = NULL, *newnode;

    while(p1 != NULL && p2 != NULL)
    {
        newnode = (struct node *)malloc(sizeof(struct node));
        newnode->next = NULL;

        if(p1->exp == p2->exp)
        {
            newnode->coeff = p1->coeff + p2->coeff;
            newnode->exp = p1->exp;
            p1 = p1->next;
            p2 = p2->next;
        }
        else if(p1->exp > p2->exp)
        {
            newnode->coeff = p1->coeff;
            newnode->exp = p1->exp;
            p1 = p1->next;
        }
        else
        {
            newnode->coeff = p2->coeff;
            newnode->exp = p2->exp;
            p2 = p2->next;
        }

        if(result == NULL)
        {
            result = temp = newnode;
        }
        else
        {
            temp->next = newnode;
            temp = newnode;
        }
    }

    while(p1 != NULL)
    {
        newnode = (struct node *)malloc(sizeof(struct node));
        newnode->coeff = p1->coeff;
        newnode->exp = p1->exp;
        newnode->next = NULL;

        temp->next = newnode;
        temp = newnode;
        p1 = p1->next;
    }

    while(p2 != NULL)
    {
        newnode = (struct node *)malloc(sizeof(struct node));
        newnode->coeff = p2->coeff;
        newnode->exp = p2->exp;
        newnode->next = NULL;

        temp->next = newnode;
        temp = newnode;
        p2 = p2->next;
    }

    return result;
}

int main()
{
    struct node *poly1 = NULL, *poly2 = NULL, *result = NULL;

    printf("Enter First Polynomial (Descending order of exponents)\n");
    create(&poly1);

    printf("\nEnter Second Polynomial (Descending order of exponents)\n");
    create(&poly2);

    printf("\nFirst Polynomial:\n");
    display(poly1);

    printf("Second Polynomial:\n");
    display(poly2);

    result = add(poly1, poly2);

    printf("Resultant Polynomial:\n");
    display(result);

    return 0;
}

Friday, June 19, 2026

NEP-SYBCS- DS-I Assignment- 3

 Assignment 3 Sorting Algorithms –Counting Sort, Merge Sort, Quick Sort


Set A
a) Accept the array of n integers from user and sort the array in ascending order by using
recursive Counting sort algorithm.
/*
 * C Program for counting sort
 */
#include <stdio.h>  
 
void counting_sort(int A[], int k, int n)
{
    int i, j;
    int B[15], C[100];
    for (i = 0; i <= k; i++)
        C[i] = 0;
    for (j = 1; j <= n; j++)
        C[A[j]] = C[A[j]] + 1;
    for (i = 1; i <= k; i++)
        C[i] = C[i] + C[i-1];
    for (j = n; j >= 1; j--)
    {
        B[C[A[j]]] = A[j];
        C[A[j]] = C[A[j]] - 1;
    }
    printf("The Sorted array is : ");
    for (i = 1; i <= n; i++)
        printf("%d ", B[i]);
}

int main()
{
    int n, k = 0, A[15], i;
    printf("Enter the number of input : ");
    scanf("%d", &n);
    printf("\nEnter the elements to be sorted :\n");
    for (i = 1; i <= n; i++)
    {
        scanf("%d", &A[i]);
        if (A[i] > k) {
            k = A[i];
        }
    }
    counting_sort(A, k, n);
    printf("\n");
    return 0;
}


b) Create random array of n integers and sort the array in ascending order by using recursive
Merge sort algorithm
Program

#include<stdio.h>
merge(int a[10],int l,int m,int u)
{
                int c[10],i,j,k;
                i=l;
                j=m+1;
                k=0;
                while(i<=m && j<=u)
                {
                                if(a[i]<a[j])
                                {
                                                c[k]=a[i];
                                                k++;i++;
                                }
                                else
                                {
                                                c[k]=a[j];
                                                k++;j++;
                                }
                }
                while(i<=m)
                {
                                c[k]=a[i];
                                i++;k++;
                }
                while(j<=u)
                {
                                c[k]=a[j];
                                k++;j++;
                }
                for(i=l,j=0;i<=u;i++,j++)
                                a[i]=c[j];
}

void generate(int a[10],int n)
{
                int i;
                for(i=0;i<n;i++)
                                a[i]=rand()%10;
}

merge_sort(int a[10],int i,int j)
{
                int k=0;
                if(i<j)
                {
                                k=(i+j)/2;
                                merge_sort(a,i,k);
                                merge_sort(a,k+1,j);
                                merge(a,i,k,j);
                }
}

main()
{
                int i,n,a[10];
                printf("how many elements:");
                scanf("%d",&n);
                generate(a,n);
                printf("elements are:\n");
                for(i=0;i<n;i++)
                                printf("%d\t",a[i]);
                merge_sort(a,0,n-1);
                printf("\nafter sorting:\n");
                for(i=0;i<n;i++)
                printf("%d\t",a[i]);
}



c) Accept the array of n integers from user and sort the array in ascending order by using
recursive Quick sort algorithm.
Program

#include<stdio.h>
enum bool {false,true};
void disp(int a[],int l,int u)
{
                int i;
                for(i=l;i<=u;i++)
                                printf("%d\t",a[i]);
}

void quick(int a[],int l,int u)
{
                int temp,piv,left,right;
                enum bool pivot_places=false;
                left=l;
                right=u;
                piv=l;
                if(l>=u)
                                return;
                printf("\nsublist:\n");
                disp(a,l,u);
                while(pivot_places==false)
                {
                                while(a[piv]<=a[right] && piv!=right)
                                                right--;
                                if(piv==right)
                                                pivot_places=true;
                                if(a[piv]>a[right])
                                {
                                                temp=a[piv];
                                                a[piv]=a[right];
                                                a[right]=temp;
                                                piv=right;
                                }
                                while(a[piv]>=a[left] && piv!=left)
                                                left++;
                                if(piv==left)
                                                pivot_places=true;
                                if(a[piv]<a[left])
                                {
                                                temp=a[piv];
                                                a[piv]=a[left];
                                                a[left]=temp;
                                                piv=left;
                                }
                }
                disp(a,l,u);
                quick(a,l,piv-1);
                quick(a,piv+1,u);
}

void generate(int a[],int n)
{
                int i;
                for(i=0;i<n;i++)
                                a[i]=rand()%20;
}

main()
{
                int a[10],n,i;
                printf("how many elements:");
                scanf("%d",&n);
                generate(a,n);
                printf("\nelements are:");
                for(i=0;i<n;i++)
                                printf("%d\t",a[i]);
                quick(a,0,n-1);
                printf("\nafter sorting:\n");
                for(i=0;i<n;i++)
                                printf("%d\t",a[i]);
}


Set B
a) Read the data from the ‘employee.txt’ file and sort on age using Counting sort and Merge
sort. Write the sorted data to another file 'sortedemponage.txt'.

employee.txt (Input File)

Amit 25 30000
Neha 22 35000
Rahul 30 45000
Priya 24 40000
Karan 28 38000

(Format: Emp_name Age Salary)

1. Counting Sort on Age

#include <stdio.h>

#include <string.h>

struct Employee

{

    char name[30];

    int age;

    float salary;

};


int main()

{

    struct Employee emp[100], output[100];

    int count[101] = {0};

    int n = 0, i;


    FILE *fp, *fs;


    fp = fopen("employee.txt", "r");


    if (fp == NULL)

    {

        printf("File not found.\n");

        return 1;

    }


    while (fscanf(fp, "%s%d%f",

                  emp[n].name,

                  &emp[n].age,

                  &emp[n].salary) != EOF)

    {

        n++;

    }


    fclose(fp);


    // Count frequency of ages

    for (i = 0; i < n; i++)

        count[emp[i].age]++;


    // Cumulative count

    for (i = 1; i <= 100; i++)

        count[i] += count[i - 1];


    // Build output array

    for (i = n - 1; i >= 0; i--)

    {

        output[count[emp[i].age] - 1] = emp[i];

        count[emp[i].age]--;

    }


    fs = fopen("sortedemponage.txt", "w");


    fprintf(fs, "Name\tAge\tSalary\n");


    for (i = 0; i < n; i++)

        fprintf(fs, "%s\t%d\t%.2f\n",

                output[i].name,

                output[i].age,

                output[i].salary);


    fclose(fs);


    printf("Data sorted using Counting Sort.\n");


    return 0;

}



2. Merge Sort on Age

#include <stdio.h>

#include <string.h>


struct Employee

{

    char name[30];

    int age;

    float salary;

};


void merge(struct Employee a[], int low, int mid, int high)

{

    struct Employee temp[100];

    int i = low, j = mid + 1, k = low;


    while (i <= mid && j <= high)

    {

        if (a[i].age <= a[j].age)

            temp[k++] = a[i++];

        else

            temp[k++] = a[j++];

    }


    while (i <= mid)

        temp[k++] = a[i++];


    while (j <= high)

        temp[k++] = a[j++];


    for (i = low; i <= high; i++)

        a[i] = temp[i];

}


void mergeSort(struct Employee a[], int low, int high)

{

    if (low < high)

    {

        int mid = (low + high) / 2;

        mergeSort(a, low, mid);

        mergeSort(a, mid + 1, high);

        merge(a, low, mid, high);

    }

}


int main()

{

    struct Employee emp[100];

    int n = 0, i;


    FILE *fp, *fs;


    fp = fopen("employee.txt", "r");


    if (fp == NULL)

    {

        printf("File not found.\n");

        return 1;

    }


    while (fscanf(fp, "%s%d%f",

                  emp[n].name,

                  &emp[n].age,

                  &emp[n].salary) != EOF)

    {

        n++;

    }


    fclose(fp);


    mergeSort(emp, 0, n - 1);


    fs = fopen("sortedemponage.txt", "w");


    fprintf(fs, "Name\tAge\tSalary\n");


    for (i = 0; i < n; i++)

        fprintf(fs, "%s\t%d\t%.2f\n",

                emp[i].name,

                emp[i].age,

                emp[i].salary);


    fclose(fs);


    printf("Data sorted using Merge Sort.\n");


    return 0;

}


b) Read the data from the file and sort on names in alphabetical order (use strcmp) using Quick
sort and write the sorted data to another file 'sortedemponname.txt'.

employee.txt (Input File)

Amit 25 30000
Neha 22 35000
Rahul 30 45000
Priya 24 40000
Karan 28 38000

Format: Emp_name Age Salary


C Program (Quick Sort on Employee Name)

#include <stdio.h>
#include <string.h>

struct Employee
{
char name[30];
int age;
float salary;
};

void quickSort(struct Employee emp[], int low, int high);
int partition(struct Employee emp[], int low, int high);

int main()
{
struct Employee emp[100];
int n = 0, i;
FILE *fp, *fs;

fp = fopen("employee.txt", "r");

if (fp == NULL)
{
printf("File not found.\n");
return 1;
}

while (fscanf(fp, "%s%d%f",
emp[n].name,
&emp[n].age,
&emp[n].salary) != EOF)
{
n++;
}

fclose(fp);

quickSort(emp, 0, n - 1);

fs = fopen("sortedemponname.txt", "w");

if (fs == NULL)
{
printf("Cannot create output file.\n");
return 1;
}

fprintf(fs, "Name\tAge\tSalary\n");

for (i = 0; i < n; i++)
{
fprintf(fs, "%s\t%d\t%.2f\n",
emp[i].name,
emp[i].age,
emp[i].salary);
}

fclose(fs);

printf("Data sorted successfully using Quick Sort.\n");
printf("Sorted data stored in sortedemponname.txt\n");

return 0;
}

int partition(struct Employee emp[], int low, int high)
{
struct Employee pivot = emp[high], temp;
int i = low - 1, j;

for (j = low; j < high; j++)
{
if (strcmp(emp[j].name, pivot.name) < 0)
{
i++;
temp = emp[i];
emp[i] = emp[j];
emp[j] = temp;
}
}

temp = emp[i + 1];
emp[i + 1] = emp[high];
emp[high] = temp;

return i + 1;
}

void quickSort(struct Employee emp[], int low, int high)
{
if (low < high)
{
int p = partition(emp, low, high);
quickSort(emp, low, p - 1);
quickSort(emp, p + 1, high);
}
}