Eukleides project

from http://d.hatena.ne.jp/u5_h/

線形探索リスト(動作可、関数の参考程度に)

番兵を用いた整列リストに昇順のデータを入力する。
/*list (LINIER SEARCH) */
#include

struct cell{
int data;
struct cell *next;
};

struct cell *header;
struct cell *sentinel;

/*線形探索関数*/
void lin_serch(int x)
{
sentinel->data=x;/*targetのセット*/
struct cell *p=header->next;
struct cell *previous=header;
struct cell *ptr;

ptr=(struct cell *)malloc(sizeof(struct cell));
ptr->data=x;
while(x>p->data)
{
previous=p;
p=p->next;
}
if(xdata)
{
ptr->next=p;
previous->next=ptr;
}
else if(x==p->data)
{
if(p==sentinel)
{
ptr->next=p;
previous->next=ptr;
}
else

printf("すでに入力されています\n");
}
}

/*操作評価関数*/
int my_str_comp(char str1,char str2)
{
int j;
for(j=0;str1[j] == str2[j];j++)
{
if(str1[j] == '\0')
{
return 0;
}
}
return str1[j] - str2[j];
}

int main(void)
{
/*初期設定*/
header=(struct cell *)malloc(sizeof(struct cell));
sentinel=(struct cell *)malloc(sizeof(struct cell));
header->next=sentinel;

int a;
char b[3];
struct cell *q;
printf("挿入するデータがありますか? (yes/no) --->");
scanf("%s",b);
while(my_str_comp(b,"yes")==0)
{
printf("データを入力して下さい. --->");
scanf("%d",&a);
lin_serch(a);
for(q=header->next;q!=sentinel;q=q->next)
printf("->%d",q->data);
printf("\n");
printf("挿入するデータがありますか? (yes/no) --->");
scanf("%s",b);
}
if(my_str_comp(b,"no")==0)
printf("挿入操作を終了します.\n");

return 0;
}
授業通り作れば簡単簡単!