赞
踩
话不多说,直接上码:
#include<iostream> using namespace std; typedef struct node{ int data; struct node* pre; struct node* next; }dnodelist; dnodelist * createNodelist() { int num; dnodelist *H ,*p, *r; if((H = (dnodelist *)malloc(sizeof(dnodelist))) == NULL) { cout<<"malloc filed!"<<endl; return NULL; } H->pre = H; H->next = H;//创建完头节点; r = H; while(1) { cout<<"亲输入你的数字吧"<<endl; cin >> num; if((p = (dnodelist *)malloc(sizeof(dnodelist))) == NULL) { cout<<"malloc filed!"<<endl; return NULL; } p->data = num; p->next = r->next;//增加节点四个步骤; r->next = p; p->pre = r; H->pre = p; r = p;//r 指针一直在移动; } return H; } void displayDnodelist(dnodelist *H) { dnodelist *p; p = H->next; while(p != H) { cout<<p->data<<" "; p = p->next; } cout<<endl; } int main() { displayDnodelist(createNodelist()); return 0; }
难点:
主要是在节点的插入过程,可以分为4步,如下图
r = p ,r 一直保持在最后一位;
基本思想就是这样啦!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。