异想天开

What's the true meaning of light, Could you tell me why

链表反转-链表操作

日期:2015-07-14 14:49:15
  
最后更新日期:2015-07-27 12:25:09
node.h
[code lang="cpp"]
#ifndef __NODE_H__
#define __NODE_H__
struct node{
char data;
struct node *next;
};

struct node * reverse(struct node *tree);
void add_node(struct node **root,char v);

#endif
[/code]
node.c
[code lang="cpp"]
#include<stdio.h>
#include<stdlib.h>
#include "node.h"

struct node * reverse(struct node *tree)
{
struct node *p = NULL,*tmp = NULL;

for (; tree != NULL;)
{
tmp=tree;
tree = tree->next;
tmp->next = p;
p=tmp;
}
return p;
}

int tree_equ(struct node *t1, struct node *t2)
{
int ret = 1;
if ((t1 == NULL && t2) || (t2 == NULL && t1)){
return 0;
}
for (; t1 != NULL; ){
if (t1->data != t2->data){
ret = 0;
break;
}
t1 = t1->next;
t2 = t2->next;
}
return ret;
}

void add_node(struct node **root,char v){
struct node *p = NULL;
p = (struct node*)malloc(sizeof(struct node));
p->data=v;
p->next=*root;
*root=p;
}

void print_tree(struct node *tree)
{
struct node *tmp = tree;
for(;tmp!=NULL;){
printf("[%d %p] ",tmp->data,tmp->next);
tmp=tmp->next;
}
printf("\n");
}
[/code]

单元测试文件略