Advertisement
chunkyguy

sid

Jan 11th, 2011
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. /*
  2.     sid@whackylabs.com
  3.     student
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9.  
  10. #define N 100
  11. #define M 9
  12. #define W 10
  13.  
  14. typedef struct
  15. {
  16.     char w[W];
  17.     struct node *nxt;
  18. }node;
  19.  
  20. node *create_node(char *w);
  21. int sj_strcmp(char *s1, char *s2);
  22.  
  23. int main(int argc, char **argv)
  24. {
  25.     int n,m;
  26.     char w[W],tmp[W];
  27.     node *head, *p, *q, *r;
  28.    
  29.     if(argc != 2)
  30.     {
  31.         fprintf(stderr,"Usage: %s <file-in>\n",argv[0]);
  32.         return;
  33.     }
  34.     freopen(argv[1],"r",stdin);
  35.     for(scanf(" %d",&n);n && scanf(" %d",&m);n--,printf("\n"))
  36.     {
  37.         head = NULL;
  38.         //create list -- sorted
  39.         while(m-- && scanf(" %s",w))
  40.         {
  41.             //printf("%s\n",w);
  42.             if(!head)
  43.                 head = create_node(w);
  44.             else
  45.             {
  46.                 p = create_node(w);
  47.                 for(q=head;q->nxt && sj_strcmp(p->w,q->w) > 0;r = q,q=q->nxt)
  48.                     ;
  49.                 if(q == head)
  50.                 {
  51.                     if(sj_strcmp(p->w,q->w) > 0)
  52.                         head->nxt = p;
  53.                     else
  54.                     {
  55.                         p->nxt = head;
  56.                         head = p;
  57.                     }
  58.                 }
  59.                 else if((q->nxt == NULL) && (sj_strcmp(p->w,q->w) > 0))
  60.                         q->nxt = p;
  61.                 else
  62.                 {
  63.                     r->nxt = p;
  64.                     p->nxt = q;
  65.                 }
  66.             }
  67.         }
  68.         //print
  69.         for(p = head; p;p = p->nxt)
  70.             printf("%s\n",p->w);
  71.         //free list
  72.         while(p = head)
  73.         {
  74.             head = head->nxt;
  75.             free(p);
  76.         }
  77.     }
  78.     return 0;
  79. }
  80.  
  81. node *create_node(char *w)
  82. {
  83.     //printf("new node: %s\n",w);
  84.     node *n = (node *)malloc(sizeof(node));
  85.     n->nxt = NULL;
  86.     strcpy(n->w,w);
  87.     return n;
  88. }
  89.  
  90. int sj_strcmp(char *s1, char *s2)
  91. {
  92.     int res = 0;
  93.     while((res = *s1++ - *s2++) == 0)
  94.         ;
  95.     return res;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement