I'm having a trouble with my school project. I was asked to use a linked list, I'm almost finish but then I got stuck in how do I sort my data by looking at the date
I want to display these data by sorting the date
I have searched everywhere and I still can't find how to sort a linked list multiple parameters say these are the parameters:
struct Data {
int kode;
char variety[20];
int weight;
int date;
struct Data *next;
} *headIn, *tempIn;
with a main
function like this:
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( " \n\nMAIN MENU :\n");
printf( " 1. Input Product\n\n");
printf( " 2. Print List\n\n");
printf( " enter your choice (1 - 2) : ");
scanf("%d", &menu);
if (menu < 1 || menu > 2) {
system("cls");
printf ("your input is not available\n");
getch();
}
switch (menu) {
case 1:
userInput();
break;
case 2:
printList(tempIn);
break;
}
}
while (menu != 3);
system("cls");
printf ("============ Thankyou ============");
return 0;
}
and then I apply this function so that the data is going to be a linked list:
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;
}
}
and then I have this 2 function for asking user's input and the other is for printing
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 = Lobster\n\t\t\t ");
for (int i = 0; i < amount; i ) {
printf("\n\nProduct-%d", pointIn 1);
printf("\n\tInput code\t : "); scanf(" %d", &code);
if (code < 0 || code > 4) {
printf ("this code product is not available\n");
i--;
} else {
printf("\tJenis\t\t : %s\n", variety[code]);
printf("\tINPUT weight\t : "); 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();
}
please help me I don't understand and I can't find any references, it's just this sorting part I'm stuck in.
CodePudding user response:
In the Insert
function, you must find the proper place to insert the node by iterating in the list past the nodes with a lesser date:
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 struct\n");
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;
}
}
CodePudding user response:
I have solved my problem for sorting this program
first i add a new struct
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);
}
and then i initiate an array
struct Data order[100];
int k=0;
and then make the sorting functions
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);
}
}
and then i update my printing process so that it can print the sorted products
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();
}