【问题标题】:Split a single linked list into 2 SIngly linked lists -one containing nodes with even data and other containing nodes with odd data将单个链表拆分为 2 个单链表 - 一个包含具有偶数数据的节点,另一个包含具有奇数数据的节点
【发布时间】:2020-06-17 09:02:34
【问题描述】:

我尝试对上述问题进行编码,但出现分段错误。以下是我编写的代码:

       #include<stdio.h>
       #include<stdlib.h>
       #include<math.h>
       struct oddeven
       {
           int data;
           struct oddeven *link;
       };
       typedef struct oddeven m;
       int main()
       {
            int z;
            m *head=NULL,*ptr,*current;
            m *x,*y,*q,*head1=NULL,*current1,*head2=NULL,*current2; 
            while(1)
            {
                 int ch;
                 ptr=(m*)malloc(sizeof(m));
                 printf("Enter the data: ");
                 scanf("%d",&ptr->data);
                 ptr->link=NULL;
                 if(head==NULL)
                 {
                     head=ptr;
                     current=ptr;
                 }
                 else 
                 {
                     current->link=ptr;
                     current=ptr;
                 }
                 printf("Do you want to continue?Y=1/N=0");
                 scanf("%d",&ch);
                 if(ch!=1)
                     break;
             }
             x=head;
             while(x!=NULL)
             {
                  z=x->data;
                  if(z%2==0)
                  {
                       ptr=(m*)malloc(sizeof(m));
                       ptr->data=z;
                       ptr->link=NULL;
                       if(head1==NULL)
                       {
                            head1=ptr;
                            current1=ptr;
                       }
                       else
                       {
                            current1->link=ptr;
                            current1=ptr;
                       }

                  }
                 else
                 {
                       ptr=(m*)malloc(sizeof(m));
                       ptr->data=z;
                       ptr->link=NULL;
                       if(head2=NULL)
                       {
                            head2=ptr;
                            current2=ptr;
                       }
                       else
                       {
                           current2->link=ptr;
                           current2=ptr;
                       }
                  }
                  x=x->link;    
            }
            y=head1;
            q=head2;
            while (y!=NULL)
            {
                printf("%d\t",y->data); 
                y=y->link;
            }
            printf("\n");
            while (q!=NULL)
            {
                  printf("%d\t",q->data);
                  q=q->link;
            } 
      }

我不知道我哪里出错了。任何帮助将非常感激。 它接受输入,但之后它说分段错误。将给定的单链表拆分为两个,我可以分别存储奇数和偶数。我尝试了不同的方法,但无法让它工作。

【问题讨论】:

  • 为什么拆分列表时需要分配更多的节点内存?您可以简单地将每个节点重新链接到“偶数”列表或“奇数”列表。
  • @Gaming DEITY if 语句中有错字。 if(head2=NULL) 必须是 if(head2==NULL)
  • @Gaming DEITY 请注意,您的方法不会执行原始列表的拆分。
  • 这能回答你的问题吗? Uninitialized local variable error

标签: c data-structures singly-linked-list


【解决方案1】:

@VladfromMoscow 提到了您的程序出错的一个原因,但无论如何它太复杂了。这是一个更简单的版本。使用单链表,当head == NULL 时,您不需要对第一个元素进行单独的操作。拆分时不需要任何新节点,只需重新链接即可。

#include <stdio.h>
#include <stdlib.h>

struct oddeven {
   int data;
   struct oddeven *link;
};

typedef struct oddeven m;

int main(void) {
    int num;
    m *head = NULL, *ptr;

    // read the list and end it with any letter or non-digit
    while(scanf("%d", &num) == 1) {
        ptr = malloc(sizeof *ptr);
        if(ptr == NULL) {
            exit(1);
        }
        ptr->data = num;
        ptr->link = head;
        head = ptr;
    }

    // split the list
    m *x, *odd = NULL, *even = NULL, *next;
    x = head;
    while(x != NULL) {
        next = x->link;           // remember what the link was
        if(x->data % 2 == 0) {
            x->link = even;
            even = x;
        }
        else {
            x->link = odd;
            odd = x;
        }
        x = next;    
    }

    // output the odds
    m *y = odd;
    while (y != NULL) {
        printf("%d\t", y->data); 
        y = y->link;
    }
    printf("\n");

    // output the evens
    m *q = even;
    while (q != NULL) {
        printf("%d\t", q->data);
        q = q->link;
    } 
    printf("\n");
}

节目环节:

1 5 8 3 44 9 0 1 4 3 6 77 3 42 q
1       5       3       9       1       3       77      3
8       44      0       4       6       42

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多