// Dinam1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
struct rec{
       char fam[22];
       rec *p;};
void main()
   {rec *n,*first,*q,*p,a;
    int flag;
    first=new rec;
	q= new rec;
    first->p=q;
	q->p=NULL;
	puts("Input string or empty string");
    gets(q->fam);
    gets(a.fam);
    while(a.fam[0]!='\0'){
       n=new rec;
       strcpy(n->fam,a.fam);
       flag=0;
       p=first;
       q=first;
       while((q=q->p)!=NULL&&!flag)
	  {if(strlen(n->fam)<strlen(q->fam)||
	      strlen(n->fam)==strlen(q->fam)&&
	      strcmp(n->fam,q->fam)<0)
		      {n->p=p->p;
		       p->p=n;
		       flag=1;}
	   else {p=q;}
	  }
       if(!flag)
	  {n->p=NULL;
	   p->p=n;}
      gets(a.fam);
       }
       q=first->p;
    while(q!=NULL){
       puts(q->fam);
       q=q->p;}
 }
