双循环链表之分离
来源:互联网 发布:java webservice加密 编辑:程序博客网 时间:2024/05/22 10:28
要求:
把a1,a2,a3,a4,....an分离成
a1,a3,a5,....an,..a4,a2;
代码如下:
//双循环链表//带头结点的循环链表#include <iostream>using namespace std;typedef int ElemType;//结点类型定义typedef struct DoubleListNode {ElemType data;DoubleListNode *prior;DoubleListNode *next;}DoubleListNode;DoubleListNode* CreateList(DoubleListNode *&root,int n){DoubleListNode *last=new DoubleListNode;last->prior=last->next=NULL;last=root;cout<<"请输入结点的信息"<<endl;int i;ElemType temp;for(i=0;i<n;++i){cin>>temp;DoubleListNode *newnode=new DoubleListNode;newnode->data=temp;newnode->prior=newnode->next=NULL;newnode->next=last->next;last->next=newnode;newnode->prior=last;root->prior=newnode;last=newnode;}return root;}void Display(DoubleListNode *root){DoubleListNode *current=root->next;DoubleListNode *temp=new DoubleListNode;while (current!=root){temp=current;cout<<current->data<<" "<<"它的前驱结点是";if(temp->prior==root)cout<<temp->prior->prior->data;else{cout<<temp->prior->data;}cout<<" "<<"它的后继结点是";if(temp->next==root)cout<<temp->next->next->data<<endl;else{cout<<temp->next->data<<endl;}current=current->next;}}void Separate(DoubleListNode *&root){DoubleListNode *current=new DoubleListNode;current->prior=current->next=NULL;current=root->next;while (current->next!=root&¤t->next->next!=root){current->next=current->next->next; //跳过偶数结点current=current->next;}if(current->next==root) current->next=root->prior->prior; //如果原数组最后一位是奇数,则连接最后一个偶数else if(current->next->next==root) //最后一位是偶数,则连接次偶数{current->next=root->prior;}current=current->next; //此时current定位为最后一个结点,现在开始连偶数点while (current->prior->prior!=root){current->next=current->prior->prior;current=current->next;}current->next=root;for(current=root;current->next!=root;current=current->next)current->next->prior=current;root->prior=current;}int main(){DoubleListNode *root=new DoubleListNode;root->prior=root->next=root;int n;cout<<"创建几个结点的双循环链表"<<endl;cin>>n;root=CreateList(root,n);cout<<"链表创建成功"<<endl;Display(root);cout<<endl;Separate(root);cout<<"分离后的链表"<<endl;Display(root);system("pause");return 0;}
- 双循环链表之分离
- 数据结构之双循环链表
- 循环链表之双循环链表
- 线性表之双循环链表
- 线性表之双循环链表
- 双循环链表
- 双循环链表
- 数据结构-双循环链表
- 双循环链表
- 尹成 双循环链表
- 双循环链表(C++)
- 带头结点双循环链表
- 数据结构之—线性表之—双向链表之—浅谈双循环链表
- 单循环链表变成双循环链表
- 数据结构(线性表):双循环链表
- Linux内核中的双循环链表
- 双循环链表的一些操作
- 双循环链表『4』
- 常成员变量、常成员函数及常对象
- web office使用
- 惊闻Google Reader将被关闭
- python 学习笔记 -- 字符串、数值及运算符
- emacs 安装cedet和ecb
- 双循环链表之分离
- Objective-C中nil与release的区别与用法
- 1217: Problem C
- 编程之美-数组循环移位
- 极点五笔状态栏菜单不显示,如何再让它显示?
- CAD 单位及比例尺
- 3-1调试类
- fireevent在QTP的应用,丰富了控件的行为。
- 黑马程序员 笔记(六)——面向对象(static关键字、静态、main函数、、帮助文档、对象的调用和初始化、单例)