设为首页 加入收藏

TOP

c语言 链表基本操作(二)
2012-12-10 12:41:12 来源: 作者: 【 】 浏览:1312
Tags:语言   基本操作

 

    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;

    }

      

首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言编程之结构体强制类型转换 下一篇C语言连接 Access 数据..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: