Tuesday, April 7, 2015

Sort the nodes of SLL in Descending order


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
 int data;
 struct node *next;
};


void append(struct node **q)
{
 struct node *temp;
  temp=*q;
 if(temp==NULL)
 {
  temp=(struct node *)malloc(sizeof(struct node ));
  printf("\nenter the data in Empty list : ");
  scanf("%d",&temp->data);
  temp->next=NULL;
  *q=temp;
}
else
{
 while(temp->next!=NULL)
 temp=temp->next;

 temp->next=(struct node *)malloc(sizeof(struct node ));
 temp=temp->next;
  temp->next=NULL;
 printf("\nenter the data : ");
 scanf("%d",&temp->data);
 }
}

void sort(struct node **q)
{
 struct node *a,*b,*temp;
  a=*q;
 while(a!=NULL)
 {
  b=a->next;
   while(b!=NULL)
   {
    if(b->data>a->data)
    {
      temp->data=b->data;
      b->data=a->data;
      a->data=temp->data;
    }
  b=b->next;
   }
 a=a->next;
}

}
void display(struct node *q)
{
 struct node *temp;
  temp=q;
 while(temp!=NULL)
  {
   printf(" %d",temp->data);
   temp=temp->next;
  }
}

void main()
{
 struct node *start;
 clrscr();
  start=NULL;
  append(&start);
  append(&start);
  append(&start);
  append(&start);
 printf("\nUnSorted List :");
  display(start);
 sort(&start);
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
 int data;
 struct node *next;
};


void append(struct node **q)
{
 struct node *temp;
  temp=*q;
 if(temp==NULL)
 {
  temp=(struct node *)malloc(sizeof(struct node ));
  printf("\nenter the data in Empty list : ");
  scanf("%d",&temp->data);
  temp->next=NULL;
  *q=temp;
}
else
{
 while(temp->next!=NULL)
 temp=temp->next;

 temp->next=(struct node *)malloc(sizeof(struct node ));
 temp=temp->next;
  temp->next=NULL;
 printf("\nenter the data : ");
 scanf("%d",&temp->data);
 }
}

void sort(struct node **q)

{

 struct node *a,*b,*temp;

  a=*q;

 while(a!=NULL)

 {

  b=a->next;

   while(b!=NULL)

   {

    if(b->data>a->data)

    {

      temp->data=b->data;

      b->data=a->data;

      a->data=temp->data;

    }

  b=b->next;

   }

 a=a->next;

}

}
void display(struct node *q)
{
 struct node *temp;
  temp=q;
 while(temp!=NULL)
  {
   printf(" %d",temp->data);
   temp=temp->next;
  }
}

void main()
{
 struct node *start;
 clrscr();
  start=NULL;
  append(&start);
  append(&start);
  append(&start);
  append(&start);
 printf("\nUnSorted List :");
  display(start);
 sort(&start);
  printf("\nSort list in Descending order");
  display(start);
 getch();
}

The following will be the output :

Sort the nodes in Ascending order Singly Link List


Monday, April 6, 2015

Write a program to delete Last node in Doubly Link List.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
struct node *prev;
int data;
struct node *next;
};

void append(struct node **q)
{
if(*q==NULL)
 {
   *q=(struct node *)malloc(sizeof(struct node));

  printf("\nenter the data in the link Doubly Link list: ");
  scanf("%d",&(*q)->data);
   (*q)->next=NULL;
   (*q)->prev=NULL;
 }
else
 {
  struct node *temp=*q;

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

  temp->next=(struct node*)malloc(sizeof(struct node));
  temp=temp->next;

  printf("\nEnter data in the Begining ");
  scanf("%d",&temp->data);
   temp->next=NULL;
  temp->prev=(*q);
  }
}


void display(struct node *q)
{
 struct node *temp;
  temp=q;

 while(temp!=NULL)
 {
  printf(" %d",temp->data);
  temp=temp->next;
  }
}

void delete(struct node **q)
{
 struct node *temp,*t;
  temp=*q;
   while(temp->next!=NULL)
   {
      t=temp;
     temp=temp->next;
    }
    free(temp);
    t->next=NULL;
  }


void main()
{
struct node *start;
 clrscr();
  start=NULL; //empty list
  append(&start);
  append(&start);
  append(&start);
  append(&start);
  printf("\nDoubly Link List is :");
   display(start);
    delete(&start);
    printf("\nAfter deleting a node in the list :");
   display(start);
getch();
} 

The output will be :







 


Write a programm to add node in the begining of Doubly Link List




#include<stdio.h>
#include<conio.h>
#include<alloc.h>

struct node
{
int data;
struct node *prev,*next;

};

void last(struct node **q)
{
 if(*q==NULL)
 {
  *q=(struct node*)malloc(sizeof(struct node));
  printf("\nlist is empty\nEnter the data in node :");
  scanf("%d",&(*q)->data);
  (*q)->prev=NULL;
  (*q)->next=NULL;

 }
else
 {
 struct node *temp;
 temp=*q;
 temp->prev=(struct node*)malloc(sizeof(struct node));
 temp=temp->prev;
 temp->next=(*q);
 (*q)->prev=temp;
 temp->prev=NULL;
  printf("\nenter the data in the Begining : ");
  scanf("%d",&temp->data);
 (*q)=temp;
 }
 printf("\nnode is created successfully!!");
}

void display(struct node *q)
{
 struct node *temp;
  temp=q;
  printf("\nnow the list is : ");
 while(temp!=NULL)
 {
  printf(" %d",temp->data);
  temp=temp->next;
 }
}

void main()
{
 struct node *start;
 struct node **q;
  clrscr();
 start=NULL;
 last(&start);
 last(&start);
 last(&start);
 last(&start);
 display(start);
getch();
}



No. of times Nodes repeated

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
   int data;
  struct node * next;
};

void append(struct node **q)
{
 struct node *temp;
    temp=*q;
   if(temp==NULL)
  {
   *q=(struct node *)malloc(sizeof(struct node));
    printf("\nenter the data");
   scanf("%d",&(*q)->data);
   (*q)->next=NULL;
}
else
{
  while(temp->next!=NULL)
   temp=temp->next;

   temp->next=(struct node *)malloc(sizeof(struct node));
    temp=temp->next;

  printf("\nenter the data");
    scanf("%d",&temp->data);
  temp->next=NULL'
  }
}

void count(struct node **q)
{
    struct node *temp,*n;
    int count=1;
       temp=*q;
       n=NULL;
printf("\nenter the no. to search");
  n=(struct node *)malloc(sizeof(struct node));  //new node to store the enter data uses for comparison
    scanf("%d",&n->data);
   n->next=NULL;  //node created
   while(temp->next!=NULL)
  {
    if(temp->data==n->data)  //camparing the input value to the node value
      count++;
    temp=temp->next;
  }
printf(\n %d repeated %d times");
   temp->data;
getch();
}

}
void display(struct node *q)
{
  struct node *temp;
  temp=q;
    while(temp!=NULL)
    {
      printf(" %d",temp->data);
      temp=temp->next;
     }
}

void display(struct node *q)
{
  struct node *temp;
  temp=q;
    while(temp!=NULL)
    {
      printf(" %d",temp->data);
      temp=temp->next;
     }
}

void main()
{
 struct node *start,*start2;
 start=NULL; //empty list1
  printf("\nenter the nodes in link list : ")  append(&start);
 append(&start);
 append(&start);
 append(&start);
  display(start) ;
 count(&start);
getch();
}
 The Output Will Be :