Вы находитесь на странице: 1из 8

Page 1 of 8

Data Structures and algorithms


Digital assessment

Name: Prasad Kumkar

Reg no.: 18BCB0047

Q. Show how to implement a queue using two stacks. 



Solution:

A queue can be implemented using two stacks. Let queue to be


implemented be q and stacks used to implement q be stack1 and
stack2.
q can be implemented in two ways:

By making enQueue:
enQueue(q, x)
1) While stack1 is not empty, push everything from stack1 to
stack2.
2) Push x to stack1 (assuming size of stacks is unlimited).
3) Push everything back to stack1.
Here time complexity will be O(n)

deQueue(q)
1) If stack1 is empty then error
2) Pop an item from stack1 and return it
Here time complexity will be O(1)

By making deQueue
Page 2 of 8

enQueue(q, x)
1) Push x to stack1 (assuming size of stacks is unlimited).
Here time complexity will be O(1)

deQueue(q)
1) If both stacks are empty then error.
2) If stack2 is empty
While stack1 is not empty, push everything from stack1 to
stack2.
3) Pop the element from stack2 and return it.
Here time complexity will be O(n)

Q.Given a queue Q, write a function FindMax(queue Q) that will find


and return the maximum element in the queue. 


struct QNode

    int key;

    struct QNode *next;

};

  

// The queue, front stores the front node of LL and rear stores ths

// last node of LL

struct Queue

    struct QNode *front, *rear;

};

  

// A utility function to create a new linked list node.

struct QNode* newNode(int k)

    struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode));

    temp->key = k;

    temp->next = NULL;

    return temp; 

  

// A utility function to create an empty queue

struct Queue *createQueue()

    struct Queue *q = (struct Queue*)malloc(sizeof(struct Queue));

    q->front = q->rear = NULL;

    return q;

Page 3 of 8
}

  

// The function to add a key k to q

void enQueue(struct Queue *q, int k)

    // Create a new LL node

    struct QNode *temp = newNode(k);

  

    // If queue is empty, then new node is front and rear both

    if (q->rear == NULL)

    {

       q->front = q->rear = temp;

       return;

    }

  

    // Add the new node at the end of queue and change rear

    q->rear->next = temp;

    q->rear = temp;

  

// Function to remove a key from given queue q

struct QNode *deQueue(struct Queue *q)

    // If queue is empty, return NULL.

    if (q->front == NULL)

       return NULL;

  

    // Store previous front and move front one node ahead

    struct QNode *temp = q->front;

    q->front = q->front->next;

  

    // If front becomes NULL, then change rear also as NULL

    if (q->front == NULL)

       q->rear = NULL;

    return temp;

Page 4 of 8

int Findmax(q)
{
int max_value =0;

std::size_t size = q.size();


while (size-- > 0) {
int x = q.front();
q.pop();
q.push(x);
if (x > max_value)
max_value = x;
return x;
}

int main(){

    struct Queue *q = createQueue();

    enQueue(q, 10);

    enQueue(q, 20);

    deQueue(q);

    deQueue(q);

    enQueue(q, 30);

    enQueue(q, 40);

    enQueue(q, 50);

cout<<Findmax(q);

Return 0;

Page 5 of 8

Q.Write an algorithm to reverse a linked list.

- The reverse of an empty list is an empty list.


- The reverse of one element list is same list.
- The reverse of an n element list is the reverse of the second element
followed by the first element

Input : head node of the linked list


Begin:

If (head != NULL) then


prevNode ← head
head ← head.next
curNode ← head
prevNode.next ← NULL
While (head != NULL) do
head ← head.next
curNode.next ← prevNode
prevNode ← curNode
curNode ← head
End while

head ← prevNode
End if

End

Time complexity O(n)


Space complexity O(1)
Page 6 of 8

Q.In a doubly linked list, each cell contains a data element, a


next pointer, and also a previous pointer which points to the
previous cell. Implement, (a) Insertion in the nth position using
doubly linked list. 


#include<stdio.h>  
#include<conio.h>  
#include<stdlib.h>  
  
typedef struct node  

{  
int data;  
struct  node* next,*prev;  
      
}node;  
struct node* head;  
void insertAtEnd(int x)  
{  
    struct node* temp=NULL;  
    temp=(node*)malloc(sizeof(struct node));  
    temp->data=x;  
    if(head==NULL)  
    {  
        temp->next=NULL;  
        head=temp;  
        temp->prev=head;  
      
    }  
    else  
    {  
        struct node* temp2=head;  
        while(temp2->next!=NULL)  
        {  
            temp2=temp2->next;  
        }  
        temp2->next=temp;  
        temp->prev=temp2->next;  
        temp->next=NULL;  
Page 7 of 8

    }  
}  
void print()  
{  
    struct node* temp=head;  
    while(temp!=NULL)  
    {  
        printf("%d",temp->data);  
        temp=temp->next;  
    }  
}  
void insertAtAnyPosition(int x,int pos)  
{     
    struct node* temp=NULL;  
    temp=(node*)malloc(sizeof(struct node));  
    temp->data=x;  
    if(head==NULL)  
    {  
        temp->next=NULL;  
        head=temp;  
        temp->prev=head;   
    }  
    else  
    {  
         struct node* temp2=head;  
          struct node* temp3=NULL;  
        int i=0;  
        for(i=0;i<pos-1;i++)  
        {  
            temp2=temp2->next;  
            temp3=temp2->next;  
        }  
        temp->next=temp3;  
        temp3->prev=temp->next;  
        temp2->next=temp;  
        temp->prev=temp2;  
    }  
    printf(" After Insertion at any Position  ");  
Page 8 of 8

    print();  
      
}  
void main()  
{  
    insertAtEnd(2);  
    insertAtEnd(3);  
    insertAtEnd(9);  
    insertAtEnd(8);  
    print();  
    insertAtAnyPosition(23,3);  
      
    getch();  
}  

Вам также может понравиться