Read in your tongue here..

Showing posts with label linked list. Show all posts
Showing posts with label linked list. Show all posts

Monday, July 8, 2013

Circular Linked List (singly)...

#include<alloc.h>
struct list{
      int data;
      struct list *next;
}*head=NULL,*temp,*node;
void laado(void),infirst(void), inpos(void), inlast(void), delfront(void), delpos(void), dellast(void), display(void), count(void);
int num,pos,empty(void);
char con;
int empty(void)
{
      if(head==NULL)
      {
            clrscr();
            printf("List is empty!!!");
            return 1;
      }
      else
            return 0;
}
void main()
{
printf("\nMain Menu\n1.Insert at first\n2.Insert at specified position\n3.Insert at last\n4.Delete first\n5.Delete specified position\n6.Delete from last\n7.Display\n8.Count number of nodes\n9.Exit\nEnter choice:");
      scanf("%d",&num);
      clrscr();
      switch(num)
      {
            case 1:
                  infirst();
                  break;
            case 2:
                  inpos();
                  break;
            case 3:
                  inlast();
                  break;
            case 4:
                  delfront();
                  break;
            case 5:
                  delpos();
                  break;
            case 6:
                  dellast();
                  break;
            case 7:
                  display();
                  break;
            case 8:
                  count();
                  break;
            case 9:
                  clrscr();
                  exit();
            case 45:
                  laado();
                  break;
            default:
                  clrscr();
                  puts("\nWRONG INPUT!!");
      }
      main();
}
void infirst(void)
{
      do
      {
            printf("Enter number:");
            scanf("%d",&num);
            if(head==NULL)
            {
                  head=(struct list*)malloc(sizeof(struct list));
                  head->data=num;
                  head->next=head;
            }
            else
            {
                  temp=head;
                  while(temp->next!=head)
                        temp=temp->next;
                  node=(struct list*)malloc(sizeof(struct list));
                  node->data=num;
                  node->next=head;
                  temp->next=node;
                  head=node;
            }
            printf("Continue [y/n]?:");
            con=getche();
      }while(con=='y'||con=='Y');
}
void inpos(void)
{
      do
      {
            num=0;
            temp=head;
            printf("Enter position to insert at:");
            scanf("%d",&pos);
            while(temp->next!=head)
            {
                  num++;
                  if(num==pos-1)
                        break;
                  temp=temp->next;
            }
            printf("Enter number:");
            scanf("%d",&num);
            node=(struct list*)malloc(sizeof(struct list));
            node->data=num;
            node->next=temp->next;
            temp->next=node;
            printf("Continue [y/n]?:");
            con=getche();
      } while(con=='y'||con=='Y');
}
void inlast(void)
{
      do
      {
            printf("\nEnter number:");
            scanf("%d",&num);
            temp=head;
            while(temp->next!=head)
                  temp=temp->next;
            node=(struct list *)malloc(sizeof(struct list));
            node->data=num;
            node->next=head;
            temp->next=node;
            printf("\nContinue [y/n]?:");
            con=getche();
      } while(con=='y'||con=='Y');
}

void delfront(void)
{
      do
      {
            if(empty()==1) break;
            else
            {
                  temp=head;
                  while(temp->next!=head)
                        temp=temp->next;
                  if(temp==head)
                  {
                        free(head);
                        head=NULL;
                  }
                  else
                  {
                        temp->next=head->next;
                        free(head);
                        head=temp->next;
                  }
                  printf("Continue [y/n]?:");
                  con=getche();
            }
      }while(con=='y'||con=='Y');
}
void delpos(void)
{
      do
      {
            if(empty()==1) break;
            if(head->next==head)
            {
                  free(head);
                  head=NULL;
            }
            else
            {
                  num=0;
                  temp=head;
                  printf("Enter position:");
                  scanf("%d",&pos);
                  while(temp->next!=head)
                  {
                        num++;
                        if(num==pos-1)
                              break;
                        temp=temp->next;
                  }
                  node=temp->next->next;
                  free(temp->next);
                  temp->next=node;
                  printf("Continue [y/n]?:");
                  con=getche();
            }
      }while(con=='y'||con=='Y');
}
void dellast(void)
{
      do
      {
            if(empty()==1) break;
            else
            {
                  temp=head;
                  while(temp->next!=head)
                  {
                        node=temp;
                        temp=temp->next;
                  }
                  if(temp==head)
                  {
                        free(head);
                        head=NULL;
                  }
                  else
                  {
                        node->next=head;
                        free(temp);
                  }
                  printf("Continue [y/n]?:");
                  con=getche();
            }
      } while(con=='y'||con=='Y');
}

