Wednesday, 9 April 2014

write a program to implement queue using linked list.
#include<stdio.h>
#include<stdlib.h>
void insertion();
void display();
void create_node();
void deletion();
struct link_list
{
  int data;
  struct link_list *next;
}*start=NULL;
typedef struct link_list node;
node *current,*nnew,*temp;
int main()
 {
  int a;
  char ch;
   do
   {
    printf("\n enter your choice\n\n1->insertion\n2->display\n3->deletion\n");
    scanf("%d",&a);
    switch(a)
      {
      case 2:
      display();
       break;
      case 1:
       {
        insertion();
        }
         break;
      case 3:
       {
        deletion();
       }
        break;
      default:
        printf("wrong choice");
     }
    printf("\n \n press any key to continue and press n to quit->");
    scanf("%s",&ch);
   }while(ch!='n');
 return 0;
 }

void insertion()
{
 int i=0;
 printf("\nhow many nodes you want to create->");
 scanf("%d",&i);
 while(i!=0)
  {
  if(start==NULL)
   {
    create_node();
    start=nnew;
    current=start;
    printf("\tnode inserted successfully\n");
   }
  else
    {
       current=start;
       while(current->next!=NULL)
       {
       current=current->next;
       }
       create_node();
       current->next=nnew;
       current=nnew;
       printf("\tinserted successfully\n");
     }
   i--;
  }
}
void create_node()
{
       int k;
       nnew=malloc(sizeof(node));
       nnew->next=NULL;
       printf("enter the data for this node->");
       scanf("%d",&k);
       nnew->data=k;
}
void display()
{
       int c=0,i;
       current=start;
       while(current!=NULL)
{c++;
       current=current->next;
}
   for(i=0;i<c;i++)
   printf("=====");
   printf("\n");
   current=start;
while(current!=NULL)
{
   printf("%d | " ,current->data);
   current=current->next;
}
   printf("\n");
 for(i=0;i<c;i++)
  printf("=====");
}

void deletion()
{
       int d,i;
       printf("enter how many element you want to delete->");
       scanf("%d",&d);
       for(i=0;i<d;i++)
       {
       if(start==NULL)
       {
              start=NULL;
     printf("Queue is empty\n");
       }
       Else
     {
      temp=start;
      start=start->next;
      free(temp);
      printf("data is deleted");
     }
  }
}



write a programme of stack using linked list.
#include<stdio.h>
#include<stdlib.h>
void display(void);
void push(void);
void pop(void);
struct link_list
{
int data;
struct link_list *next;
}*start=NULL;
typedef struct link_list node;
node *current,*nnew;

int main()
{
int a;
char ch;
do
{
printf("\n enter your choice\n1->push\n2->pop\n3->display\n");
scanf("%d",&a);
switch(a)
{
case 3:
display();
break;
case 2:
       pop();
       break;
case 1:
push();
break;
default:
printf("wrong choice");
}
printf("\n press any key to continue and press n to quit->");
scanf("%s",&ch);
}while(ch!='n');
return (0);
}

void push(void)
{
int i=0,k;
printf("how many elements you want to enter->");
scanf("%d",&i);
while(i!=0)
{
nnew=malloc(sizeof(node));
nnew->next=NULL;
if(start==NULL)
{
printf("enter any integer data->");
scanf("%d",&k);
nnew->data=k;
start=nnew;
current=start;
printf("\t%d pushed successfully\n",start->data);
}
else
{
printf("enter any integer data->");
scanf("%d",&k);
nnew->data=k;
nnew->next=start;
start=nnew;
printf("\t%d pushed successfully\n",start->data);
}
i--;
}
}

void display(void)
{
              current=start;
              printf("\n");
              while(current!=NULL)
              {
              printf("\t%d\n" ,current->data);
              current=current->next;
       }
}
void pop(void)
{
       int count=0;
       printf("how many elements you want to pop->");
       scanf("%d",&count);
       current=start;
       while(count!=0)
       {
       if(start==NULL)
              printf("stack underflow!");
              else
              {
       printf("%d popped successfully\n",start->data);
       current=start;
start=start->next;
free(current);
}
       count--;
}

} 




Tuesday, 8 April 2014

/*write a program to implement merge sort*/
#include<stdio.h>
#define MAX 50

void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);

int main(){

    int merge[MAX],i,n;

    printf("Enter the total number of elements: ");
    scanf("%d",&n);

    printf("Enter the elements which to be sort: ");
    for(i=0;i<n;i++){
         scanf("%d",&merge[i]);
    }

    partition(merge,0,n-1);

    printf("After merge sorting elements are: ");
    for(i=0;i<n;i++){
         printf("%d ",merge[i]);
    }

   return 0;
}

void partition(int arr[],int low,int high){

    int mid;

    if(low<high){
         mid=(low+high)/2;
         partition(arr,low,mid);
         partition(arr,mid+1,high);
         mergeSort(arr,low,mid,high);
    }
}

void mergeSort(int arr[],int low,int mid,int high){

    int i,m,k,l,temp[MAX];

    l=low;
    i=low;
    m=mid+1;

    while((l<=mid)&&(m<=high)){

         if(arr[l]<=arr[m]){
             temp[i]=arr[l];
             l++;
         }
         else{
             temp[i]=arr[m];
             m++;
         }
         i++;
    }

    if(l>mid){
         for(k=m;k<=high;k++){
             temp[i]=arr[k];
             i++;
         }
    }
    else{
         for(k=l;k<=mid;k++){
             temp[i]=arr[k];
             i++;
         }
    }

    for(k=low;k<=high;k++){
         arr[k]=temp[k];
    }
}