Advertisement
Guest User

Untitled

a guest
May 24th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct node{ // create the user defined data type
  5. int data;
  6. struct node *next;
  7. };
  8.  
  9. /* In this linked list I've defined two types of data such as Interger 'data' and a
  10. pointer variable to store user defined data type memory addresses. The 'next' variable
  11. holds address of the next node of the linked list */
  12.  
  13.  
  14. struct node *head = NULL; // here I create the 'head' to catch the begining of the linked list
  15.  
  16.  
  17. /* This function print the all data elements in the linked list */
  18. void display(){
  19. struct node *p;
  20. p = head;
  21. printf("Values in the linked list: ");
  22. while(p != NULL){ // begin from the 'head' and iterate until the end
  23. printf("%d ", p->data);
  24. p = p->next;
  25. }
  26. printf("\n");
  27. }
  28.  
  29. /* The below function get the user input value and attach the newly created node
  30. to the linked list */
  31. void append(){
  32.  
  33. struct node* temp;
  34. temp = (struct node*)malloc(sizeof(struct node)); // create a memory location and converts its address
  35. printf("Enter the data: ");
  36. scanf("%d", &temp->data);
  37. temp -> next = NULL; // The newly created node is attached to the end, therefor last node 'next' must be NULL
  38.  
  39. if(head == NULL){ //means linked list is empty
  40. head = temp;
  41. }else{
  42. struct node* p;
  43. p = head;
  44. while( p->next != NULL){ //find the end of the list, logic: last node has no connection to another
  45. p = p->next;
  46. }
  47. p->next = temp;
  48. }
  49. }
  50.  
  51. void appendAtBegin(){
  52. struct node* temp;
  53. temp = (struct node*)malloc(sizeof(struct node));
  54. printf("Enter the data: ");
  55. scanf("%d", &temp->data);
  56. temp->next = NULL;
  57. if(head == NULL){
  58. head = temp;
  59. }else{
  60. temp->next = head;
  61. head = temp;
  62. }
  63.  
  64. }
  65.  
  66. int length(){
  67. int count;
  68. struct node* temp;
  69. temp = head;
  70. while(temp != NULL){
  71. count++;
  72. temp = temp->next;
  73. }
  74. return count;
  75. }
  76.  
  77. void delete(){
  78. int location;
  79. printf("Enter the location to delete: ");
  80. scanf("%d", &location);
  81. struct node* temp;
  82. if(location > length()){
  83. printf("Invalid location entered\n");
  84. }else if(location == 1){
  85. temp = head;
  86. head = temp->next;
  87. temp->next = NULL;
  88. free(temp);
  89. }else{
  90. struct node* p = head;
  91. int i=1;
  92. while(i < location-1){
  93. p = p->next;
  94. i++;
  95. }
  96. struct node* q = p->next;
  97. p->next = q->next;
  98. q->next = NULL;
  99. free(q);
  100. }
  101. }
  102.  
  103. void sortLinkedList(){
  104. struct node *pp;
  105. pp = head;
  106. while(pp != NULL){
  107. struct node* q;
  108. q = pp;
  109. while(q->next != NULL){
  110. printf("%d \n", pp->data);
  111. if(pp->data > q->next->data){
  112. int temp = q->next->data;
  113. q->next->data = pp->data;
  114. pp->data = temp;
  115. }
  116. q = q->next;
  117. }
  118. pp = pp->next;
  119. }
  120. }
  121.  
  122. int main(){
  123.  
  124. char choice;
  125. do{
  126. printf("Enter the choice: a: append, b: appent at begining, d: display, r: delete, s: sort, e: exit: ");
  127. scanf("%s", &choice);
  128. switch(choice){
  129. case 'a':
  130. append();
  131. break;
  132. case 'b':
  133. appendAtBegin();
  134. break;
  135. case 'd':
  136. display();
  137. break;
  138. case 'r':
  139. delete();
  140. break;
  141. case 's':
  142. sortLinkedList();
  143. break;
  144. case 'e':
  145. printf("\nThank You!\n");
  146. exit(0);
  147. break;
  148. default:
  149. printf("Invalide choice! please try again!\n");
  150. break;
  151. }
  152.  
  153. }while(choice != 'e');
  154.  
  155.  
  156. return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement