YTU 2620: B 链表操作

2620: B 链表操作


Description

(1)编写一个函数createlink,用来建立一个动态链表(链表中的节点个数由参数count来控制)。

节点结构如下:

struct Node
{
int data;
Node * next; 
};

函数createlink的声明如下:

Node * createlink(int count);


(2)编写一个函数printlink,用来遍历输出一个链表。

函数printlink的声明如下:

void printlink(Node * head);

(3) 在主程序中调用函数createlink来动态创建链表,然后调用printlink函数遍历输出链表节点数据。

主程序如下:(提交时不用提交主程序)

int main()
{
Node * head=NULL; 
int n;
cin>>n;
head=createlink(n);
printlink(head);
return 0;
}

Input

输入要建立链表的节点个数
输入各个节点的数据

Output

输出链表中各个节点的数据

Sample Input

4
1 2 3 4

Sample Output

1 2 3 4

HINT

提交时不用提交主程序,其他都要提交

#include<iostream>
#include<cstdio>
using namespace std;

struct Node
{
int data;
Node * next;
};

int main()
{
    Node * createlink(int count);
    void printlink(Node * head);
    Node * head=NULL;
    int n;
    cin>>n;
    head=createlink(n);
    printlink(head);
    return 0;
}

Node * createlink(int count)
{
    Node *p,*s,*r;
    int n,m,i;
    Node *L = new Node;
    scanf("%d",&m);
    L->data=m;
    p=L;
    r=L;
    for(i=0;i<count-1;i++)
    {
        cin>>n;
        Node *p = new Node;
        p->data=n;
        r->next=p;
        r=p;
    }
    r->next=NULL;
    return L;
}

void printlink(Node * head)
{
     while(head!=NULL)
     {
         printf("%d ",head->data);
         head=head->next;
     }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Loganer

感谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值