Advertisement
mbah_bejo

Stupid link list

Mar 5th, 2020
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6.  
  7. typedef struct node
  8. {
  9.     char c;
  10.     struct node *next;
  11.    
  12. }*NodePtr;
  13.  
  14.  
  15.  
  16. void printlist(NodePtr list)
  17. {
  18.     NodePtr temp = list;
  19.     do{
  20.         printf("%c", temp->c);
  21.         temp=temp->next;
  22.     }while(temp!=NULL);
  23.     printf("\n");
  24. }
  25.  
  26.  
  27. NodePtr newNode(char str)
  28. {
  29.     NodePtr np = (NodePtr) malloc(sizeof (NodePtr));
  30.     np -> c = str;
  31.     np -> next = NULL;
  32.     return np;
  33. }
  34.  
  35. void replace(NodePtr list, char c1, char c2)
  36. {
  37.     while(list){
  38.         if(list->c == c1 )
  39.         list->c = c2;
  40.        
  41.         list=list->next;
  42.     }
  43. }
  44.  
  45. NodePtr removenode(NodePtr list, char c3)
  46. {
  47.     NodePtr temp =list, prev = NULL;
  48.     while(temp!=NULL){
  49.         if(temp->c == c3 )
  50.     {
  51.         if(list == temp ){
  52.             list=temp->next;
  53.             free(temp);
  54.             temp=list;
  55.         }else
  56.         {
  57.             prev->next= temp->next;
  58.             free(temp);
  59.             temp= prev->next;
  60.         }
  61.     }
  62.         else
  63.         {
  64.             prev = temp;
  65.             temp= temp->next;
  66.         }
  67.     }
  68.     //printlist(list);
  69.     return list;
  70. }
  71. void toUpper(NodePtr list)
  72. {
  73.     while(list)
  74.     {
  75.         list->c = toupper(list->c);
  76.         list = list->next;
  77.     }
  78. }
  79. void toLower(NodePtr list)
  80. {
  81.     while(list)
  82.     {
  83.         list->c = tolower(list->c);
  84.         list = list->next;
  85.     }
  86. }
  87.  
  88. NodePtr alfaSort(NodePtr list)
  89. {
  90.     NodePtr bfrFirst=NULL, First=NULL, prev = NULL, temp =list;
  91.     char FirstChar = '~'; //karena memiliki nilai terbesar di ASCII Table
  92.    
  93.     if(list->next==NULL)
  94.     return list;
  95.    
  96.     while(temp)
  97.     {
  98.         if(temp->c < FirstChar){
  99.             FirstChar = temp->c;
  100.             First = temp;
  101.             if(prev !=NULL)
  102.                 bfrFirst= prev;
  103.         }
  104.         prev= temp;
  105.         temp= temp->next;
  106.     }
  107.    
  108.     if(bfrFirst!=NULL)
  109.         bfrFirst->next= First->next;
  110.     if(First!=list)
  111.         First->next = list;
  112.    
  113.     list=First;
  114.     list->next = alfaSort(list->next);
  115.    
  116.     return list;
  117. }
  118.  
  119. int compare(NodePtr list, NodePtr cmp)
  120. {
  121.     NodePtr temp = list;
  122.     NodePtr Banding = cmp;
  123.     int tanda = 0;
  124.     while(temp){
  125.     //  printf("0");
  126.         if(temp->c == Banding->c)
  127.         {  
  128.             tanda=1;
  129.             if(Banding->next== NULL) return 1;
  130.             else {
  131.             temp=temp->next;
  132.             Banding=Banding->next;
  133.         //      printf("1");
  134.             }
  135.         //  printf("2");
  136.         } else temp = temp->next;
  137.     }
  138.     if (tanda == 0) return 0;
  139. }
  140. int main(){
  141.     char str[100],c1,c2;
  142.     int n=0;
  143.     NodePtr head =NULL, tail, temp, sorted;
  144.     printf("masukkan Kalimat: ");
  145.     gets(str);
  146.  
  147.     while(str[n]!='\0')
  148.     {
  149.         temp= newNode(str[n]);
  150.         if(head==NULL)  head=temp;
  151.         else
  152.         tail->next = temp;
  153.         tail = temp;
  154.         n++;
  155.     }
  156.     //printlist(head);
  157.     // -------------------fungsi E -----------------------
  158.     printf("Kalimat yang akan dibandingkan: ");
  159.     gets(str);
  160.     NodePtr cmp=NULL, ctemp,ctail;
  161.     n=0;
  162.     while(str[n]!='\0')
  163.     {
  164.         ctemp= newNode(str[n]);
  165.         if(cmp==NULL) cmp=ctemp;
  166.         else
  167.         ctail->next = ctemp;
  168.         ctail = ctemp;
  169.         n++;
  170.     }
  171.     if(compare(head, cmp)==1) printf("Sama\n");
  172.     else printf("Beda\n");
  173.     // -------------------fungsi A-----------------------
  174.     printf("huruf yang ingin di ganti dan penggantinya: ");
  175.     scanf("%c %c",&c1, &c2);
  176.     replace(head,c1,c2);
  177.    
  178.     printlist(head);
  179.     getchar();
  180.     // -------------------fungsi B -----------------------
  181.     printf("huruf yang ingin dihapus: ");
  182.     scanf("%c",&c1);
  183.     head = remove(head,c1);
  184.     printlist(head);
  185.     // -------------------fungsi C -----------------------
  186.     printf("Huruf Kapitalis: ");
  187.     toUpper(head);
  188.     printlist(head);
  189.     // -------------------fungsi D -----------------------
  190.     printf("Huruf yang Terurut: ");
  191.     sorted = head;
  192.     toLower(sorted);
  193.     alfaSort(sorted);
  194.     //getchar();
  195.     printlist(sorted);
  196.    
  197.     //printlist(cmp);
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement