Posts

Showing posts from April, 2021

Intersection of lists (PYTHON)

  n1 =  int ( input ( "Enter the size of list 1 : " )) l1 = [int(input("enter the elements : ")) for i in range(n1)] n2 = int(input("Enter the size of list 2 : ")) l2 = [int(input("enter the elements : ")) for i in range(n2)] l3 = [] for i in range(n1):     if l1[i] not in l3 and l1[i] in l2:         l3.append(l1[i]) for i in range(n2):     if l2[i] not in l3 and l2[i] in l1:         l3.append(l2[i]) print(l3)

Binary and Linear search(PYTHON)

 def binary_search(size, list1, z):     list1.sort()     low = 0     high = size-1     index = -1     while(low <= high):         mid = (low+high)//2         if list1[mid] < z:             low = mid+1         elif list1[mid] > z:             high = mid-1         elif list1[mid] == z:             index = mid             print("found at index: ", mid)             break     if index == -1:         print("Not found") def linear_search(size, list1, z):     index = -1     for i in range(size):         if list1[i] == z:             index = i             print("found at index :"...

Priority Scheduling

  #include   <stdio.h> #include <conio.h> struct proc {     int id;     int burst;     int pi; }; int main() {     int n, i, j, total_waiting_time, waiting_time;     printf("Enter the number of processess: ");     scanf("%d", &n);     struct proc p[n], temp; //To reduce complexity we have taken arrival time of all - // - processes to be same(0); //If we take arrival time, than first sort on basis of arrival time,     //and can also sort for burst time for processes having same priority.     for (i =...

Shortest_Job_First CPU scheduling

#include <stdio.h> #include <conio.h> struct proc {     int id;     int burst;     int at; }; int main() {     int n, i, j, total_waiting_time, waiting_time;     printf("Enter the number of processess: ");     scanf("%d", &n);     struct proc p[n], temp;     for (i = 0; i < n; i++)     {         p[i].id = i;         printf("Enter the arrival time of the process: ");         scanf("%d", &p[i].at);         printf("Enter the burst t...

Last third element from end

  #include<stdio.h> #include<stdlib.h> struct node {     int data;     struct node *next; }; struct node * head, *temp, *newnode, *temp2; void create() {    head=0;     int choice=1;     while(choice==1)     {        newnode=(struct node*)malloc(sizeof(struct node));        newnode->next=0;        printf("enter data : ");        scanf("%d",&newnode->data);        if(head==0)        {            head=newnode;            temp=newnode;        }        else         { ...

mid element of linked list in single pass

  #include<stdio.h> #include<stdlib.h> struct node {     int data;     struct node *next; };   int f=1; struct node * head, *temp, *newnode, *temp2;   void create() {    head=0;     int choice=1;     while(choice==1)     {        newnode=(struct node*)malloc(sizeof(struct node));        newnode->next=0;        printf("enter data");        scanf("%d",&newnode->data);        if(head==0)        {            head=newnode;            temp=newnode;        }        else    ...

Reverse Singly linked list, METHOD 1, RECURSIVE (head declared in main)

  SinglyLinkedListNode *   head ,   * nextp ,   * temp2 ; SinglyLinkedListNode*reverse(SinglyLinkedListNode* temp)  {      if ( temp->next==0 )     {          temp2=temp;          return  temp2;     }      head=reverse(temp->next); //Now, head will contain the address of last node.      nextp=temp->next;     nextp->next=temp;     temp->next=0;      return  head; //Value of head will remain same, and same will be returned back and back //and same back to main from where reverse is called. } int main() {     .....     ....      head=reverse(head);     //or could use new head variable.     //head1=reverse(head);    ...

Reverse Singly linked list, METHOD 1, RECURSIVE (head globally declared)

void  rev( struct  node* temp) {      if (temp->next==0)     { head=temp;     return; }      rev(temp->next);     tempnext=temp->next;     tempnext->next=temp;     temp->next=0;      } int main() {     ....     ....     //head is passed as parameter from main.     rev(head);     ....     ... }

Reversing singly Linked list, METHOD 2, iterative

  SinglyLinkedListNode *   reverse ( SinglyLinkedListNode *   head )   { SinglyLinkedListNode*prev, *current, *next; prev=0, next=0; current=head; while (current!=0) {      next=current->next;     current->next=prev;     prev=current;     current=next; } head=prev; return  head; }

Reversing a Doubly Linked List by Iteration

//Just swap the prev node value with the next node value, with the help of two pointers. //in case there is no tail pointer, we can have temp pointer always just behind current pointer. //after coming out of while loop, current and nextnode both will be having null value and temp- //- will be just behind current, hence the temp will be at last node after the end of loop, hence head=temp. DoublyLinkedListNode *   reverse ( DoublyLinkedListNode *   head ) {     DoublyLinkedListNode*current, *nextnode, *temp;     current=head;      while (current!=0)     {          nextnode=current->next;         current->next=current->prev;         current->prev=nextnode;         temp=current;        ...

How to find cycles in singly linked list

bool  has_cycle(SinglyLinkedListNode* head) { SinglyLinkedListNode*slow, *fast; slow=head, fast=head; while (slow!= 0  && fast!= 0  && fast->next!= 0 ) {     slow=slow->next;     fast=fast->next->next;      if (fast==slow)      return   1 ; } return   0 ; }

Printing the linked list in reverse order

//The function to print the linked list in reverse order without //actually reversing it; //using recursion. void  reversePrint(SinglyLinkedListNode* head)  { if (head->next!= 0 ) reversePrint(head->next); cout<<head->data<<endl; }

n rotations the left and right of array

#include<bits/stdc++.h> using namespace std; int main() {     int n,i,r;     cout<<"enter the size of array : ";     cin>>n;     int a[n];     for(i=0;i<n;i++)     cin>>a[i];     cout<<"enter the no. of rotations : ";     cin>>r;     r=r%n;     cout<<"For the left rotations\n";         for(i=r;i<n;i++)         cout<<a[i]<<" ";         for(i=0;i<r;i++)         cout<<a[i]<<" ";     cout<<"\nFo...

Python Basics

#split() will be used to enter the multiple strings seperated by spaces at once  #in multiple variables while map is used to enter the numberic value x, y = input("enter two strings").split() print(x, y) m, n = map(int, input("enter two numbers").split()) print(m, n) #input #bhaskar varshney #2 3

Check Bracket Sequence

#include<bits/stdc++.h> using namespace std; int main() {     int i,f,top=-1,size;     string s;     cin>>s;     size=s.size();     char a[size];     f=1;     for(i=0;i<s.size() && f==1;i++)     {         if(s[i]=='(')         a[++top]='(';         else if(s[i]==')')         {             if(top==-1)             f=0;             else             top-=1;         }     }     if(top!=-1 || f==0)     cout<<"invalid\n";     else     cout<<"Valid"; }

First_Come_First_Serve CPU Scheduling

#include<stdio.h> #include<conio.h> struct proc {     int id;     int burst;     int at; }; int main() {     int n,i,j,total_waiting_time,waiting_time;     printf("Enter the number of processess: ");     scanf("%d",&n);     struct proc p[n],temp;     f or(i=0;i<n;i++)     {         p[i].id=i;         printf("Enter the arrival time of the process: ");         scanf("%d",&p[i].at);         printf("Enter the burst time of the process: ");         scanf("%d",&p[i].burst);     }     for(i=0;i<n;i++)     {         for(j=i+1;j<n;j++)         {             if(p[j].at<p[i].at)             {                 ...