SHARE
TWEET

Untitled

a guest Jul 16th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node{
  5.     int data;
  6.     struct node* next;
  7. }node,*LIST;
  8.  
  9. void display(LIST L){
  10.     LIST temp;
  11.     for(temp=L;temp!=NULL;temp=temp->next){
  12.         printf("%d\n",temp->data);
  13.     }
  14. }
  15.  
  16. void initLL(LIST *L){
  17.    
  18.     *L = NULL;
  19. }
  20.  
  21. void insertSorted(LIST *L, int elem)
  22. {  
  23.     /*Step 1: Declare temp and trav.*/
  24.     LIST temp,*trav;
  25.    
  26.     /*Step 2: Assign trav to head, and traverse the list until you find the right place for
  27.               insertion.*/
  28.     for(trav=L; *trav!=NULL && (*trav)->data <= elem ; trav=&(*trav)->next){}
  29.    
  30.     /*Step 3: Allocate memory for a node using temp. After which, check if the malloc succeeded.*/
  31.     temp= (LIST)malloc(sizeof(node));
  32.     if(temp!=NULL){
  33.        
  34.         /*Step 4: Assign the data of temp to be the data which is to be inserted, then insert the
  35.                   new node.*/
  36.         temp->data=elem;
  37.         temp->next=*trav;
  38.         *trav=temp;
  39.        
  40.         printf("Insertion for %d is successful! \n", elem);
  41.     }
  42. }
  43.  
  44. void insertFirst(LIST *L, int elem){
  45.     LIST temp;
  46.     temp= (LIST)malloc(sizeof(node));
  47.     if(temp!=NULL){
  48.         temp->data=elem;
  49.         temp->next=*L;
  50.         *L=temp;
  51.         printf("Insertion for %d is successful! \n", elem);
  52.     }
  53. }
  54.  
  55. void insertLastUnique(LIST *L, int elem)
  56. {
  57.     LIST temp, *trav;
  58.     for(trav=L; *trav!=NULL && (*trav)->data != elem; trav= &(*trav)->next){}
  59.         if(*trav==NULL){
  60.             temp=(LIST)malloc(sizeof(node));
  61.             if(temp!=NULL){
  62.                 temp->data=elem;
  63.                 temp->next=*trav;
  64.                 *trav=temp;
  65.             }
  66.             printf("Insertion for %d is successful! \n", elem);
  67.         }else{
  68.             printf("Failed! %d is found \n", elem);
  69.         }
  70.        
  71. }
  72.  
  73. void insertLast(LIST *L, int elem)
  74. {
  75.     /*Step 1: Declare temp and trav.*/
  76.     LIST temp, *trav;
  77.    
  78.     /*Step 2: Traverse the list until you've reached the end of the list.*/
  79.     for(trav = L; *trav != NULL; trav = &(*trav)->next){}
  80.    
  81.     /*Step 3: Allocate space. After which, check if the malloc succeeded.*/
  82.     temp = (LIST) malloc (sizeof(node));
  83.     if(temp != NULL){
  84.        
  85.         /*Step 4: Insert the data into the newly allocated memory*/
  86.         temp->data = elem;
  87.        
  88.         /*Step 5: Update the next.*/
  89.         temp->next = *trav;
  90.        
  91.         /*Step 6: Update the trav.*/
  92.         *trav = temp;
  93.     }
  94.     printf("Insertion for %d is successful! \n", elem);
  95. }
  96.  
  97. int member(LIST L, int elem){
  98.     LIST trav;
  99.     for(trav=L; trav!=NULL && elem!=trav->data; trav=trav->next){}
  100.         return (trav!=NULL)? 1:0;
  101. }
  102.  
  103. void deleteMem(LIST *L, int elem)
  104. {
  105.     /*Step 1: Declare temp and trav.*/
  106.     LIST temp, *trav;
  107.     printf("Delete member 6...\n");
  108.    
  109.     /*Step 2: Assign trav to head, and traverse the list until you find the node to delete
  110.               or you've reached the end of the list.*/
  111.     for(trav=L; *trav!=NULL && (*trav)->data!=elem; trav= &(*trav)->next){}
  112.    
  113.     /*Step 3: Check if you've reached the end of the list. If you haven't, then *trav is the node
  114.               to be deleted.*/
  115.         if(*trav!=NULL){
  116.            
  117.             /*Step 4: Delete the node*/
  118.             temp=*trav;
  119.             *trav=temp->next;
  120.             free(temp);
  121.             printf("Successfully deleted %d\n",elem);  
  122.         }else{
  123.         printf("\nItem does not belong in the list");
  124.     }  
  125. }
  126.  
  127. void deleteMultiples(LIST *L, int elem){
  128.     LIST temp, *trav;
  129.     printf("Delete multiples of 2...\n");
  130.     for(trav=L; *trav!=NULL;){
  131.         if((*trav)->data % elem == 0){
  132.             temp=*trav;
  133.             *trav=temp->next;      
  134.             free(temp);
  135.         }else{
  136.             trav= &(*trav)->next;
  137.         }
  138.     }
  139.        
  140. }
  141.  
  142. void selectionSort(LIST head)
  143. {
  144.     int temp;
  145.     LIST trav, store;
  146.     for(;head!=NULL;head=head->next){
  147.         store=head;
  148.         for(trav=head;trav!=NULL;trav=trav->next){
  149.             if(trav->data < store->data){
  150.                 store=trav;
  151.             }
  152.         }
  153.         temp=store->data;
  154.         store->data=head->data;
  155.         head->data=temp;
  156.     }
  157.     printf("Selection sort:\n");
  158. }
  159.  
  160. LIST copyAllElements1 (LIST A)
  161. {
  162.     LIST B = NULL;
  163.     LIST temp, trav, *trav2;
  164.     for(trav=A; trav!=NULL; trav=trav->next){
  165.         for(trav2=&B; *trav2!=NULL; trav2=&(*trav2)->next){}
  166.         temp= (LIST)malloc(sizeof(node));
  167.         if(temp!=NULL){
  168.             temp->data=trav->data;
  169.             temp->next=*trav2;
  170.             *trav2=temp;
  171.         }
  172.     }
  173.     printf("Returning Linked list in ascending order...\n");
  174.     return B;
  175. }
  176.  
  177. LIST copyAllElements2 (LIST A)
  178. {
  179.     LIST B = NULL;
  180.     LIST temp, trav;
  181.     for(trav=A; trav!=NULL; trav=trav->next){
  182.         temp= (LIST)malloc(sizeof(node));
  183.         if(temp!=NULL){
  184.             temp->data=trav->data;
  185.             temp->next=B;
  186.             B=temp;
  187.         }
  188.     }
  189.     printf("Returning Linked list in descending order...\n");
  190.     return B;
  191. }
  192.  
  193. //int getSum(LIST L){ -->wla ni sir haha
  194. //  LIST temp;
  195. //  int sum=0;
  196. //  for(temp=L;temp!=NULL;temp=temp->link){
  197. //      sum+=temp->data;
  198. //  }
  199. //      printf("Sum: %d\n",sum);
  200. //}
  201.  
  202. void initLL(LIST*);
  203. void insertSorted(LIST*, int);
  204. void insertFirst(LIST*, int);
  205. void insertLastUnique(LIST*, int);
  206. void insertLast(LIST*, int);
  207. int member(LIST, int);
  208. void deleteMem(LIST*, int);
  209. void deleteMults(LIST*, int);
  210. void selectionSort(LIST);
  211. //int getSum (LIST);
  212. void display(LIST);
  213. LIST copyAllElements1(LIST);
  214. LIST copyAllElements2(LIST);
  215.  
  216.  
  217. int main() {
  218.     LIST newLL;
  219.     int x;
  220.    
  221.     initLL(&newLL);
  222.    
  223.     insertSorted(&newLL,9);
  224.     insertSorted(&newLL,6);
  225.     insertSorted(&newLL,5);
  226.     insertSorted(&newLL,8);
  227.     insertSorted(&newLL,4);
  228.    
  229.     selectionSort(newLL);
  230.    
  231.     display(newLL);
  232.    
  233.     insertFirst(&newLL,1);
  234.    
  235.     display(newLL);
  236.    
  237.     deleteMem(&newLL,6);
  238.    
  239.     display(newLL);
  240.    
  241.     deleteMultiples(&newLL,2);
  242.    
  243.     display(newLL);
  244.    
  245.     insertLastUnique(&newLL, 55);
  246.    
  247.     display(newLL);
  248.    
  249.     insertLast(&newLL, 100);
  250.    
  251.     display(newLL);
  252.    
  253.     LIST retVal= copyAllElements1(newLL);
  254.    
  255.     display(retVal);
  256.    
  257.     LIST retVal2= copyAllElements2(newLL);
  258.    
  259.     display(retVal2);
  260.    
  261.    
  262.    
  263. //  int sum;
  264. //  for(x=0;x<3;x++){
  265. //      insertFirst(&newAL,x+2);
  266. //  }
  267. //  sum=getSum(newAL);
  268.  
  269.        
  270.     return 0;
  271. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top