Click here to Skip to main content
13,665,277 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
i have posted my question please look ahead and debug it

What I have tried:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct {int date; int month; int year;}DOB;
struct data{int num;char name[64]; DOB birth;int height; float weight;struct data *previous;struct data *next;};
void scan(struct data *a)
{
    if(a!=0)
    {
        scanf("%s %2d %2d %04d %4d %f",a->name,&((a->birth).date),&((a->birth).month),&((a->birth).year),&(a->height),&(a->weight));
        scan(a->next);
    }
    else
        return ;
}
void print(struct data *a)
{
     if(a!=0)
    {
        printf("%s %02d%02d%04d %04d %.2f \n",a->name,((a->birth).date),((a->birth).month),((a->birth).year),(a->height),(a->weight));
        print(a->next);
    }
    else
        return ;
}
void swap(struct data *a,struct data *b)
{
   struct data temp;
   strcpy(temp.name,a->name);
   ((temp.birth).date)=((a->birth).date);
   ((temp.birth).month)=((a->birth).month);
   ((temp.birth).year)=((a->birth).year);
   (temp.height)=(a->height);
   (temp.weight)=(a->weight);
   strcpy(a->name,b->name);
   ((a->birth).date)=((b->birth).date);
   ((a->birth).month)=((b->birth).month);
   ((a->birth).year)=((b->birth).year);
   (a->height)=(b->height);
   (a->weight)=(b->weight);
   strcpy(b->name,temp.name);
   ((b->birth).date)=((temp.birth).date);
   ((b->birth).month)=((temp.birth).month);
   ((b->birth).year)=((temp.birth).year);
   (b->height)=(temp.height);
   (b->weight)=(temp.weight);
}
int compare(struct data *x,struct data *y)
{
    if ((x->birth).year > ((y->next)->birth).year)
    {
        return 1;

    }
    else if((x->birth).year == ((y->next)->birth).year)
        {
            if ((x->birth).month > ((y->next)->birth).month)
            {
                 return 1;

            }
            else if((x->birth).month == ((y->next)->birth).month)
            {
                if ((x->birth).date > ((y->next)->birth).date)
                {
                    return 1;

                }
                else if((x->birth).date == ((y->next)->birth).date)
                {
                    return 0;
                }
                else
                    return -1;
            }
            else if((x->birth).month < ((y->next)->birth).month)
            {
                return -1;
            }
        }
        else if((x->birth).year < ((y->next)->birth).year)
        {
            return -1;
        }
}
struct data* partition(struct data *p,struct data *q)
{
    int i;
    struct data *x,*y=p,*temp=p;
    x=q;
    int a;

    for(i=y->num;i<x->num;i++)
    {
        a=compare(temp,x);
        if (a==-1||a==1)
        {
            swap(temp,y);
            temp=temp->next;
        }
    }
    swap(y,q);
    return y;
}
int quicksort(struct data *a,struct data *b)
{
    if(a->num >= b->num)
    {
        return 0;
    }
    struct data *pivot;
    pivot=partition(a,b);
    quicksort(a,pivot->previous);
    quicksort(pivot->next,b);
}
int main ()
{
    int n,i,j;
    struct data *head, *tail, *empty;
    printf("enter no. of entry you want\n");
    scanf("%d",&n);
    if(n==0)
        return 0;
    head= (struct data*)malloc(sizeof(struct data));
    empty=head;
    head->previous=0;
    head->num=1;
    for(i=0;i<n-1;i++)
    {

       tail=(struct data*)malloc(sizeof(struct data));
       tail->previous=empty;
       empty->next=tail;
       empty=tail;
       tail->num=i+2;

    }
    tail->next=0;
    scan(head);
    //print(head);
    quicksort(head,tail);
    print(head);
    return 0;

}
Posted 12-Nov-17 9:56am
Updated 12-Nov-17 23:12pm
v2
Comments
Dave Kreskowiak 12-Nov-17 17:26pm
   
No, you've posted your homework and are now begging someone else to fix it for you.

That's not how this site works. You're not going to get someone to do it for you. You wouldn't learn a damn thing if we did that.

Describe the problem and you'll get pointed in the right direction.
ppolymorphe 13-Nov-17 5:27am
   
You should state your problem.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

A nice tutorial on linked lists.

After reading it, you should understand my additional tips:

- create a struct for the person. It should include DOB, name and so on.
- create also a node struct. A person (the data) and prev and next. If the person is a pointer then will be sorting easier, but you must than alloc and free the memory yourself.
- write some test data for debug your code
- make some debugging output to understand what is going on in your code
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05-2016 | 2.8.180810.1 | Last Updated 13 Nov 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100