c语言 链表基本操作(二)

2012-12-10 12:41:12 · 作者: · 浏览: 1333

 

    temp=p1=head->next;

    p2=p3=p1->next;

    while(p3!=NULL)

    {

    p3=p3->next;

    p2->next=p1;

    p1=p2;

    p2=p3;

    }

    temp->next=NULL;

    head->next=p1;

    }

    return head;

    }

    struct student *destroy(struct student *head)            //销毁一个链表

    {

    struct student *p1,*p2;

    if(head->next==NULL)

    {

    printf(“you have not creat a list!!!!!!!!!!!!!!!\n”);

    }

    else

    {

    p1=p2=head->next;

    while(p1!=NULL)

    {

    p1=p1->next;

    free(p2);

    p2=p1;

    }

    }

    head->next=NULL;

    return head;

    }

    int main()

    {

    int choose;

    struct student *head;

    head=(struct student *)malloc(len);

    head->next=NULL;

    while(1)

    {

    printf(“1:Creat a new list\n”);

    printf(“2:Print the list\n”);

    printf(“3:Add a note to the list\n”);

    printf(“4:Delete a note from the list\n”);

    printf(“5:Invert the list \n”);

    printf(“6:Destroy the list\n”);

    printf(“7:Exit\n”);

    printf(“\nPlease choose one choice:”);

    scanf(“%d”,&choose);

    switch(choose)

    {

    case 1:

    printf(“\n---------------creat a new list:-------------\n”);

    head=creat(head);

    printf(“\n”);

    break;

    case 2:

    printf(“\n---------------print list:-------------------\n”);

    head=print_list(head);

    printf(“\n”);

    break;

    case 3:

    printf(“\n---------------add a new note:-----------------\n”);

    head=add_list(head);

    printf(“\n”);

    break;

    case 4:

    printf(“\n---------------delete a note:------------------\n”);

    head=delete_list(head);

    printf(“\n”);

    break;

    case 5:

    printf(“\n---------------invert the list-----------------\n”);

    head=invert(head);

    printf(“\n”);

    break;

    case 6:

    printf(“\n--------------destory the list-----------------\n”);

    head=destroy(head);

    printf(“\n”);

    break;

    case 7:

    exit(0);

    default:

    printf(“Enter error\n”);

    printf(“\n”);

    }

    }

    return 0;

    }