Name-Pandey Kartikey Kaushal ROLL NO-1904093 SEC - E.T.C - 2 Dsa Lab Assigment

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 31

NAME- PANDEY KARTIKEY KAUSHAL

ROLL NO-1904093
SEC- E.T.C- 2

DSA LAB ASSIGMENT

Contents
SERIAL NO PAGE NUMBER

1. SECTION 1 2-12
a. Arrays
b. Dynamic array/ insert delete

2. Section 2 12-26
A. Singly Linked list
B. Polynomial add & Multipy
C. Doubly Linked List

3. Section 3
A. Stack And Ques 26- 32
SECTION 1

Q1 .Given an unsorted dynamic array arr and two


numbers x and y, find the minimum
distance between x and y in arr. The array might also
contain duplicates. You may assume that both x and y
are different and present in arr.
#include<stdio.h>
#include<stdlib.h>
int main()
{
  int *arr,n,i,lower_lim,upper_lim,min=999,counter;
   printf("Enter the size of the array");
   scanf("%d",&n);
  arr=(int*)malloc(n*sizeof(int));
  for(i=0;i<n;i++)
   {
      scanf("%d",arr+i);
   }
  printf("Enter the lowerbound:-");
   scanf("%d",&lower_lim);
  printf("Enter the upperbound:-");
   scanf("%d",&upper_lim);
  for(i=0;i<n;i++)
   {
      if(*(arr+i)==lower_lim)
       { 
           int j=i;
           while(*(arr+j)!=upper_lim){
             counter++;
             j++;
             }
             if(counter<min){
                min=counter;
             }
               
       }
       counter=0;
   }
   printf("The minimum distance between %d & %d is:- %d",upper_lim,lower_lim,min);
    return 0;
 }

Output:
Q2. WAP to find out the second smallest and second
largest element stored in a dynamic array.
ANS -

#include<stdio.h>
#include<stdlib.h>
int main()
 {
    int *arr,large=0,small=0,smax=0,smin=0,i,n;
    printf("Enter the size of the dynamic array");
     scanf("%d",&n);
    arr=(int*)malloc(n*sizeof(int));
    for(i=0;i<n;i++)
     {
         scanf("%d",arr+i);
      }
     large=0;
     small=*(arr+0);
     smax=0;
     smin=*(arr+0);
    for(i=0;i<n;i++)
    {
       if(*(arr+i)>large)
         large=*(arr+i);
       if(*(arr+i)<small)
          small=*(arr+i);
     }
     for(i=0;i<n;i++)
     {
        if((*(arr+i)>smax)&&(*(arr+i)!=large))
             smax=*(arr+i);
         if((*(arr+i)<smin)&&(*(arr+i)!=small))
             smin=*(arr+i);
     }
    printf(" The Second largest element in the array is:- %d \n The Second minimal element array is :- %d "
,smax,smin);
    return 0;
}
Output:

Q3. WAP to arrange the elements of a dynamic


array such that all even numbers are followed by all
odd numbers.

