Double Linked List

Discussion in 'C' started by shabbir, Aug 27, 2006.

  1. C implementation of double linked list
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    #define N 100
    
    struct dlinklist
    {
    	struct dlinklist *prev;  /** Stores address of previous node **/
    	int roll_no;			 /** stores roll number **/
    	char name[N];            /** stores Name **/
    	float marks;             /** stores Marks **/
    	struct dlinklist *next;  /** stores address of next node **/
    };
    
    /** Redefining dlinklist as node **/
    typedef struct dlinklist node;
    
    void init(node*);     /** Input function **/
    void ins_aft(node*);  /** Function inserting before **/
    node* ins_bef(node*); /** Function inserting after **/
    node* del(node*);     /** Function deleting a node **/
    void search(node*);   /** Function for searching node **/
    void disp(node*);     /** Function for displaying node **/
    void rollsrch(node*); /** Function for searching node by roll number **/
    void namesrch(node*); /** Function for searching node by name **/
    void marksrch(node*); /** Function for searching node by marks **/
    
    void main()
    {
    	node *head;
    	char ch;                         /* Choice inputing varible */
    	int opt;                         /* Option inputing variable*/
    	static int flag=0;               /* Unchanged after iniialization */
    	clrscr();
    	head=(node*)malloc(sizeof(node));
    	head->next=NULL;
    	head->prev=NULL;
    	do
    	{
    again:
    	printf("\nEnter your option\n");
    	printf("\n1. Initialize the node\n");
    	printf("\n2. Insert before a specified node\n");
    	printf("\n3. Insert after a specified node\n");
    	printf("\n4. Delete a particular node\n");
    	printf("\n5. Search the nodes\n");
    	printf("\n6. Display all the nodes\n");
    	scanf("%d",&opt);
    	if(flag==0 && opt!=1)
    	{
    		printf("\nNo. You must first initialize at least one node\n");
    		goto again;
    	}
    	if(flag==1 && opt==1)
    	{
    		printf("\nInitialisation can occur only once.\n");
    		printf("\nNow you can insert a node\n");
    		goto again;
    	}
    	if(opt==4 && head->next==NULL)
    	{
    		printf("\nYou cannot delete the one and only the single node\n");
    		goto again;
    	}
    	if(flag==0 && opt==1)
    		flag=1;
    	switch(opt)
    	{
    	case 1:
    		init(head);
    		break;
    	case 2:
    		head=ins_bef(head);
    		break;
    	case 3:
    		ins_aft(head);
    		break;
    	case 4:
    		head=del(head);
    		break;
    	case 5:
    		search(head);
    		break;
    	case 6:
    		disp(head);
    		break;
    	}
    	printf("\nDo you wish to continue[y/n]\n");
    	ch=getche();
    	}while(ch=='Y' || ch=='y');
    	printf("\nDone by \"SHABBIR\"\n");
    	printf("\nPress any key to exit\n");
    	getch();
    }
    
    void init(node *current)
    {
    	current->prev=NULL;
    	printf("\nEnter Roll number\n");
    	scanf("%d",&current->roll_no);
    	printf("\nEnter the name\n");
    	fflush(stdin);
    	gets(current->name);
    	printf("\nEnter the marks\n");
    	scanf("%f",&current->marks);
    	current->next=NULL;
    }
    
    void ins_aft(node *current)
    {
    	int rno;             /* Roll number for inserting a node*/
    	int flag=0;
    	node *newnode;
    	newnode=(node*)malloc(sizeof(node));
    	printf("\nEnter the roll number after which you want to insert a node\n");
    	scanf("%d",&rno);
    	init(newnode);
    	while(current->next!=NULL)
    	{
    		/***  Insertion checking for all nodes except last  ***/
    		if(current->roll_no==rno)
    		{
    			newnode->next=current->next;
    			current->next->prev=newnode;
    			current->next=newnode;
    			newnode->prev=current;
    			flag=1;
    		}
    		current=current->next;
    	}
    	if(flag==0 && current->next==NULL && current->roll_no==rno)
    	{
    		/***  Insertion checking for last nodes  ***/
    		newnode->next=current->next;
    		current->next=newnode;
    		flag=1;
    	}
    	else if(flag==0 && current->next==NULL)
    		printf("\nNo match found\n");
    }
    
    node* ins_bef(node *current)
    {
    	int rno;            /* Roll number for inserting a node*/
    	node *newnode,*temp;
    	newnode=(node*)malloc(sizeof(node));
    	printf("\nEnter the roll number before which you want to insert a node\n");
    	scanf("%d",&rno);
    	init(newnode);
    	if(current->roll_no==rno)
    	{
    		/*** Insertion checking for first node ***/
    		newnode->next=current;
    		current->prev=newnode;
    		current=newnode;
    		return(current);
    	}
    	temp=current;
    	while(temp->next!=NULL)
    	{                                       
    		/*** Insertion checking for all node except first ***/
    		if(temp->next->roll_no==rno)
    		{
    			newnode->next=temp->next;
    			temp->next->prev=newnode;
    			temp->next=newnode;
    			newnode->prev=temp;
    			return(current);
    		}
    		temp=temp->next;
    	}
    	/*
    	If the function does not return from any return statement.
    	There is no match to insert before the input  roll number.
    	*/
    	printf("\nMatch not found\n");
    	return(current);
    }
    
    node* del(node *current)
    {
    	int rno;               /* Roll number for deleting a node*/
    	node *newnode,*temp;
    	printf("\nEnter the roll number whose node you want to delete\n");
    	scanf("%d",&rno);
    	newnode=current;
    	if(current->roll_no==rno)
    	{
    		/***  Checking condition for deletion of first node  ***/
    		newnode=current; /*  Unnecessary step  */
    		current=current->next;
    		current->prev=NULL;
    		free(newnode);
    		return(current);
    	}
    	else
    	{
    		while(newnode->next->next!=NULL)
    		{
    			/***  Checking condition for deletion of   ***/
    			/*** all nodes except first and last node  ***/
    			if(current->next->roll_no==rno)
    			{
    				newnode=current;
    				temp=current->next;
    				newnode->next=newnode->next->next;
    				newnode->next->prev=current;
    				free(temp);
    				return(current);
    			}
    			newnode=newnode->next;
    		}
    		if(newnode->next->next==NULL && newnode->next->roll_no==rno)
    		{
    			/***  Checking condition for deletion of last node  ***/
    			temp=newnode->next;
    			free(temp);
    			newnode->next=NULL;
    			return(current);
    		}
    	}
    	printf("\nMatch not found\n");
    	return(current);
    }
    
    void search(node *current)
    {
    	int ch;                  /* Choice inputing variable */
    	printf("\nEnter the criteria for search\n");
    	printf("\n1. Roll number\n");
    	printf("\n2. Name\n");
    	printf("\n3. Marks\n");
    	scanf("%d",&ch);
    	switch(ch)
    	{
    	case 1:
    		rollsrch(current);
    		break;
    	case 2:
    		namesrch(current);
    		break;
    	case 3:
    		marksrch(current);
    		break;
    	default:
    		rollsrch(current);
    	}
    }
    
    void rollsrch(node *current)
    {
    	int rno;
    	printf("\nEnter the roll number to search\n");
    	scanf("%d",&rno);
    	while(current->next!=NULL)
    	{
    		if(current->roll_no==rno)
    			printf("\n%d\t%s\t%f\n",current->roll_no,current->name,current->marks);
    		current=current->next;
    	}
    	if(current->next==NULL && current->roll_no==rno)
    		printf("\n%d\t%s\t%f\n",current->roll_no,current->name,current->marks);
    }
    
    void namesrch(node *current)
    {
    	char arr[20];
    	printf("\nEnter the name to search\n");
    	fflush(stdin);
    	gets(arr);
    	while(current->next!=NULL)
    	{
    		if(strcmp(current->name,arr)==NULL)
    			printf("\n%d\t%s\t%f\n",current->roll_no,current->name,current->marks);
    		current=current->next;
    	}
    	if(current->next==NULL && strcmp(current->name,arr)==NULL)
    		printf("\n%d\t%s\t%f\n",current->roll_no,current->name,current->marks);
    }
    
    void marksrch(node *current)
    {
    	float marks;
    	printf("\nEnter the marks to search\n");
    	scanf("%f",&marks);
    	while(current->next!=NULL)
    	{
    		if(current->marks==marks)
    			printf("\n%d\t%s\t%f\n",current->roll_no,current->name,current->marks);
    		current=current->next;
    	}
    	if(current->next==NULL && current->marks==marks)
    		printf("\n%d\t%s\t%f\n",current->roll_no,current->name,current->marks);
    }
    
    void disp(node *current)
    {
    	while(current!=NULL)
    	{
    		printf("\n%d\t%s\t%f",current->roll_no,current->name,current->marks);
    		current=current->next;
    	}
    }
    
     
  2. rahul.mca2001

    rahul.mca2001 New Member

    Joined:
    Feb 13, 2008
    Messages:
    103
    Likes Received:
    0
    Trophy Points:
    0
    well explained
     
  3. sateshb

    sateshb New Member

    Joined:
    Jun 19, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hey dude my question is that if a read a name in from a file and i want to add it to the linked list how do i do that ?

    typedef struct list{
    name[30];
    struct *next, *prev;
    }

    in my add function i wanna add a my name "satesh" but get if from a file.
    Won't work if i have i had:
    ...... myName[10] = "satesh"
    head->name = myName; .......

    help !
     
  4. prabakongu

    prabakongu New Member

    Joined:
    Apr 17, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    tiruppur
    hi.......sir........i little bit know only poiter........so now i have confuesd........pls give me full explantion in doubly linked list................
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice