user-0 提问: q的值丢了，求并集的时候

回复:
user: user-1
content: 再供参考：#include <algorithm> #include <iostream> #include <functional> #include <cstring> using namespace std; int main() {     char *Alphabet = "abcdefghijklmnopqrstuvwxyz" ;     char *Vowels   = "aeiou" ;     char *AlphaNum = "0123456789abcdef" ;     char result[45] ;     char *last ;      int lenA  = strlen(Alphabet) ;     int lenV  = strlen(Vowels  ) ;     int lenAN = strlen(AlphaNum) ;      cout << "Alphabet = " << Alphabet << endl ;     cout << "Vowels   = " << Vowels   << endl ;     cout << "AlphaNum = " << AlphaNum << endl ;      cout << "\nusing non-predicate versions" << endl ;      //non-predicate set_difference     last = set_difference(Alphabet, Alphabet+lenA,                           AlphaNum, AlphaNum+lenAN,                           result) ;     *last = 0 ;     cout << "set_difference(Alphabet, AlphaNum) =  " << result << endl ;      //non-predicate set_intersection     last = set_intersection(Alphabet, Alphabet+lenA,                             AlphaNum, AlphaNum+lenAN,                             result) ;     *last = 0 ;     cout << "set_intersection(Alphabet, AlphaNum) =  " << result << endl ;      //non-predicate set_symmetric_difference     last = set_symmetric_difference(Alphabet, Alphabet+lenA,                                     Vowels  , Vowels  +lenV,                                     result) ;     *last = 0 ;     cout << "set_symmetric_difference(Alphabet, Vowels) =  " << result << endl ;      //non-predicate set_union     last = set_union(Alphabet, Alphabet+lenA,                      AlphaNum, AlphaNum+lenAN,                      result) ;     *last = 0 ;     cout << "set_union(Alphabet, AlphaNum) =  " << result << endl ;      cout << "\nusing predicate versions" << endl ;      //predicate set_difference     last = set_difference(Alphabet, Alphabet+lenA,                           AlphaNum, AlphaNum+lenAN,                           result  , less<char>()) ;     *last = 0 ;     cout << "set_difference(Alphabet, AlphaNum) =  " << result << endl ;      //predicate set_intersection     last = set_intersection(Alphabet, Alphabet+lenA,                             AlphaNum, AlphaNum+lenAN,                             result  , less<char>()) ;     *last = 0 ;     cout << "set_intersection(Alphabet, AlphaNum) =  " << result << endl ;      //predicate set_symmetric_difference     last = set_symmetric_difference(Alphabet, Alphabet+lenA,                                     Vowels  , Vowels  +lenV,                                     result  , less<char>()) ;     *last = 0 ;     cout << "set_symmetric_difference(Alphabet, Vowels) =  " << result << endl ;      //predicate set_union     last = set_union(Alphabet, Alphabet+lenA,                      AlphaNum, AlphaNum+lenAN,                      result  , less<char>()) ;     *last = 0 ;     cout << "set_union(Alphabet, AlphaNum) =  " << result << endl ;      return 0 ; }

