Monday, November 22, 2021

sorted insertion....With Last insertion

 #include<stdio.h>
#include<stdlib.h>
struct node {
    int data;
    struct node* next;
};
void inElFr(struct node** head, int val) {
    struct node* temp = (struct node*)malloc(sizeof(struct node));
    temp->data = val;
    temp->next = NULL;
    if(*head == NULL)
      *head = temp;
      else {
          struct node* run = *head;
          while(run->next != NULL) {
              run = run->next;
          }
            run->next = temp;
       }
}
void midInsert(struct node** head, int elt) {
    struct node* find = *head;
    struct node* prvs = *head;
    struct node* temp = malloc(sizeof(struct node));
    while(find->next != NULL) {
        if(find->data > elt) {
            break;
        } else {
            prvs = find;
            find = find->next;
        }
    }
    temp->data = elt;
    prvs->next = temp;
    temp->next = find;
    
}
void shElFr(struct node* head) {
    struct node* sho = head;
    while(sho!=NULL) {
        printf("%d->", sho->data);
        sho = sho->next;
    }
    printf("Reached");
}
int main() {
    int num;
    struct node* head = NULL;
    inElFr(&head, 10);
    inElFr(&head, 20);
    inElFr(&head, 30);
    inElFr(&head, 50);
    shElFr(head);
    printf("\n Enter the number \t");
    scanf("%d", &num);
    midInsert(&head, num);
    shElFr(head);

    
    return 0;
}

No comments:

Post a Comment

Priority queue deleting elements after ascending inputs Gaand Fadd

 using namespace std; #include<iostream> #define N 5 class pq { public:     int* arr;     int front;     int rear;     pq() {         ...