Showing posts with label LinkedList Programs. Show all posts
Showing posts with label LinkedList Programs. Show all posts

Tuesday, October 1, 2013

Linked List Program in C


/*
LinkedList.c: this program is implementation of LinkedList in C. 

Date: 02/10/2013. 

*/


#include<stdio.h>
#include<string.h> 

void CreateNode(); 
void InsertElements(); 
void DisplayElements();
void DeleteElement(int a);

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

typedef Node *List;
List head,tail,temp; 

int main()
{
 int choice,data;  
 temp =NULL; head =NULL ; tail =head; 
 while(1){
 printf("==Linked List Menu==\n1.Insert Element\n2.Display\n3.Delete\n4.Exit\n");
 scanf("%d",&choice); 
 switch(choice)
 {
 case 1:
  InsertElements(); 
  break; 
 case 2:
  DisplayElements();
  break;
 case 3:
  printf("Enter the number that you want to delete\n");  
  scanf("%d",&data);
  DeleteElement(data); break; 
 default:
  printf("Thanks for using My programming ..\n");
  exit(0); 
 }
 }// end of infinite loop  


 return 0; 
}

void CreateNode()
{

 int data; 
 printf("Enter your element\n"); 
 scanf("%d",&data);
 temp = (List)malloc(sizeof(Node)); 
 temp->data = data; 
 temp->next =NULL; 
 
} 
void InsertElements()
{
 CreateNode(); 
 if(head == NULL)
 {
  head = temp; 
  tail =head; 
 }
 else 
 {
  tail->next = temp; 
  tail = tail->next;  
 }
}
void DisplayElements()
{
 if(head!=NULL)
 {
  printf("Elements===>\n");
 for(temp =head; temp!= NULL ; temp = temp->next)
  {
   printf("%d\t",temp->data);
  }
  printf("\n");
 }
 else
 {
  printf("Your Linked List Empty\n");
 }
}

void DeleteElement(int a)
{
 if(head!=NULL)
 {
  if(head->data == a)
  {
   head = head->next;
  }
  else{
   tail = head; 
   temp = head->next; 
   int found =0; 
  while(temp !=NULL)
   {
    if(temp->data ==a)
     {
     tail->next = temp->next;
 
          printf("element is found\\deleted\nPlease use option 2 to see remaining");
     found =1;     
     break;  
            }  
     else
     {
      tail = temp; 
      temp = temp->next;  
            }   
   }
   if(!found)
   printf("element is not present\n");   
      
      }
  
 
 }
 else
 {
  printf("Your Linked List Empty\n");
 }
}

Friday, April 8, 2011

Doubly Linked List Program in C

 /* Doubly linked list */  
 /* This program seg,ent creates a doubly linked list then prints in forward and backword */  
 /* Double_l.c */  
 #include <stdio.h>  
 #include <malloc.h>  
 void main(void)  
 {  
      int i;  
      struct ListEntry {  
           int number;  
           struct ListEntry *next;  
           struct ListEntry *previous;  
      } start, *node;  
      start.next = NULL; /* Empty list */  
      start.previous = NULL;  
      node = &start;   /* Point to the start of the list */  
      for (i = 1; i <= 10; i++)  
      {  
           node->next = (struct ListEntry *) malloc(sizeof(struct ListEntry));  
           node->next->previous = node;  
           node = node->next;  
           node->number = 50+i;  
           node->next = NULL;  
      }  
      /* Display the list */  
      node = start.next;  
      do {  
           printf("%d ", node->number);  
           node = node->next;  
      } while (node->next); /* Show 60 only one time */  
      do {  
           printf("%d ", node->number);  
           node = node->previous;  
      } while (node->previous);  
 }  

Merging Two Doubly Linked Lists

 /* MERGING TWO DOUBLY LINKED LISTS (ASCENDING ORDER) */  
 /* DBL_MRG.C */  
 # include <stdio.h>  
 # include <malloc.h>  
 struct Double  
 {  
      int info;  
      struct Double *next;  
      struct Double *previous;  
 };  
 int i;  
 struct Double start, start1, *new1, *local;  
 void Doubly_insertion (struct Double * );  
 void Doubly_Create (struct Double * );  
 void Display (struct Double *);  
 /* Function create a list of five nodes */  
 void Doubly_Create (struct Double *node)  
 {  
      start.next = NULL; /* Empty list */  
      start.previous = NULL;  
      node = &start;   /* Point to the start of the list */  
      for (i = 1; i < 10; i += 2)  
      {  
           node->next = (struct Double *) malloc(sizeof(struct Double ));  
           node->next->previous = node;  
           node = node->next;  
           node->info = i;  
           node->next = NULL;  
      }  
 }  
 void Doubly_insertion (struct Double *node)  
 {  
      struct Double *new1;  
      start1.next = NULL; /* Empty list */  
      start1.previous = NULL;  
      new1 = &start1;   /* Point to the start of the list */  
      for (i = 2; i <= 10; i += 2)  
      {  
           new1->next = (struct Double *) malloc(sizeof(struct Double ));  
           new1->next->previous = new1;  
           new1= new1->next;  
           new1->info = i;  
           new1->next = NULL;  
      }  
      new1 = start1.next;  
      printf("\n Second list is as follows");  
      while(new1)  
      {  
           printf("\n 0x%x", new1);  
           printf(" %d", new1->info);  
           new1 = new1->next;  
      }  
      new1 = start1.next;  
      while(new1)  
      {  
           int found = 0;  
           local = (struct Double *) malloc(sizeof(struct Double));  
           local = new1;  
           new1 = new1->next;  
           node = start.next;  
           do  
           {  
                if ( node->info > local->info )  
                {  
                     local->next = node;  
                     local->previous = node->previous;  
                     node->previous->next = local;  
                     node->previous = local;  
                     found = 1;  
                     break;  
                }  
                else  
                     node = node->next;  
           } while ((node->next) && (! found));  
           if (! found)  
                if (node->info> local->info)  
                {  
                     local->next = node;  
                     local->previous = node->previous;  
                     node->previous->next = local;  
                     node->previous = local;  
                }  
                else  
                {  
                     local->next = NULL;  
                     local->previous = node;  
                     node->next = local;  
                }  
      }  
 }  
 /* Display the list */  
 void Display (struct Double *node)  
 {  
      node = start.next;  
      while (node)  
      {  
           printf("\n 0x%x", node);  
           printf(" %d", node->info);  
           node = node->next;  
      }  
 }  
 /* Function main */  
 void main()  
 {  
      struct Double *node = (struct Double *) malloc(sizeof(struct Double));  
      Doubly_Create (node);  
      printf("\n First list is as follows\n");  
      Display (node);  
      Doubly_insertion (node);  
      printf("\n List after merging above two lists (Ascending order)\n");  
      Display (node);  
 }  

Creating Simple Doubly LInked List

 /* CREATING A SIMPLE DOUBLY LINKED LIST */  
 /* DBLINK.C */  
 # include <stdio.h>  
 # include <malloc.h>  
 struct Double  
 {  
      int info;  
      struct Double *next;  
      struct Double *previous;  
 };  
 int num ;  
 struct Double start;  
 void Doubly_link_list (struct Double *);  
 void display (struct Double *);  
 /* Function creates a simple doubly linked list */  
 void Doubly_link_list(struct Double *node)  
 {  
      char ch;  
      start.next = NULL; /* Empty list */  
      start.previous = NULL;  
      node = &start;   /* Point to the start of the list */  
      num = 0;  
      printf("\n Input choice n for break: ");  
      ch = getchar();  
      while( ch != 'n')  
      {  
           node->next = (struct Double *) malloc(sizeof(struct Double));  
           node->next->previous = node;  
           node = node->next;  
           printf("\n Input the values of the node : %d: ", (num+1));  
           scanf("%d", &node->info);  
           node->next = NULL;  
           fflush(stdin);  
           printf("\n Input choice n for break: ");  
           ch = getchar();  
           num ++;  
      }  
      printf("\n Total nodes = %d", num);  
 }  
 /* Display the list */  
 void display (struct Double *node)  
 {  
      node = start.next;  
      do {  
           printf("\n 0x%x", node);  
           printf(" %d", node->info);  
           node = node->next;  
      } while (node->next); /* Show value of last node only one time */  
      do {  
           printf("\n 0x%x", node );  
           printf(" %d", node->info);  
           node = node->previous;  
      } while (node->previous);  
 }  
 void main()  
 {  
      struct Double *node = (struct Double *) malloc(sizeof(struct Double));  
      Doubly_link_list(node);  
      printf("\n Created doubly linked list is as follows\n");  
      display(node);  
 }  
    

