0% found this document useful (0 votes)
58 views8 pages

DSA Theory Exam

The document contains two programming questions. The first asks to implement a LinkList class with functions to construct an ordered list from two sorted linked lists. The code provided implements this using a merge sort approach. The second asks to implement a PriorityQueue class with an emergency dequeue function. The code dequeues the requested flight by stacking other flights and rebuilding the queue.

Uploaded by

Anonymous
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views8 pages

DSA Theory Exam

The document contains two programming questions. The first asks to implement a LinkList class with functions to construct an ordered list from two sorted linked lists. The code provided implements this using a merge sort approach. The second asks to implement a PriorityQueue class with an emergency dequeue function. The code dequeues the requested flight by stacking other flights and rebuilding the queue.

Uploaded by

Anonymous
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Name: Amna Zarish

Reg No: SP19-BSE-026

QUESTION NO 1:
Implement the following class (10)

struct node

int num;

node * next;

};

class LinkList

private:

node * start;

public:

constructAnOrderedList (node *orderedList1, node * orderedList2);

};

Answer:
struct node {

int num;

node* next;

};

class LinkList{
private:

node *start;

public:

void merge_sort(node** headPtr)

node* head = *headPtr;

node* n1;

node* n2;

if ((head == NULL) || (head.next == NULL)) {

return;

split(head, &n1, &n2);

merge_sort(&n1);

merge_sort(&n2);

/* answer = merge the two sorted lists together */

*headPtr = constructAnOrderedList(n1, n2);

node* sorted_merge(node* a, node* b)

{
node* result = NULL;

if (a == NULL)

return (b);

else if (b == NULL)

return (a);

if (a.num <= b.num) {

result = a;

result.next = sorted_merge(a.next, b);

else {

result = b;

result.next = sorted_merge(a, b.next);

return result;

void split(node* source,

node** frontRef, node** backRef)

node* fast;

node* slow;

slow = source;
fast = source.next;

while (fast != NULL) {

fast = fast.next;

if (fast != NULL) {

slow = slow.next;

fast = fast.next;

*frontRef = source;

*backRef = slow.next;

slow.next = NULL;

void printList(node* node)

while (node != NULL) {

cout << node.num << " ";

node = node.next;

void insert(node** head_ref, int new_data)


{

node* new_node = new Node();

new_node.data = new_data;

new_node.next = (*head_ref);

(*head_ref) = new_node;

int constructDemoList()

node* res = NULL;

node* a = NULL;

push(&a, 15);

push(&a, 10);

push(&a, 5);

push(&a, 20);

push(&a, 3);

push(&a, 2);

merge_sort(&a);

cout << "Sorted Linked List is: \n";

printList(a);

return 0;
}

};

void main()

LinkList list = new LinkList();

list.constructDemoList();

Question No 2:
Implement the following class (10)

struct flightInfo

int flightId;

int fuel;

};

const int size = 100;

class PriorityQueue

private:

flightInfo FlightQueue[size];

int tail;

int front;

public:

DeQueueFlighFacingEmergency(int FlightNumber);

};
Answer:
#include <iostream>

using namespace std;

struct flightInfo

    int flightId;

    int fuel;

};

const int size = 20;

class PriorityQueue

    private:

        flightInfo FlightQueue[size];

        int tail;

        int Front;

        int counter;

    public:

    

        void DeQueueFlighFacingEmergency(int FlightNumber)

    {

             flightInfo Stk[size];

            int  front= 0;

          
            while(FlightQueue[tail-1].flightId != FlightNumber)

      {

                Stk[front++] = FlightQueue[--tail];

      }

            tail--;

            while (front >= 0)

      {

                FlightQueue[tail++] = Stk[--front];

      }

            counter--;

    }

};

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy