创建一个链接列表,以存储日期的名称,出生日期,身高,体重和快速排序 [英] Create a linked list to store name, date of birth, height, weight and quicksort on date
本文介绍了创建一个链接列表,以存储日期的名称,出生日期,身高,体重和快速排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经发布了我的问题,请向前看并调试它
我尝试过:
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;
}
推荐答案
一个不错的关于链接列表的教程。
阅读之后,你应该了解我的额外提示:
- 为此人创建结构。它应该包括DOB,名称等。
- 创建一个节点结构。一个人(数据)和上下文。如果这个人是一个指针然后将更容易排序,但你必须自己分配和释放内存。
- 写一些测试数据来调试你的代码
- make一些调试输出,以了解您的代码中发生了什么
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
这篇关于创建一个链接列表,以存储日期的名称,出生日期,身高,体重和快速排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文