Deleting node from singly linked list

This is a discussion on Deleting node from singly linked list within the C Programming forums, part of the General Programming Boards category; Following code deletes the node in liked list just by traversing once. Wondering any other alternative method to achieve the ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    22

    Deleting node from singly linked list

    Following code deletes the node in liked list just by traversing once. Wondering any other alternative method to achieve the same.

    Code:
    Code for deleting a node
    
    struct node 
    {
    	int data;
    	struct node *next;
    }node;
    
    struct node *first,*save,*new1;
    
    add(int i)
    {
    	if (!first)
    	{
    		first = (struct node *)malloc(sizeof(node));
    		first->next = NULL;
    		first->data = i;
    		save = first;
    	}
    	else
    	{
    		new1 = (struct node *)malloc(sizeof(node));
    		new1->data = i;
    		new1->next = NULL;
    		save->next = new1;
    		save = new1;
    	}
    }
    
    
    disp2()
    {
    	struct node *first1 = first;
    
    	printf("\n\nNumbers are \n");
    
    	while ( first1 )
    	{
    		printf("%d\t",first1->data);
    		first1 = first1->next;
    	}
    }
    
    delete_nth_node_from_end(int n)
    {
    	struct node *first1,*n_th_node,*save;
    	int count = 0;
    	save = first1 = n_th_node = first;
    
    	while ( first1->next )
    	{
    		first1 = first1->next;
    		count++;
    		if ( count == (n-1) )
    			break;
    	}
    
    	if ( count < (n-1) )
    	{
    		printf("Link list is not large enough\n");
    		return;
    	}
    
    	if ( first1->next == 0 )
    	{
    		first = n_th_node->next;
    		free(first);
    		return;
    	}
    
    	while ( first1->next != 0 )
    	{
    		first1 = first1->next;
    		save = n_th_node;
    		n_th_node = n_th_node->next;
    	}
    	
    	save->next = n_th_node->next;
    	free(n_th_node);
    	return;
    }
    
    
    main()
    {
    	int i;
    	int n;
    	while ( 1 )
    	{
    		printf("Enter the number:");
    		scanf("%d",&i);
    		if ( i )
    			add(i);
    		else
    			break;
    	}
    	printf("\n\nBefore deletion\n");
    	disp2();
    	delete_nth_node_from_end(n = 4);
    	printf("\n\nAfter deletion\n");
    	disp2();
    }

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    22
    I mean deleting nth node from end in single traverse.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I mean deleting nth node from end in single traverse.
    This question has been asked and answered fully recently.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 08:53 PM
  2. Need urgent help in linked list
    By Cassandra in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2009, 08:47 PM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  4. Dynamic list of Objects in External File
    By TechWins in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2002, 02:05 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21