Double Link List


 /* Name: doubly linklist  
  Description:The code is for creating  adding deleting and printing searching inserting

  
Comliped on turboc 3.0
Errors:0
Side Effects: Will make you completely understand
this code is copyrighted and has limited warranties.

TOPIC DOUBLYLINKED LIST (In Place Of Array of Structures)
All Basic Functions in a DOUBLY LINKED LIST
THE AUTHOR IS NOT RESPONSIBLE for ANY DAMAGE OR PLEASURE OR FUSS IN
YOUR HEAD OR PC. this CODE IS for EDUCATIONAL PURPOSE ONLY AND WILL
void WARRENTIES if USED ILLEGELY OR ABUSED IN ANY FORM...
*/

#include "stdio.h"
#include "alloc.h"

typedef struct dll
		 {
		 int data;
		 struct dll *llink,*rlink;
		 }*DLL;
DLL start,temp,end,tail,ptr;
int flag=0;

// ->x------->x-------->x------------>x
//  |                                 |
//  start                           tail

DLL AllocNode();
DLL Search(int,int );

void main(void)
	 {
	  int choice,Item;
	  start=NULL;
	  while(1)
	       {

		clrscr();
		printf("\n \t\t\tM E N U");
		printf("\n 1: Create \n 2: Append \n 3: print \n 4: Delete  \n 5: Search \n 6: Insert \n 7: Exit \n\n\t\t Enter Option [  ]\b\b");
		scanf("%d",&choice);
		      switch(choice)
			    {
			    case 1: Create(); puts("\npress any key...");getch();break;
			    case 2: Append(); break;
			    case 3: Print();  puts("\npress any key...");	getch();break;
			    case 4: Delete(); break;
			    case 5: printf("find : ");scanf("%d",&Item);temp=Search(Item,0);if(temp){puts("present");}else{puts("absent...");}getch();break;
			    case 6: Insert();break;
			    case 7: exit();
			    default: puts("...notthing to do..."); puts("\npress any key...");getch();break;
			    }
		  }    // e-o-while


      } // e-f-main


   Create()
	   {
	    if(start==NULL)
	       {
		printf("\n Creating list...");
		temp=AllocNode();
	       // temp->rlink=NULL;
		//temp->llink=NULL;
		printf("\n Enter starting data (in integer) :");
		scanf("%d",&temp->data);
		start=temp;

	       }

	      else{ printf("\n List already created @ %d with %d as data...",start,start->data);}

	   }



  Append()
	{
	   end=start;
	   if(start==NULL){Create();} //if not create
	   else
	      {

	       temp=AllocNode();
	       printf("\n Enter Data (in integer) :");
	       scanf("%d",&temp->data);
	       temp->rlink=NULL;

	       while(end->rlink!=NULL)    // find last node
		     end=end->rlink;

		    end->rlink=temp; // list is updated here  ->
		    temp->llink=end; //                       <-


		    tail=end->rlink;  // put pointer at tail;

	      }
   }



Print()
{
   DLL tmp;
   if(start==NULL){printf("\n List not created !!!"); getch();Create();}
   temp=start;
   tail=end->rlink; //dont miss this...
   printf("\n---------Printing...-------------\nForward direction\n");
   while(temp!=NULL)             // forward direction
	 {
	  printf("\t %d",temp->data);
	  temp = temp->rlink;
	 }
	 printf("\n");
  printf("\n---------Printing...-------------\nBackward direction\n");
	 temp=start;
	 if(temp->rlink==NULL){printf("%d",temp->data);return; } //for only one node
   while(tail!=NULL)             // forward direction
	 {

	  printf("\t %d",tail->data);
	  tail = tail->llink;
	 }


}



 Delete()
       {
	int value;
	DLL tmp;
	if(start==NULL){printf("\n List not created !!!"); getch();Create();}
	//temp=start;
	printf("\n data to delete :");
	scanf("%d",&value);
	ptr=Search(value,1);      // go to the node
	//printf("\n \t %d %d",ptr,ptr->data);
	ptr->llink->rlink=ptr->rlink;   // we break the links ->
	ptr->rlink->llink=ptr->llink;  //same here <-
	tmp=ptr;
	free(tmp); // free the node


       }



DLL Search(int item,int flag)
{
   temp = start;
   if(start==NULL){printf("\n List not created !!!"); getch();Create();}
   while(temp!=NULL)
     {
      if(temp->data==item )
       {
       if(flag==0){return(1);}      // for search only
       else{return(temp);}          // for ins and deletion
       }
      temp=temp->rlink;
     }
    // return(0);
}


DLL AllocNode()
 {
  DLL tmp;
  tmp=malloc(sizeof(struct dll));
   if(tmp==NULL)
     {
      printf("\n Insufficient memory...");
     }
     tmp->rlink=tmp->llink=NULL;
  return(tmp);
 }



Insert()
 {
  int node;
  DLL me;
  if(start==NULL){printf("\n List not created !!!"); getch();Create();}
  me=AllocNode();
  printf("After Which node ?  : ___& What is new Data___ :");
  scanf("%d",&node);
  scanf("%d",&me->data);
  ptr=Search(node,1);
  if(ptr->rlink==NULL){printf("\n notthing to do"); getch();return;}

  me->llink=ptr;                 //link to new node
  me->rlink=ptr->rlink;

  ptr->rlink->llink=me;
  ptr->rlink=me;

  printf("\n Done..........");
  getch();
 }
C program for Double Link List operations
Title: Another Double Link List Example (297 clicks)
Caption: C program for Double Link List operations
Filename: doublylinkedlist.zip
Size: 2 KB
C program for Double Link List operations
Title: Double Link List (296 clicks)
Caption: C program for Double Link List operations
Filename: doublelinklist.zip
Size: 9 KB