Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- sid@whackylabs.com
- student
- */
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define N 100
- #define M 9
- #define W 10
- typedef struct
- {
- char w[W];
- struct node *nxt;
- }node;
- node *create_node(char *w);
- int sj_strcmp(char *s1, char *s2);
- int main(int argc, char **argv)
- {
- int n,m;
- char w[W],tmp[W];
- node *head, *p, *q, *r;
- if(argc != 2)
- {
- fprintf(stderr,"Usage: %s <file-in>\n",argv[0]);
- return;
- }
- freopen(argv[1],"r",stdin);
- for(scanf(" %d",&n);n && scanf(" %d",&m);n--,printf("\n"))
- {
- head = NULL;
- //create list -- sorted
- while(m-- && scanf(" %s",w))
- {
- //printf("%s\n",w);
- if(!head)
- head = create_node(w);
- else
- {
- p = create_node(w);
- for(q=head;q->nxt && sj_strcmp(p->w,q->w) > 0;r = q,q=q->nxt)
- ;
- if(q == head)
- {
- if(sj_strcmp(p->w,q->w) > 0)
- head->nxt = p;
- else
- {
- p->nxt = head;
- head = p;
- }
- }
- else if((q->nxt == NULL) && (sj_strcmp(p->w,q->w) > 0))
- q->nxt = p;
- else
- {
- r->nxt = p;
- p->nxt = q;
- }
- }
- }
- //print
- for(p = head; p;p = p->nxt)
- printf("%s\n",p->w);
- //free list
- while(p = head)
- {
- head = head->nxt;
- free(p);
- }
- }
- return 0;
- }
- node *create_node(char *w)
- {
- //printf("new node: %s\n",w);
- node *n = (node *)malloc(sizeof(node));
- n->nxt = NULL;
- strcpy(n->w,w);
- return n;
- }
- int sj_strcmp(char *s1, char *s2)
- {
- int res = 0;
- while((res = *s1++ - *s2++) == 0)
- ;
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement