我是C语言的学生,我需要编写一个函数:
例如如果用户提供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;
}
您在这里。