刚学新的东西的时候总是摸不着头脑,计算机就是这样,以前做过的几个作业源码拿出来分享一下,给需要的人作下参考,当然我做的肯定不完美,有改进建议的话请q我吧,有时间会去再看看(刚开始学的时候感觉这些好难的,现在发现这几个有点简单,花点时间就能做出来的,高手就不要看了,新手记得自己动手做,可以参考,但是不要抄袭,不然以后你会后悔的)
1、还记得经典的约瑟夫环么,就是它(刚学的时候做的,后来懒得改了,可以瞄两眼)
head.h
#include<iostream>
typedef int ElemType;
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
#define OK 1
#define ERROR 0
#define OVERFLOW -2
LNode.h
typedef struct LNode
{
ElemType number;
ElemType code;
struct LNode *next;
}LNode,*LinkList;
Operator.cpp
#include"header.h"
#include"LNode.h"
using namespace std;
Status CreatList(LinkList &L,int n) //L指向尾节点
{
if(n<=0)
{
cout<<"警告:输入n值有误!"<<endl;
exit(ERROR); //若n值小于等于0,报错并终止程序
}
LinkList p,q; //定义指针p,q
L=(LinkList)malloc(sizeof(LNode)); //创建结点赋给L
if(!L)
exit(OVERFLOW); //若结点创建失败,则终止程序
p=L; //p指向刚创建的结点
p->number=1; //给刚创建的第一个结点number域赋序号1
cout<<"Please input the code of the 1th student:";
cin>>p->code;
int i;
for(i=2;i<=n;i++)
{
q=(LinkList)malloc(sizeof(LNode)); //创建结点赋给q
if(!q)
exit(OVERFLOW); //若结点创建失败,则终止程序
p->next=q;
q->number=i; //给刚创建的结点number域赋序号
cout<<"Please input the code of the "<<i<<"th student:";
cin>>q->code;
p=q;
}
p->next=L; //形成循环链表
L=p; //L赋为尾节点
return OK;
}
Status DeleteLNode(LinkList &priorL) //priorL为指向要删除节点前一个结点的LinkList指针
{
if(!priorL->next)
{
return ERROR; //若priorL指向的结点的next域为null则报错
}
LinkList p;
p=priorL->next;
priorL->next=priorL->next->next;
free(p); //删除priorL之后的结点
return OK;
}
void Output(LinkList end,int n,int m) //输出排序
{
if(m<=0)
{
cout<<"警告:输入m值有误!"<<endl;
exit(ERROR);
}
LinkList p=end;
int a,b;
if(n>1)
{
b=m;
cout<<"出列顺序为:"<<endl;
for(int i=0;i<n-1;i++)
{
for(int i=0;i<b-1;i++)
{
p=p->next;
}
a=p->next->number;
b=p->next->code;
DeleteLNode(p);
cout<<a<<endl;
}
cout<<p->number<<endl;
free(p);
}
else
{
cout<<"出列顺序为:/n"<<endl;
cout<<p->number<<endl;
free(p);
}
}
YuesefuMain.cpp
#include"header.h"
#include"LNode.h"
using namespace std;
Status CreatList(LinkList &L,int n);
void Output(LinkList end,int n,int m);
void main()
{
int m,n;
LinkList L;
cout<<"Please input the number of student 'n':";
cin>>n;
cout<<"Please input the first code number 'm':";
cin>>m;
CreatList(L,n);
Output(L,n,m);
}