zoukankan      html  css  js  c++  java
  • 单链表的基础操作练习

    对单链表的创建,查找,删除,添加。

    #include<stdio.h >
    #include<stdlib.h>
    struct link
    {
        int date;
        struct link *next;
    };
    struct link *chuangJian(void);
    struct link *chaZhao(struct link *head,int num); 
    struct link *shanChu(struct link *head,int num);
    struct link *tianJia(struct link *head,int num );
    int main()
    {
        struct link *head=NULL,*p=NULL;
        head=chuangJian();
        p=head;
        while(p!=NULL)  /*不要用head指针,因为循环完之后head将指向链表的尾部,下面再用head指针将越界*/ 
        {
            printf("%d
    ",p->date );
            p=p->next ;
        }
        int n=0;
        printf("请输入你所查找的第N个数据:"); 
        scanf("%d",&n);
        p=chaZhao(head,n);
        if(p==NULL)
            printf("结点不存在。
    ");
        else
            printf("%d
    ",p->date); 
        printf("请输入你要删除的第N个数据:"); 
        scanf("%d",&n);
        shanChu(head,n);
        p=head;
        while(p!=NULL)  
        {
            printf("%d
    ",p->date );
            p=p->next ;
        }
        printf("请输入你要添加数据的位置:"); 
        scanf("%d",&n);
        p=tianJia (head,n);
        if(p==NULL)
            printf("超出范围。
    ");
        else
            while(p!=NULL)  
            {
                printf("%d
    ",p->date );
                p=p->next ;
            }
        return 0;
    }
    struct link *chuangJian(void)
    {
        struct link *head=NULL,*tail,*new;
        int count=0;
        while(1)
        {
            printf("请输入第%d个数据:",count+1);
            new=(struct link *)malloc(sizeof(struct link));
            scanf("%d",&new->date );
            new->next =NULL;
            if(new->date==-1)
            {
                free(new);
                return head;
             } 
             else
             {
                 count++;
                 if(count==1)
                 {
                 head=tail=new;
                 }
                 else
                 {
                     tail->next =new;
                     tail=new;
                 }
             }
        }
    }
    struct link *chaZhao(struct link *head,int num)
    {
        int i=1;
        struct link *p=head;
        if(p==NULL)
        {
            return  NULL;
        }
        while(p!=NULL&&i<num) 
        {
            p=p->next ;
            i++;
        }
        return p;
    }
    struct link *shanChu(struct link *head,int num)
    {
        struct link *p=NULL,*q=NULL;
        int i=1;
        p=head;
        if(p==NULL)
            return NULL;
        else
        {
            if(num==0)
            {
                p=head;
                head=head->next;
                free(p);
                return head;
            }
            while(p!=NULL&&i<num-1) /*让p指向所要删除的结点的前一个结点*/ 
            {
                p=p->next ;
                i++;
            }
            if(p==NULL)
            {
                printf("结点不存在。
    ");
            }
            else
            {
                q=p->next ;
                p->next =q->next ;
                free(q);
                return head;
            }
        }
     } 
     struct link *tianJia(struct link *head,int num )
    {
        struct link *p=NULL,*new=NULL,*q=NULL;
        int i=1;
        if(num<1)
        {
            printf("输出位置不合法。
    ");
            return NULL;
        }
        new=(struct link *)malloc(sizeof(struct link));
        printf("请输入新建结点的数据:");
        scanf("%d",&new->date );
        new->next =NULL;
        if(head==NULL)    /*将新节点插入空链表*/ 
        {
            head=new;
            return head;
        }
        if(num==1)
        {
            p=head;         /*或者new->next=head;head=new;*/ 
            head=new;
            head->next =p;
            return head;
        }
        else
        {
            i=1;
            p=head;
            for(;i<num-1&&p!=NULL;p=p->next,i++ );
            if(p==NULL)
            {
                return NULL;
            }
            q=p->next ;   /*或new->next=p->next;p->next=new; */
            p->next =new;
            new->next =q;
            return head;
        }
    }

    总结:理解花费了不少时间,比较抽象,搞不懂了就照着课本多码几遍,画结构图想一想。

  • 相关阅读:
    ubuntu在图形界面下打开一个终端
    [置顶] 屠夫与大夫
    service bound(一)
    Android Interface Definition Language (AIDL)
    service bound(二)
    移动应用开发原则
    Service bound(三)
    Linux 安装SSH服务
    JDK中设计模式
    Bad Hair Day【单调栈】
  • 原文地址:https://www.cnblogs.com/TX980502/p/6605823.html
Copyright © 2011-2022 走看看