Thread: Linked List Problem

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    Linked List Problem

    I was wanting to see if I could write a decent linked list in C by myself and this code runs with 0 errors or warnings. The problem is that when I run the program and try to put in a name it crashes. I tried to debug it, but I am not too comfortable with the debugger. All I noticed was that it seemed to have a problem with my while(current->next!=null) lines, saying that there was an access violation. Any help would be appreciated. Thanks in advance. Here's my code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    struct person
    {
    char name[80];
    struct person *next;
    };
    typedef struct person PERSON;
    typedef PERSON* LINK;
    void add(LINK ptr, LINK data);
    void remove(LINK ptr, char *name);
    void print(LINK ptr);
    int main()
    {
    LINK head=NULL;
    LINK person=(PERSON*)malloc(sizeof(PERSON));
    int selection=0;
    char name[80];
    printf("Hello. Enter your selection.\n");
    while(1)
    {
    printf("1. Add\n2.Delete\n3.Print\n4.Exit\n");
    fflush(stdin);
    scanf("%d",&selection);
    switch(selection)
    {
    case 1:
    printf("Enter a name.\n");
    fflush(stdin);
    gets(name);
    strcpy(person->name,name);
    add(head,person);
    break;
    case 2:
    printf("Enter the name you wish to delete.\n");
    fflush(stdin);
    gets(name);
    remove(head,name);
    break;
    case 3:
    print(head);
    break;
    case 4:
    return 0;
    default:
    printf("Invalid selection.\n");
    }
    }
    return 0;
    }

    void add(LINK head, LINK data)

    {
    //Puts the new entry in alphabetical order
    LINK current=head;
    LINK insert=data;
    LINK previous=head;
    while(current->next!=NULL)
    {
    if(strcmp(current->name,insert->name)>0)
    {
    insert->next=previous->next;
    previous->next=insert;
    break;
    }
    previous=current;
    current=current->next;
    }
    }
    void remove(LINK head, char *name)
    {
    LINK current=head;
    LINK previous=current;
    while(current->next!=NULL)
    {
    if(strcmp(current->name,name)==0)
    {
    previous->next=current->next;
    free(current);
    }

    }
    }
    void print(LINK head)
    {
    LINK current=head;
    while(current->next!=NULL)
    {
    printf("%s\n",current->name);
    }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) Use code tags. It angers everyone when you don't. (Or maybe just me.)
    Code:
    fflush(stdin);
    2) Do not flush input streams. It angers your compiler. (Actually, it's undefined behaviour, which angers Prelude. )
    Code:
    gets(name);
    3) Don't use gets. It likes to break programs. (The problem is, it doesn't stop at the size of your array, so if you enter 9999999 characters, it will actually try to read that many.)

    I'll stop there because it's just too damn hard to read.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    Alright, thanx!

    Thanx for the help. Yeah I saw the message about code tags after I posted(whoops!). I'll make sure to use them in the future.So I won't use fflush() and should I use scanf() rather than gets()?

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    This should be easier to read

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    //This is my(Brandon Tennant's) implementation of a linked list in C.
    //Enjoy.
    struct person
    	{
    		char name[80];
    		struct person *next;
    	};
    typedef struct person PERSON;
    typedef PERSON* LINK;
    void add(LINK ptr, LINK data);
    void remove(LINK ptr, char *name);
    void print(LINK ptr);
    int main()
    {
    	LINK head=NULL;
    	LINK person=(PERSON*)malloc(sizeof(PERSON));
    	int selection=0;
    	char name[80];
    	printf("Hello. Enter your selection.\n");
    	while(1)
    	{
    	printf("1. Add\n2.Delete\n3.Print\n4.Exit\n");
    	fflush(stdin);
    	scanf("%d",&selection);
    	switch(selection)
    	{
    	case 1:
    		printf("Enter a name.\n");
    		fflush(stdin);
    		gets(name);
    		strcpy(person->name,name);
    		add(head,person);
    		break;
    	case 2:
    		printf("Enter the name you wish to delete.\n");
    		fflush(stdin);
    		gets(name);
    		remove(head,name);
    		break;
    	case 3:
    		print(head);
    		break;
    	case 4:
    		return 0;
    	default:
    		printf("Invalid selection.\n");
    	}
    	}
    	return 0;
    }
    
    void add(LINK head, LINK data)
    
    {
    	//Puts the new entry in alphabetical order
    	LINK current=head;
    	LINK insert=data;
    	LINK previous=head;
    	while(current->next!=NULL)
    	{
    		if(strcmp(current->name,insert->name)>0)
    		{
    			insert->next=previous->next;
    			previous->next=insert;
    			break;
    		}
    		previous=current;
    		current=current->next;
    	}
    }
    void remove(LINK head, char *name)
    {
    	LINK current=head;
    	LINK previous=current;
    	while(current->next!=NULL)
    	{
    		if(strcmp(current->name,name)==0)
    		{
    			previous->next=current->next;
    			free(current);
    		}
    
    	}
    }
    void print(LINK head)
    {
    	LINK current=head;
    	while(current->next!=NULL)
    	{
    		printf("%s\n",current->name);
    	}
    }

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350

    Re: Alright, thanx!

    Originally posted by Brew
    should I use scanf() rather than gets()?
    Use fgets

    fgets(name, 80, stdin);

    fgets leaves '\n' in there, so you need to strip it out.

    length = strlen(name);
    name[length-1] = '\0';
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Re: Alright, thanx!

    Originally posted by ronin
    fgets leaves '\n' in there, so you need to strip it out.

    length = strlen(name);
    name[length-1] = '\0';
    Slight modification. fgets may not read a '\n'. There is no guarintee that your string will have one. As such, you may be overwriting data.

    A better method would be:
    Code:
    char *ptr;
    ptr = strchr( name, '\n' );
    if( ptr != NULL )
        *ptr = '\0';
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The thing is that the list would be debuggable if you would simply separate the user interface first then run the list on dummy values. Now you can easiy step through the debugger without all that clutter, and without disaster prone user input obscuring the debugging trail.

    First off, you set head to NULL and then send it to a function that try's to access this NULL pointer (access violation!).

    This one is missing something too:
    while(current->next!=NULL)
    {
    if(strcmp(current->name,name)==0){
    previous->next=current->next;
    free(current); }
    }

    You also didn't delete some of your memory.

    Solve those first.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM