双向链表的相关操作(创建、顺序遍历、逆序遍历、逆序查找、删除、插入、非递减排序)

  1 #include<iostream>
  2 using namespace std;
  3 
  4 struct node     //建立拥有next和prior指针的结构体
  5 {
  6     int data;
  7     struct node *prior, *next;
  8 };
  9 
 10 node* Init_List(int n, node *list)  //建立双向链表
 11 {
 12     node *p, *q, *head;
 13     p = new node; q = new node;
 14     head = NULL; 
 15     for (int i = 0; i <= n; i++)
 16     {
 17         if (i == 0)
 18             head = p;
 19         else
 20         {
 21             if (i == 2)
 22                 p->prior = NULL;
 23             p->next = q;
 24             q->prior = p;
 25             p = q;
 26             cin >> p->data;
 27             q = new node;
 28         }
 29     }
 30     p->next =NULL;
 31     
 32     return head;
 33 }
 34 
 35 void travle_List(node* list)          //顺序和逆序遍历双向链表
 36 {
 37     node *p = list; node *q=NULL;
 38     p = p->next;
 39     cout << "链表的顺序遍历结果:" << endl;
 40     while (p != NULL)
 41     {
 42         q = p;
 43         cout << p->data<<" ";
 44         p = p->next;
 45     }
 46     
 47     cout << endl;
 48     cout << "链表的逆置遍历结果:" << endl;
 49     while (q != NULL)
 50     {
 51         cout << q->data << " ";
 52         q = q->prior;
 53     }
 54 }
 55 
 56 int List_search(int n,node *list)             //逆向查找双向链表
 57 {
 58     node *p = list; node *q=NULL;
 59     while (p != NULL)
 60     {
 61         q = p;
 62         p = p->next;
 63     }
 64     for (int i = 0; i < n-1; i++)
 65         q = q->prior;
 66     return q->data;
 67 }
 68 
 69 void List_insert(int n, node *list,int e)        //双向链表的插入操作
 70 {
 71     node *p = list; node *p1=NULL,*p2=NULL,*q=new node;
 72     for (int i = 0; i < n; i++)
 73         p = p->next;
 74     q->data = e;
 75     if (p->next== NULL&&n!=0)
 76     {
 77         p1 = p;
 78         q->prior = p1;
 79         p1->next = q;
 80         q->next = NULL;
 81     }
 82     else{
 83         p1 = p;
 84         p2 = p->next;
 85         if (n == 0)
 86             q->prior = NULL;
 87         else
 88             q->prior = p1;
 89         p1->next = q;
 90         q->next = p2;
 91         p2->prior = q;
 92     }
 93 }
 94 
 95 void List_delete(int n, node* list)           //双向链表的删除操作
 96 {
 97     node *p = list; node *p1, *q, *p2;
 98     for (int i = 0; i < n - 1; i++)
 99         p = p->next;
100     p1 = p;
101     q = p1->next;
102     if (q->next == NULL)
103     {
104         p1->next = NULL;
105         delete q;
106     }
107     else{
108         p2 = q->next;
109         p1->next = p2;
110         if (n == 1)
111             p2->prior = NULL;
112         else
113             p2->prior = p1;
114         delete q;
115     }
116 }
117 
118 node* feidijianpaixu_List(node *list1, node *list2)    //两个双向链表的非递减排序
119 {
120     node *p1, *p2, *p3, *head3;
121     p1 = list1; p2 = list2; p3 = new node; head3 = NULL;
122     p1 = p1->next; p2 = p2->next;
123     head3 = p3; int i = 0;
124     while (p1 != NULL&&p2 != NULL)
125     {
126         i++;
127         if (p1->data > p2->data)
128         {
129             p3->next = p2;
130             if (i == 1)
131                 p2->prior = NULL;
132             else
133                 p2->prior = p3;
134             p3 = p2;
135             p2 = p2->next;
136         }
137         if (p1->data < p2->data)
138         {
139             p3->next = p1;
140             if (i == 1)
141                 p1->prior = NULL;
142             else
143                 p1->prior = p3;
144             p3 = p1;
145             p1 = p1->next;
146         }
147     }
148     if (p1 == NULL&&p2 != NULL)
149     {
150         p3->next = p2;
151         p2->prior = p3;
152     }
153     else if (p1 != NULL&&p2 == NULL)
154     {
155         p3->next = p1;
156         p1->prior=p3;
157     }
158     return head3;
159 }
160 
161 int main()
162 {
163     node *list = NULL;                   //创建双向链表一操作
164     cout << "请输入链表的长度:" << endl;
165     int n;
166     cin >> n;
167     cout << "请输入链表中的元素:"<<endl;
168     list=Init_List(n, list);
169 
170 
171     travle_List(list);     //顺序和逆序遍历双向链表一操作
172     cout << endl;
173 
174     cout << "请输入你想查找的数在倒数第几个位置:";   //逆向查找操作
175     int m;
176     cin >> m;
177     cout<<"你查找的数是:"<<List_search(m, list);
178     cout << endl;
179 
180     int a, e;               //双向链表的插入操作
181     cout << "请输入你要插在第几个数之后:";
182     cin >> a;
183     cout << "请输入你想插入的数:";
184     cin >> e;
185     List_insert(a, list, e);
186     travle_List(list);
187     cout << endl;
188 
189     cout << "请输入你想删除数的位置:";     //双向链表的删除操作
190     int b;
191     cin >> b;
192     List_delete(b, list);
193     travle_List(list);
194     cout << endl;
195 
196 
197     node *list1 = NULL, *list2 = NULL; int n1, n2;  //双向链表的非递减排序操作
198     cout << "两个双向链表的非递减排序" << endl;
199     cout << "请输入第一个链表的长度:" << endl;
200     cin >> n1;
201     cout << "请输入这个链表的元素:"<<endl;
202     list1 = Init_List(n1, list1);
203     cout << "请输入第二个链表的长度:" << endl;
204     cin >> n2;
205     cout << "请输入这个链表的元素:" << endl;
206     list2 = Init_List(n2, list2);
207     list1=feidijianpaixu_List(list1, list2);  //顺序逆序遍历这个非递减排序好的表
208     travle_List(list1);
209     cout << endl;
210 
211     return 0;
212 }

 

 
 
双向链表的相关操作(创建、顺序遍历、逆序遍历、逆序查找、删除、插入、非递减排序)

posted on 2017-01-18 16:56  小调~  阅读(1384)  评论(0编辑  收藏  举报

导航