Inserting Nodes in the Doubly Linked List

 /* INSERTING SOME NODES IN THE DOUBLY LINKED LIST */  
 /* DBL_IL.C */  
 # include <stdio.h>  
 # include <malloc.h>  
 struct Double {  
      char info;  
      struct Double *next;  
      struct Double *previous;  
 };  
 int i;  
 struct Double start, *new1;  
 void Doubly_insertion_Last (struct Double *);  
 void Doubly_Create_Last (struct Double *);  
 void Display (struct Double *);  
 /* Function create a list of five nodes */  
 void Doubly_Create_Last (struct Double *node)  
 {  
      int i = 0;  
      char ch;  
      start.next = NULL; /* Empty list */  
      start.previous = NULL;  
      node = &start;   /* Point to the start of the list */  
      printf("\n Input choice n for break: ");  
      ch = getchar();  
      while (ch != 'n')  
      {  
           node->next = (struct Double *) malloc(sizeof(struct Double ));  
           node->next->previous = node;  
           node = node->next;  
           printf("\n Input the value for: %d: ", i+1);  
           scanf("%c", &node->info);  
           node->next = NULL;  
           i++;  
           fflush(stdin);  
           printf("\n Input choice n for break: ");  
           ch = getchar();  
      }  
 }  
 void Doubly_insertion_Last (struct Double *node)  
 {  
      node = start.next;  
      new1 = (struct Double *) malloc(sizeof(struct Double ));  
      fflush(stdin);  
      printf("\n Input the last node value: ");  
      scanf("%c", &new1->info);  
      if (node == NULL)  
      {  
           printf("\n List is empty\n");  
           printf("\n Insert as last node\n");  
      }  
      else  
           while(node)  
           {  
                node = node->next;  
           }  
      new1->next = node;  
      new1->previous = node->previous;  
      node->previous->next = new1;  
      node->next = new1;  
 }  
 /* Display the list */  
 void Display (struct Double *node)  
 {  
      node = start.next;  
      while (node)  
      {  
           printf("\n 0x%x", node);  
           printf(" %c", node->info);  
           node = node->next;  
      }  
 }  
 /* Function main */  
 void main()  
 {  
      struct Double *node = (struct Double *) malloc(sizeof(struct Double));  
      Doubly_Create_Last (node);  
      printf("\n Created list is as follows\n");  
      Display(node);  
      Doubly_insertion_Last (node);  
      printf("\n List after insertion of last node \n");  
      Display (node);  
 }  
    

