Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. typedef struct LinkList
  6. {
  7. int priority;
  8. char filler[100];
  9. struct LinkList*link;
  10. };
  11.  
  12. // Function builds list
  13. void buildList(struct LinkList**start_ref, char filler, int priority);
  14.  
  15. // Function to sort list using buubble sort
  16. void sort_List(struct LinkList*start);
  17.  
  18. // Function to swap string of two nodes a and b
  19. void bubbleSwap(struct LinkList*a, struct LinkList*b);
  20.  
  21. // Function to print list
  22. void displayLinkedList(struct LinkList*start);
  23.  
  24. int main()
  25. {
  26. char arr[100][100] = {"Spagatti", "And", "Meatballs", "Are" , "The", "Greatest"};
  27. int priorityArr[] = {4, 5, 2, 3 , 1, 6};
  28. int list_size, i;
  29.  
  30. struct LinkList*start = NULL;
  31.  
  32. for (i = 0; i< 6; i++)
  33. buildList(&start, arr[i], priorityArr[i]);
  34.  
  35. /* print list */
  36. printf("n Linked list before sorting ");
  37. displayLinkedList(start);
  38.  
  39. /* sort the linked list */
  40. sort_List(start);
  41.  
  42. /* print list after sorting */
  43. printf("n Linked list after sorting ");
  44. displayLinkedList(start);
  45.  
  46.  
  47. return 0;
  48. }
  49.  
  50.  
  51. // Function to buildList
  52. void buildList(struct LinkList**start_ref, char filler, int priority)
  53. {
  54. struct LinkList*ptr1 = (struct iorb*)malloc(sizeof(struct iorb));
  55. strcpy(ptr1->filler, filler);
  56. ptr1->link = *start_ref;
  57. ptr1->priority = priority;
  58. *start_ref = ptr1;
  59. }
  60.  
  61. //Function to print list
  62. void displayLinkedList(struct LinkList*start)
  63. {
  64. struct LinkList*temp = start;
  65. printf("n");
  66. while (temp!=NULL)
  67. {
  68. printf("%s, ", temp->filler);
  69. temp = temp->link;
  70. }
  71. }
  72.  
  73. /* Bubble sort the given linked list */
  74. void sort_List(struct LinkList*start)
  75. {
  76. int isComplete;
  77. int i;
  78. struct LinkList*ptr1;
  79. struct LinkList*lptr = NULL;
  80. do
  81. {
  82. isComplete = 0;
  83. ptr1 = start;
  84.  
  85. while (ptr1->link != lptr)
  86. {
  87. if (ptr1->priority > ptr1->link->priority)
  88. {
  89. bubbleSwap(ptr1, ptr1->link);
  90. isComplete = 1;
  91. }
  92. ptr1 = ptr1->link;
  93. }
  94. lptr = ptr1;
  95. }
  96. while (isComplete == 1);
  97. }
  98.  
  99. // function to swap a and b
  100. void bubbleSwap(struct LinkList*a, struct LinkList*b)
  101. {
  102. char temp;
  103. strcpy(temp, a->filler);
  104. int tempP = a->priority;
  105. a->priority = b->priority;
  106. strcpy(a->filler, b->filler);
  107. strcpy(b->filler, temp);
  108. b->priority = tempP;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement