数据结构之链表的实例
来源:互联网 发布:mac tomcat端口被占用 编辑:程序博客网 时间:2024/06/07 16:43
/*约瑟夫死亡游戏*/#include <iostream>using namespace std;class Node{public: int data; Node *next;};class List{public: List(); ~List(); void createList(int num); void ListDelete(int num,int n,int other); void ListTraverce(); Node *pList;};List::List(){ pList=new Node; pList->next=pList;}List::~List(){ Node *currentNode=pList; while(currentNode->next!=pList) { Node *temp=currentNode->next; delete currentNode; currentNode=temp; } delete pList; pList=NULL;}void List::createList(int num){ if(num==0) { cout<<"输入有误"; } else { pList->data=1; } Node *currentNode=pList; for(int i=2;i<=num;i++) { Node *newNode=new Node; newNode->data=i; newNode->next=currentNode->next; currentNode->next=newNode; currentNode=newNode; }}void List::ListDelete(int num,int n,int other){ Node *currentNode=pList; Node *currentNodebefore=currentNode; while(num>other) { for(int i=0;i<n-1;i++) { currentNodebefore=currentNode; currentNode=currentNode->next; } cout<<currentNode->data<<endl; num--; Node *temp=new Node; temp=currentNode->next; currentNodebefore->next=temp; currentNode=temp; }}void List::ListTraverce(){ Node *currentNode=pList; while(currentNode->next!=pList) { cout<<currentNode->data<<endl; currentNode=currentNode->next; } cout<<currentNode->data<<endl;}int main(){ cout<<"请确定总人数"; int num; cin>>num; cout<<"请确认间隔的人数"; int n; cin>>n; cout<<"请确认剩余多少人"; int other; cin>>other; List *p=new List(); p->createList(num); // p->ListTraverce(); p->ListDelete(num,n,other); cout<<endl<<"剩余"<<endl; p->ListTraverce(); delete p; p=NULL; return 0;}
阅读全文
0 0
- 数据结构之链表的实例
- 数据结构之链表的实例
- 数据结构之队列实例
- 【数据结构】单向链表实例
- 数据结构3-链表生成的实例与分析
- C语言数据结构之图的邻接矩阵的应用实例
- 数据结构之栈的简单实例c++实现
- 数据结构10-排序方法之直接插入排序的实例
- 数据结构11-排序方法之选择排序的实例
- 数据结构12-排序方法之冒泡排序的实例
- 数据结构13-排序方法之希尔排序的实例
- 数据结构14-排序方法之快速排序的实例
- 数据结构之队列应用实例
- 数据结构之线性链表的算法
- 数据结构之双向链表的实现
- 数据结构之链表的删除
- 数据结构之链表的插入
- 数据结构之链表的Java实现
- C语言——完数
- Springmvc的入门---配置
- python数据持久化到文件并读取:pickle的使用
- struts报错严重: Dispatcher initialization failed Unable to load configuration.
- 文本特征选择-互信息
- 数据结构之链表的实例
- log4j在spring4中的配置
- Retrofit上传头像
- Java: 对象的初始化过程
- leetcode 303. Range Sum Query
- 2 认识java虚拟机的基本结构
- StringBuffer的用法
- get请求与post提交区别的简易理解
- Caffe——Python接口学习:绘制loss和accuracy曲线