我的学校项目遇到了麻烦。我被要求使用链接列表,我几乎完成了,但后来我陷入了如何通过查看日期来排序数据的困境
我想通过排序日期来显示这些数据
我到处找了,仍然找不到如何对多个参数的链表进行排序假设这些是参数:
struct Data {
int kode;
char variety[20];
int weight;
int date;
struct Data *next;
} *headIn, *tempIn;
具有main
功能,如
int menu;
int pointIn = 0;
int amount = 0;
void insert(int kode, char *variety, int weight, int date);
void userInput();
void printList(struct Data *tempIn);
int main() {
headIn = NULL;
int menu = 0;
do {
system ("cls");
printf( " nnMAIN MENU :n");
printf( " 1. Input Productnn");
printf( " 2. Print Listnn");
printf( " enter your choice (1 - 2) : ");
scanf("%d", &menu);
if (menu < 1 || menu > 2) {
system("cls");
printf ("your input is not availablen");
getch();
}
switch (menu) {
case 1:
userInput();
break;
case 2:
printList(tempIn);
break;
}
}
while (menu != 3);
system("cls");
printf ("============ Thankyou ============");
return 0;
}
然后我应用这个函数,使数据成为一个链表:
void insert(int kode, char *variety, int weight, int date) {
struct Data *dataIn = (struct Data *)malloc(sizeof(struct Data));
dataIn->kode = kode;
strcpy(dataIn->variety, variety);
dataIn->weight = weight;
dataIn->date = date;
dataIn->next = NULL;
if (headIn == NULL) {
headIn = dataIn;
} else {
dataIn->next = headIn;
headIn = dataIn;
}
}
然后我有两个功能,一个是询问用户的输入,另一个是打印
void userInput() {
int code;
int weight;
int date;
amount = 0;
char variety[5][20] = { "Fish", "Crab", "Squid", "Clam", "Lobster" };
system ("cls");
printf("Number of data you want to enter : "); scanf("%d", &amount);
printf( "_________________________________________________________________n");
printf("n Kode 0 = Fish n kode 1 = Crab n kode 2 = Squid n kode 3 = Clam n kode 4 = Lobsternttt ");
for (int i = 0; i < amount; i++) {
printf("nnProduct-%d", pointIn + 1);
printf("ntInput codet : "); scanf(" %d", &code);
if (code < 0 || code > 4) {
printf ("this code product is not availablen");
i--;
} else {
printf("tJenistt : %sn", variety[code]);
printf("tINPUT weightt : "); scanf(" %d", &weight );
printf("tInput date (YYYYMMDD)t : ");scanf(" %d", &date);
pointIn++;
insert(code, variety[code], weight, date);
}
}
getch();
}
void printList(struct Data *tempIn) {
system ("cls");
tempIn = headIn;
//sort function here
printf("***** DATA PRODUCT ***** n" );
printf("| DATE | CODE | NAME | WEIGHT | n");
while (tempIn != NULL) {
printf(" %d %d %s %d n", tempIn->date, tempIn->kode, tempIn->variety, tempIn->weight);
tempIn = tempIn->next;
}
getch();
}
请帮帮我,我不明白,也找不到任何参考资料,只是我被困在这个分类部分。
在Insert
函数中,您必须通过在列表中迭代日期较短的节点来找到插入节点的正确位置:
void insert(int kode, const char *variety, int weight, int date) {
struct Data *dataIn = (struct Data *)malloc(sizeof(struct Data));
if (dataIn == NULL) {
fprintf(stderr, "cannot allocate memory for Data structn");
return;
}
dataIn->kode = kode;
strcpy(dataIn->variety, variety);
dataIn->weight = weight;
dataIn->date = date;
if (headIn == NULL || headIn->date > date) {
// insert node at the head
dataIn->next = headIn;
headIn = dataIn;
} else {
// insert node in the list
struct Data *np = head;
while (np->next && np->next->date >= date) {
np = np->next;
}
dataIn->next = np->next;
np->next = dataIn;
}
}
我已经解决了排序这个程序的问题
首先我添加一个新的结构
struct node{
int kode;
char variety[20];
int weight;
int date;
struct node* left;
struct node* right;
};
struct node* newNode(int data){
struct node* node = (struct node*) malloc(sizeof(struct node));
node->date = data;
node->left = NULL;
node->right = NULL;
return(node);
}
然后我启动阵列
struct Data order[100];
int k=0;
然后使分拣功能
struct node* insertSort(struct node* node, int data)
{
if (node == NULL)
return(newNode(data));
else
{
if (data <= node->date)
node->left = insertSort(node->left, data);
else
node->right = insertSort(node->right, data);
return node;
}
}
void sort(struct node* node) {
struct node* current = node;
if (node != NULL) {
sort (current->left);
order[k++].date =node->date ;
sort (current->right);
}
}
然后我更新我的打印流程,这样它就可以打印分类后的产品
void printList(struct Data *tempIn) {
system ("cls");
tempIn = headIn;
int angka =0;
struct node* root = NULL;
while(tempIn!=NULL){
if(root==NULL){
root = insertSort(root, tempIn->date);
}
else {
insertSort(root, tempIn->date);
}
tempIn = tempIn->next;
}
tempIn = headIn;
printf("***** DATA PRODUCT ***** n" );
printf("| DATE | CODE | NAME | WEIGHT | n");
k=0;
for (int i=0;i<100;i++){
order[i].date=0;
order[i].kode =0;
order[i].weight=0;
}
sort(root);
tempIn = headIn;
for(int i=0;i<100;i++){
tempIn = headIn;
while (tempIn != NULL) {
if(order[i].date==tempIn->date){
printf(" %d %d %-10s %d n", tempIn->date, tempIn->kode, tempIn->variety, tempIn->weight);
}
tempIn = tempIn->next;
}
}
getch();
}