user: user-1
content: 仅供参考：//不带表头结点的单向链表 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <time.h> #include <locale.h> struct NODE {     int          data;     struct NODE *next; } *head,*p,*q,*s,*p1,*p2,*q1,**ta; int i,k,n,t,m,v,N=10; int main() {     setlocale(LC_ALL,"chs");     srand(time(NULL));      head=NULL;      printf("创建%d个节点的单链表:",N);//创建N个节点的单链表     p=head;     for (i=0;i<N;i++) {         q=(struct NODE *)malloc(sizeof(struct NODE));         if (NULL==q) exit(1);         q->data=rand()%100;//填写0..99的随机值         q->next=NULL;         if (NULL==p) {             head=q;             p=head;         } else {             p->next=q;             p=q;         }     }      //输出整个单链表     s=head;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      k=3;     v=5;     printf("将值为%d的结点插入到单链表的第%d个结点前:",v,k);//将值为v的结点插入到单链表的第k个结点前     n=0;     p=head;     while (1) {         if (NULL==p) {             break;         }         n++;         if (k==1) {             q=(struct NODE *)malloc(sizeof(struct NODE));             if (NULL==q) exit(1);             q->data=5;             q->next=head;             head=q;             break;         } else {             if (k-1==n) {                 q=(struct NODE *)malloc(sizeof(struct NODE));                 if (NULL==q) exit(1);                 q->data=5;                 q->next=p->next;                 p->next=q;                 break;             }         }         p=p->next;     }      //输出整个单链表     s=head;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      k=5;     printf("删除第%d个节点:",k);//删除第k个节点     n=0;     p=head;     while (1) {         if (NULL==p) {             break;         }         n++;         if (k==1) {             q=head;             head=head->next;             free(q);             break;         } else {             if (k-1==n) {                 q=p->next;                 if (q) {                     p->next=q->next;                     free(q);                 }                 break;             }         }         p=p->next;     }      //输出整个单链表     s=head;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      printf("从小到大排序:");//从小到大排序     for (p=head,p1=NULL;p!=NULL;p1=p,p=p->next) {         for (q=p->next,q1=p;q!=NULL;q1=q,q=q->next) {             if (p->data > q->data) {                  //交换data //              printf("swap %02d %02d\n",p->data,q->data); //              t=p->data;p->data=q->data;q->data=t;                  //或者                  //交换next //              printf("swap %02d %02d\n",p->data,q->data);                 if (p==head) {//p是头                     if (p->next==q) {//pq挨着                         head=q;                         p->next=q->next;                         q->next=p;                         q=p;                         p=head;                     } else {//pq不挨着                         head=q;                         p2=p->next;                         p->next=q->next;                         q->next=p2;                         q1->next=p;                         q=p;                         p=head;                     }                 } else {//p不是头                     if (p->next==q) {//pq挨着                         p1->next=q;                         p->next=q->next;                         q->next=p;                         q=p;                         p=p1->next;                     } else {//pq不挨着                         p1->next=q;                         p2=p->next;                         p->next=q->next;                         q->next=p2;                         q1->next=p;                         q=p;                         p=p1->next;                     }                 }                  //输出整个单链表 //              s=head; //              while (1) { //                  if (NULL==s) { //                      printf("\n"); //                      break; //                  } //                  printf("%02d->",s->data); //                  s=s->next; //              } //              getchar();             }         }     }      //输出整个单链表并计算链表长度n     n=0;     s=head;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         n++;         s=s->next;     }      printf("将整个链表逆序:");//将整个链表逆序     if (n>=2) {         p=head;         q=p->next;         p->next=NULL;         while (1) {             q1=q->next;             q->next=p;             p=q;             q=q1;             if (NULL==q) break;         }         head=p;     }      //输出整个单链表     s=head;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      m=4;     n=6;     printf("将单链表中前%d个结点和后%d个结点进行互换:",m,n);//将单链表中前m个结点和后n个结点进行互换,m+n为链表总长     k=0;     p=head;     while (1) {         if (NULL==p) {             break;         }         k++;         if (m==k) {             q=p;         }         s=p;         p=p->next;     }     q1=head;     head=q->next;     s->next=q1;     q->next=NULL;      //输出整个单链表     s=head;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      //释放所有节点     p=head;     while (1) {         if (NULL==p) {             break;         }         q=p->next;         free(p);         p=q;     }      return 0; } //创建10个节点的单链表:08->74->07->23->03->99->31->56->88->16-> //将值为5的结点插入到单链表的第3个结点前:08->74->05->07->23->03->99->31->56->88->16-> //删除第5个节点:08->74->05->07->03->99->31->56->88->16-> //从小到大排序:03->05->07->08->16->31->56->74->88->99-> //将整个链表逆序:99->88->74->56->31->16->08->07->05->03-> //将单链表中前4个结点和后6个结点进行互换:31->16->08->07->05->03->99->88->74->56-> //

