2401_84401465 2024-10-28 08:53 采纳率: 71.4%
浏览 7
已结题

为什么我的代码运行结果是这样?我想用两个链表储存单词loading和being,相同部分字母(ing)使用同一个储存空间


#include<stdio.h>
#include<stdlib.h>
typedef struct node {
    char data;
    struct node* next;
}Node;
Node* initlist() {
    Node* L = (Node*)malloc(sizeof(Node));
    L->data = 0;
    L->next = NULL;
    return L;
}
Node* InitWithElem(char e) {
    Node* L = (Node*)malloc(sizeof(Node));
    L->data = e;
    L->next = NULL;
    return L;
}
void insertElem(Node* L,Node* E) {
    Node* p = L;
    while (p->next != NULL) {
        p = p->next;
        p->next = E;
    }
}
void insertlist(Node * L, char e) {
    Node* p = L;
    while (p->next != NULL) {
        p = p->next;
    }//尾結點
    Node* q = (Node*)malloc(sizeof(Node));
    q->next = NULL;
    q->data = e;
    p->next = q;
}
void traval(Node* L) {
    Node* p = L;
    while (p->next != NULL) {
        p = p->next;
        printf("%c\n", p->data);
    }
}
int main() {
    
    Node* str1 = initlist();
    Node* str2 = initlist();
    Node* stri = InitWithElem('i');
    Node* strn = InitWithElem('n');
    Node* strg = InitWithElem('g');
    insertlist(str1, 'l');
    insertlist(str1, 'o');
    insertlist(str1, 'a');
    insertlist(str1, 'd');
    insertlist(str2, 'b');
    insertlist(str2, 'e');
    insertElem(str1, stri);
    insertElem(str1, strn);
    insertElem(str1, strg);
    insertElem(str2, stri);
    insertElem(str2, strn);
    insertElem(str2, strg);
    traval(str1);
    traval(str2);
    return 1;
}

img

  • 写回答

1条回答 默认 最新

  • 爱做游戏的小学生 2024-10-28 20:25
    关注

    要找到两个单词相同部分并共享存储,首先需要完整地构建链表来存储单词的字符。对于给定的两个单词“loabing”和“being”,可以按字符依次比较,当遇到相同部分时,让两个链表的相应节点指向同一个新创建的节点(用于存储相同部分“ing”)
    试试这个:

    
    #include <stdio.h>
    #include <stdlib.h>
    
    // 定义链表节点结构体
    typedef struct node {
        char data;
        struct node *next;
    } Node;
    
    // 创建新节点函数
    Node *createNode(char c) {
        Node *newNode = (Node *)malloc(sizeof(Node));
        if (newNode == NULL) {
            perror("Memory allocation failed");
            return NULL;
        }
        newNode->data = c;
        newNode->next = NULL;
        return newNode;
    }
    
    // 构建链表函数
    Node *buildList(char *word) {
        Node *head = NULL;
        Node *tail = NULL;
        int i = 0;
        while (word[i]!='\0') {
            Node *newNode = createNode(word[i]);
            if (head == NULL) {
                head = newNode;
                tail = newNode;
            } else {
                tail->next = newNode;
                tail = newNode;
            }
            i++;
        }
        return head;
    }
    
    // 查找相同部分并共享存储函数
    void findSameAndShare(Node *list1, Node *list2) {
        Node *p1 = list1;
        Node *p2 = list2;
        Node *prev1 = NULL;
        Node *prev2 = NULL;
        while (p1!= NULL && p2!= NULL) {
            if (p1->data == p2->data) {
                Node *newNode = createNode(p1->data);
                if (prev1!= NULL) {
                    prev1->next = newNode;
                } else {
                    list1 = newNode;
                }
                if (prev2!= NULL) {
                    prev2->next = newNode;
                } else {
                    list2 = newNode;
                }
                prev1 = newNode;
                prev2 = newNode;
            } else {
                prev1 = p1;
                prev2 = p2;
            }
            p1 = p1->next;
            p2 = p2->next;
        }
    }
    
    // 打印链表函数
    void printList(Node *head) {
        Node *p = head;
        while (p!= NULL) {
            printf("%c", p->data);
            p = p->next;
        }
        printf("\n");
    }
    
    int main() {
        char word1[] = "loabing";
        char word2[] = "being";
        Node *list1 = buildList(word1);
        Node *list2 = buildList(word2);
        findSameAndShare(list1, list2);
        printList(list1);
        printList(list2);
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月18日
  • 已采纳回答 11月10日
  • 修改了问题 10月28日
  • 创建了问题 10月28日