Untitled

 avatar
unknown
plain_text
3 years ago
17 kB
10
Indexable
SORTING

BUBBLE SORTING

#include <stdio.h>

int main()
{
    int i,j,n,k,temp;
    printf("Enter the size of array\n");
    scanf("%d",&n);
    int a[n];
    
    //Taking values in array
    printf("Enter values in array\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for ( i = 0; i < (n-1); i++)
    {
       for ( j = 0; j < n-i-1; j++)
       {
        if (a[j]>a[j+1])
        {
            temp=a[j];
            a[j]=a[j+1];
            a[j+1]=temp;
        }
        
       }
       
    }
    printf("Sorted array\n");
    for (size_t i = 0; i < n; i++)
    {
        printf("%d\t",a[i]);
    }
    
    
}

SELECTIN SORT

#include <stdio.h>

int main()
{
    int i,j,n,k,temp;
    printf("Enter the size of array");
    scanf("%d",&n);
    int A[n];
    
    //Taking values in array
    printf("Enter values in array");
    for(i=0;i<n;i++)
    {
        scanf("%d",&A[i]);
    }
    
    //sorting the array
    for(i=0;i<n-1;i++)
    {
        for(j=k=i;j<n;j++)
        {
            if(A[j]<A[k])
            {
                k=j;
            }
        }
        temp=A[i];
        A[i]=A[k];
        A[k]=temp;
    }
    printf("Sorted array");
    for(i=0;i<n;i++)
    {
        printf("\n %d",A[i]);
    }
}


MERGE SORT

#include <stdio.h> 
void Merge(int A[],int l,int mid,int h)
{
 int i=l,j=mid+1,k=l;
 int B[100];
 
 while(i<=mid && j<=h)
 {
 if(A[i]<A[j])
 B[k++]=A[i++];
 else
 B[k++]=A[j++];
 }
 for(;i<=mid;i++)
 B[k++]=A[i];
 for(;j<=h;j++)
 B[k++]=A[j];
 
 for(i=l;i<=h;i++)
 A[i]=B[i];
}
void IMergeSort(int A[],int n)
{
 int p,l,h,mid,i;
 
 for(p=2;p<=n;p=p*2)
 {
for(i=0;i+p-1<=n;i=i+p)
 {
 l=i;
 h=i+p-1;
 mid=(l+h)/2;
 Merge(A,l,mid,h);
 }
 }
 if(p/2<n)
 Merge(A,0,p/2-1,n);
 
}
int main()
{
 int A[]={11,13,7,12,16,9,24,5,10,3},n=10,i;
 
 IMergeSort(A,n);
 
 for(i=0;i<10;i++)
 printf("%d ",A[i]);
 printf("\n");
 
 return 0;
}

INSERTION SORT
#include <stdio.h>

int main()
{
    int i,j,n,k,temp;
    printf("Enter the size of array\n");
    scanf("%d",&n);
    int a[n];
    
    //Taking values in array
    printf("Enter values in array\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for ( i = 1; i < n; i++)
    {
       temp=a[i];
       j=i-1;
       while(j>=0 && a[j]>temp)
       {
        a[j+1]=a[j];
        j--;
       }
       a[j+1]=temp;
    }
    printf("Sorted array\n");
    for(i=0;i<n;i++)
    {
        printf("\n %d",a[i]);
    }
    return 0;
}

SEARCHING
LINEAR SEARCH
#include <stdio.h>
int main()
{
    int i,size,key,pos,c=0;
    printf("enter the size of array\n");
    scanf("%d", &size);
    int a[size];
    printf("enter the elements in array\n");
    for (int i = 0; i < size; i++)
    {
        scanf("%d", &a[i]);
    }
    printf("enter the key to search\t");
    scanf("%d",&key);
    for ( i = 0; i < size; i++)
    {
        if (a[i]==key)
        {
            c++;
            pos=i;
            break;
        }
    }
    if (c==1)
    {
        printf("found\t");
    }
    else
    {
        printf("not found\t");
    }
        return 0;
}

BINARY SEARCH
#include <stdio.h>
int main()
{
    int i, size, key, mid, end, start, c = 0;
    printf("enter the size of array\n");
    scanf("%d", &size);
    int a[size];
    printf("enter the elements in array\n");
    for (int i = 0; i < size; i++)
    {
        scanf("%d", &a[i]);
    }
    printf("enter the key to search\t");
    scanf("%d", &key);
    start = 0;
    end = size - 1;
    while (start <= end)
    {
        mid=(start+end);
        if (a[mid] == key)
        {
            c++;
            break;
        }
        else if (key>a[mid])
        {
            start=mid+1;
        }
        else if (key<a[mid])
        {
            end=mid-1;
        }       
    }
    if (c==1)
    {
        printf("found");
    }
    else
    {
        printf("not found");
    }
}

QUEUE
#include <stdio.h>

#define size 10
void enqueue(int x);
void dequeue();
void display();
int queue[size],front=-1,rear=-1;

int main()
{
  int value,choice;
  do
  {
    printf("\n Menu-\n 1.Insert \n 2.Delete \n 3.Display \n 4.Exit");
    printf("\n Enter your choice");
    scanf("%d",&choice);
    switch(choice)
    {
      case 1:
      printf("Enter value to be inserted");
      scanf("%d",&value);
      enqueue(value);
      break;

      case 2:
      dequeue();
      break;

      case 3:
      display();
      break;
    }
  }while(choice!=4);
}


void enqueue(int x)
{

  if(rear==size-1)
  {
    printf("QUEUE IS FULL");
  }
  else
  {
    if(front==-1)
    {
      front=0;
    }
    else
    {
      rear++;
      queue[rear]=x;
    }
  }
}



void dequeue()
{
  int x=-1;
  if(front==-1)
  {
    printf("QUEUE IS EMPTY");
  }
  else
  {
    x=queue[front];
    printf("DELETED ELEMENT FROM QUEUE %d",x);
    front++;
    if(front>rear)
    front=rear=-1;
  }
 }



 void display()
 {
     if(front==-1)
     {
        printf("QUEUE IS EMPTY");
     }
     else
     {
       printf("\n ELEMENTS OF QUEUE ARE");
          for(int i=front;i<=rear;i++)
         {
            printf("\n %d",queue[i]);
         }
     }
 }

PRIORITY QUEUE

// Priority Queue implementation in C

#include <stdio.h>
int size = 0;
void swap(int *a, int *b) {
  int temp = *b;
  *b = *a;
  *a = temp;
}

// Function to heapify the tree
void heapify(int array[], int size, int i) {
  if (size == 1) {
    printf("Single element in the heap");
  } else {
    // Find the largest among root, left child and right child
    int largest = i;
    int l = 2 * i + 1;
    int r = 2 * i + 2;
    if (l < size && array[l] > array[largest])
      largest = l;
    if (r < size && array[r] > array[largest])
      largest = r;

    // Swap and continue heapifying if root is not largest
    if (largest != i) {
      swap(&array[i], &array[largest]);
      heapify(array, size, largest);
    }
  }
}

// Function to insert an element into the tree
void insert(int array[], int newNum) {
  if (size == 0) {
    array[0] = newNum;
    size += 1;
  } else {
    array[size] = newNum;
    size += 1;
    for (int i = size / 2 - 1; i >= 0; i--) {
      heapify(array, size, i);
    }
  }
}

// Function to delete an element from the tree
void deleteRoot(int array[], int num) {
  int i;
  for (i = 0; i < size; i++) {
    if (num == array[i])
      break;
  }

  swap(&array[i], &array[size - 1]);
  size -= 1;
  for (int i = size / 2 - 1; i >= 0; i--) {
    heapify(array, size, i);
  }
}

// Print the array
void printArray(int array[], int size) {
  for (int i = 0; i < size; ++i)
    printf("%d ", array[i]);
  printf("\n");
}

// Driver code
int main() {
  int array[10];

  insert(array, 3);
  insert(array, 4);
  insert(array, 9);
  insert(array, 5);
  insert(array, 2);

  printf("Max-Heap array: ");
  printArray(array, size);

  deleteRoot(array, 4);

  printf("After deleting an element: ");

  printArray(array, size);
}
COPPING CONTENT
#include<stdio.h>
#include<stdlib.h>
void main()
{
    FILE *fptr1=NULL,*fptr2=NULL;
    char ch;
    fptr1=fopen("abc.txt","r");
    fptr2=fopen("destination.txt","w");
    if (fptr1==NULL)
    {
        printf("no file");
        exit(1);
    }
    if (fptr2==NULL)
    {
        printf("no file");
        exit(1);
    }
    while (ch=fgetc(fptr1)!= EOF)
    {
        fputc(ch,fptr2);
    }
    fclose(fptr1);
    fclose(fptr2);
    printf("successfully addede");
}


STUDENTS DETAILS
#  include <stdio.h>
struct student{
    int roll;
    char name[20];
    char address[50];
}s;
void main()
{
    FILE * fptr;
    fptr=fopen("student.txt","w");
    if (fptr == NULL)
    {
        printf("FILE IS EMPTY\n");
    }
    else
    {
        printf("ENTER STUDENT INFO\n");
        printf("enter roll no.\n");
        scanf("%d",&s.roll);
        printf("enter name\n");
        scanf("%s",&s.name);
        printf("enter address.\n");
        scanf("%s",&s.address);
        printf("\ndetails added successfully");
        fclose(fptr);
    }

POLYNOMIAL LINKEDLIST

}#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct Node
{
 int coeff;
 int exp;
 struct Node *next;
}*poly=NULL;
void create()
{
 struct Node *t,*last=NULL;
 int num,i;
 
 printf("Enter number of terms");
 scanf("%d",&num);
 printf("Enter each term with coeff and exp\n");
 
 for(i=0;i<num;i++)
 {
 t=(struct Node *)malloc(sizeof(struct Node));
 scanf("%d%d",&t->coeff,&t->exp);
 t->next=NULL;
 if(poly==NULL)
 {
 poly=last=t;
 }
 else
 {
 last->next=t;
 last=t;
 }
 }
}
void Display(struct Node *p)
{
 while(p)
 {
 printf("%dx%d +",p->coeff,p->exp);
 p=p->next;
 }
 printf("\n");
}
long Eval(struct Node *p, int x)
{
 long val=0;
 
 while(p)
 {
 val+=p->coeff*pow(x,p->exp);
 p=p->next;
 }
 return val;
}
int main()
{
 create();
 Display(poly);
 printf("%ld\n",Eval(poly,1));
 return 0;

DOUBLY LINKEDLIST

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


struct Node{
  int data;
  struct Node *next;
  struct Node *prev;
};

int getLength(struct Node* node);

void insertStart(struct Node** head, int data){

  struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));

  newNode->data = data;
  newNode->next = *head;
  newNode->prev = NULL;
  
  //If the linked list already had atleast 1 node
  if(*head !=NULL)
    (*head)->prev = newNode;
    // *head->prev = newNode; would not work it has (*head) must be used


  //changing the new head to this freshly entered node
  *head = newNode; 
}

void insertLast(struct Node** head, int data){

  struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));

  newNode->data = data;
  newNode->next = NULL;

  //need this if there is no node present in linked list at all
  if(*head==NULL){
    *head = newNode;
    newNode->prev = NULL;
    return;
  }

  struct Node* temp = *head;

  while(temp->next!=NULL)
    temp = temp->next;

  temp->next = newNode;
  newNode->prev = temp;
}

