C++——集合与结构

本文介绍了位运算符在实现集合运算中的应用,如按位与、按位或、按位异或等,以及如何利用这些运算符实现集合的并集、交集和差集等操作。同时,讲解了结构体的概念,包括结构体的定义、访问方式以及单向链表的创建与遍历。此外,还展示了如何在有序链表中进行插入和删除操作。

位运算

运算符说明
&按位与
l按位或
^按位异或
<<左移
>>右移
~按位取反

· 两次异或运算,不回改变原理的值

集合运算

运算(默认A为左操作数)说明
并集A和B全部元素
交集A和B共同元素
属于A但不属于B
包含A中所有元素在B中
补集全集不在中的所有元素
属于x在A中
空集集合中没有元素

使用位运算实现集合运算

集合运算对应的位运算
并集AlB
交集A&B
A&(~(A&B))
包含AlB==B
补集~A
属于l<<(x-1)&A==1<<(x-1)
空集A==0
//输入集合元素
void setPut(unsigned &S){
	unsigned x;
	cin>>x;
	while(x){
	putX(S,x);
	}
}
//输出集合S中的全部元素
void setDisplay(unsigned S){
	unsigned c;
	unsigned bitMask=1;
	if(NULL(S){
	cout<<"{}\n";
	return;
	}
	cout<<"{";
	for(c=1;c<32;c++){
	if(S&&bitMask)
		cout<<c<<",";
	bitMask<<=1;
	}
	cout<<"\b\b}\n";
	return;
}
//元素x并入集合S中
unsigned putX(unsigned &S,unsigned x){
	unsigned bitMask=1;
	bitMask<<=x-1;
	S|=	bitMask;
	return S;
}
//求并集
unsigned Com(unsigned A,unsigned B)
{ return A|B };
//求交集
unsigned setInt(unsigned A,unsigned B)
{ return A&B };
//求差集
unsigned setDif(unsigned A,unsigned B)
{ return A&(~(A&B)); }
//判蕴含
bool Inc(unsigned A,unsigned B){ 	
	if((A|B)==B) return true;
	else return false;
}
//判属于
bool In(unsigned S,unsigned x){
	unsigned bitMask==1;
	bitMask<<=x-1;
	if(S&bitMask)	return true;
	else return false;
}
//判空集
bool Null(unsigned S){
	if(S) return false;
	return trus;
}

结构

  1. 由数目固定的成员构成,各成员可以具有不同的数据类型
  2. 一个结构变量在内存占有一片连续的存储空间
  3. 因为各数据成员的类型不同,所以具有特定的定义和访问形式

结构定义

struct Employee1{
	char name[10];
	long code;
	double salary;
	char *address;
	char phone[20];
}worker1,worker2,*Emp;

结构访问

· 数据成员必须在结构变量说明后才有存储意义

	Employee2 secretary;//结构普通变量
	Employee2 *p;//结构指针变量
	secretary.name;
	*p.name;
	p->name;

单向链表的创建与遍历

struct Node{
	datatype data;
	Node *next;
};
//创建单向链表
void CreateList(Node * &head){
	Node *s,*p;//p指向链表尾部
	s=new Node;
	cin>>s->data;
	while(s->data!=0){
		if(head==NULL)
			head=s;
		else
			p->next=s;
			p=s;
			s=new Node;
			cin>>s->data;
	}
	p->next=NULL;
	delete s;
	s=NULL;
	return;
}
//遍历链表
void ShowList(Node *head){
	while(head){
	cout<<head->data<<'\t';
	head=head->next;
	}
	cout<<endl;
}

有序链表的插入与删除

struct List{
	int data;
	List *next;
};
//把数据插入有序链表
void insert(List * &head,int num){
	List *s,*p,*q;
	s=new List;
	s->data=num;
	s->next=NULL;
	if(head==NULL){
		head=s;
		return;
	}
	if(head->data>s->data){
		s->next=head;
		head=s;
		return;
	}
	for(q=head,p=head->next;p;q=p,p=p->next){//在p之前插入
		if(p->data>s->data){
			s->next=p;
			q->next=s;
			return;
		}
	q->next=s;//插在链表最后
	return;
}
//删除
void del(List *&head,int key){
	List *p;
	if(!head)
	{cout<<"List null!\n"; return;}
	if(head->data==key){
		p=head;
		head=head->next;
		delete p;
		p=NULL;
		return;
	}
	for(List *pg=head;pg->next;pg=p->next){//删除pg
		if(pg->next->data=key){
			p=pg->next;
			pg->next=p->next;
			delete p;
			p=NULL;
			return;
		}
	}
	cout<"there is not key."<<endl;
	return;
}
			
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值