Like Tree1Likes
  • 1 Post By iMalc

Problem with linked list

This is a discussion on Problem with linked list within the C Programming forums, part of the General Programming Boards category; Hey all, I'm having a trouble with this code, which supposed to get names of students and their grades, determine ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    4

    Problem with linked list

    Hey all,

    I'm having a trouble with this code, which supposed to get names of students and their grades, determine fail students and passed, sort the names in lexicographical order and then prints two lists of failed and passed students... if you have any clue about fixing it I'll be more than glad! thank you.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define MAX_NAME_SIZE 30
    
    
    typedef struct student_node
    {
    	char name[MAX_NAME_SIZE];
    	double grade;
    	struct Student* next;
    } Student;
    
    
    void free_mem(Student *head)
    {
    	if (head == NULL)
    		return;
    
    
    	free_mem(head->next);
    	free(head);
    }
    
    
    Student *read_students()
    {
    	char name[MAX_NAME_SIZE];
    	double grade;
    	
    	Student *head = NULL, *nstd = NULL;
    
    
    	printf("Enter name:\n");
    	scanf("%s",&name);
    	
    	while (strcmp(name, "exit")!= 0)
    	{
    		printf("Enter grade:\n");
    		scanf("%lf", &grade);
    		nstd = (Student*) malloc (sizeof(Student));
    		
    		if (nstd == NULL)
    		{
    			printf("Fatal error: memory allocation failed!\n");            
    				return 0;
    		}
    
    
    		strcpy(nstd->name, name);
    		nstd->grade = grade;
    		nstd->next = head;
    		head = nstd;
    
    
    		printf("Enter Name:\n");
    		scanf("%s",name);
    	}
    		return head;
    }
    
    
    void print_list(Student *pHead)
    {
    	Student* list;
    	
    	for (list = pHead; list != NULL; list = list->next)
    		printf("\n%s %g\n", list->name, list->grade);
    	
    	return;
    }
    
    
    void split_list(Student *pHead, Student** pass, Student** fail)
    {
    	Student* list= pHead;
    	Student *passed_student = NULL;
    	Student *failed_student = NULL;
    	Student* buffer=pHead;
    
    
    	while(list!=NULL)
    	{
    		buffer=list->next;
    
    
    		if (list->grade >= 60)
    		{
    			list->next = *pass;
    			*pass = list;
    		}
    
    
    		if (list->grade < 60)
    		{
    			list->next = *fail;
    			*fail = list;
    		}
    
    
    		list = buffer;
    	}
    }
    
    
    Student *sort_list(Student **pHead)
    {
    	Student* buffer=NULL, *list=*pHead, *last=NULL, *tmp=*pHead, *first=*pHead;
    	if(first==NULL)
    		return;
    	while(first->next!=last)
    	{
    		for(list=first;list->next!=last;list=list->next)
    		{
    			if((strcmp(list->name, list->next->name)>0) && (list == first))
    			{
    					buffer=list->next;
    					first->next=buffer->next;
    					buffer->next=list;
    					first=buffer;
    					list=buffer;		
    			}
    			
    			else
    				{
    					buffer=list->next;
    					tmp->next=buffer;
    					list->next=buffer->next;
    					buffer->next=list;
    					list=buffer;
    				}
    
    
    			tmp=list;
    		}
    
    
    		last=tmp->next;
    	}
    	if(strcmp(first->name, first->next->name)>0)
    			{
    				buffer=list->next;
    				first->next=buffer->next;
    				buffer->next=list;
    				first=buffer;
    				list=buffer;
    			}
    	*pHead=first;
    	return 0;
    }
    
    
    
    
    
    
    int main()
    {
    	Student *head = read_students();
    	Student *pass_list=NULL, *fail_list=NULL;
    	
    	split_list(head, &pass_list, &fail_list);
    
    
    	sort_list(&pass_list);
    	sort_list(&fail_list);
    
    
    	printf("The Student that passed the test are\n");
    	print_list(pass_list);
    
    
    	printf("The Student that failed the test are\n");
    	print_list(fail_list);
    
    
    	free_mem(head);
    	free_mem(pass_list);
    	free_mem(fail_list);
    
    
    	return 0;
    }
    the problem is in the sort_list function....

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,278
    Please be specific. What exactly is the problem? How do you know it is in the sort_list function? It could be anywhere, given the number of warnings your code generates. You have some garbage characters at the end of line 47 that caused a few problems. Removing those, here's what I get when I compile your code (if you don't see something similar, turn up the warnings on your compiler):
    Code:
    $ gcc -Wall -g    llist.c   -o llist
    llist.c: In function ‘free_mem’:
    llist.c:23: warning: passing argument 1 of ‘free_mem’ from incompatible pointer type
    llist.c:17: note: expected ‘struct Student *’ but argument is of type ‘struct Student *’
    llist.c: In function ‘read_students’:
    llist.c:37: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[30]’
    llist.c:54: warning: assignment from incompatible pointer type
    llist.c: In function ‘print_list’:
    llist.c:69: warning: assignment from incompatible pointer type
    llist.c: In function ‘split_list’:
    llist.c:86: warning: assignment from incompatible pointer type
    llist.c:91: warning: assignment from incompatible pointer type
    llist.c:98: warning: assignment from incompatible pointer type
    llist.c:80: warning: unused variable ‘failed_student’
    llist.c:79: warning: unused variable ‘passed_student’
    llist.c: In function ‘sort_list’:
    llist.c:112: warning: ‘return’ with no value, in function returning non-void
    llist.c:113: warning: comparison of distinct pointer types lacks a cast
    llist.c:115: warning: comparison of distinct pointer types lacks a cast
    llist.c:115: warning: assignment from incompatible pointer type
    llist.c:117: error: dereferencing pointer to incomplete type
    llist.c:119: warning: assignment from incompatible pointer type
    llist.c:121: warning: assignment from incompatible pointer type
    llist.c:128: warning: assignment from incompatible pointer type
    llist.c:129: warning: assignment from incompatible pointer type
    llist.c:131: warning: assignment from incompatible pointer type
    llist.c:140: warning: assignment from incompatible pointer type
    llist.c:142: error: dereferencing pointer to incomplete type
    llist.c:144: warning: assignment from incompatible pointer type
    llist.c:146: warning: assignment from incompatible pointer type
    Quite a few errors there. Fix all of them:
    • Line 23,17: Your struct declaration at the top is a little funky. Put the typedef on a line by itself above the struct and use the typedef'ed type in your struct for the next pointer. See below.
    • Line 37: Don't use & with char arrays, the name of the array is a pointer to the first element.
    • Line 112: Just a return by itself returns garbage unless the function returns void. At the bottom of sort_list you return 0 (NULL since the function returns a pointer), and you never use the return value when you call it, so make it return void.
    • Most/all of the rest of the errors seem to be related to the funky typedef.

    Code:
    typedef struct student_node      Student;
    struct student_node {
        char name[MAX_NAME_SIZE];
        double grade;
        Student *next;
    };

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    4
    first of all thanks for helping.

    Ok, this code basically have few stages. The first stage gets names and grades. Second stage splits the input to two lists, first list is the students who failed and second list is the students who passed. Then, the code supposed to sort the names of the students in lexicographical order and print the students who failed and students who passed. The code does work until the "bubble-sort". So the obvious guess is that the sort_list is the one making the problem crash.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Zeevi88 View Post
    first of all thanks for helping.

    Ok, this code basically have few stages.
    I've got an idea. How about you do one stage at a time. Write the first part. Make sure it all works exactly the way it is supposed to, and then move on to the next part.


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

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    4
    That's exactly what I did. The programs crashes after I add the last part which is the sorting.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Don't try and Bubble Sort a linked list. Bubble Sort is suited well to an array where it is easy to implement, but it is surprisingly very very tricky to get right for a linked-list.

    The shortest and easiest to write linked-list sorting algorithm is Insertion Sort. It's also very good efficiency-wise for small lists. I strongly suggest you put what you have aside and try writing that instead.
    anduril462 likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    4
    Do you have any idea how to write the Student *sort_list(Student **pHead) function using the same "function name" in the insertion sort you mentioned?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with linked list
    By nickman in forum C Programming
    Replies: 12
    Last Post: 06-09-2010, 10:06 AM
  2. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  3. linked list problem
    By cathzee in forum C Programming
    Replies: 1
    Last Post: 11-07-2003, 07:11 AM
  4. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 08:14 AM
  5. Linked List problem
    By unregistered in forum C Programming
    Replies: 65
    Last Post: 04-16-2002, 10:04 PM

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