设为首页 加入收藏

TOP

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

    链表是数据结构中最基础的知识,也是应用中最常用的动态分配内存的方法.

    下面我以具体的例子来说明对它的操作.

    #include<stdio.h>

    #include<stdlib.h>

    #include<string.h>

    #define len sizeof(struct student)

    struct student

    {

    char name ;

    int age;

    struct student *next;

    };

    struct student *creat(struct student *head)                //创建一个链表

    {

    int length=0,yes=1;

    struct student *p1,*p2;

    p1=p2=(struct student *)malloc(len);

    printf(“please enter one student's name and age:\n”);

    scanf(“%s%d”,p1->name,&p1->age);

    while(yes)

    {

    length++;

    if(length==1)

    head->next=p1;

    else

    p2->next=p1;

    p2=p1;

    printf(“continue creat list (0--no):”);

    scanf(“%d”,&yes);

    if(!yes)

    break;

    else

    {

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

    printf(“please enter one student's name and age:\n”);

    scanf(“%s%d”,p1->name,&p1->age);

    }

    }

    p2->next=NULL;

    return head;

    }

    struct student *print_list(struct student *head)      //打印一个链表

    {

    struct student *p1;

    if(head->next==NULL)

    {

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

    }

    else

    {

    p1=head->next;

    while(p1!=NULL)

    {

    printf(“%s %d\n”,p1->name,p1->age);

    p1=p1->next;

    }

    }

    return head;

    }

    struct student *add_list(struct student *head)            // 增加一个节点

    {

    int x,n=0;

    char name ={0};

    struct student *new,*p1,*p2;

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

    printf(“please enter one student's name and age:\n”);

    scanf(“%s%d”,new->name,&new->age);

    p1=head->next;

    while(p1!=NULL)

    {

    n++;

    p1=p1->next;

    }

    printf(“you have %d place to add a new note,where do you want (1 to %d) :”,n+1,n+1);

    scanf(“%d”,&x);

    if(x<=0||x>(n+1))

    {

    printf(“enter error\n”);

    return head;

    }

    p2=head;

    while(--x && (p2=p2->next));

    new->next=p2->next;

    p2->next=new;

    return head;

    }

    struct student *delete_list(struct student *head)       //删除一个节点

    {

    char name[20];

    struct student *p1,*p2;

    int flag;

    if(head->next==NULL)

    {

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

    }

    else

    {

    printf(“Who you want to delete from the list:\n”);

    scanf(“%s”,name);

    p1=head->next;

    p2=p1->next;

    if(!strcmp(p1->name,name))

    {

    head->next=p1->next;

    }

    else

    {

    while(p2!=NULL)

    {

    if(!strcmp(p2->name,name))

    {

    p1->next=p2->next;

    flag=1;

    break;

    }

    else

    {

    p1=p1->next;

    p2=p2->next;

    }

    }

    if(!flag)

    {

    printf(“The list dosen't contain the name\n”);

    }

    }

    }

    return head;

    }

    struct student *invert(struct student *head)      //将链表反转

    {

    struct student *p1,*p2,*p3,*temp;

    if(head->next==NULL)

    {

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

    }

    else

    {

   

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

评论

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