ANS -
#include<stdio.h>
#include<stdlib.h>
int main()
 {
    int *arr,i,n,count=0;
    printf("Enter the size of the dynamic array");
     scanf("%d",&n);
    arr=(int*)malloc(n*sizeof(int));
     for(i=0;i<n;i++)
     {
         scanf("%d",arr+i);
      }
   int left = 0, right = n-1,temp=0; 
    while (left < right) 
    { 
        
        while (arr[left]%2 == 0 && left < right) 
            left++; 
  

        while (arr[right]%2 == 1 && left < right) 
            right--; 
  
        if (left < right) 
        { 
        
            temp=arr[left];
            arr[left]=arr[right];
            arr[right]=temp;
            left++; 
            right--; 
        }  for(i=0;i<n;i++)
     {
         printf("%d ",*(arr+i));
     }
     return 0;
 }

OUTPUT:

Q4.Write a program to replace every element in the


dynamic array with the next greatest element
present in the same array.

ANS -
#include <stdio.h>
#include<stdlib.h>
int main()
{
    int *arr,n,x,i,count=0,a,b,next,j;
    printf("enter the number of elements:");
    scanf("%d",&n);
    arr=(int*)malloc(n*sizeof(int));
    printf("enter elements");
    for(i=0;i<n;i++)
    {
        scanf("%d",arr+i);
    }
    for(i=0;i<n;i++)
    {
        next=-1;
        for(j=i+1;j<n;j++)
        {
            if(*(arr+i)<*(arr+j))
            {
                next=*(arr+j);
                break;
            }
        }
        printf("%d %d \n",*(arr+i),next);
    }
    return 0;
    
}
Output:

Q5. WAP to replace every dynamic array element


by multiplication of previous and next of an n
element.

ANS -
#include<stdio.h>
#include<stdlib.h>
int main()
 {
    int *arr,*brr,i,n,k;
    printf("Enter the size of the dynamic array");
     scanf("%d",&n);
    arr=(int*)malloc(n*sizeof(int));
    brr=(int*)malloc(n*sizeof(int));
    for(i=0;i<n;i++)
     {
         scanf("%d",arr+i);
      }
    for(i=0;i<n;i++)
    {
        if(i==0)
       *(brr+i)=*(arr+(n-1)) * *(arr+(i+1));
       else if(i==(n-1))
         *(brr+i)=*(arr+(i-1)) * *(arr+0);
         else
       *(brr+i)=*(arr+(i-1)) * *(arr+(i+1));
     }
   for(i=0;i<n;i++)
    {
       printf("%d",*(brr+i));
    }
return 0;
}

OUTPUT:
Q6. WAP to find the largest number and counts the
occurrence of the largest number in a dynamic array
of n integers using a single loop.

ANS-
#include<stdio.h>
#include<stdlib.h>
int main()
 {
    int *arr,i,n,max,rep,occ=0;
    printf("Enter the size of the dynamic array");
     scanf("%d",&n);
    arr=(int*)malloc(n*sizeof(int));
     for(i=0;i<n;i++)
     {
         scanf("%d",arr+i);
      }
    max=arr[0];
   for(i=0;i<n;i++)
   {
       if(max<*(arr+i))
         {
          occ=0;
         max=*(arr+i);
         rep=max;
         }
       if(rep==*(arr+i))
        occ++;
    }
   printf(" The largest number in the array is %d and it occured %d times in the array ",max,occ);
 return 0;
}
       

Output:
Q7.You are given an array of 0s and 1s in random
order. Segregate 0s on left side and 1s on right side
of the array. Traverse array only once.
ANS-
#include<stdio.h>
#include<stdlib.h>
int main()
 {
    int *arr,i,n,count=0;
    printf("Enter the size of the dynamic array");
     scanf("%d",&n);
    arr=(int*)malloc(n*sizeof(int));
     for(i=0;i<n;i++)
     {
         scanf("%d",arr+i);
      }
    for(i=0;i<n;i++)
     {
         if(*(arr+i)==0)
          count++;
     }
    for(i=0;i<count;i++)
      *(arr+i)=0;
    for(i=count;i<n;i++)
       *(arr+i)=1;
    for(i=0;i<n;i++)
      printf("%d ",*(arr+i));
  return 0;}

Output:
Q8. WAP to swap all the elements in the 1st column
with all the corresponding elements in the last
column, and 2nd column with the second last
column and 3rd with 3rd last etc. of a 2-D dynamic
array. Display the matrix.

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

int main() 

    int r ,c; 
    printf(" enter the value of rows");
    scanf("%d",&r);
     printf(" enter the value of columns");
     scanf("%d",&c);
    int left=0,right=(r-1); 
     int arr[r][c]; 
    int i, j, count = 0,temp; 
    
     for (i = 0; i < r; i++) 
     {
    for (j = 0; j < c; j++) 
        {
           printf("Enter data in [%d][%d]: ", i, j);
            scanf("%d", &arr[i][j]);
        }
       
     }

    printf("The Matrix is :-");
    printf("\n");
    for (i = 0; i < r; i++) 
     {
    for (j = 0; j < c; j++) {
        printf(" %d\t",arr[i][j]); 
    }
         printf("\n");
    }
        
       //Algortithm  
         for(j=0;j<c;j++)
         {
         if(left<right)
         for(i=0;i<r;i++)
         {
             int t=arr[i][j];
             arr[i][j]=arr[i][right];
             arr[i][right]=t;
        }
          left++;
          right--;
 }
     
     printf("The Swaped matrix is :- ");
     printf("\n");
     for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++) {
        printf(" %d\t",arr[i][j]); 
    }
        printf("\n");
    }
    return 0; 

