首页 > 其他分享 >最简单的链表实现

最简单的链表实现

时间:2022-10-05 10:13:32浏览次数:45  
标签:Node head struct 实现 next 链表 int 简单 NULL

输入数字并输出的链#include<iostream>#include <stdlib.h>

using namespace std;

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

int main()
{
int num;
cin>>num;
Node*head,*p,*q,*t;
head=NULL;
for(int i=0;i<num;i++)
{
p=(struct Node*)malloc(sizeof(struct Node));
cin>>p->data;
       p->next=NULL;
       if(head==NULL)
      {
      head=p;
}
       q->next=p;
       q=q->next;
}
for(t=head;t!=NULL;t=t->next)
{
cout<<t->data;
}
}
p->data相当于*p.data
要设置头指针
牢记指向下一个结点
q->next=p;
q=q->next;
不可以用p=p->next;

标签:Node,head,struct,实现,next,链表,int,简单,NULL
From: https://www.cnblogs.com/weinan030416/p/16755118.html

相关文章