2116数据结构实验之链表一:顺序建立链表

来源:互联网 发布:php超时时间设置 编辑:程序博客网 时间:2024/04/25 10:28

2116数据结构实验之链表一:顺序建立链表

#include<iostream>#include<malloc.h>using namespace std;struct node{    int data;    struct node *next;};struct node *creat(int n){    struct node *head,*tail,*p;    int i;    head=(struct node *)malloc(sizeof(struct node));    head->next=NULL;    tail=head;    for(i=1;i<=n;i++)    {        p=(struct node *)malloc(sizeof (struct node ));        cin>>p->data;        p->next=NULL;        tail->next=p;        tail= p;           }//顺逆序的根本区别    return head;};void display(struct node *head){    struct node *q;    q=head->next;    while(q!=NULL)    {        cout << q->data;        if(q->next!=NULL)        cout<< " ";        q=q->next;    }    cout<<endl;}int main(){    int n,h;    struct node *head;    cin >>n;    head=creat(n);    display(head);    return 0;}
0 0