1 #include <iostream>
 2 #include <stdlib.h>
 3 using namespace std;
 4 
 5 typedef struct DLNode
 6 {
 7     int data;
 8     struct DLNode *prior;
 9     struct DLNode *next;
10 }DLNode;
11 
12 void CreateDRear(DLNode *&L,int a[],int n)
13 {//尾插法建双链表
14     DLNode *s,*r;
15     int i;
16     L=(DLNode*)malloc(sizeof(DLNode));
17     L->next=NULL;
18     r=L;
19     for(i=0;i<n;++i)
20     {
21         s=(DLNode *)malloc(sizeof(DLNode));
22         s->data=a[i];
23         r->next=s;
24         s->prior=r;
25         r=s;
26     }
27     r->next=NULL;
28 }
29 
30 void PrintD(DLNode *L)
31 {
32     DLNode *p;
33     p=L->next;
34     while(p!=NULL)
35     {
36         cout<<"  "<<p->data<<"  ";
37         p=p->next;
38     }
39 }
40 
41 DLNode* SearchNode(DLNode *L,int x)
42 {//查找结点值为x的结点,若找到,则返回结点指针;否则返回NULL
43     DLNode *p=L->next;
44     while(p!=NULL)
45     {
46         if(p->data==x)
47             break;
48         p=p->next;
49     }
50     return p;
51 }
52 
53 int main()
54 {
55     DLNode *L,*loc;
56     int a[10]={1,2,3,4,5,6,7,8,9,10},n=10;
57     int x=5;
58     cout<<"\n------------------------------------"<<endl;
59     cout<<"待建表的元素序列为:\n";
60     for(int i=0;i<n;++i)
61         cout<<"  "<<a[i]<<"  ";
62 
63     cout<<"\n------------------------------------"<<endl;
64     cout<<"双链表中的元素序列为:\n";
65     CreateDRear(L,a,n);
66     PrintD(L);
67 
68     cout<<"\n------------------------------------"<<endl;
69     cout<<"查找双链表中的元素"<<x<<"的结点指针为: ";
70     loc=SearchNode(L,x);
71     cout<<loc<<endl;
72 
73     return 0;
74 }

 

posted on 2015-12-30 08:52  Xbert  阅读(176)  评论(0编辑  收藏  举报