创建与正反向输出双向链表

如题。

其他基本操作可参照单链表

代码如下:

 1 #include <iostream>
 2 #include <string>
 3 #include <cmath>
 4 #include <algorithm>
 5 using namespace std;
 6 struct Node
 7 {
 8     Node *pre;
 9     int v;
10     Node *next;
11 };
12 Node* create_lian(int n)
13 {
14     Node *a=new Node,*b;
15     b=a;
16     cin>>a->v;
17     a->next=NULL;
18     a->pre=NULL;
19     for(int i=2;i<=n;i++)
20     {
21         a->next=new Node;
22         a->next->pre=a;
23         a=a->next;
24         cin>>a->v;
25         a->next=NULL;
26     }    
27     cout<<"Node created."<<endl;
28     return b;
29 }
30 //正、反向输出双向链表 
31 void out_lian(Node *p)
32 {
33     Node *q;
34     do
35     {
36         cout<<p->v<<endl;
37         q=p;
38         p=p->next;
39     }
40     while(p!=NULL);
41     p=q;
42     do
43     {
44         cout<<p->v<<endl;
45         p=p->pre;
46     }
47     while(p!=NULL);
48 }
49 main()
50 {
51     int n;
52     Node *a,*b=new Node,*pre,*head;
53     cin>>n;
54     head=create_lian(n);
55     out_lian(head); 
56 }

 

转载于:https://www.cnblogs.com/wanjinliu/p/11414894.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值