zoukankan      html  css  js  c++  java
  • Problem D: 逆置链式链表(线性表)

    Problem D: 逆置链式链表(线性表)

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 594  Solved: 346
    [Submit][Status][Web Board]

    Description

    本题只需要提交填写部分的代码
    (线性表)试编写算法将线性表就地逆置,以链式存储结构实现。
    代码:
    #include <stdio.h>
    #include <malloc.h>
    struct Num
    {
        int n;
        struct Num *next;
    }num;
    struct Num *createlist(struct Num *head);
    void print(struct Num *head);
    void destroy(struct Num *head);
    void destroy(struct Num *head)
    {
     struct Num *p;
     while(head!=NULL)
     {
      p=head->next;
      delete(head);
      head=p;
     }
    }

    int main()
    {
        struct Num *head=NULL;
        head=createlist(head);       //建立
        print(head);//输出
     destroy(head);
        return 0;
    }
    struct Num *createlist(struct Num *head)                //头插法建立链表
    {
        struct Num *p;
        p=head=(struct Num*)malloc(sizeof(struct Num));
        head=NULL;                                    
        p=(struct Num*)malloc(sizeof(struct Num));            //p建立新结点
        while(scanf("%d",&p->n)!=EOF)                      //将新结点插到开头的位置
        {
            /***************/
                添加代码
            /*****************/
            p=(struct Num*)malloc(sizeof(struct Num));         //p每次建立新结点
        }
        return head;
    }
    void print(struct Num *head)
    {
        struct Num *current=head;
        while(current!=NULL)
        {
            printf("%d ",current->n);
            current=current->next;
        }
    }

    Input

    1 2 3 4 5 6 7 8 9

    Output

    9 8 7 6 5 4 3 2 1

    Sample Input

    10 23 56 89 11

    Sample Output

    11 89 56 23 10 
            p->next=head;
            head=p;
    

      

     
  • 相关阅读:
    用于重启Greenbrowser的插件[V1.3]
    奥运开幕式没有八卦
    头像和Karma汽车
    ScribeFire试用
    人生三宝
    月光:一个漂亮的flash
    google chromium浏览器绿色版+升级器+启动器
    firefox 缓存问题导致“载入页面时到服务器的连接被重置”
    生命的使命和程序(V2)
    性,野心和梦
  • 原文地址:https://www.cnblogs.com/mjn1/p/8893279.html
Copyright © 2011-2022 走看看