位运算
| 运算符 | 说明 |
|---|
| & | 按位与 |
| 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);
}
}
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;
}
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;
}
结构
- 由数目固定的成员构成,各成员可以具有不同的数据类型
- 一个结构变量在内存占有一片连续的存储空间
- 因为各数据成员的类型不同,所以具有特定的定义和访问形式
结构定义
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;
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){
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){
if(pg->next->data=key){
p=pg->next;
pg->next=p->next;
delete p;
p=NULL;
return;
}
}
cout<"there is not key."<<endl;
return;
}