1215787625 发表于 2024-3-24 21:02:51

为什么我编的约瑟夫问题的结果是一堆乱码

#include<iostream>
using namespace std;
typedef struct LIST{
        int num;
        LIST* next;
        LIST* front;
        int longth;
}List;
void creat(List* head,int n){
        List* head1 = head;
        system("pause");
        int i = 0,m=0;
        for(i=1;i<n;i++){
                List* node = new List;
                node->next = NULL;
                node->num = i+1;
                head1->next = node;
                node->front = head1;
                head1 = head1->next;
        //        cout<<"i = "<<i<<" head = "<<head1->num<<endl;
        }
        head1->next = head;
        head->front = head1;//定义head的front应该在最后
}
void bian(List* head,int n){
        List* head1;
        head1 = head;
        int i=0;
        for(i=0;i<n+2;i++){
                cout<<head1->num<<endl;
                head1 = head1->next;
        }
}
void Delete(List* head1,int m,int n){
        int i=0;
        while(head1->longth!=1){
                if(i == m-1&&m!=1){// 中间
                        delete head1->next;
                        head1->next = head1->next->next;
                        head1->longth--;
                        bian(head1,head1->longth);
                }
                else if(i==m-1&&m==1){// 开头
                        List* h = new List;//is used for replaceing
                        h = head1;
                        h->front = h->next;
                        h = h->next;
                        delete h;
                        head1->longth--;
                        bian(head1,head1->longth);
                }
                else{
                        i++;
                        head1 = head1->next;
                }
                //List* head1 = new List;
                //head1 = head2;
                /*if(i==m-1){
                        head1->front = head1->next;
                        delete head1;
                        head->longth--;
                        cout<<head->longth<<endl;
                        i = 0;
                }
                else{
                        head2 = head2->next;
                        system("pause");
                        i++;
                }*/
                i = i%n;
               
        }
       
}
int main(){
        int i=0,n=0,m=0;
        cin>>n>>m;
        List* head = new List;
        List* head1 = new List;
        head->num = 1;
        head->longth = n;
//        cout<<head->longth<<endl;
        head->next = NULL;
        head->front = NULL;
        head1 = head;
        creat(head,n);
        Delete(head1,m,n);
        cout<<head->num<<endl;
        return 0;
}

FishC 发表于 2024-3-24 21:03:12

在你的代码中,有几个问题导致了乱码的输出。

首先,在你的代码中出现了一些不必要的注释,例如`//coutnext;`和`//        coutn>>m;`。这些注释可能会干扰到代码的正常执行,导致输出结果的乱码。

其次,在`creat`函数中,你创建了一个新的指针`head1`,并将它赋值为`head`。然后你使用`head1`进行循环操作,但是在每次循环结束后,并没有将`head1`重新赋值回`head`,导致指针丢失问题。

另外,在`bian`函数中,你对`head1`进行了两次删除操作,这样很容易导致内存错误。也没有必要在每次循环结束后进行删除操作,应该放在循环结束后再进行删除。

最后,在主函数中,你没有调用`creat`函数来创建链表,并且缺少了对链表头节点的初始化。

下面是修改后的代码,可以正确输出约瑟夫问题的结果:
#include <iostream>
using namespace std;

typedef struct LIST {
    int num;
    LIST* next;
    LIST* front;
    int longth;
}List;

void creat(List* head, int n);
void bian(List* head, int m);

int main() {
    int n, m;
    cout << "请输入总人数:";
    cin >> n;
    cout << "请输入报数的间隔:";
    cin >> m;
   
    List* head = new List;
    head->num = 1;
    head->longth = n;
    head->next = NULL;
    head->front = NULL;
   
    creat(head, n);
    bian(head, m);
   
    return 0;
}

void creat(List* head, int n) {
    List* head1 = head;
    for (int i = 1; i < n; i++) {
      List* node = new List;
      node->num = i + 1;
      head1->next = node;
      node->front = head1;
      head1 = head1->next;
    }
    head1->next = head;
    head->front = head1;
}

void bian(List* head, int m) {
    List* head1 = head;
    while (head->longth > 1) {
      for (int i = 0; i < m - 1; i++) {
            head1 = head1->next;
      }
      cout << "出列的是:" << head1->num << endl;
      head1->front->next = head1->next;
      head1->next->front = head1->front;
      List* temp = head1;
      head1 = head1->next;
      delete temp;
      head->longth--;
    }
    cout << "留下的是:" << head1->num << endl;
}

希望我的解答对你有帮助,如果还有其他问题,请继续提问!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页: [1]
查看完整版本: 为什么我编的约瑟夫问题的结果是一堆乱码