Inserting First Node in Doubly Linked List

 /* INSERTING FIRST NODE IN THE DOUBLY LINKED LIST */  
 /* DBL_IF.C */  
 # include <stdio.h>  
 # include <malloc.h>  
 struct Double  
 {  
      char info[20];  
      struct Double *next;  
      struct Double *previous;  
 };  
 int i;  
 struct Double start, *new1;  
 void Doubly_insertion_First (struct Double *);  
 void Doubly_Create_First (struct Double *);  
 void Display (struct Double *);  
 /* Function create a list of five nodes */  
 void Doubly_Create_First (struct Double *node)  
 {  
      int i = 0;  
      char ch;  
      start.next = NULL; /* Empty list */  
      start.previous = NULL;  
      node = &start;   /* Point to the start of the list */  
      fflush(stdin);  
      printf("\n Input choice n for break: ");  
      ch = getchar();  
      while (ch != 'n')  
      {  
           node->next = (struct Double *) malloc(sizeof(struct Double ));  
           node->next->previous = node;  
           node = node->next;  
           printf("\n Input the value for: %d: ", i+1);  
           gets(node->info);  
           node->next = NULL;  
           i++;  
           fflush(stdin);  
           printf("\n Input choice n for break: ");  
           ch = getchar();  
      }  
 }  
 void Doubly_insertion_First (struct Double *node)  
 {  
      node = start.next;  
      new1 = (struct Double *) malloc(sizeof(struct Double ));  
      fflush(stdin);  
      printf("\n Input the first node value: ");  
      gets(new1->info);  
      printf("\n new1 address: 0x%x", new1);  
      printf("\n Node address: 0x%x", node);  
      new1->next = node;  
      printf("\n New1 next address: 0x%x", new1->next);  
      printf("\n Node previous address: 0x%x", node->previous);  
      new1->previous = node->previous;  
      node->previous->next = new1;  
      printf("\n node->previous.next: 0x%x", node->previous->next);  
      printf("\n Node previous address: 0x%x", node->previous);  
      printf("\n Node next address: 0x%x", node->next);  
      printf("\n new1 address: 0x%x", new1);  
      node->previous = new1;  
      printf("\n node->previous: 0x%x", node->previous);  
 }  
 /* Display the list */  
 void Display (struct Double *node)  
 {  
      node = start.next;  
      while (node)  
      {  
           printf("\n 0x%x", node);  
           printf(" %s", node->info);  
           node = node->next;  
      }  
 }  
 /* main function */  
 void main()  
 {  
      struct Double *node = (struct Double *) malloc(sizeof(struct Double));  
      Doubly_Create_First (node);  
      printf("\n Created list is as follows\n");  
      Display(node);  
      Doubly_insertion_First (node);  
      printf("\n List after insertion of first node \n");  
      Display (node);  
 }  

Delete Node from a Doubly Linked List

 /* DELETE A NODE FROM A SIMPLE DOUBLY LINKED LIST */ 
 # include <stdio.h>  
 # include <malloc.h>  
 struct Double  
 {  
      char info;  
      struct Double *next;  
      struct Double *previous;  
 };  
 int num ;  
 struct Double start;  
 void Doubly_Link_Del (struct Double *);  
 void Doubly_Link_Creat (struct Double *);  
 void display (struct Double *);  
 /* Function creates a doubly linked list */  
 void Doubly_Link_Creat (struct Double *node)  
 {  
      char ch;  
      start.next = NULL; /* Empty list */  
      start.previous = NULL;  
      node = &start;   /* Point to the start of the list */  
      num = 0;  
      printf("\n Input choice n for break: ");  
      ch = getchar();  
      while(ch != 'n')  
      {  
           node->next = (struct Double *) malloc(sizeof(struct Double));  
           node->next->previous = node;  
           node = node->next;  
           printf("\n Input the values of the node: %d:", (num+1));  
           scanf("%d", &node->info);  
           node->next = NULL;  
           fflush(stdin);  
           printf("\n Input choice n for break: ");  
           ch = getchar();  
           num ++;  
      }  
      printf("\n Total nodes = %d", num);  
 }  
 /* Function delete */  
 void Doubly_Link_Del (struct Double *node)  
 {  
      int delete_node;  
      int search_counter = 0;  
      printf("\n Input the node number to which you want delete: ");  
      scanf("%d", &delete_node);  
      node = start.next;  
      if ( node == NULL)  
      {  
           printf("\n Underflow\n");  
           printf("\n List is empty\n");  
      }  
      else  
           while(node)  
           {  
                if((search_counter + 1) == delete_node)  
                {  
                     node->previous->next = node->next ;  
                     node->next->previous = node->previous ;  
                     free(node);  
                }  
                else  
                {  
                     node = node->next;  
                }  
                search_counter++;  
           }  
 }  
 /* Display the list */  
 void display(struct Double *node)  
 {  
      node = start.next;  
      while (node)  
      {  
           printf("\n 0x%x", node);  
           printf(" %d", node->info);  
           node = node->next;  
      }  
 }  
 /* Function main */  
 void main()  
 {  
      struct Double *node = (struct Double *) malloc(sizeof(struct Double));  
      Doubly_Link_Creat(node);  
      printf("\n Created linked list is as follows\n");  
      display(node);  
      Doubly_Link_Del(node);  
      printf("\n After deletion of a node linked list is as follows\n");  
      display(node);  
 }   

