DSA LAB CHALLENGING TASKS
TRISHIT GUPTA
                                                         20BIT0374
Q7) #include<stdio.h>
#define n 5
struct cd
{ int que[n];
int front;
int rear;
}q;
int isfull()
{if(q.front==(q.rear+1)%n)
return 1;
else
return 0;
}
int isempty()
{
if(q.front==-1)
return 1;
else
return 0;
}
void enqueue(int data)
{ if(q.front==-1)
q.front=q.rear=0;
else
q.rear =(q.rear+1)%n;
q.que[q.rear]=data;
}
int dequeue()
{ int item;
item=q.que[q.front];
if(q.front==q.rear)
q.front=q.rear=-1;
else
q.front =(q.front+1)%n;
return item;
}
void display()
{ if(isempty())
{
printf("There is no data to display\n");
return;
}
int i;
i=q.front;
while(i!=q.rear)
{
printf("%d\n",q.que[i]);
i=(i+1)%n; }
printf("%d\n",q.que[i]);
}
int main()
{ int c,data;
q.front=-1;
q.rear=0;
while(c!=4)
{
printf("\n\n The CD Menu:\n1.Write data on the Circular Buffer \n 2.Burn data on the
DVD\n3.Display the data on the Circular Buffer \n4.STOP\n");
printf("\n Your option");
scanf("%d",&c);
switch(c)
{ case 1:
if(isfull())
printf("The data limit is reached. Please burn the data\n");
else
{
printf("Enter data: \n");
scanf("%d",&data);
enqueue(data);
}
break;
case 2:
if(isempty())
printf("Circular Buffer is Empty: Please Enter New Data to Read\n");
else
printf("The Data Burned on the DVD is %d\n", dequeue());
break;
case 3:
display();
break;
case 4:
printf("Thank you for using the Circular Buffer");
break;
}}
return 0;
}
Q8)
#include<iostream>
using namespace std;
int arr[20];int k=0,a=0,ser=0;
struct node
{
     int data;
     struct node *next;
};
struct node *top=NULL;
bool isempty()
{
     if(top==NULL)
         return true;
     else
         return false;
}
void push()
{
     struct node *temp;int id;
     temp=new node;
     cout<<"Input id of truck to be moved into garage : ";
     cin>>id;
     if(arr[a]==id)
     {
                   temp->data=id;ser++;
                   if(top==NULL)
                   {
                          temp->next=NULL;
                            top=temp;
                    }
                    else
                    {
                            temp->next=top;
                            top=temp;
                    }a++;
           }
           else
           {
                    cout<<"Truck "<<id<<" cannot be moved\n";
           }
}
void pop()
{
    int x;struct node *temp;
    if(isempty())
    {
        cout<<"Stack is empty\n";
    }
    else
    {
        x=top->data;
        temp=top;
        top=top->next;
        delete(temp);
        cout<<"Poped out truck id "<<x;
    }
}
void display()
{
    struct node *temp;
    temp=top;
    while(temp!=NULL)
    {
        cout<<temp->data<<"\n";
        temp=temp->next;
    }
}
void truck(int id)
{
            arr[k++]=id;
}
int main()
{
    int ch,id;
    while(ch!=5)
    {
    cout<<"\n\n1-Input Truck on road\n2-Push in garage\n3-Pop out of garage\n4-
Display garage stack\n5-Exit\nenter your choice : ";
        cin>>ch;
        switch(ch)
        {
                           case 1:cout<<"Enter truck id on road: ";
                                         cin>>id;
                                         truck(id);
                                         break;
            case 2:push();break;
            case 3:pop();break;
            case 4:display();break;
                           case 5:break;
        }
    }
    return 0;
}
Q9) #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
struct node
{
       struct node *prev;
       struct node *next;
       int roll;
       char name[10];
       int age;
};
struct node *head;
struct node *tail;
void insertion()
{
       struct node *ptr=(struct node *)malloc(sizeof(struct node));
       int r;
       char n[10];
       int a;
       printf("Enter Student Name: ");
       scanf("%s",&n);
       printf("Enter Student Roll Number: ");
       scanf("%d",&r);
       printf("Enter Student Age: ");
       scanf("%d",&a);
       strcpy(ptr->name,n);
       ptr->roll=r;
       ptr->age=a;
       if(head==NULL && tail==NULL)
       {
                ptr->prev=NULL;
                ptr->next=NULL;
                head=ptr;
                tail=ptr;
       }
       else if(head->prev==NULL && tail->next==NULL)
       {
                tail->next=ptr;
                ptr->next=NULL;
                ptr->prev=tail;
                tail=ptr;
       }
       else if(head->prev!=NULL && tail->next!=NULL)
       {
                ptr->prev=tail;
                ptr->next=head;
                tail->next=ptr;
                head->prev=ptr;
       }
       printf("\nStudent Record Inserted");
}
void low_level()
{
       struct node *ptr;
       int r;
       bool flag=false;
int k=0;
ptr=head;
if(ptr==NULL)
{
       printf("List is Empty");
}
else
{
       printf("Enter the Roll Number of the Student Record: ");
       scanf("%d",&r);
       while(ptr!=NULL)
       {
                if(ptr==head)
                {
                         k++;
                         if(k==2)
                         {
                                flag=false;
                                break;
                         }
                }
                if(ptr->roll==r && ptr==head && ptr==tail)
           {
                printf("\nNo Predecessor or Successor Records to display");
                flag=true;
                break;
                }
                else if(ptr->roll==r && ptr==head && ptr->prev==NULL)
                {
                               printf("\n\nStudent Name of Successor Record: %s",ptr-
>next->name);
                               printf("\nStudent Roll Number of Successor Record: %d",ptr-
>next->roll);
                               printf("\nStudent Age of Successor Record: %d",ptr->next-
>age);
                               printf("\nNo Predecessor Record as this is the First Student
Record");
                               flag=true;
                               break;
                        }
                        else if(ptr->roll==r && ptr==tail && ptr->next==NULL)
                        {
                               printf("\nStudent Name of Predecessor Record: %s",ptr-
>prev->name);
                               printf("\nStudent Roll Number of Predecessor Record:
%d",ptr->prev->roll);
                               printf("\nStudent Age of Predecessor Record: %d",ptr->prev-
>age);
                               printf("\nNo Successor Record as this is the Last Student
Record");
                               flag=true;
                               break;
                        }
                    else if((ptr->roll==r && ptr!=head && ptr!=tail) || (ptr->roll==r &&
ptr==head && ptr->prev!=NULL) || (ptr->roll==r && ptr==tail && ptr->next!=NULL))
                        {
                               printf("\nStudent Name of Predecessor Record: %s",ptr-
>prev->name);
                               printf("\nStudent Roll Number of Predecessor Record:
%d",ptr->prev->roll);
                               printf("\nStudent Age of Predecessor Record: %d",ptr->prev-
>age);
                                    printf("\n\nStudent Name of Successor Record: %s",ptr-
>next->name);
                                    printf("\nStudent Roll Number of Successor Record: %d",ptr-
>next->roll);
                                    printf("\nStudent Age of Successor Record: %d",ptr->next-
>age);
                                    flag=true;
                                    break;
                         }
                         ptr=ptr->next;
                  }
                  if(flag==false)
                  {
                         printf("\nStudent Record not found");
             }
         }
}
void middle_level()
{
         struct node *ptr;
         struct node *temp;
         int r;
         bool flag=false;
         int k=0;
         ptr=head;
         if(ptr==NULL)
         {
                  printf("List is Empty");
         }
         else
      {
            printf("Enter the Roll Number of the Student Record: ");
            scanf("%d",&r);
            while(ptr!=NULL)
            {
                    if(ptr==head)
                    {
                             k++;
                             if(k==2)
                             {
                                      flag=false;
                                  break;
                             }
                    }
                    if(ptr->roll==r && ptr==head && ptr==tail)
                {
                    printf("\nNo Predecessor or Successor Records to display");
                    head=NULL;
                    tail=NULL;
                    free(head);
                    free(tail);
                    printf("\nStudent Record has been successfully deleted");
                    flag=true;
                    break;
                    }
                    else if(ptr->roll==r && ptr==head)
                    {
                             printf("\n\nStudent Name of Successor Record: %s",ptr-
>next->name);
                               printf("\nStudent Roll Number of Successor Record: %d",ptr-
>next->roll);
                               printf("\nStudent Age of Successor Record: %d",ptr->next-
>age);
                               printf("\nNo Predecessor Record as this is the First Student
Record");
                               head=head->next;
                               head->prev=NULL;
                               free(ptr);
                               printf("\nStudent Record has been successfully deleted");
                               flag=true;
                               break;
                        }
                        else if(ptr->roll==r && ptr==tail)
                        {
                               printf("\nStudent Name of Predecessor Record: %s",ptr-
>prev->name);
                               printf("\nStudent Roll Number of Predecessor Record:
%d",ptr->prev->roll);
                               printf("\nStudent Age of Predecessor Record: %d",ptr->prev-
>age);
                               printf("\nNo Successor Record as this is the Last Student
Record");
                               tail=tail->prev;
                               tail->next=NULL;
                               free(ptr);
                               printf("\nStudent Record has been successfully deleted");
                               flag=true;
                               break;
                        }
                    else if((ptr->roll==r && ptr!=head && ptr!=tail) || (ptr->roll==r &&
ptr==head && ptr->prev!=NULL) || (ptr->roll==r && ptr==tail && ptr->next!=NULL))
                        {
                                   printf("\nStudent Name of Predecessor Record: %s",ptr-
>prev->name);
                                   printf("\nStudent Roll Number of Predecessor Record:
%d",ptr->prev->roll);
                                   printf("\nStudent Age of Predecessor Record: %d",ptr->prev-
>age);
                                   printf("\n\nStudent Name of Successor Record: %s",ptr-
>next->name);
                                   printf("\nStudent Roll Number of Successor Record: %d",ptr-
>next->roll);
                                   printf("\nStudent Age of Successor Record: %d",ptr->next-
>age);
                                   ptr->prev->next=ptr->next;
                                   ptr->next->prev=ptr->prev;
                                   free(ptr);
                                   printf("\nStudent Record has been successfully deleted");
                                   flag=true;
                                   break;
                        }
                        ptr=ptr->next;
                 }
                 if(flag==false)
                 {
                        printf("\nStudent Record not found");
             }
         }
}
void high_level()
{
}
int main()
{
       int choice;
       while(true)
       {
              printf(" Students Record Section");
              printf("\n\t1. Enter a Student Record");
              printf("\n\t2. Low Level: Search for a Particular Record and display the Data
of Predecessor and Successor Records");
              printf("\n\t3. Middle Level: Delete a Particular Record and display the Data
of Predecessor and Sucessor Records");
              printf("\n\t4. High Level: Doubly Linking the First and Last Records");
              printf("\n\t5. Exit");
              printf("\n\nEnter your choice: ");
              scanf("%d",&choice);
              printf("\n");
              if(choice==1)
              {
                     insertion();
              }
              else if(choice==2)
              {
                     low_level();
              }
              else if(choice==3)
              {
                     middle_level();
              }
              else if(choice==4)
              {
           }
           else if(choice==5)
           {
                  printf("\t\t\t-----Thank You-----");
                  break;
           }
           else
           {
                  printf("Invalid Choice. Please enter your choice again\n");
           }
           printf("\n");
    }
    return 0;
}