码迷,mamicode.com
首页 > 编程语言 > 详细

单链表的简单c++实现

时间:2015-06-24 22:40:09      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

以下代码只实现了单链表的手动创建以及输出功能

#include<iostream>
using namespace std;
struct node
{
  int data;
  node *next;
};
class list
{
public:
    void creat();
    void show();

private:
    node *head;

};
void list::creat()   //创建链表
{
    node *f=new node();  //建立链表的第一个元素
    f->data=44;
    f->next=NULL;
    head=f;

    f=new node();       //建立链表的第二个元素
    f->data=72;
    f->next=NULL;
    head->next=f;

    f=new node();        //建立链表的第三个元素
    f->data=220;
    f->next=NULL;

    head->next->next=f;
}
void list::show()  //输出链表
{
  node *p=head;
  while(p->next)
  {
    cout<<p->data<<"->";
    p=p->next;
  }
  cout<<p->data<<endl;

}
int main()
{
 list L1;
 L1.creat();
 L1.show();
 system("pause");
 return 0;

}

单链表的简单c++实现

标签:

原文地址:http://blog.csdn.net/adminabcd/article/details/46625855

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!