void insertAfter(struct Node** head, int n, int data)
{
    int len = getLength(*head);
    
    // if insertion has to happen at start
    if(n == 0){
        insertStart(head, data);
        return;
    }
    // if insertion has to happen at end
    if(n == len){
        insertLast(head, data);
        return;
    }
    
    // other wise if insertion needs to happen in the middle
    
    // if position to enter after is greater than size of List
    // if position to enter is negative, we can't insert
    if(n < 0 || n > len) 
        printf("Invalid position to insert\n"); 
    
    else 
    { 
        struct Node* newNode = (struct Node*) malloc(sizeof(struct Node)); 
        newNode->data = data;
        newNode->next = NULL;        
        newNode->prev = NULL;        
        
        // temp used to traverse the Linked List
        struct Node* temp = *head; 
        
        // traverse till the nth node
        while(--n)
            temp=temp->next;
        
        // assigning (n+1)th node's previous to this new node
        (temp->next)->prev = newNode;
        
        // newNode's next assigned to (n+1)th node
        // newNode's previous assigned to nth node
        newNode->next= temp->next;
        newNode->prev = temp;
        
        // assign nth node's next to newNode
        temp->next = newNode;
    }
}

//function to print the doubly linked list
void display(struct Node* node) 
{ 
    struct Node *end = NULL;
    printf("List in Forward direction: ");
    while (node != NULL) { 
        printf(" %d ", node->data); 
        end = node;
        node = node->next; 
    }
    
    printf("\nList in backward direction: ");

    while (end != NULL) { 
        printf(" %d ", end->data); 
        end = end->prev; 
    }
}
// required for insertAfter
int getLength(struct Node* node){
    int len = 0;

    while(node!=NULL){
        node = node->next;
        len++;
    }
    return len;
}
int main()
{
    struct Node* head = NULL;

    insertStart(&head,20);
    insertStart(&head,0);

    insertLast(&head,40);
    insertLast(&head,60);
    insertLast(&head,80);
    
    // 500 to be entered after 3rd node
    insertAfter(&head,3, 500);

    // & not required to be passed as head will not change
    display(head); 
    return 0; 
}

