暴力枚举之求子集

求子集

编写程序,输入n个整数,求出它们的非空子集(组合数)
例如,n=3,分别为1,2,3三个整数,子集如下
1
2
3
1,2
2,3
1,3
1,2,3

解答:

void sub_set(int n, int* number){
	int max = (1<<n)-1;
	for(int i=1;i<=max;i++){
		for(int j=0;j<n;j++){
			if(((1<<j)&i)){  //切记不要写结果==1;这个是左移运算符而不是右移运算符!!!
				printf("%d",number[j]);
			}
		}
		printf("\n");
	}
}

链表之逆转链表

链表之逆转链表

传入一个Node指针,将它指向的链表进行逆置,返回逆置后的新链表,注意操作过程中不要额外申请空间,即在传入的链表中进行节点逆置.

代码:

Node * reverse_list(Node *head){

	Node * pre=NULL;
	Node * cur=head;
	while(cur!=NULL){
		Node * back = cur->next;
		cur->next=pre;
		pre=cur;
		cur=back;
	}
	return pre;

}

链表之归并有序链表

链表之归并有序链表

传入两个Node指针ptr1与ptr2,它们指向的链表中的元素有序递增,将它们合并为一个新的有序链表newptr,注意操作过程中不要额外申请空间.

代码:

Node *merge_list(Node *ptr1,Node *ptr2){

	if(ptr1==NULL&&ptr2==NULL)
		return NULL;
	if(ptr1==NULL&&ptr2!=NULL)
		return ptr2;
	if(ptr2==NULL&&ptr1!=NULL)
		return ptr1;
	Node * base,*other;
	if(ptr1->data>=ptr2->data){
		base=ptr2;
		other=ptr1;
	}else{
		base=ptr1;
		other=ptr2;
	}

	//Node *current = NULL;

	Node *current=base;
	Node * new_ptr=base;
	base=base->next;

	while(base!=NULL&&other!=NULL){
	if(base->data<=other->data){
		current->next=base;
		current=base;
		base=base->next;
	}else{
		current->next=other;
		current=other;
		other=other->next;
	}
	}
	if(other!=NULL){
		current->next=other;
	}else if(base!=NULL){
		current->next=base;
	}
	return new_ptr;

}

链表之带环的链表

链表之带环的链表

1.首先判断是否是带环的链表
2.找到环点

判断是否带环

传入一个Node指针,判断它指向的链表是否有环,有环返回1,无环返回0

int is_list_has_circle(Node *ptr){

	if(ptr==NULL)
		return NULL;
	Node * fast= ptr;
	Node * slow=ptr;

	while(fast!=NULL){
		
		if(fast->next!=NULL){
			fast=fast->next->next;
		}else{
			return 0;
		}
		slow=slow->next;

		if(slow==fast){
			return 1;
		}
		
	}

	return 0;
}

找到环点

Node* find_circle_node(Node *ptr){

	if(ptr==NULL)
		return NULL;
	Node * fast= ptr;
	Node * slow=ptr;

	Node * head=ptr;

	while(fast!=NULL){
		
		if(fast->next!=NULL){
			fast=fast->next->next;
		}else{
			return NULL;
		}
		slow=slow->next;

		if(slow==fast){
			slow=head;
			while(fast!=NULL){

				fast=fast->next;
				slow=slow->next;

				if(fast==slow){

					return slow;
				}
			}
		}
	}

	return NULL;


}

链表的基本操作

链表的基本操作

链表的基本操作包括创建、销毁、插入、删除、查找、打印。这里附上代码:

list.h

typedef struct Node {
	int data;
	Node *next;

} PNode;

typedef struct List{

	Node head;
	Node *last;
} List;

void list_init(List *list);
void list_destroy(List *list);
void list_insert(List *list,int data);
void list_erase(List *list,int data);
Node * list_find(List *list,int data);
void list_print(List *list);
int get_list_max(List *list);
int get_list_count(List *list);
int get_list_min(List *list);

list.c


#include "list.h";
#include <stdio.h>;
#include<stdlib.h>;

void list_init(List *list){

	Node node;
	node.data=0;
	node.next=NULL;
	list->head=node;
	list->last=&list->head;


}


void list_insert(List *list,int data){

	Node *node = (Node*)malloc(sizeof(Node));
	node->data=data;
	node->next=NULL;
	list->last->next=node;
	list->last=node;
	
	

}


void list_print(List *list){
	Node * cur =list->head.next;
	printf("head");
	while(cur!=NULL){
		printf("->[%d]",cur->data);
		cur=cur->next;
	}
	printf("\n");

}

void list_print(Node *p){
	while(p!=NULL){
		printf("->[%d]",p->data);
		p=p->next;
	}
}

void list_erase(List *list, int data){
	Node * pre = &list->head;
	Node * cur =list->head.next;

	while(cur!=NULL){
		if(cur->data==data){
			Node *del = cur;
			pre->next=cur->next;
			free(del);
			cur=pre->next;

		}else{
			pre=cur;
			cur=cur->next;

		}


	}


}

Node * list_find(List *list,int data){

	Node *cur = list->head.next;
	while(cur){

		if(cur->data==data){
			return cur;
		}
		cur=cur->next;
	}
	return NULL;

}

int get_list_max(List *list){

	Node *cur = list->head.next;
	int max=0;
	while(cur){

		if(cur->data>=max){
			max=cur->data;
		}
		cur=cur->next;
	}
	return max;
}

int get_list_min(List *list){

	Node *cur = list->head.next;
	int min;
	if(cur)
		min=cur->data;
	while(cur){

		if(cur->data<=min){
			min=cur->data;
		}
		cur=cur->next;
	}
	return min;
}

int get_list_count(List *list){

	Node *cur = list->head.next;
	int count=0;
	
	while(cur){

		
			count+=cur->data;
		
		cur=cur->next;
	}
	return count;

}

void list_destroy(List *list){
	Node * cur = list->head.next;

	while(cur){
	Node * del= cur;
	cur=cur->next;
	free(del);
	}
	list_init(list);
}

Enter text in Markdown. Use the toolbar above, or click the ? button for formatting help.

链表的倒数第k个节点

链表的倒数第k个节点

传入一个Node指针,求出它指向的链表的倒数第k个节点,假如k超过了链表节点的个数,直接返回第一个节点

代码:

Node *find_buttom_kth(Node * ptr,int k){
	if(ptr==NULL||k<=0)
		return NULL;

	Node * fast=ptr;
	Node * slow=ptr;

	while(fast!=NULL&&k--){

		fast=fast->next;
	}

	if(fast==NULL){
		return slow;
	}

	while(fast){
		slow=slow->next;
		fast=fast->next;
	}

	return slow;

}