1 /* 双向循环链表c++版 */
2 /* Gxjun coder */
3 #include<iostream>
4 #define type int //<由于内容不定所以就这样定义吧>
5 using namespace std;
6
7 typedef struct Node
8 {
9 type inf; //<表示的事节点的内容>
10 struct Node* pre ; //<上一节点>
11 struct Node* next ; //<下一节点>
12 }node ;
13
14 /*创建,插入,删除,打印*/
15
16 void creat( node*& head )
17 {
18 node *ps; /* <作为辅助指针>*/
19 node* temp= new node ;
20 cout<<" <输入数字,输入0结束>"<<endl ;
21 while(cin>>temp->inf,temp->inf!=0)
22 {
23 if(head==NULL)
24 {
25 ps=head=temp;
26 }
27 else
28 {
29 ps->next=temp;
30 temp->pre=ps;
31 ps=temp;
32 }
33 temp->next=head;
34 head->pre=temp;
35 temp = new node;
36 }
37 }
38
39
40
41 void insert( type inf, int pos , node* &head )
42 {
43 node* temp=head,*tem;
44 /*<表示head所在位置为原坐标轴>*/
45 if(pos>0) /* <表示向下> */
46 {
47 while(pos-->1)
48 temp=temp->next;
49 }
50 else
51 {
52 /* <表示向上> */
53 while(pos++<0)
54 temp=temp->pre;
55 }
56 tem= new node;
57 tem->inf=inf;
58 tem->next=temp->next;
59 temp->next->pre=tem;
60 temp->next=tem;
61 tem->pre=temp;
62 }
63
64 void delet( type inf ,node*& head )
65 {
66 node* temp=head;
67 int n=0;
68 while(n++==0||temp!=head)
69 {
70 if( inf==temp->inf )
71 {
72 temp->pre->next=temp->next;
73 temp->next->pre=temp->pre;
74 }
75 temp=temp->next;
76 }
77 }
78
79 void print(node*& head)
80 {
81 node *temp=head;
82 while( temp->next!=head )
83 {
84 cout<<temp->inf<<endl;
85 temp=temp->next;
86 }
87 cout<<temp->inf<<endl;
88 }
89
90 int main()
91 {
92 node *head=NULL;
93 type data;
94 int pos;
95 creat(head);
96 cout<<" <输入你要插入的位置(+n 向下,-n向上),以0结束>"<<endl;
97 while(cin>>pos,pos!=0)
98 {
99 cout<<"<输入数字inf>"<<endl;
100 cin>>data;
101 insert(data,pos,head);
102 }
103 cout<<"<输入你要删除的数>"<<endl;
104 cin>>data;
105 delet(data,head);
106 print(head);
107 return 0;
108 }