STACK USING LINKEDLIST


#include <stdio.h>  
#include <stdlib.h>  
void push();  
void pop();  
void display();  
struct node   
{  
int val;  
struct node *next;  
};  
struct node *head;  
  
void main ()  
{  
    int choice=0;     
    printf("\n*********Stack operations using linked list*********\n");  
    printf("\n----------------------------------------------\n");  
    while(choice != 4)  
    {  
        printf("\n\nChose one from the below options...\n");  
        printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");  
        printf("\n Enter your choice \n");        
        scanf("%d",&choice);  
        switch(choice)  
        {  
            case 1:  
            {   
                push();  
                break;  
            }  
            case 2:  
            {  
                pop();  
                break;  
            }  
            case 3:  
            {  
                display();  
                break;  
            }  
            case 4:   
            {  
                printf("Exiting....");  
                break;   
            }  
            default:  
            {  
                printf("Please Enter valid choice ");  
            }   
    };  
}  
}  
void push ()  
{  
    int val;  
    struct node *ptr = (struct node*)malloc(sizeof(struct node));   
    if(ptr == NULL)  
    {  
        printf("not able to push the element");   
    }  
    else   
    {  
        printf("Enter the value");  
        scanf("%d",&val);  
        if(head==NULL)  
        {         
            ptr->val = val;  
            ptr -> next = NULL;  
            head=ptr;  
        }   
        else   
        {  
            ptr->val = val;  
            ptr->next = head;  
            head=ptr;  
               
        }  
        printf("Item pushed");  
          
    }  
}  
  
