help with lists plz

This is a discussion on help with lists plz within the C Programming forums, part of the General Programming Boards category; hey im new with this all, im doing assigmnet for class, what it does it gets a list of students ...

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

    help with lists plz

    hey im new with this all, im doing assigmnet for class, what it does it gets a list of students and their grade, splits them to fail and pass, and at the end it should each group be ordered by lexicographic order, plz help me out, i assume or the swap or the find_min or the sort r wrong, because the rest of the prog i've checked and it worked, i havent posted all the code but assumes the func that are there are working

    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_node* next;
    }Student;
    
    
    
    
    Student *read_students(); /*gets list of students from user*/
    void print_list(Student *pHead); /*prints list of students from user*/
    void split_list(Student *pHead, Student** pass, Student** fail); /*splits students list to fail&pass*/
    void split_cpy_list(Student *pHead, Student** pass, Student** fail); /*splits students list to fail&pass*/
    Student *sort_list(Student *pHead); /*sots students list lexicographi*/
    void swap(Student *swp1, Student *swp2); /*swaps between structs*/
    Student *find_min(Student *pHead); /*finds the struct where name is with smallest lexicographi*/
    
    
    
    
    int main()
    {
    	Student *pass1=NULL, *fail1=NULL;
    	Student *list = read_students();
    	printf("\n");
    	print_list(list);
    	split_list(list, &pass1, &fail1);
    	printf("\n students above 60: \n"); 
    	sort_list(pass1);
    	print_list(pass1);
    	printf("\n students bellow 60: \n");
    	sort_list(fail1);
    	print_list(fail1);
    	free(list);
    
    
    	return 0;
    }
    
    
    }
    void swap(Student *swp1, Student *swp2)
    {
    	Student *swp_tmp = NULL;
    	strcpy(swp_tmp->name,swp1->name); 
    	swp_tmp->grade = swp1->grade;
    
    
    	strcpy(swp1->name,swp2->name); 
    	swp1->grade = swp2->grade;
    
    
    	strcpy(swp2->name,swp_tmp->name); 
    	swp2->grade = swp_tmp->grade;
    }
    Student *find_min(Student *pHead)
    {
        Student *tmp_find = NULL;
        tmp_find = pHead;
        while(pHead != NULL)
        {
          if(strcmp(pHead->name,tmp_find->name)<0)
          *tmp_find = *pHead;
          pHead = pHead->next;
        }
       return tmp_find;
    }
    Student *sort_list(Student *pHead)
    {
    	Student *tmp_sort = NULL;
    	tmp_sort = pHead;
    	while(tmp_sort != NULL)
    	{
        if(strcmp(tmp_sort->name, find_min(tmp_sort->next)->name)>0)           
    	swap(tmp_sort,find_min(tmp_sort->next));
    	tmp_sort=tmp_sort->next;
    	}
    	return tmp_sort;
    }
    Last edited by Salem; 02-04-2012 at 01:40 PM. Reason: Fixed code by removing lots of useless font/colour

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > Student *swp_tmp = NULL;
    > strcpy(swp_tmp->name,swp1->name);
    > swp_tmp->grade = swp1->grade;
    Well until you dereference NULL anyway.

    Try say
    Code:
        Student swp_tmp;
        strcpy(swp_tmp.name,swp1->name);
        swp_tmp.grade = swp1->grade;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Additionally, you may want to take a look at How to define main()-FAQ
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  2. lists
    By cprog12 in forum C Programming
    Replies: 2
    Last Post: 12-19-2010, 02:48 PM
  3. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  4. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 12:08 PM
  5. XOR lists?
    By bgrahambo in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2002, 10:02 PM

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