作业2

作者: cjs2019 | 来源:发表于2019-03-24 17:29 被阅读5次

If you have certain questions, please try to solve it in the remark area. The writer is busy nowadays. And he has something important with……(you know……).

只给最爱写文章,私信一律不回复。(

QAQ

感谢每一个曾经帮助过我的人……

—————————我是分割线——————————

A题:实验11_9_链表归并

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DATA_TYPE int
#define STOP_SIGNAL (-1)
struct listNode{
    DATA_TYPE data;
    listNode* next;
};

void create_one_listNode(listNode** headPtr,listNode** presentPtr,DATA_TYPE num){//创建链表的一个节点
    listNode *ptr=NULL;
    ptr=(listNode*)malloc(sizeof(listNode));
    if (ptr!=NULL){
        ptr->data=num;
    }
    if (*headPtr==NULL){
        (*headPtr)=(*presentPtr)=ptr;
        (*headPtr)->next=NULL;
        (*presentPtr)->next=NULL;
        return;
    }
    (*presentPtr)->next=ptr;
    (*presentPtr)=ptr;
    (*presentPtr)->next=NULL;
}
void input_one_list(listNode** headPtr){//输入一个链表(创建一个链表)
    DATA_TYPE num;
    listNode* presentPtr=(*headPtr);
    scanf("%d",&num);
    while (num != STOP_SIGNAL){
        create_one_listNode(headPtr,&presentPtr,num);
        scanf("%d",&num);
    }
}
void show_one_list(listNode** headPtr){//输出一个链表
    listNode *ptr=(*headPtr);
    while (ptr!=NULL){
        printf("%d ",ptr->data);
        ptr=ptr->next;
    }
    printf("\n");
}
void insert(listNode **headPtr,DATA_TYPE num){//在一个链表中插入一个节点
    listNode *ptr=NULL;
    ptr=(listNode*)malloc(sizeof(listNode));
    if (ptr!=NULL){
        ptr->data=num;
    }
    if (num<(*headPtr)->data){
        ptr->next=(*headPtr);
        (*headPtr)=ptr;
    }
    else{
        listNode *pre_Ptr=(*headPtr);
        int flag=1;
        if (pre_Ptr->next==NULL){
            if (pre_Ptr->data<num){
                pre_Ptr->next=ptr;
                ptr->next=NULL;
                flag=0;
            }
        }
        while (pre_Ptr->next!=NULL && flag==1){
            if (pre_Ptr->data<num && num<pre_Ptr->next->data){
                ptr->next=pre_Ptr->next;
                pre_Ptr->next=ptr;
            }
            pre_Ptr=pre_Ptr->next;
            if (pre_Ptr->next==NULL){
                if (pre_Ptr->data<num){
                    pre_Ptr->next=ptr;
                    ptr->next=NULL;
                    flag=0;
                }
            }
        }
    }
}
void free_one_list(listNode** headPtr){
    if (*headPtr==NULL){
        return;
    }
    listNode *ptr=(*headPtr);
    while (ptr!=NULL){
        (*headPtr)=(*headPtr)->next;
        free(ptr);
        ptr=(*headPtr);
    }
}

int main(){
    listNode *headA=NULL,*headB=NULL;
    input_one_list(&headA);
    input_one_list(&headB);//输入两个链表
    //show_one_list(&headA);
    //show_one_list(&headB);
    listNode *temPtr_for_listA=NULL,*temPtr_for_listB=NULL;
    temPtr_for_listA=headA;
    temPtr_for_listB=headB;
    DATA_TYPE flag=0;
    listNode *current_ptr=headB;
    while (current_ptr==headB){//特判,如果当前位置为B链表的头节点
        temPtr_for_listA=headA;
        flag=0;
        if (headB==NULL) {//特判,头节点为空,即该链表为空链表
            break;
        }
        while (temPtr_for_listA!=NULL){//遍历查找A中有无相同元素(实在是懒得优化了,不然又要多20行代码)
            if (temPtr_for_listA->data==temPtr_for_listB->data){
                flag=1;//判断A中有无相同元素
                break;
            }
            temPtr_for_listA=temPtr_for_listA->next;
        }
        if (flag==1){//根据上述判断,进行处理,如果有相同元素,处理下一个……
            temPtr_for_listB=temPtr_for_listB->next;
            current_ptr=temPtr_for_listB;
        }
        else{//如果没有相同元素,在B中删除该节点,在A中插入该节点(依然没有采用最难的那种方法……
            listNode *ptr=NULL;
            ptr=headB;
            if (headA==NULL) {
                listNode *new_ptr=NULL;
                new_ptr=(listNode*)malloc(sizeof(listNode));
                if (new_ptr!=NULL) {
                    new_ptr->data=temPtr_for_listB->data;
                    headA=new_ptr;
                }
                headA->next=NULL;
                new_ptr->next=NULL;
            }
            insert(&headA,temPtr_for_listB->data);//插入“该节点”……
            listNode *tem=NULL;
            tem=headB;
            headB=headB->next;
            current_ptr=headB;
            temPtr_for_listB=current_ptr;
            free(tem);
        }
    }
    listNode *pre_ptr=headB;//接下来处理 当前节点不是头节点的情况
    if (headA!=NULL && headB!=NULL) {//特判,防止“Segmentation fault”?
        while (pre_ptr->next !=NULL){
            if (headB==NULL ||headA==NULL) {
                break;
            }
            temPtr_for_listA=headA;
            flag=0;
            while (temPtr_for_listA!=NULL){
                if (temPtr_for_listA->data==pre_ptr->next->data){
                    flag=1;
                    break;
                }
                temPtr_for_listA=temPtr_for_listA->next;
            }
            if (flag==1){
                pre_ptr=pre_ptr->next;
            }
            else{
                insert(&headA,pre_ptr->next->data);
                listNode *ptr=NULL;
                ptr=pre_ptr->next;
                pre_ptr->next=pre_ptr->next->next;
                free(ptr);
            }
        }
    }
    if (headA!=NULL){//输出,没有什么可以炫技的地方,还是不写成函数了……
        printf("The new list A:");
        show_one_list(&headA);
    }
    else{
        printf("There is no item in A list.\n");
    }

    if (headB!=NULL){
        printf("The new list B:");
        show_one_list(&headB);
    }
    else{
        printf("There is no item in B list.\n");
    }

    free_one_list(&headA);//释放动态分配的内存
    free_one_list(&headB);


    return 0;
}

—————————我是分割线——————————

B. 实验11_15_拆分链表

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DATA_TYPE char

struct listNode{
    DATA_TYPE data;
    listNode* next;
};
void create_one_listNode(listNode** headPtr,listNode** presentPtr,DATA_TYPE num){//创建链表的一个节点
    listNode *ptr=NULL;
    ptr=(listNode*)malloc(sizeof(listNode));
    if (ptr!=NULL){
        ptr->data=num;
    }
    if (*headPtr==NULL){
        (*headPtr)=(*presentPtr)=ptr;
        (*headPtr)->next=NULL;
        (*presentPtr)->next=NULL;
        return;
    }
    (*presentPtr)->next=ptr;
    (*presentPtr)=ptr;
    (*presentPtr)->next=NULL;
}
void show_one_list(listNode** headPtr){//输出一个链表
    listNode *ptr=(*headPtr);
    while (ptr!=NULL){
        printf("%c ",ptr->data);
        ptr=ptr->next;
    }
    printf("\n");
}
void free_one_list(listNode** headPtr){
    if (*headPtr==NULL){
        return;
    }
    listNode *ptr=(*headPtr);
    while (ptr!=NULL){
        (*headPtr)=(*headPtr)->next;
        free(ptr);
        ptr=(*headPtr);
    }
}
void swap(listNode* pre_x,listNode* pre_y){
    DATA_TYPE tem=pre_x->data;
    pre_x->data=pre_y->data;
    pre_y->data=tem;
}
void sort_for_list(listNode** headPtr){
    listNode *lastPtr=NULL,*ptr=(*headPtr);
    if ((*headPtr)->next==NULL){
        return;
    }
    else if((*headPtr)->next->next==NULL){
        if ((*headPtr)->data>(*headPtr)->next->data){
            swap(*headPtr,(*headPtr)->next);
        }
    }
    ptr=(*headPtr);
    while (ptr->next!=NULL){
        lastPtr=ptr->next;
        while(lastPtr!=NULL){
            if (ptr->data>lastPtr->data){
                swap(ptr,lastPtr);
            }
            lastPtr=lastPtr->next;
        }
        ptr=ptr->next;
    }
}
listNode* SelectSort(listNode** headPtr){
    listNode* outputList_head=NULL,*outputList_lastPtr=NULL,*ptr=NULL,*minPtr=NULL;
    while ((*headPtr)->next != NULL){ //当头部节点的指针域为NULL时,整个链表中就只剩下了一个元素 ———— **headPtr;
        long long num=0;
        minPtr=ptr=*headPtr;
        while (ptr->next != NULL) { // ptr 的后继为空,即ptr是指向最后一个节点的指针;
            if (ptr->next->data < minPtr->next->data) { // 如果当前节点的数据域小于最小值节点的数据域;
                minPtr = ptr; //将他的前驱赋给最小值节点的前驱;
            }
            ptr = ptr->next;// 指向当前节点的指针后移;
        }
        //由上面的循环可知,ptr如果是最后一个节点,那么就会跳出循环,也就是说,最后一个节点并没有参与比较,
        // 但是很明显,最后一个节点作为倒数第二个节点的后继,参与了比较,由于这是单向链表,所以真正没有参与比较的,其实就只有头部节点,因为头部节点没有前驱;

        if ((*headPtr)->data < minPtr->next->data){ //如果头部节点的数据比最小值还要小,
            num=(*headPtr)->data; //将最小值赋给num,
            (*headPtr)=(*headPtr)->next; //并将头指针指向之前头指针的后继,从而完成了对该节点的删除;
        }
        else {
            num=minPtr->next->data;
            minPtr->next=minPtr->next->next;
        }
        create_one_listNode(&outputList_head,&outputList_lastPtr,num);//新建一个节点,在输出列表中储存这个数字;
    }
    create_one_listNode(&outputList_head,&outputList_lastPtr,(*headPtr)->data);//新建一个节点,在输出列表中储存最后一个节点;
    return outputList_head;//返回输出链表的头指针;
}

int main(){
    char a[1000];
    gets(a);
    long len=strlen(a);
    listNode *headA=NULL,*presentA=NULL,*headB=NULL,*presentB=NULL,*headC=NULL,*presentC=NULL;
    for (int i = 0; i < len-3; ++i) { //输入数据;
        if (a[i]!=' '){
            if ((a[i]<='Z'&&a[i]>='A')||(a[i]<='z'&&a[i]>='a')){
                create_one_listNode(&headA,&presentA,a[i]);
            }
            else if (a[i]<='9'&& a[i]>='0'){
                create_one_listNode(&headB,&presentB,a[i]);
            }
            else{
                create_one_listNode(&headC,&presentC,a[i]);
            }
        }
    }
    //输出(如果非空表,排序后输出)
    if (headA==NULL){
        printf("There is no item in A list.\n");
    }
    else{
        sort_for_list(&headA);
        printf("The list A is: ");
        show_one_list(&headA);
        free_one_list(&headA);
    }
    if (headB==NULL){
        printf("There is no item in B list.\n");
    }
    else{
        sort_for_list(&headB);
        printf("The list B is: ");
        show_one_list(&headB);
        free_one_list(&headB);
    }
    if (headC==NULL){
        printf("There is no item in C list.\n");
    }
    else{
        sort_for_list(&headC);
        printf("The list C is: ");
        show_one_list(&headC);
        free_one_list(&headC);
    }


    return 0;
}

—————————我是分割线——————————

(以下都是写给“the one”的,请大家屏蔽……

(与大家 或者说是 任何一个你认识的人)毫无关系的尾注:

My heart is yours.
My time is yours.
My attention is yours.
My energy is yours.
……
I am yours.
Now and always.

3 days before “Δύο επέτειο”.
“Είμαι στο σταδιακά να δοκιμάζω νέα πράγματα, την κατανόηση των νέων εντελώς διαφορετικές απόψεις και τη ζωή.”
σ 'αγαπώ.
το αγαπημένο σου πρόσωπο

相关文章

网友评论

    本文标题:作业2

    本文链接:https://www.haomeiwen.com/subject/mdxsvqtx.html