user: user-2
content: 有以下几个问题：（我都在代码里注释了） 1.creatsl函数里感觉p=(lnode*)malloc(sizeof(lnode));这句多余的，没必要malloc,malloc以后你就让他指向别的了，内存泄漏！ 2.uns里当链表h2循环一次以后，需要让q重新再指回第一个节点，j也需要置1 3.合并好以后将h2的头结点free掉  #include <stdio.h> #include <stdlib.h> typedef struct NODE{ 	int data; 	struct NODE* next; }lnode,*node;  node creatsl(int A)//创建单链表 { 	node h; 	node p,s; 	int i; 	p=(lnode*)malloc(sizeof(lnode));  	h=(lnode*) malloc(sizeof(lnode));//初始化空表 	h->data=0; 	h->next=NULL;  	p=h; 	for(i=0;i<A;i++) 	{ 		s=(lnode*)malloc(sizeof(lnode)); 		scanf("%d",&s->data); 		p->next=s; 		p=s; 	} 	p->next=NULL; 	return h;   }  /*void insert(node h,int i,int x)//在第i个节点后插入 { node p,s; h->next=p; int j=0; while(j!=i-1) { p=p->next; j++; } s->data=x; s->next=p->next; p->next=s; }*/  void dele_node(node h,int i)//删除第i个节点 { 	node p,s; 	//p=h; 	int j=0; 	p=h;  	while(j!=(i-1)) 	{ 		p=p->next; 		j++; 	} 	s=p->next; 	p->next=s->next; 	free(s); }  node uns(node h1,node h2)//求并集 { 	void dele_node(node h,int i);  	node p,q,m,n; 	int i=1; 	int	j=1;   	p=h1->next; 	q=h2->next; 	 	m=h2->next;  	while(p) 	{ 		q = m;//重新让q指向h2的第一个结点 		j = 1;//j也要置1 		while(q) 		{ 			if((p->data)==(q->data))//执行到此处出现问题 			{ 				q=q->next;//删除前先指向先一下结点，否则的话该结点删除掉以后再运行q=q->next之后再取它的data会产生异常 				dele_node(h2,j); 				j--; 			} 			else 			{ 				q=q->next; 				j++; 			} 		} 		i++; 		n=p; 		p=p->next; 	} 	n->next=h2->next; 	m=h2; 	free(m); 	//dele_node(h1,i); 	return(h1);  }   int main(void) { 	node creatsl(int A); 	//void insert(node h,int i,int x); 	void dele_node(node h,int i); 	node uns(node h1,node h2); 	node p; 	node h1,h2; 	node p1,p2;  	//	node dpre; 	//	int dnum;  	int num_A,num_B;  	printf("请输入集合A的数目："); 	scanf("%d",&num_A); 	while(num_A<1) 	{ 		printf("请输入正整数"); 		printf("请输入集合A的数目："); 		scanf("%d",&num_A);  	} 	h1=creatsl(num_A);  	printf("请输入集合B的数目："); 	scanf("%d",&num_B); 	while(num_B<1) 	{ 		printf("请输入正整数"); 		printf("请输入集合B的数目："); 		scanf("%d",&num_B);  	} 	h2=creatsl(num_B);  	p1=h1->next; 	while(p1) 	{printf("%d,",p1->data); 	p1=p1->next;} 	printf("\n");  	p2=h2->next; 	while(p2) 	{printf("%d,",p2->data); 	p2=p2->next;}//创建并输出两链表 	printf("\n");   	p=uns(h1,h2)->next; 	while(p!=NULL) 	{ 		printf("%d ",p->data); 		p=p->next; 	} 	printf("\n");  	/*printf("请问您需要删除第几个节点："); 	scanf("%d",&dnum); 	while((dnum>num_B)|(dnum<0)) 	{ 	printf("请输入小于%d的正整数：",num_B); 	scanf("%d",&dnum); 	} 	dele_node(h2,dnum); 	dpre=h2->next;  	while(dpre) 	{ 	printf("%d,",dpre->data); 	dpre=dpre->next; 	}*/ 	return(0);  }