首页 > 其他分享 >只用一个头结点一个头指针的链表写法

只用一个头结点一个头指针的链表写法

时间:2022-10-18 21:57:06浏览次数:45  
标签:Node 结点 struct int 一个头 head next 链表

#include<iostream>
#include<stdlib.h>
using namespace std;

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

int main()
{
    int num,n;
    cin>>num;
    Node *head,*p;
    head=NULL;
    head = p = (Node*)malloc(sizeof(struct Node));
    for(int i=0;i<num;i++)
    {
        p->next=(Node*)malloc(sizeof(struct Node));
        cin>>n;
        p->data=n;
        p=p->next;
    }
    p->next=NULL;
    for(p=head;p->next!=NULL;p=p->next)
    {
        cout<<p->data;
    }
}

 

标签:Node,结点,struct,int,一个头,head,next,链表
From: https://www.cnblogs.com/weinan030416/p/16804331.html

相关文章