zoukankan      html  css  js  c++  java
  • 数据结构-双链表

     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<<"
    ------------------------------------"<<endl;
    59     cout<<"待建表的元素序列为:
    ";
    60     for(int i=0;i<n;++i)
    61         cout<<"  "<<a[i]<<"  ";
    62 
    63     cout<<"
    ------------------------------------"<<endl;
    64     cout<<"双链表中的元素序列为:
    ";
    65     CreateDRear(L,a,n);
    66     PrintD(L);
    67 
    68     cout<<"
    ------------------------------------"<<endl;
    69     cout<<"查找双链表中的元素"<<x<<"的结点指针为: ";
    70     loc=SearchNode(L,x);
    71     cout<<loc<<endl;
    72 
    73     return 0;
    74 }
  • 相关阅读:
    团队的最佳诠释
    诸葛烤鱼
    Agile Web Development with Rails 读书笔记 (四)
    Agile Web Development with Rails 读书笔记 (三)
    客户教育
    To Be Or Not To Be Is The Question
    [zz]记录一linux命令:xargs
    Ubuntu 12.04 开启 wubi 安装
    [zz]mknod用法以及主次设备号
    [zz]centos下安装KVM方法
  • 原文地址:https://www.cnblogs.com/Xbert/p/5087681.html
Copyright © 2011-2022 走看看