Output:-

Q9. Let A be nXn square dynamic matrix. WAP by using


appropriate user defined functions for the following:
a) Find the number of nonzero elements in A
b) Find the sum of the elements above the leading
diagonal.
c) Display the elements below the minor diagonal.
d) Find the product of the diagonal elements.
ANS-

Output:
Section 2

Q1- Add Two Polynomial Using Linked List ?


ANS-
#include<stdio.h>
#include<stdlib.h>
  // Node structure containing power and coefficient of variable 
struct Node 

    int coeff; 
    int pow; 
    struct Node *next; 
}; 
              
// Function to create new node 
void create_node(int x, int y, struct Node **temp) 

    struct Node *r, *z; 
    z = *temp; 
    if(z == NULL) 
    { 
        r =(struct Node*)malloc(sizeof(struct Node)); 
        r->coeff = x; 
        r->pow = y; 
        *temp = r; 
        r->next = (struct Node*)malloc(sizeof(struct Node)); 
        r = r->next; 
        r->next = NULL; 
    } 
    else
    { 
        r->coeff = x; 
        r->pow = y; 
        r->next = (struct Node*)malloc(sizeof(struct Node)); 
        r = r->next; 
        r->next = NULL; 
    } 

  
// Function Adding two polynomial numbers 
void polyadd(struct Node *poly1, struct Node *poly2, struct Node *poly) 

while(poly1->next && poly2->next) 
    { 
        // If power of 1st polynomial is greater then 2nd, then store 1st as it is 
        // and move its pointer 
        if(poly1->pow > poly2->pow) 
        { 
            poly->pow = poly1->pow; 
            poly->coeff = poly1->coeff; 
            poly1 = poly1->next; 
        } 
          
        // If power of 2nd polynomial is greater then 1st, then store 2nd as it is 
        // and move its pointer 
        else if(poly1->pow < poly2->pow) 
        { 
            poly->pow = poly2->pow; 
            poly->coeff = poly2->coeff; 
            poly2 = poly2->next; 
        } 
          
        // If power of both polynomial numbe       else
        { 
            poly->pow = poly1->pow; 
            poly->coeff = poly1->coeff+poly2->coeff; 
            poly1 = poly1->next; 
            poly2 = poly2->next; 
        } 
          
        // Dynamically create new node 
        poly->next = (struct Node *)malloc(sizeof(struct Node)); 
        poly = poly->next; 
        poly->next = NULL; 
    } 
while(poly1->next || poly2->next) 
    { 
        if(poly1->next) 
        { 
            poly->pow = poly1->pow; 
            poly->coeff = poly1->coeff; 
            poly1 = poly1->next; 
        } 
        if(poly2->next) 
        { 
            poly->pow = poly2->pow; 
            poly->coeff = poly2->coeff; 
            poly2 = poly2->next; 
        } 
        poly->next = (struct Node *)malloc(sizeof(struct Node)); 
        poly = poly->next; 
        poly->next = NULL; 
    } 

  
