逆置链式链表(线性表)

2020年1月17日 1174点热度 0人点赞 0条评论

逆置链式链表(线性表)

时间: 1ms        内存:128M

描述:

本题只需要提交填写部分的代码
(线性表)试编写算法将线性表就地逆置,以链式存储结构实现。
代码:
#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;
    }
}

输入:

1 2 3 4 5 6 7 8 9

输出:

9 8 7 6 5 4 3 2 1

示例输入:

10 23 56 89 11

示例输出:

11 89 56 23 10 

提示:

参考答案:

解锁文章

没有看到答案?微信扫描二维码可免费解锁文章

微信扫描二维码解锁

使用微信扫描二维码打开广告页面后可以立即关闭,再刷新此页面即可正常浏览此文章

所跳转广告均由第三方提供,并不代表本站观点!

已经扫描此二维码?点此立即跳转

code

这个人很懒,什么都没留下

文章评论