void display(void)
{
      if(empty()==1) return;
      else
      {
            for(temp=head;temp->next!=head;temp=temp->next)
                  printf("%d ",temp->data);
            printf("%d",temp->data);
      }
}

void count(void)
{
      if(empty()==1);
      else
      {
            num=1;
            temp=head;
            while(temp->next!=head)
            {
                  num++;
                  temp=temp->next;
            }
            printf("Total number of nodes=%d",num);
      }
}
void laado(void)
{
      clrscr();
      temp=head;
      printf("Dynamic View of the list:\n");
      while(temp->next!=head)
      {
            printf("[%u|%d|%u]--->",temp,temp->data,temp->next);
            temp=temp->next;
      }
      printf("[%u|%d|%u]",temp,temp->data,temp->next,head);
}

Friday, June 28, 2013

Singly Linked List

#include<alloc.h>
struct list{
     int data;
     struct list *next;
}*head=NULL,*temp,*node;
void infirst(void), inpos(void), inlast(void), delfront(void), delpos(void), dellast(void), display(void), count(void);
int num,pos,empty(void);
char con;
int empty(void)
{
     if(head==NULL)
     {
           clrscr();
           printf("The list is empty!!");
           return 1;
     }
     return 0;
}
void main()
{
printf("\n\n\tMain Menu\n1.Insert at first\n2.Insert at specified position\n3.Insert at last\n4.Delete first\n5.Delete specified position\n6.Delete from last\n7.Display\n8.Count number of nodes\n9.Exit\nEnter choice:");
     scanf("%d",&num);
     switch(num)
     {
           case 1:
                infirst();
                break;
           case 2:
                inpos();
                break;
           case 3:
                inlast();
                break;
           case 4:
                delfront();
                break;
           case 5:
                delpos();
                break;
           case 6:
                dellast();
                break;
           case 7:
                display();
                break;
           case 8:
                count();
                break;
           case 9:
                clrscr();
                exit();
           default:
                clrscr();
                puts("\nWRONG INPUT!!");
     }
     main();
}
void infirst(void)
{
     do
     {
           printf("\nEnter number:");
           scanf("%d",&num);
           temp=(struct list*)malloc(sizeof(struct list));
           temp->data=num;
           temp->next=head;
           head=temp;
           printf("Continue [y/n]?:");
           con=getche();
     }while(con=='y'||con=='Y');
}
void inpos(void)
{
     do
     {
           num=0;
           temp=head;
           printf("Enter position to insert at:");
           scanf("%d",&pos);
           while(temp->next!=NULL)
           {
                num++;
                if(num==pos-1)
                     break;
                temp=temp->next;
           }
           printf("Enter number:");
           scanf("%d",&num);
           node=(struct list*)malloc(sizeof(struct list));
           node->data=num;
           node->next=temp->next;
           temp->next=node;
           printf("Continue [y/n]?:");
           con=getche();
     } while(con=='y'||con=='Y');
}
void inlast(void)
{
     do
     {
           printf("\nEnter number:");
           scanf("%d",&num);
           temp=head;
           while(temp->next!=NULL)
                temp=temp->next;
           node=(struct list *)malloc(sizeof(struct list));
           node->data=num;
           node->next=NULL;
           temp->next=node;
           printf("\nContinue [y/n]?:");
           con=getche();
     } while(con=='y'||con=='Y');
}

void delfront(void)
{
     do{
           if(empty()==1)
                break;
           else
           {
                temp=head;
                head=temp->next;
                free(temp);
                printf("Continue [y/n]?:");
                con=getche();
           }
     } while(con=='y'||con=='Y');
}
void delpos(void)
{
     do{
           if(empty()==1)
                break;
           else
           {
                num=0;
                temp=head;
                printf("Enter position:");
                scanf("%d",&pos);
                while(temp->next!=NULL)
                {
                     num++;
                     if(num==pos-1)
                     break;
                     temp=temp->next;
                }
                node=temp->next->next;
                free(temp->next);
                temp->next=node;
                printf("Continue [y/n]?:");
                con=getche();
           }
     } while(con=='y'||con=='Y');
}
void dellast(void)
{
     do{
           if(empty()==1)
                break;
           else
           {
                temp=head;
                while(temp->next!=NULL)
                {
                     node=temp;
                     temp=temp->next;
                }
                node->next=NULL;
                free(temp);
                printf("Continue [y/n]?:");
                con=getche();
           }
     } while(con=='y'||con=='Y');
}
void display(void)
{
     if(empty()==1)
return;
     else
     {
           clrscr();
           printf("The list is: ");
          for(temp=head;temp!=NULL;temp=temp->next)
                printf("%d ",temp->data);
     }
}
void count(void)
{
     num=0;
     temp=head;
     while(temp!=NULL)
     {
           num++;
           temp=temp->next;
     }
     clrscr();
     printf("Total number of nodes=%d",num);
}