// Display Linked list 
void show(struct Node *node) 

while(node->next != NULL) 
    { 
    printf("%dx^%d", node->coeff, node->pow); 
    node = node->next; 
    if(node->next != NULL) 
        printf(" + "); 
    } 

  
// Driver  program 
int main() 

    struct Node *poly1 = NULL, *poly2 = NULL, *poly = NULL; 
      
    // Create first list of 5x^2 + 4x^1 + 2x^0 
    create_node(5,2,&poly1); 
    create_node(4,1,&poly1); 
    create_node(2,0,&poly1); 
      
    // Create second list of 5x^1 + 5x^0 
    create_node(5,1,&poly2); 
    create_node(5,0,&poly2); 
      
    printf("1st Number: ");  
    show(poly1); 
      
    printf("\n2nd Number: "); 
    show(poly2); 
      
    poly = (struct Node *)malloc(sizeof(struct Node)); 
      
    // Function add two polynomial numbers 
    polyadd(poly1, poly2, poly); 
      
    // Display resultant List 
    printf("\nAdded polynomial: "); 
    show(poly); 
return 0; 

Output:-

Q2-Delete all even nodes from double linked list


ANS-
// C++ implementation to delete all 
// the even nodes from the doubly 
// linked list 
#include<stdio.h>
#include<stdlib.h>

// Node of the doubly linked list 
typedef struct node { 
    int data; 
    struct node *prev, *next; 
} Node; 
// function to insert a node at the beginning 
// of the Doubly Linked List 
void push(Node** head_ref, int new_data) 

    // allocate node 
    Node* new_node = (Node*)malloc(sizeof(Node)); 
    // put in the data 
    new_node->data = new_data; 
    // since we are adding at the beginning, 
    // prev is always NULL 
    new_node->prev = NULL; 
    // link the old list off the new node 
    new_node->next = (*head_ref); 
    // change prev of head node to new node 
    if ((*head_ref) != NULL) 
        (*head_ref)->prev = new_node; 
    // move the head to point to the new node 
    (*head_ref) = new_node; 

// function to delete a node in a Doubly Linked List. 
// head_ref --> pointer to head node pointer. 
// del --> pointer to node to be deleted 
void deleteNode(Node** head_ref, Node* del) 

    // base case 
    if (*head_ref == NULL || del == NULL) 
        return; 
    // If node to be deleted is head node 
    if (*head_ref == del) 
        *head_ref = del->next; 
    // Change next only if node to be 
    // deleted is NOT the last node 
    if (del->next != NULL) 
        del->next->prev = del->prev; 
    // Change prev only if node to be 
    // deleted is NOT the first node 
    if (del->prev != NULL) 
        del->prev->next = del->next; 
    // Finally, free the memory occupied by del 
    free(del); 
    return; 

// function to delete all the even nodes 
// from the doubly linked list 
void deleteEvenNodes(Node** head_ref) 

    Node* ptr = *head_ref; 
    Node* next; 
    while (ptr != NULL) { 
        next = ptr->next; 
        // if true, delete node 'ptr' 
        if (ptr->data % 2 == 0) 
            deleteNode(head_ref, ptr); 
        ptr = next; 
    } 

// function to print nodes in a 
// given doubly linked list 
void printList(Node* head) 

    while (head != NULL) { 
        printf("%d-->",head->data); 
        head = head->next; 
    } 

// Driver program 
int main() 

    // start with the empty list 
    Node* head = NULL; 
    // create the doubly linked list 
    // 15 <-> 16 <-> 7 <-> 6 <-> 17 
    push(&head, 17); 
    push(&head, 6); 
    push(&head, 7); 
    push(&head, 16); 
    push(&head, 15); 
    printf("Original List: "); 
    printList(head); 
    deleteEvenNodes(&head); 
    printf("\nModified List: "); 
    printList(head); 
    return 0;

