逆转顺序表中的所有元素,算法:元素一次对调
Void reverse(int a[], int n){
int i ,t ;
for(i = 0 ; i<n/2 ;i++){
T = a[i];
a[i] = a[n-i-1]
a[n-i-1] = T;
}
}
将单链表反转
list *reserver_list(list *head)
{
//把数据分为单个区域,a为已经反转好的区域,b为正在反转的,c为还未反转
list *a = null,*b= null, *c= head;
while(c){
b = c; //首先把c设置为正在反转
b->next = a;//反转完之后的b,应该放在已经反转好的a之前(反向的原因)
a = b;//b为已经反转好的
c = c->next;//继续反转下一个节点
}
return a;//返回反转好的链表
}
计算链表有多少个节点
unsigned int list_count(list *head)
{
unsigned int count = 0;
while(head){
head = head ->next ;
count ++;
}
return count;
}
删除单链表
list *free_list(list *head){
list *tmp = head;
while(head){
head = head->next;
//通过删除tmp,来删除链表
delete tmp ;
//保存每次的节点为tmp
tmp = head;
}
return null;
}
删除单链表某个节点
list *delete_list(list *head, dataType data)
{
//tmp要删除的节点,pre删除节点的前一个节点
list *tmp = head, *pre = head;
while(tmp){ //循环寻找要删除的节点
if(tmp->data == data){ //找到要删除的节点
if(tmp == head)// 如果是头节点,删除第一个节点,并把头节点设为下一个节点
head = head->next;
else//非第一个节点
pre->next = tmp->next; //被删除的前一个节点的next 设为被删除节点的下一个节点,已实现删除节点
delete tmp;
tmp = NULL; //把指针置为空,避免野指针
break;
}
pre = tmp; //保存前一个节点
tmp = tmp->next; //节点往后移动
}
return head;
}
约瑟夫问题(n个人围成圈报数,报m出列,最后剩下的是几号)循环链表
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node; //定义循环链表的结构体
node * create(int n)//创建循环链表
{
//P为当前节点,head为头结点
node *p = null ; *head;
//申请链表的空间,并赋给头节点
head =(node*)malloc(sizeof(node));
p=head;//第一次当前节点为头结点
node *s;//定义需要创建的节点
int i =1;
if(n!= 0)//创建N各节点,并给N个节点赋值
{
while(i < = n)
{
s = (node*)malloc(sizeof(node));//给要创建的节点申请空间
s->data = i++;
p->next = s;
p=s;//当前节点设为s节点,继续创建
}
//创建完成后,把当前节点指向第一个个节点(头节点的下一个节点)形成循环链表
s->next = head->next ;
}
free(head);//最后删除头节点
return s->next;//最后返回整个循环列表
}
int main(){
int n =41;//41个人
int m = 3;//报3出列
int i;
node *p = create(n);
node *temp ;//临时变量,保存要被珊除的节点
m%=n;//m为2
While( p!=p->next) //循环链表只剩一个元素(自己的下一个节点等于自己)时退出循环(自己绕成一个圈)
{
For( i=1 ; i <m-1 ;i++)
{
P=p->next; //p向后移动m-1个位置,p的下一个节点则为要删除(出列)的节点
}
Printf(“%d->”,p->next->data); //打印被删除的节点
Temp =p->next ;// 被删除的节点赋给temp
P->next =temp ->next ; //p的下一个节点等于被删除节点(temp)的下一个节点
Free( temp) ;//删除temp;
P=p->next; // p指向p的下一个节点,删除一次后,让p往后移动一次,再配合上面for循环里面在循环使p再次指向被删除节点额前一个节点
}
Printf(“%d”,p->data);
Return 0;
}
求二叉树的深度
Int depth(Btree T)
{
Int ldepth, rdepth;
If(T==NULL)
Return 0;
Else{
Ldepth = depth(T->lchild );
Rdepth = depth(T->rchild);
If(ldepth>rdepth)
Return ldepth+1;
Else
Return rdepth+!;
}
}
求二叉树所有带权路径的总和,把左右节点分别看成一个树,递归实现
Typedef struct tree{
Int weight;
Tree *lchild,*rchild;
}tree;
Int tree_weight(tree *root ,int height)
{
If(root==null)
Return 0;
If(root->lchild && root ->rchild)
Return root->weight*height;
else
Return tree_wight(root->lchild,height+1)+tree_weight(root->rchild,height+1); //把左右节点分别看成一个数,递归
}
Int main(){
Tree *root;
Tree_weight(root, 0); //深度从零开始
}
//统计二叉树中节点的个数
Int numofnode( tree *t)
{
If(t==null)
Return 0;
else
Return (numofnode(tree->lchild)+numofnode(tree->rchild)+1);
}
//判断二叉树是否等价
bool isequal(tree *t1 , tree *t2)
{
Int t;
T=0;
If(t1==null && t2==null) t=1;
else
If(t1!==null && t2!=null)
If(t1->data == t2->data) //根节点的值相等
If(isequal(t1->lchild,t2->lchild)) //判断左节点是否相等
T=isequal(t1->lchild,t2->rchild) //左节点相等,则判断右节点是否相等
Return t;
}