0


约瑟夫环问题解决

链表

structList{int data;structList* next;}

创建链表

单链表
实现
structList*listCreate(){int data;structList* head =NULL;structList* pre =NULL;structList* current =NULL;while(scanf("%d",&data)&& data !=-1){
        current =(structList*)malloc(sizeof(structList));if(head ==NULL)
            head = current;else
            pre->next = current;
        current->next =NULL;
        current->data = data;
        pre = current;}return head;}
错例
structList*listCreate(){int data;;structList* current =NULL;structList* head = current;while(scanf("%d",&data)&& data !=-1){
        current =(structList*)malloc(sizeof(structList));if(head ==NULL)
            head = current;
        current->data = data;
        current = current->next;}return head;}

在使用malloc函数开辟的空间中,不要进行指针的移动,因为一旦移动之后可能出现申请的空间和释放空间大小的不匹配

循环链表

单独创建
structList*circle_listCreate(){int data;structList* head =NULL;structList* pre =NULL;structList* current =NULL;while(scanf("%d",&data)&& data !=-1){
        current =(structList*)malloc(sizeof(structList));if(head ==NULL)
            head = current;else
            pre->next = current;
        current->next = head;
        current->data = data;
        pre = current;}return head;}
逐节点创建
voidAppend(structList** L,int data){structList* head =*L;structList* newNode =NULL;if((*L)==NULL){(*L)=(structList*)malloc(sizeof(structList));(*L)->data = data;
        head =(*L);(*L)->next = head;}else{while((*L)->next != head){(*L)=(*L)->next;}
        newNode =(structList*)malloc(sizeof(structList));
        newNode->data = data;(*L)->next = newNode;
        newNode->next = head;*L = head;}}
约瑟夫环问题
voidAppend(structList** L,int data){structList* head =*L;structList* newNode =NULL;if((*L)==NULL){(*L)=(structList*)malloc(sizeof(structList));(*L)->data = data;
        head =(*L);(*L)->next = head;}else{while((*L)->next != head){(*L)=(*L)->next;}
        newNode =(structList*)malloc(sizeof(structList));
        newNode->data = data;(*L)->next = newNode;
        newNode->next = head;*L = head;}}voidDisplay(structList* L,int num){structList* head = L;structList* pre =NULL;structList* kill =NULL;int nodeNum =0;while(L->next != head){
        nodeNum++;
        L = L->next;}
    pre = L;
    L = L->next;
    nodeNum++;while(nodeNUm){if(nodeNum ==1){printf("%d",L->data);free(L);return;}for(int i=1; i < m; i++){
            pre = L;
            L = L->next;}printf("%d ", L->data);
        kill = L;
        L = L->next;free(kill);
        nodeNum--;}}

删除节点

实现方式一:

structlist*listDelete(structlist* L,int data){structlist* pre = L;structlist* head = L;structlist* kill;while(head !=NULL&& head->data == m){
        kill = head;
        head = head->next;free(kill);}if(head ==NULL)return head;
    
    pre = head;
    kill = head->next;while(kill!=NULL){if(kill->data == m){
            pre->next = kill->next;free(kill);
            kill = pre->next;}else{
            pre = kill;
            kill = kill->next;}}return head;}

实现方式二:

structlist*listDelete(structlist** L,int data){structlist* head =(*L),* pre =(*L);structlist* newL =*L;structlist* kill =NULL;while(*L !-NULL){if((*L)->data == data){if((*L)== newL)
                newL == newL->next;else
                pre->next =(*L)->next;
            kill =(*L);(*L)=(*L)->next;free(kill);}else{
            pre =(*L);(*L)=(*L)->next;}}*L = newL;return head;}

删除节点并建立新链表

structlist*list_Delete_Create(structlist** L)//数据为奇数存为新链表{structlist* newhead =NULL,* newcurrent =NULL,* newpre =NULL;structlist* newL =*L;structlist* kill =NULL;structlist* pre =*L;while(*L){if((*L)->data%2==1){
            newcurrent =(structlist*)malloc(sizeof(structlist));if(newhead ==NULL)
                newhead = newcurrent;else
                newpre->next = newcurrent;
            newcurrent->data =(*L)->data;
            newcurrent->next =NULL;
            newpre = newcurrent;if((*L)== newL)
                newL = newL->next;else
                pre-next =(*L)->next;
            kill =(*L);(*L)=(*L)->next;free(kill);}else{
            pre =(*L);(*L)=(*L)->next;}}*L = newL;return newhead;}

逆置链表

实现


本文转载自: https://blog.csdn.net/Star__01/article/details/135578062
版权归原作者 stoAir 所有, 如有侵权,请联系我们删除。

“约瑟夫环问题解决”的评论:

还没有评论