Output:-
Q3 Wap for Linked List :-
1) To insert an element at beg/end/anyposition
2) To delete an element at beg/end/particular element
3) To count the number of element
4) To search a particular element
ANS-
#include<stdio.h>
#include<stdlib.h>
struct node{
    int data;
   struct node *next;
};
struct node *head=NULL;
void create(){
    struct node *ptr,*temp;
    temp=(struct node*)malloc(sizeof(struct node));
     if(temp==NULL)
        {
                printf("no space");
                exit(0);
        }
     printf("nEnter the data value for the node:t");
        scanf("%d",&temp->data);
     temp->next=NULL;
     if(head==NULL){
         head=temp;
     }
    else
        {
                ptr=head;
                while(ptr->next!=NULL)
                {
                        ptr=ptr->next;
                }
                ptr->next=temp;
        }
}
void insert_beg(){
   
    struct node *temp;
     temp=(struct node *)malloc(sizeof(struct node));
        if(temp==NULL)
        {
                printf("nOut of Memory Space:n");
                return;
        }
        printf("nEnter the data value for the node:t" );
        scanf("%d",&temp->data);
        temp->next=NULL;
        if(head==NULL){
            head=temp;
        }
        else{
            temp->next=head;
            head=temp;
        }
}
void insert_end(){
    struct node *new_node;
    int num;
     new_node=(struct node *)malloc(sizeof(struct node));
      if(new_node==NULL)
        {
                printf("nOut of Memory Space:n");
                return;
        }
     printf("Enter the data:-");
     scanf("%d",&num);
     new_node->data=num;
    struct node  *ptr=head;
     if(head==NULL){
         head=new_node;
     }
     else{
         while(ptr!=NULL){
             ptr=ptr->next;
         }
         new_node->next=ptr->next;
         free(ptr);
 }
}
void insert_pos(){
    struct node *new_node;
    int pos,i,num;
    if(head==NULL)
    {
        printf("List is empty!!");
        return;
    }
    new_node=(struct node*)malloc(sizeof(struct node));
   
    printf("Enter the positon you want to enter the node");
    scanf("%d",&pos);
    printf("Enter the value in node:");
    scanf("%d",&num);
    new_node->data=num;
    struct node  *ptr=head;
     if(pos==1)
    {
        
                        new_node->next=head;
                        head=new_node;
                        
                        return;
        }
       for( i=1; i<(pos-1);i++)
        {
           
           if(ptr->next==NULL)
        {
            printf("There are less elements!!");
            return;
        }
            ptr=ptr->next;
       
       }
       new_node->next=ptr->next;
       ptr->next=new_node;
       
   }
