链表 (四)

2014-11-24 02:51:53 · 作者: · 浏览: 9
"链表为空,无法打印节点信息!/n");
return LIST_ERROR;
}
while( NULL!=head->next )
{
printf("------------------------------>/n");
printf("%s %-4d%-4d/n", head->next->name, head->next->chinese, head->next->math);
head=head->next;
}
return LIST_ERROR;
}
int invert_list(STU* head)
{
STU* pPre;
STU* pCur;
STU* pNext;
if( NULL==head->next || NULL==head || NULL==head->next->next)
{
printf("无法逆置链表!/n");
return LIST_ERROR;
}
pCur=head->next;
pNext=pCur->next;
pCur->next=NULL;
pPre=pCur;
pCur=pNext;
while( NULL!=pCur )
{
pNext=pCur->next;
pCur->next=pPre;
pPre=pCur;
pCur=pNext;

}
head->next=pPre;
return LIST_SUCCESS;
}
int main(int argc, char *argv[])
{
int Quite=0;
int choice;
char name[20];
STU* pHead=NULL;
STU stu;
STU* pstu=NULL;
while(1)
{
printf("****************************************/n");
printf("1、初始化链表请输入 1/n");
printf("2、插入节点请输入 2/n");
printf("3、删除节点请输入 3/n");
printf("4、查找节点请输入 4/n");
printf("5、删除链表请输入 5/n");
printf("6、逆置链表请输入 6/n");
printf("6、退出请输入 7/n");
printf("****************************************/n");
scanf("%d", &choice);
switch ( choice )
{
case 1 :
init_list(&pHead);
break;
case 2 :
printf("请输入学生的姓名、语文成绩、数学成绩:/n");
scanf("%s%d%d", stu.name, &stu.chinese, &stu.math);
insert_node_sorted(&stu, pHead);
printf("插入节点后链表的内容为:/n");
display_list(pHead);
break;
case 3:
printf("请输入你要删除节点的学生的名字:/n");
scanf("%s", name);
remove_node(name, pHead);
printf("删除节点后,链表的内容为:/n");
display_list(pHead);
break;
case 4:
printf("请输入你要查找节点的学生的名字:/n");
scanf("%s", name);
pstu=travel_list(name, pHead);
if( NULL==pstu )
{
printf("没有查到你所需要的信息!/n");
}
else
{
printf("你所查学生的信息为:/n");
printf("%s %-4d%-4d/n", pstu->name, pstu->chinese, pstu->math);
}
break;
case 5:
delete_list(&pHead);
break;
case 6:
printf("逆置前链表内容为:/n");
display_list(pHead);
if(LIST_SUCCESS==invert_list(pHead))
{
printf("逆置后链表内容为:/n");
display_list(pHead);
}
break;
case 7:
Quite=1;
break;
default :
break;
}
if( 1==Quite )
{
break;
}
}
return 0;
}