CREATING CIRCULAR HEADER LINKED LIST in C

 /* CREATING CIRCULAR HEADER LINKED LIST */  
 /* CIRCLIST.C */  
 # include <stdio.h>  
 # include <malloc.h>  
 struct link  
 {  
      int info;  
      struct link *next;  
 };  
 int i; /* Represents number of nodes in the list */  
 int number;  
 struct link *start, *new1;  
 void insertion(struct link *);  
 void create_circular_list(struct link *);  
 void display(struct link *);  
 /* Function create a circular header linked list */  
 void create_circular_list( struct link *node)  
 {  
      char ch;  
      node = start;   /* Point to the header node in the list */  
      node->next = (struct link *) malloc(sizeof(struct link));  
      i = 0;  
      printf("\n Input choice n for break: ");  
      ch = getchar();  
      while(ch != 'n')  
      {  
           node->next = (struct link* ) malloc(sizeof(struct link));  
           node = node->next;  
           printf("\n Input the node: %d:", (i+1));  
           scanf("%d", &node->info);  
           fflush(stdin);  
           printf("\n Input choice n for break: ");  
           ch = getchar();  
           i++;  
      }  
      printf("\n Total nodes = %d", i);  
      node = start;  
      node->info = i; /* Assign total number of nodes to the header node */  
 }  
 /* Inserting a node in circular header linked */  
 void insertion(struct link *node)  
 {  
      int count = node->info;  
      int node_number = 0;  
      int insert_node;  
      node = start;  
      node = node->next;  
      printf("\n Input node number you want to insert: ");  
      printf("\n Value should be less are equal to the");  
      printf("\n number of nodes in the list: ");  
      scanf("%d", &insert_node);  
      while(count)  
      {  
           if((node_number+1) == insert_node)  
           {  
                new1 = (struct link* ) malloc(sizeof(struct link));  
                new1->next = node->next ;  
                node->next = new1;  
                printf("\n Input the node value: ");  
                scanf("%d", &new1->info);  
                node = node->next;  
                count--;  
           }  
           else  
           {  
                node = node->next;  
                count--;  
           }  
           node_number ++;  
      }  
      if (count == 0)  
      {  
           node = start; /* Points to header node */  
           node->info = node->info+1;  
      }  
 }  
 /* Display the list */  
 void display(struct link *node)  
 {  
      int count = node->info;  
      node = start;  
      node = node->next;  
      while (count-1)  
      {  
           printf("\n 0x%x", node);  
           printf(" %d ", node->info);  
           node = node->next;  
           count --;  
      }  
 }  
 /* Function main */  
 void main()  
 {  
      struct link *node = (struct link *) malloc(sizeof(struct link));  
      create_circular_list(node);  
      printf("\n Before inserting a node list is as follows:\n");  
      display(node);  
      insertion(node);  
      printf("\n After inserting a node list is as follows:\n");  
      display(node);  
 }  

Friday, January 28, 2011

Double Linked List Program in C

 /*  
      dlinkedlist.c: This programs is implementation of doubly linked list.   
      @uthor: itsafiz@gmail.com   
      Date: 11Oct2010.   
 */  
 #include<stdio.h>   
 typedef struct   
      {  
           int data;   
           struct dnode *next;   
           struct dnode *prev;   
      }dnode;  
 typedef dnode *list;   
 list head, tail;   
 main()  
 {  
      list temp,paste;   
      head=NULL;   
      int ch, n;   
      while(1)  
      {  
      printf("\t##Double Linked list ##\n\t1.Add element\n\t2.Display\n\t3.Delete\n\t4.Exit\n");  
      scanf("%d",&ch);  
      switch(ch)  
           {  
           case 1:   
           temp=(list)malloc(sizeof(dnode));  
           printf("Enter Data = ");  
           scanf("%d",&n);  
           temp->data=n;  
           if(head==NULL)  
           {  
           temp->prev=NULL;   
           head=temp;   
           }  
           else  
           {  
           temp->prev=tail;  
           tail->next=temp;  
           }  
           temp->next=NULL;  
           tail=temp;   
           break;   
           case 2:   
                paste=head;  
                for(;paste!=NULL;paste=paste->next)  
                     {  
                          printf("%d\t",paste->data);  
                     }  
                printf("\n");  
           break;   
           case 3: break;   
           case 4: exit(1);   
           }  
      }  
 }