void delete_begin()
{
        struct node *ptr;
        if(ptr==NULL)
        {
                printf("nList is Empty:n");
                return;
        }
        else
        {
                ptr=head;
                head=head->next ;
                printf("nThe deleted element is :%dt",ptr->data);
                free(ptr);
        }
}
void delete_end()
{
        struct node *temp,*ptr;
        if(head==NULL)
        {
                printf("nList is Empty:");
                exit(0);
        }
        else if(head->next ==NULL)
        {
                ptr=head;
                head=NULL;
                printf("nThe deleted element is:%dt",ptr->data);
               
        }
        else
        {
                ptr=head;
                while(ptr->next!=NULL)
                {
                        temp=ptr;
                        ptr=ptr->next;
                }
                temp->next=NULL;
                printf("nThe deleted element is:%dt",ptr->data);
                
        }
}
void delete_pos()
{
        int i,pos;
        struct node *temp,*ptr;
        if(head==NULL)
        {
                printf("nThe List is Empty:n");
                exit(0);
        }
        else
        {
                printf("nEnter the position of the node to be deleted:t");
                scanf("%d",&pos);
                if(pos==0)
                {
                        ptr=head;
                        head=head->next ;
                        printf("nThe deleted element is:%dt",ptr->data  );
                        free(ptr);
                }
                else
                {
                        ptr=head;
                        for(i=0;i<pos;i++) { temp=ptr; ptr=ptr->next ;
                                if(ptr==NULL)
                                {
                                        printf("nPosition not Found:n");
                                        return;
                                }
                        }
                        temp->next =ptr->next ;
                        printf("nThe deleted element is:%dt",ptr->data );
                        free(ptr);
                }
        }
}
void del_key(){
    int k;
    printf("the number to be deleted");
    scanf("%d",&k);
    struct node *temp,*prev;
    prev=NULL;
    if(head==NULL){
        printf("List is Empty");
        return;
    }
    temp=head;
    if(temp!=NULL && temp->data==k){
        temp=temp->next;
    }
    while(temp !=NULL && temp->data !=k){
           prev=temp;
           temp=temp->next;
       }
    prev->next=temp->next;
}
void total_nod(){
    struct node *temp;
    temp=head;
    int ctr=0;
    while(temp!=NULL){
        temp=temp->next;
        ctr++;
    }
   printf("The number of nodes in the list is %d ",ctr);
}
void display()
{
        struct node *ptr;
        if(head==NULL)
        {
                printf("nList is empty:n");
                return;
        }
        else
        {
                ptr=head;
                printf("nThe List elements are:n");
                while(ptr!=NULL)
                {
                        printf("%d----->",ptr->data );
                        ptr=ptr->next ;
                }
        }
}
void search(){
        int n,k=1;
        printf("Enter the element to be searched :-");
        scanf("%d",&n);
        struct node *temp;
        temp=head;
        while(temp!=NULL){
                temp=temp->next;
                if(temp->data==n){
                        printf("element is found");
                        k=0;
                        break;
                }
        }
        if(k==1){
                printf("Element not found");
        }
}
int main()     
{
        int choice;
        while(1){
               
                printf("n                MENU                             \n");
                printf("n 1.Create     \n");
                printf("n 2.Display    \n");
                printf("n 3.Insert at the beginning    \n");
                printf("n 4.Insert at the end  \n");
                printf("n 5.Insert at specified position       \n");
                printf("n 6.Delete from beginning      \n");
                printf("n 7.Delete from the end        \n");
                printf("n 8.Delete from specified position     \n");
                printf("n 9.delete particular element \n");
                printf("n 10.calculate total number of nodes \n");
                printf("n 11.search an element in linked list \n");
                 printf("n 12. exit(0) \n");
                printf("n--------------------------------------n");
                printf("Enter your choice:- ");
                scanf("%d",&choice);
                switch(choice)
                {
                        case 1:
                                        create();
                                        break;
                        case 2:
                                        display();
                                        break;
                        case 3: 
                                        insert_beg();
                                        break;
                        case 4:
                                        insert_end();
                                        break;
                        case 5:
                                        insert_pos();
                                        break;
                        case 6:
                                        delete_begin();
                                        break;
                        case 7:
                                        delete_end();
                                        break;
                        case 8:
                                        delete_pos();
                                        break;
                        case 9:
                                        del_key();
                                        break;
                        case 10:       total_nod();
                                       break;
                                       
                        case 11:
                                       search();
                                        break;
                        case 12:
                                        exit(0);
                                        break;
                             
                        default:
                                        printf("n Wrong Choice:n");
                                        break;
                }
        }
        return 0;
        free(head);
       //free(struct node);
}

