Linked List Help

This is a discussion on Linked List Help within the C Programming forums, part of the General Programming Boards category; Hello! I've been having quite a struggle trying to conquer linked lists. Here is a portion of code. Essentially, it ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    Linked List Help

    Hello! I've been having quite a struggle trying to conquer linked lists.

    Here is a portion of code. Essentially, it creates a node and appends it to the end of a linked list. Or theoretically, that's what it should do.

    Here is my linked list node definition.
    Code:
    typedef struct node
    {
    char last[1024];
    char first[1024];
    char position;
    int value;
    struct node *link;
    }Node;
    Essentially, in my main, I call my insertNode function whenever I want to add a new node. It works fine for two nodes, as well as the error case of if the node->last already exists within the list for these two nodes. However, when I try to enter a third node, the function will promp me to enter values for last, first,position and value. However, after this, the function simply doesn't proceed or exit. It just stops working. I can hit letters and spaces and hit enter on the keyboard and it will just print these on screen. I'm having trouble identifying why my function stops working after two nodes.

    Thanks in advance!

    Here's the insertNode function.

    Code:
    Node *insertNode (Node *head)
    {
    Node *node = (Node *)malloc(sizeof(Node));
    Node *current = head;
    char lastName[1024];
    printf("  family name: ");
    safegets(lastName,MAX_LENGTH+1);
    
    printf("  first name: ");
    safegets(node->first,MAX_LENGTH+1);
    
    printf("  position: ");
    scanf("%c",&(node->position));
    getchar();
    
    printf("  value: ");
    scanf("%d",&(node->value));
    getchar();
    
    node->link=NULL;
    
    
    if (head==NULL)
    	{
    	strcpy((node->last),lastName);
    	return node;
    	}
    
    else if (head->link == NULL)
    	{
    	strcpy((node->last),lastName);
    	if (strcmp(lastName,head->last)==0)
    		{
    		familyNameDuplicate(lastName);
    		return head;
    		}
    	head->link=node;
    	return head;
    	}
    
    
    
    
    while (current->link != NULL);
    {
    	if (strcmp(lastName,current->last)==0)
    		{
    		familyNameDuplicate(lastName);
    		return head;
    		}
    current=current->link;
    }
    strcpy((node->last),lastName);
    current->link=node; 
    return head;
    }
    
    
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Can you post how you call your function?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    while (current->link != NULL);
    Try dropping that semicolon. It's an infinite loop since that semicolon means there's no loop body and thus current isn't being incremented (your actual loop body is treated as just a section of code in braces).

    Also, in the future, please post your code with proper indenting and make sure we have enough code to compile your program and reproduce the error (provide your safegets function and a small main function with 3 insertNode calls, e.g.).

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Essentially, I first declare the head
    Code:
    Node *head = NULL;
    There is a do loop that prompts the user for input of either I, D, S, V, P or Q to quit. The section with the function is:

    Code:
    if (response == 'I')
            {
            head = insertNode(head);
            }

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    @anduril462, good eyes!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

Tags for this Thread


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