Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Topics

Recent Status Updates

View All Updates

Photo
- - - - -

Problem with linked list

linked list

  • Please log in to reply
3 replies to this topic

#1 Zeevi888

Zeevi888

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 03 February 2012 - 04:23 AM

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.

#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;
}






  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 03 February 2012 - 11:14 AM

What is it doing now?
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 Zeevi888

Zeevi888

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 03 February 2012 - 11:48 AM

well, it crashes. the problems is with the sort_list function, but I don't seem to find what's the problem with it...
  • 0

#4 CurlyBonesHopkins

CurlyBonesHopkins

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 03 February 2012 - 06:03 PM

I ran it and I realized that the strcmp function does not run because the pointer does not reference to a string(char array). However as far as I can see your pointer references to a char array. You might want to try casting but....I am more than 50% sure that will not work. I will look through the program in its entirety again and see what I can find
  • 0





Also tagged with one or more of these keywords: linked list

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download