将节点移到数组的末尾,并删除其在C中的重复项

问题描述 投票:0回答:1

我是C语言的学生,我需要编写一个函数:

  1. 向用户询问N个整数的数量
  2. 询问这些整数
  3. 将它们反向存储到列表中
  4. 打印我们存储的内容
  5. 要求从此列表中搜索特定的整数
  6. 找到数字后,必须删除所有重复的数字,并且将找到的第一个数字移到末尾

例如如果用户提供5个数字(2、2x,3、2、4),则列表为:

4 2 3 2 2

然后,用户将给出数字2,而没有重复的新列表将是:

4 2 3

并且在将这个数字移到末尾之后:

4 3 2

struct listNode * removeDupl_and_PushBack(struct listNode * head, int number)
 struct listNode *p2, *p1, *l;
 l=*head;
 p1=l;

 if(l->next!=NULL && l->value!=n){
         puts("We found the number");
            p1=l;
            l=l->next;
        if(l->next==NULL && l->value!=n){
}


if (l->next==NULL){
        printf("The number can't be moved")
        return 1;}


if(l->next!=NULL && l->next->value == n){
            p2=*node;
    do{
        p2=p2->next;

        }while(p2->next!= NULL);
       p1->next=l->next;
       p2->next=l;
       l->next=NULL;

    }

struct list* reverse(struct list *head )
{
        struct list *next;
        struct list *current ;
        struct list *result = NULL;

        current = head;

        while(current != NULL )
        {
            next = current->next;
            current->next = result;
            result = current;
            current = next;
        }
        return (result);
}

void removeDuplicates(struct listNode *start)
{
    struct listNode *ptr1, *ptr2, *dup;
    ptr1 = start;

    /* Pick elements one by one */
    while (ptr1 != NULL && ptr1->next != NULL)
    {
        ptr2 = ptr1;

/* Compare the chosen element with the rest of the elements */
        while (ptr2->next != NULL)
        {
            /* If duplicate then delete it */
            if (ptr1->data == ptr2->next->data)
            {
                dup = ptr2->next;
                ptr2->next = ptr2->next->next;
                delete(dup);
            }
            else
                ptr2 = ptr2->next;
        }
        ptr1 = ptr1->next;
    }
}

int main()
{
    struct listNode *head,*p1;
    int amount, a[100], n, i=0;

    head = NULL;

    printf("\n Give me your desirable amount of integers for the list: \n");
    scanf("%d", &amount);
    head=(struct listNode*)malloc(amount * sizeof(struct listNode));

    printf("\n Give me %d of numbers: \n", amount);

     for(i=0; i<amount; i++)
      {
      scanf("%d",&a[i]);
      }
      printlist(head);


      head = reverse(head);

                printf("List after reverse:  ");
                printList(head);
                printf("\n\n");


   printf("\n Give me the number you want to search; \n");
        scanf("%d",&n);
        removeDuplicates(head,n);

        return 0;
  }
c linked-list duplicates singly-linked-list function-definition
1个回答
0
投票

您在这里。

© www.soinside.com 2019 - 2024. All rights reserved.