首页 > 代码库 > 建立单链表的方法
建立单链表的方法
#include<iostream> using namespace std; struct node{ int d; struct node *next; };//定义结点 node *build1()//头插法构造单链表 { node *p;//指向新建结点 node *head;//头指针 head=NULL; p=head; int x; cin>>x; while(x!=-1) { p=new node; p->d=x; p->next=head; head=p; cin>>x; } return head; } node *build2()//尾插法构造单链表 { node *head;//头指针 node *p,*s;//p指向当前结点,s指向尾结点 head=NULL; p=head; s=head; int x; cin>>x; while(x!=-1) { p=new node; p->d=x; if(!head)head=p; else s->next=p; s=p; cin>>x; } if(s)s->next=NULL; return head; } int main() { node *p; p=build2(); while(p!=NULL) { cout<<(p->d); p=p->next; } return 0; }
本文出自 “7883538” 博客,请务必保留此出处http://7893538.blog.51cto.com/7883538/1435486
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。