Doubly Linked List

#include<alloc.h>
struct list{
     int data;
     struct list *prev,*next;
}*head=NULL,*temp,*node;
void infirst(void), inpos(void), inlast(void), delfront(void), delpos(void), dellast(void), display(void), count(void);
int num,pos,empty(void);
char con;
int empty(void)
{
     if(head==NULL)
     {
           clrscr();
           printf("The list is empty!!");
           return 1;
     }
     return 0;
}

void main()
{
     printf("\nMain Menu\n1.Insert at first\n2.Insert at specified position\n3.Insert at last\n4.Delete first\n5.Delete specified position\n6.Delete from last\n7.Display\n8.Count number of nodes\n9.Exit\nEnter choice:");
     scanf("%d",&num);
     switch(num)
     {
           case 1:
                infirst();
                break;
           case 2:
                inpos();
                break;
           case 3:
                inlast();
                break;
          case 4:
                delfront();
                break;
           case 5:
                delpos();
                break;
           case 6:
                dellast();
                break;
           case 7:
                display();
                break;
           case 8:
                count();
                break;
           case 9:
                clrscr();
                exit();
           default:
                clrscr();
                puts("\nWRONG INPUT!!");
     }
     main();
}
void infirst(void)
{
     do
     {
           printf("\nEnter number:");
           scanf("%d",&num);
           temp=(struct list*)malloc(sizeof(struct list));
           temp->data=num;
           temp->next=head;
           temp->prev=NULL;
           head=temp;
           printf("\nContinue [y/n]?:");
           con=getche();
     }while(con=='y'||con=='Y');
}
void inpos(void)
{
     do
     {
           num=0;
           temp=head;
           printf("\nEnter position to insert at:");
           scanf("%d",&pos);
           while(temp->next!=NULL)
           {
                num++;
                if(num==pos-1)
                     break;
                temp=temp->next;
           }
           printf("\nEnter number:");
           scanf("%d",&num);
           node=(struct list*)malloc(sizeof(struct list));
           node->data=num;
           node->next=temp->next;
           node->prev=temp;
           temp->next=node;
           printf("\nContinue [y/n]?:");
           con=getche();
     } while(con=='y'||con=='Y');
}
void inlast(void)
{
     do
     {
           printf("\nEnter data:");
           scanf("%d",&num);
           temp=head;
           while(temp->next!=NULL)
                temp=temp->next;
           node=(struct list *)malloc(sizeof(struct list));
           node->data=num;
           node->next=NULL;
           node->prev=temp;
           temp->next=node;
           printf("\nContinue [y/n]?:");
           con=getche();
     } while(con=='y'||con=='Y');
}

void delfront(void)
{
     do
     {
           if(empty()==1)
                break;

           else
           {
                temp=head;
                head=temp->next;
                head->prev=NULL;
                free(temp);
                printf("\nContinue [y/n]?:");
                con=getche();
           }
     } while(con=='y'||con=='Y');
}
void delpos(void)
{
     do
     {
           if(empty()==1)
                break;

           else
           {
                num=0;
                temp=head;
                printf("\nEnter position:");
                scanf("%d",&pos);
                while(temp->next!=NULL)
                {
                num++;
                if(num==pos-1)
                     break;
                temp=temp->next;
                }
                node=temp->next->next;
                free(temp->next);
                temp->next=node;
                node->prev=temp;
                printf("\nContinue [y/n]?:");
                con=getche();
           }
     } while(con=='y'||con=='Y');
}
void dellast(void)
{
     do
     {
           if(empty()==1)
                break;

           else
           {
                temp=head;
                while(temp->next!=NULL)
                {
                     node=temp;
                     temp=temp->next;
                }
                node->next=NULL;
                free(temp);
                printf("\nContinue [y/n]?:");
                con=getche();
           }
     } while(con=='y'||con=='Y');
}
void display(void)
{
     if(empty()==1)
return;
     else
     {
           puts("The list is:");
           for(temp=head;temp!=NULL;temp=temp->next)
                printf("%d ",temp->data);
     }
}
void count(void)
{
     num=0;
     temp=head;
     while(temp!=NULL)
     {
           num++;
           temp=temp->next;
     }
     clrscr();
     printf("\nTotal number of nodes=%d",num);
}