本帖最后由 思忆 于 2013-3-29 18:01 编辑 #include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
//////////////////////////////////////////////////////////////////////////
#define STD struct std
#define MC (STD *)malloc(sizeof(STD))
//////////////////////////////////////////////////////////////////////////
struct std
{
int num;
double score;
struct std *next;
};
void *malloc();
STD *input();
STD *Delete(STD *temp);
//////////////////////////////////////////////////////////////////////////
void main()
{
int i=0;
char order[20];
STD *temp;
do
{
printf("命令介绍\ninput 输入 | delete 删除 | exit 退出 \n请输入命令:");
scanf("%s",&order);
if (strcmp(order,"input") == 0)
{
temp = input();
i++;
}
else if (strcmp(order,"delete") == 0)
{
if (i != 0)
{
temp = Delete(temp);
}
else
{
printf("链表不存在!\n");
}
}
else if (strcmp(order,"exit") == 0)
{
break;
}
else
{
printf("输入错误!请重新输入!\n");
}
} while ( strcmp(order,"exit") != 0);
}
//////////////////////////////////////////////////////////////////////////
STD *input()
{
STD *head,*p1,*p2,*temp;
head= temp = p1 = MC;
printf("请输入学号:");
scanf("%d",&p1->num);
if ( p1->num != 0 && p1->num > 0 && p1->num < 9999)
{
while (p1->num != 0 && p1->num > 0 && p1->num < 9999)
{
printf("请输入成绩:");
scanf("%lf",&p1->score);
p2 = p1;
p1 = MC;
p2->next = p1;
printf("请输入学号:");
scanf("%d",&p1->num);
}
p2->next = NULL;
if (p2->num != 0 && p2->num > 0 && p2->num < 9999) //输入0,即为退出
{
do
{
printf("%d\t%5.1f\n",head->num,head->score);
head = head->next;
} while (head != NULL);
}
}
return temp;
}
STD *Delete(STD *temp)
{
int num,i=0;
STD *head,*p;
head = temp;
do
{
printf("%d\t%5.1f\n",head->num,head->score);
head = head->next;
} while (head != NULL);
printf("请输入删除对象:");
scanf("%d",&num);
p = head = temp;
if (num != 0 && num > 0 && num < 9999 )
{
while(head != NULL)
{
if (num == head->num)
{
if ( head == temp)
{
head = head->next;
free(p);
p = head;
break;
}
else
{
p->next = head->next;
free(head);
p = head = temp;
break;
}
}
else
{
if (i != 0)
{
p = p->next;
}
printf("没有输入的数字....\n");
head = head->next;
i++;
}
}
do
{
printf("%d\t%5.1f\n",head->num,head->score);
head = head->next;
} while (head != NULL);
}
return p;
}
|