Output:-
Section 3
Q1. Implement Stack using arrays:-
ANS-
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
    //clrscr();
    top=-1;
    printf("\n Enter the size of STACK[MAX=100]:");
    scanf("%d",&n);
    printf("\n\t STACK OPERATIONS USING ARRAY");
    printf("\n\t--------------------------------");
    printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
    do
    {
        printf("\n Enter the Choice:");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
            {
                push();
                break;
            }
            case 2:
            {
                pop();
                break;
            }
            case 3:
            {
                display();
                break;
            }
            case 4:
            {
                printf("\n\t EXIT POINT ");
                break;
            }
            default:
            {
                printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
            }
                
        }
    }
    while(choice!=4);
    return 0;
}
void push()
{
    if(top>=n-1)
    {
        printf("\n\tSTACK is over flow");
        
    }
    else
    {
        printf(" Enter a value to be pushed:");
        scanf("%d",&x);
        top++;
        stack[top]=x;
    }
}
void pop()
{
    if(top<=-1)
    {
        printf("\n\t Stack is under flow");
    }
    else
    {
        printf("\n\t The popped elements is %d",stack[top]);
        top--;
    }
}
void display()
{
    if(top>=0)
    {
        printf("\n The elements in STACK \n");
        for(i=top; i>=0; i--)
            printf("\n%d",stack[i]);
        printf("\n Press Next Choice");
    }
    else
    {
        printf("\n The STACK is empty");
    }
   
}

Output:-

Q2> Stacks Using Linked List:-


ANS-
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0

struct node
{
    int data;
    struct node *next;
};
typedef struct node node;
node *top;
void initialize()
{
    top = NULL;
}
void push(int value)
{
    node *tmp;
    tmp = malloc(sizeof(node));
    tmp -> data = value;
    tmp -> next = top;
    top = tmp;
}
int pop()
{
    node *tmp;
    int n;
    tmp = top;
    n = tmp->data;
    top = top->next;
    free(tmp);
    return n;
}
int Top()
{
    return top->data;
}
int isempty()
{
    return top==NULL;
}
void display(node *head)
{
    if(head == NULL)
    {
        printf("NULL\n");
    }
    else
    {
        printf("%d\n", head -> data);
        display(head->next);
    }
}
int main()
{
    initialize();
    push(10);
    push(20);
    push(30);
    printf("The top is %d\n",Top());
    pop();
    printf("The top after pop is %d\n",Top());
    display(top);
    return 0;
}

Output:-
Q> Implement Priorty Que using linked List:-
ANS-
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
     int data;
     int prr;
     struct Node *next;
}node;
node *new_node (int a ,int b){
   node *temp=(node*)malloc(sizeof(node));
   temp->data=a;
   temp->prr=b;
   temp->next=NULL;
   
   return temp;

}
void enque(node** head){
    int data,prio;
    printf("ENter the data for the node:- ");
    scanf("%d",&data);
    printf("Enter the priority for the node");
    scanf("%d",&prio);
    node *temp=new_node(data,prio);
     node *start= (*head);
    if((*head)->prr>prio){
        temp->next=(*head);
        (*head)=temp;
    }
    else{
        while(start->next!=NULL && start->next->prr<prio){
            start=start->next;
        }
        temp->next=start->next;
        start->next=temp;
    }
}
void deque(node** head){
node *temp=(*head);
(*head)=(*head)->next;
}
void display(node** head){
      node *temp=(*head);
      while(temp!=NULL){
          printf("%d---->",temp->data);
          temp=temp->next;
      }
      printf("\n");
  
}
int main(){
    int a,pr,ch;
    printf("Enter data and priority:- ");
    scanf("%d %d",&a,&pr);
    node *new=new_node(a,pr);
    while(1){
        printf("-------MENU---------\n");
        printf("1. ENQUE \n");
        printf("2 DEQUE \n");
        printf("3 Display \n");
        printf("4.exit \n");
        printf("Enter your choice :-");
        scanf("%d",&ch);
        switch (ch)
        {
        case 1:
               enque(&new);
            break;
        case 2:
              deque(&new);
              break;
        case 3:
             
                display(&new);
                    break;
        case 4:
              exit(0);
              break;
        
        default:
                printf("Wrong choice");
                  break;
        }
    }
    return 0;
    
}

Output;-

You might also like