满意答案
ooujfe
2014.02.08
采纳率:53% 等级:12
已帮助:40324人
#include "windows.h"
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
struct Node
{
int num;
struct Node *next;
};
struct Node *CreateLink(int n)
{
int i=0;
struct Node *head, *p;
head = (struct Node *)malloc(sizeof(struct Node));
head->num=0;
head->next=NULL;
srand((unsigned)time(NULL));
for (i=0; i
{
p = (struct Node *)malloc(sizeof(struct Node));
p->num = rand()%100;
p->next = head->next;
head->next = p;
}
return head;
}
void PrintLink(struct Node *head)
{
struct Node *p=head->next;
printf("\n");
while (p != NULL)
{
printf("%d ",p->num);
p = p->next;
}
}
struct Node *DeleteLink(struct Node *h, int num)
{
struct Node *head = h, *p,*q;
p = head;
while (p->next != NULL)
{
if (p->next->num == num)
{
q = p->next;
p->next = q->next;
free(q);
}
p = p->next;
if (p == NULL)
{
break;
}
}
return head;
}
struct Node *DeleteSameNum(struct Node *h1,struct Node *h2)
{
struct Node *p,*head1=h1;
p = h2->next;
while (p != NULL)
{
head1 = DeleteLink(head1, p->num);
p = p->next;
}
return head1;
}
int main()
{
struct Node *head1, *head2;
head1 = CreateLink(20);
PrintLink(head1);
Sleep(1000);
head2 = CreateLink(20);
PrintLink(head2);
head1 = DeleteSameNum(head1,head2);
printf("\n删除相同数据后:");
PrintLink(head1);
printf("\n");
return 0;
}
00分享举报