void pop()  
{  
    int item;  
    struct node *ptr;  
    if (head == NULL)  
    {  
        printf("Underflow");  
    }  
    else  
    {  
        item = head->val;  
        ptr = head;  
        head = head->next;  
        free(ptr);  
        printf("Item popped");  
          
    }  
}  
void display()  
{  
    int i;  
    struct node *ptr;  
    ptr=head;  
    if(ptr == NULL)  
    {  
        printf("Stack is empty\n");  
    }  
    else  
    {  
        printf("Printing Stack elements \n");  
        while(ptr!=NULL)  
        {  
            printf("%d\n",ptr->val);  
            ptr = ptr->next;  
        }  
    }  
}  
SINGLY LINKED LIST

LINKED LIST
#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node * insert(struct node *head, int data)
{
    struct node *newnode = (struct node *)malloc(sizeof(struct node *));
    newnode->data = data;
    newnode->next = head;
    return newnode;
}
void traversal(struct node *ptr)
{
    while (ptr != NULL)
    {
        printf("%d", ptr->next);
        ptr = ptr->next;
    }
}
struct node * insertmid(struct node *head, int data,int index)
{
    struct node *newnode = (struct node *)malloc(sizeof(struct node *));
    struct node * ptr=NULL;
    ptr=head;
    for (int i = 0; i < (index-1); i++)
    {
        ptr=ptr->next;
    }
    newnode->data=data;
    newnode->next=ptr->next;
    ptr->next=newnode;
    return head;
}
void printdata(struct node*head)
{
    if (head==NULL)
    {
        printf("linkedlist is empty");
    }
    struct node*ptr=NULL;
    ptr=head;
    while (ptr != NULL)
    {
       printf("%d\n",ptr->data);
       ptr=ptr->next;
    }
    

}
int main()
{
    struct node *head;  
    struct node *secound;  
    struct node *third;  
    struct node *fourth;  
    head=(struct node *)malloc(sizeof(struct node));
    secound=(struct node *)malloc(sizeof(struct node));
    third=(struct node *)malloc(sizeof(struct node));
    fourth=(struct node *)malloc(sizeof(struct node));
    head->data=40;
    head->next=secound;
    secound->data=56;
    secound->next=third;
    third->data=90;
    third->next=fourth;
    fourth->data=69;
    fourth->next=NULL;
     printf("before insertion\n");
    printdata(head);
    printf("after insertion\n");
    head=insert(head,56);
    printdata(head);
    printf("after insertionin middle\n");
    head=insertmid(head,100,2);
    printdata(head);
    return 0;
}
Editor is loading...