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

如题。

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

代码如下:

 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