Thursday, June 25, 2026

NEP-SYBCS -DBMS-I Assignment-2

 Assignment no.2 Data Definition Query (Create simple tables with referential integrity constraint).

Set A

Create tables for the information given below by giving appropriate integrity constraints as specified.

1. Create the following tables:

Table Name :-Property

Columns Column Name         Column Data Type         Constraints

1                 P-number             integer                             Primary key

2                 Description          varchar (50)                     Not null

3                 Area                     char(10)

Table Name:- Owner

Columns Column Name         Column Data Type         Constraints

1             Owner-name             varchar(50)                     Primary key

2             Address                     varchar (50)

3             Phone no                    integer

Relationship - A one-many relationships between owner and property. Define reference keys accordingly.

Queries:-

CREATE TABLE Owner (

    "Owner-name" VARCHAR(50) PRIMARY KEY,

    Address VARCHAR(50),

    "Phone no" INTEGER

);


CREATE TABLE Property (

    "P-number" INTEGER PRIMARY KEY,

    Description VARCHAR(50) NOT NULL,

    Area CHAR(10),

    "Owner-name" VARCHAR(50),

    FOREIGN KEY ("Owner-name") REFERENCES Owner("Owner-name")

);



2. Create the following tables:

Table Name:- Hospital

Columns Column Name         Column Data Type     Constraints

1             Hno                             integer                     Primary key

2             Name                         varchar (50)              Not null

3             City                             char(10)

Table Name:- Doctor

Columns         Column Name         Column Data Type         Constraints

1                     Dno                             integer                         Primary key

2                     Dname                         varchar (50)

3                     City                             char(10)

Relationship - A many-many relationships between hospital and doctor.

Query:-

CREATE TABLE Hospital (

    Hno INTEGER PRIMARY KEY,

    Name VARCHAR(50) NOT NULL,

    City CHAR(10)

);


CREATE TABLE Doctor (

    Dno INTEGER PRIMARY KEY,

    Dname VARCHAR(50),

    City CHAR(10)

);

CREATE TABLE Hospital_Doctor (

    Hno INTEGER,

    Dno INTEGER,

    PRIMARY KEY (Hno, Dno),

    FOREIGN KEY (Hno) REFERENCES Hospital(Hno),

    FOREIGN KEY (Dno) REFERENCES Doctor(Dno)

);


3. Create the following tables:

Table Name:- Patient

Columns    Column Name         Column Data Type         Constraints

1                 Pno                             integer                         Primary key

2                 Name                         varchar (50)                  Not null

3                 Address                      varchar(50)

Table Name:- Bed

Columns     Column Name         Column Data Type     Constraints

1                 Bedno                         integer                             Primary key

2                 Roomno                     integer                             Primary key

3                 Description                 varchar(50)

Relationship - a one–to-one relationship between Patient & Bed.

Query:-

CREATE TABLE Bed (

    Bedno INTEGER,

    Roomno INTEGER,

    Description VARCHAR(50),

    PRIMARY KEY (Bedno, Roomno)

);


CREATE TABLE Patient (

    Pno INTEGER PRIMARY KEY,

    Name VARCHAR(50) NOT NULL,

    Address VARCHAR(50),

    Bedno INTEGER,

    Roomno INTEGER UNIQUE,

    FOREIGN KEY (Bedno, Roomno) REFERENCES Bed(Bedno, Roomno)

);


Set B

1. Create the following tables:

Table Name:- Student

Columns     Column Name     Column Data Type         Constraints

1                 Sno                         integer                                 Primary key

2                 Name                     varchar (50)                         Not null

3                 Address                  varchar(50)

4                 Class                     varchar(10)

Table Name Teacher

Columns Column Name Column Data Type Constraints

1                 Tno                     integer                     Primary key

2                 Tname                integer                     Primary key

3                 Qualification     varchar(50)

4                 TotalExperience     float

5                 Salary                 float                         Should be greater than 0

Relationship - A Many–to-Many relationships between Student and Teacher with descriptive

attribute Marks.

Query:-

CREATE TABLE Student (

    Sno INTEGER PRIMARY KEY,

    Name VARCHAR(50) NOT NULL,

    Address VARCHAR(50),

    Class VARCHAR(10)

);


CREATE TABLE Teacher (

    Tno INTEGER PRIMARY KEY,

    Tname VARCHAR(50),

    Qualification VARCHAR(50),

    TotalExperience FLOAT,

    Salary FLOAT CHECK (Salary > 0)

);


CREATE TABLE Student_Teacher (

    Sno INTEGER,

    Tno INTEGER,

    Marks INTEGER,

    PRIMARY KEY (Sno, Tno),

    FOREIGN KEY (Sno) REFERENCES Student(Sno),

    FOREIGN KEY (Tno) REFERENCES Teacher(Tno)

);

2. Create the following tables:

Table Name Project

Columns       Column Name         Column Data Type         Constraints

1                     Pno                             integer                             Primary key

2                     Pname                         varchar (30)                     Not null

3                     Ptype                         char(20)

4                     Duration                     integer

Table Name Employee

Columns     Column Name         Column Data Type         Constraints

1                     Eno                             integer                         Primary key

2                     Ename                         varchar(20)

3                     Qualification               varchar(50)

4                     Join_Date                     date

5                     Salary                         float                     Should be greater than 0

Relationship -A Many–to-Many relationships between Project and Employee with descriptive

attribute Start date(date), no_of_hours_worked(integer).

Query:-

CREATE TABLE Project (

    Pno INTEGER PRIMARY KEY,

    Pname VARCHAR(30) NOT NULL,

    Ptype CHAR(20),

    Duration INTEGER

);

CREATE TABLE Employee (

    Eno INTEGER PRIMARY KEY,

    Ename VARCHAR(20),

    Qualification VARCHAR(50),

    Join_Date DATE,

    Salary FLOAT CHECK (Salary > 0)

);

CREATE TABLE Project_Employee (

    Pno INTEGER,

    Eno INTEGER,

    Start_Date DATE,

    No_of_Hours_Worked INTEGER,

    PRIMARY KEY (Pno, Eno),

    FOREIGN KEY (Pno) REFERENCES Project(Pno),

    FOREIGN KEY (Eno) REFERENCES Employee(Eno)

);




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