Posts

Showing posts from October, 2020

Selection sort

 In selection sort, there is an unsorted array of size n, and we'll consider a sorted array of size zero in it. Initially, we will pick the first element of array and swap it with the smallest element of remaining array. And now the size of our sorted array becomes 1 and size of unsorted array decreases by 1. But still the size of array remains same as n. Note: we will not touch the sorted array, Now we will pick second element of unsorted array and swap it the smallest element of remaining array, but don't do anything (swap or checking minimum) with sorted array. Now the size of sorted array becomes 2 and the size of unsorted array gets reduced by 2. And same process goes on. #include<conio.h> #include<stdlib.h> #include<stdio.h> void main() {     int n,i,j,temp,min;     printf("enter the size of array: ");     scanf("%d",&n);     int a[n];     printf("Enter the elements of array:\n");     for(i=0;i...

Bubble sort

 #include<conio.h> #include<stdlib.h> #include<stdio.h> void main() {     int n,i,j,temp;     printf("enter the size of array: ");     scanf("%d",&n);     int a[n];     printf("Enter the elements of array:\n");     for(i=0;i<n;i++)         scanf("%d",&a[i]);     for(i=0;i<n-1;i++)         for(j=0;j<n-1-i;j++)         {             if(a[j+1]<a[j])             {                 temp=a[j];                 a[j]=a[j+1];                 a[j+1]=temp;             }         }     printf("\ Sorted elements of array:\n");     for(i=0;i<n;i++)         printf(...

Circular Queue using linked list

#include<conio.h> #include<stdlib.h> #include<stdio.h> struct node {     int data;     struct node *next; }; struct node *newnode, *temp; void enqueue(struct node **f, struct node **r) {     int data,choice=1;     while(choice==1)     {     newnode=(struct node*)malloc(sizeof(struct node));     printf("Enter the data: ");     scanf("%d",&newnode->data);     newnode->next=0;     if(*f==0 && *r==0)     {         *f = newnode;         *r = newnode;         (*r)->next=*f;         // or (*r)->next=*r; that's same thing bcz there is only one node.     }     else     {         newnode->next=(*r)->next;         (*r)->next=newnode;         (*r)=newnode;     } ...

Circular Queue using arrays

// In Circular Queue you can insert elements on the places of deleted elements. Keep the note of (f+1)% n, (r+1)% n, (temp+1)% n in circular queue. These operations will be mostly used in circular Queue. Circular Queue are also known as "Ring Buffer" and have removed the drawback of linear queue, by inserting the elements at the deleted positions. #include<conio.h> #include<stdlib.h> #include<stdio.h> void enqueue(int a[], int *f, int *r, int n) {     // n is the size of queue.     int data,choice=1;     while(choice==1){     if(*f==-1 && *r==-1)     {         *f=0;*r=0;         printf("enter the value to be inserted: ");         scanf("%d",&data);         a[*r]=data;     }     else if((*r+1)%n==*f)     {         printf("queue is full\n");         break; ...

Queue implementation using linked list

FIFO First In First Out Since the time complexity for the both Enqueue and Dequeue operations is O(1) , So, You can't traverse the whole list to perform any of the enqueue/dequeue operations. Hence, you have to maintain two pointers for that, i.e. a head and tail pointer. Now, suppose in singly linked list you want to delete element from tail side and insert from head side, than you easily can perform the insertion operation but, for deletion you have to delete the last node and for that you have to shift tail pointer back in list which is not possible(so that tail pointer point to last second element), without traversing the whole list, And for traversing time complexity becomes O(n), which is not possible for Queue.  So, Delete from Head side and insert from tail side i.e. head as front end and tail as rear end. #include<conio.h> #include<stdlib.h> #include<stdio.h> struct node {     int data;     struct node *next; }; struct node *newnode, *...

Queue implementation Using Array

 #include<conio.h> #include<stdlib.h> #include<stdio.h> void enqueue(int a[], int *f, int *r, int *size) {     int data,choice=1;     while(choice==1){     if(*f==-1 && *r==-1)     {         *f=0;*r=0;         printf("enter the value to be inserted: ");         scanf("%d",&data);         a[*r]=data;     }     else if(*r==*size-1)     {         printf("queue is full\n");         break;     }     else     {         *r+=1;         printf("enter the value to be inserted: ");         scanf("%d",&data);         a[*r]=data;     }     printf("want another element to be inserted(0,1): ");     scanf("%d",&choice);     } ...

Stack using Linked list in C

#include<conio.h> #include<stdlib.h> #include<stdio.h> struct node     {         int data;         struct node*next;     }; struct node*head, *temp, *top, *newnode; void push() {    int choice=1;    while(choice==1)    {        newnode=(struct node*)malloc(sizeof(struct node));        newnode->next=0;        printf("enter the data: ");        scanf("%d",&newnode->data);        if(head==0)        {            head=newnode;            temp=newnode;        }        else        {            // we are pushing the elements in stack from head side;            //or we can say the top ...

Stack using Arrays using C

#include<conio.h> #include<stdio.h> #include<stdlib.h> void push(int a[],int*tp) {     int data,choice=1;     while(choice==1)     {     printf("enter the element to be inserted");     scanf("%d",&data);     *tp = *tp+1;     a[*tp]=data;     printf("want another data to be pushed (0,1): ");     scanf("%d",&choice);     } } void pop(int a[],int*tp) {     int data,choice=1;     while(choice==1)     {     data=a[*tp];     *tp=*tp-1;     printf("deleted %d from stack\n",data);     printf("want another data to be popped (0,1): ");     scanf("%d",&choice);     } } void display(int a[],int*tp) {     int i;     for(i=*tp;i>=0;i--)         printf("%d\n",a[i]); } int main() {     int arr[100],top=-1,i,choice=1,ch;     ...

Complete Singly Linked list using C

  Complete linked list, creation, insertion, deletion. #include<stdio.h> #include<stdlib.h> struct node {     int data;     struct node *next; }; int count=0,f=1; struct node * head, *temp, *newnode, *stemp; void create() {   head=0;     int choice =1;     while(choice==1)     {         count++;        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         {             temp->next=newnode;           ...

Complete Circular Singly linked list using C

 #include<stdio.h>  #include<stdlib.h>  #include<conio.h>  struct node  {      int data;      struct node * next;  };  struct node *head, *newnode, *tail, *temp, *stemp;  int count=0,f=1; // f==1 means that list is not created or became empty  void create()  {      head =0;      tail=0;      int choice=1;      while(choice==1)      {          count++;          newnode=(struct node*)malloc(sizeof(struct node));          printf("enter data");          scanf("%d",&newnode->data);          if(head==0)          {              head=newnode;              tail=newnode;         ...