c++创建单链表

来源:互联网 发布:澄海淘宝美工培训 编辑:程序博客网 时间:2024/06/11 16:18

#include "stdio.h"

#include "iostream"

#include "malloc.h"

#include "stdlib.h"

#include "math.h"

using namespace std;

  typedef int ElemType;

typedef struct LNode

{

   ElemType data;

   struct LNode *next;

}LinkList;

int main()

{

   int i,d,m;

    LinkListL,p,s;

   cout << "输入你想要输入多少个数:";

   cin>>m;

   cout << "建立单链表"<<endl;

   s=L;

   for(i=1;i<=m;i++)

    {

       p=(LinkList)malloc(sizeof(LNode));

       cin >> d;

       p->data=d;

       s->next=p;

       s=p;

    }

   p->next=NULL;

   cout <<"输出:"<<endl;

   p=L->next;

   for(i=1;i<=m;i++)

    {

       cout << p->data<<" "<<endl;

       p=p->next;

    }

   return 0;

}

原创粉丝点击