两个双向链表删除相同元素

参考:http://hi.baidu.com/beyond748/blog/item/cdeefb1ce3638a8d87d6b6c7.html/cmtid/6145d62fdd4a92361f3089e8


#include "stdafx.h"
//#include "list.h"
#include
#include
using namespace std;

typedef struct node{
int data;
struct node *front, *next;
}node, *linkHead;
//记录删除的元素
int delCnt=0;
int delTmp[16];

void creatList(linkHead *head, int ary[], int n){
int c=0;
linkHead p;
// head;
linkHead h= (linkHead)malloc(sizeof(node));
h->next=NULL;
h->front=NULL;
h->data=n;
*head = h;


while(cp = (linkHead)malloc(sizeof(node));
h->next = p;
p->front= h;
p->data = ary[c++];
h = p;
p->next = NULL;
}
}

void printList(const linkHead head)
{
linkHead p=head->next;
while(p){
printf("%d\n", p->data);
p = p->next;
}
}

int delNode(linkHead *head, int val, int save)
{
linkHead pNode; 
linkHead p = (*head)->next;
while (p)
{
if (p->data==val) {
if (save==1) delTmp[delCnt++]=p->data;
if (p->next){
pNode = p->next;
p->front->next=p->next;
p->next->front=p->front;
free(p), p=NULL;
p = pNode;
}else{
p->front->next=NULL;
free(p), p=NULL;
}

}else
p = p->next;
}


return 1;
}

void delSame(linkHead *head1,linkHead *head2){
linkHead p = (*head1)->next;
while(p){
delNode(head2, p->data, 1);
p=p->next;
}

for(int i=0; idelNode(head1, delTmp[i], 0);
}
}

void main()
{
int aa[]={1,2,3,4,5,6};
int bb[]={3,4,5,6,7,8,9,10,11,4,2};
linkHead list1,list2;
creatList(&list1,aa, sizeof(aa)/sizeof(aa[0]));        
creatList(&list2,bb, sizeof(bb)/sizeof(bb[0]));
//delSame(&list2,&list1);
delSame(&list1,&list2);

printf("\n");
printList(list1);
printf("\n");
printList(list2);
}

你可能感兴趣的:(两个双向链表删除相同元素)