Advertisement
Guest User

Sorting a linked list based on variable C

a guest
Mar 22nd, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.93 KB | None | 0 0
  1. void sortedInsert(struct listelement** list_ptr, struct listelement* newNode)
  2. {
  3.     struct listelement* temp;
  4.  
  5.     while (((temp = *list_ptr) &&
  6.            (temp->surveyDetails.ppsNo < newNode->surveyDetails.ppsNo))) {
  7.         list_ptr = &temp->next;
  8.     }
  9.  
  10.     newNode->next = temp;
  11.     *list_ptr = newNode;
  12. }
  13.  
  14. void addElement(struct listelement** list_ptr)
  15. {
  16.     int inputPPS;
  17.     struct listelement *list;
  18.     struct listelement *newNode;
  19.  
  20.     list = *list_ptr;
  21.     newNode = malloc(sizeof *newNode);
  22.  
  23.     if (list == NULL) {
  24.         printf("\nWe will now take details from you for the survery...\n");
  25.     }
  26.  
  27.     // keep asking for a PPS number, until we get a unique one
  28.     while (1) {
  29.         printf("\nPlease enter your PPS number (Number must be unique):");
  30.         scanf("%d", &inputPPS);
  31.         if (checkUnique(list, inputPPS)) {
  32.             break;
  33.         }
  34.         printf("\nWe still value your feedback on this topic! If you believe you have entered your PPS incorrectly you can now try again!");
  35.     }
  36.  
  37.     newNode->surveyDetails.ppsNo = inputPPS;
  38.     printf("\nPlease enter your first name:");
  39.     scanf("%*s", sizeof(newNode->surveyDetails.fName), newNode->surveyDetails.fName);
  40.     printf("\nPlease enter your last name:");
  41.     scanf("%*s", sizeof(newNode->surveyDetails.lName), newNode->surveyDetails.lName);
  42.     //printf("\nEnter email address: ");
  43.     //do email validation
  44.     //scanf("%*s", sizeof(newNode->studentData.email), newNode->studentData.email);
  45.  
  46.     printf("\nEnter current address:");
  47.     scanf("%*s", sizeof(newNode->surveyDetails.address), newNode->surveyDetails.address);
  48.  
  49.     printf("\nPlease enter your age:");
  50.     scanf("%d", &newNode->surveyDetails.age);
  51.     printf("\nPlease enter your yearly salary (as whole number):");
  52.     scanf("%d", &newNode->surveyDetails.income);
  53.     printf("\nHow many cigarrettes do you smoke a day? :");
  54.     scanf("%d", &newNode->surveyDetails.ciggiesSmoked);
  55.     printf("\nHow many units of alcohol do you drink in a day? :");
  56.     scanf("%d", &newNode->surveyDetails.unitsTaken);
  57.     printf("\nHow many time do you exercise every week? :");
  58.     scanf("%d", &newNode->surveyDetails.timesExercised);
  59.  
  60.     sortedInsert(list_ptr, newNode);
  61.     printf("\nSurvey stored successfully\n");
  62. }
  63.  
  64. int checkUnique(struct listelement *list, int ppsNo)
  65. {
  66.     struct listelement *temp;
  67.  
  68.     for (temp = list; temp; temp = temp->next) {
  69.         if (temp->surveyDetails.ppsNo == ppsNo) {
  70.             printf("\n There is a user in the survey system with this PPS Number.");
  71.             return 0;
  72.         }
  73.         else if (temp->surveyDetails.ppsNo > ppsNo) {
  74.             // the list is sorted, so if we find an element with a pps greater than
  75.             // the one we're looking for, then we need look no further
  76.             break;
  77.         }
  78.     }
  79.  
  80.     printf("\nPPS Number is unique. Continuing...\n We will now ask you for your survey details... ");
  81.  
  82.     return 1;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement