这里先附上上一篇博文的链接大家可以对比着看,错误已经改正https://blog.csdn.net/2302_78946488/article/details/141751514?spm=1001.2014.3001.5501
约瑟夫环
以下是详细代码
//约瑟夫环
#include<stdio.h>
#include<stdlib.h>
//建立链表结点
struct Node{
int data;
struct Node * next;
};
//创建循环单链表
struct Node* createCircularList(int n){
struct Node * head=NULL;
struct Node * prev=NULL;
for(int i=1;i<=n;i++){
struct Node * newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->data=i;
newNode->next=NULL;
if(!head){
head=newNode;
}
else{
prev->next=newNode; //尾插法建立单链表
}
prev=newNode; //相当于尾指针
}
prev->next=head; //让尾结点的next域指向头结点创建循环链表
return head; //返回头指针,创建完成
}
//解决约瑟夫环问题
int josephusProblem(int n,int m){
struct Node* current=createCircularList(n); //储存头结点
struct Node* prev=NULL;
int c=0;
while(current->next!=current){ //遍历循环链表
for(int i=1;i<m;i++){ //相当于计数器
prev=current;
current=current->next;
}
prev->next=current->next; //删除结点current
struct Node * temp=current;
c++;
int a=current->data;
printf("第%d个淘汰者的编号为%d\n",c,a);
current=current->next;
free (temp);
}
int result=current->data;
free(current);
return result;
}
int main(){
int n,m;
printf("请输入总人数和淘汰数:\n");
scanf("%d %d",&n,&m);
int lastPerson=josephusProblem(n,m);
printf("最后留下的人的编号为:%d\n",lastPerson);
return 0;
}
以下为详细运行结果
一元多项式相加相减
//一元多项式加法
//定义链表结点
#include<stdio.h>
#include<stdlib.h>
struct Node{
int coef; //系数
int exp; //指数
struct Node * next;
};
//创建多项式
struct Node * createPolynomial(){
int n;
printf("输入多项式的项数:");
scanf("%d",&n);
struct Node * head=NULL;
struct Node * prev=NULL;
for(int i=0;i<n;i++){
struct Node * newNode=(struct Node*)malloc(sizeof(struct Node));
printf("输入第%d项的系数和指数:",i+1);
int coef,exp;
scanf("%d %d",&coef,&exp);
newNode->coef=coef;
newNode->exp=exp;
printf("\n");
newNode->next=NULL; //尾插法建立单链表
if(!head){
head=newNode;
}
else{
prev->next=newNode;
}
prev=newNode;
}
return head; //返回头结点
}
//打印多项式
void printPolynomial(struct Node * head){
if(!head){ //如果结果为空,返回0
printf("0\n");
}
struct Node * current=head;
while(current){
if(current->coef!=0){
if(current->coef>0&¤t!=head){ //系数大于0,且不是头指针(即不是第一项)打印加号
printf("+");
}
if(current->coef==0){ //系数等于0,打印0
printf("0");
}
else if(current->exp==1){ //如果系数不为0,指数等于1,打印x
printf("%dx",current->coef);
}
else{ //系数不为0,指数不为1
printf("%dx^%d",current->coef,current->exp);
}
}
current=current->next;
}
printf("\n");
}
//一元多项式加法
struct Node* addPolynomial(struct Node* poly1,struct Node* poly2){
struct Node* result=NULL;
struct Node* prev=NULL;
struct Node* current1=poly1; //current1指向第一个多项式
struct Node* current2=poly2; //current2指向第二个多项式
while(current1&¤t2){ //结束条件为其中一个多项式已经遍历完
struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
if(current1->exp>current2->exp){
newNode->exp=current2->exp;
newNode->coef=current2->coef;
current2=current2->next;
}
else if(current1->exp<current2->exp){
newNode->exp=current1->exp;
newNode->coef=current1->coef;
current1=current1->next;
}
else{
newNode->exp=current1->exp;
newNode->coef=current1->coef+current2->coef;
current1=current1->next;
current2=current2->next;
}
newNode->next=NULL;
if(!result){ //相当于尾插法建立新的链表用来储存两个多项式相加的值
result=newNode;
}
else{
prev->next=newNode;
}
prev=newNode;
}
while(current1){
struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->coef=current1->coef;
newNode->exp=current1->exp;
newNode->next=NULL;
prev->next=newNode;
prev=newNode;
current1=current1->next;
}
while(current2){
struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->coef=current2->coef;
newNode->exp=current2->exp;
newNode->next=NULL;
prev->next=newNode;
prev=newNode;
current2=current2->next;
}
return result;
}
//一元多项式减法
struct Node* subtractPolynomial(struct Node* poly1,struct Node* poly2){
struct Node * current2;
current2=poly2;
while(current2){
current2->coef*=-1;
current2=current2->next;
}
return addPolynomial(poly1,poly2);
}
//主函数
int main(){
struct Node* poly1=createPolynomial();
struct Node* poly2=createPolynomial();
printf("多项式1:");
printPolynomial(poly1);
printf("多项式2:");
printPolynomial(poly2);
struct Node* sum=addPolynomial(poly1,poly2);
printf("两多项式的和为:");
printPolynomial(sum);
struct Node* subtract=subtractPolynomial(poly1,poly2);
printf("两多项式的差为:");
printPolynomial(subtract);
}
这里注意读入数据时是按照指数从低到高读进来的,所以在比较两个式子的时候应先将较小的指数所在项放在新的链表中;
如果是按照指数从高到低读入数据,两个式子比较的时候应该先将较大的指数所在项放进新的链表中。
详细运行结果如下