A - 数据结构实验之链表一:顺序建立链表(尾插法)

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int data;
    node *next;
};
int main()
{
    struct node*head;
    head=new node();
    head->next=NULL;
    node *p, *q;
    int n;
    cin>>n;
    q=head;
    for(int i=0;i<n;i++)
    {
        p=new node();
        cin>>p->data;
        q->next = p;
        p->next=NULL;
        q=p;
    }
    q=head->next;
    while(q!=NULL)
    {
        if(q->next!=NULL)
            cout<<q->data<<" ";
        else
            cout<<q->data;
        q=q->next;
    }
    return 0;
}
发布了65 篇原创文章 · 获赞 2 · 访问量 839

猜你喜欢

转载自blog.csdn.net/weixin_43797452/article/details/104826613
今日推荐