#include<stdio.h>
#include<math.h>
void answer(int a,char name[]); 该函数不同与int answer等函数,它不需要return输出。与printf连用,可节省主函数空间
int main(void)
{
char name[80]; 可以与函数中的字母一样。要将字符串输入到函数中,必
需要有‘[]’,及char name【】
int a;
printf("Hey,what's your name?\n");
gets(name); 输入字符串
printf("Ok,%s,how old are you?",name); 可以有这种方式输出字符串,结尾出不
用带‘[]’
scanf("%d",&a);
answer(a,name); 函数的引用,不需要前缀
return 0;
}
void answer(int a,char name[])
{
if(a>=25)
{
printf("Äã¿ÉÒÔ×öÈκκϷ¨µÄÊÂÇéÁË");
puts(name);
}
else if(a<25&&a>=18)
{
printf("Äã²»ÄÜ×â³µ");
puts(name);
}
}
链表
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
char name[80];//保存的数据
float money;//保存的数据
float move;//保存的数据
float pay;//保存的数据
float sum;//保存的数据
struct Node *next;
}Node;
Node* create();//创建链表 or 创建头节点
void insert(Node* head,int n);//增加新节点
void print(Node* head);//输出链表指定数据
int main()
{
int n,i;
Node* head;
head=create();
scanf("%d",&n);
insert(head,n);
print(head);
return 0;
}
Node* create(){//创建链表 or 创建头节点
Node* head=malloc(sizeof(Node));
head->next=NULL;
return head;
}
void insert(Node* head,int n){//增加新节点
int i;
Node* tail;
for(i=0;i<n;i++)
{
Node* newNode=malloc(sizeof(Node));
scanf("%s%f%f%f",newNode->name,&newNode->money,&newNode->move,&newNode->pay);
newNode->sum=newNode->money+newNode->move-newNode->pay;
if(head->next==NULL)
{
head->next=newNode;
}
else
{
tail->next=newNode;
}
tail=newNode;
}
}
void print(Node* head){//输出链表指定数据
Node* tail;
tail=head->next;
while(tail!=NULL)
{
printf("%s %.2f\n",tail->name,tail->sum);
tail=tail->next;
}
}
指针与它所指向的结点是一体的
二叉树
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
typedef struct Node{
char data;
struct Node *lchild;
struct Node *rchild;
}Node;
Node* init();//创建树
Node* insert(Node* BT);//增加分支
void print(Node* BT); //层次遍历
void q_print(Node* BT);//前序遍历
int main()
{
Node* BT;
BT=init();
BT=insert(BT);
print(BT);
return 0;
}
Node* init(){
Node* BT=malloc(sizeof(Node));
BT->data=0;
BT->lchild=NULL;
BT->rchild=NULL;
return BT;
}
Node* insert(Node* BT){
char data;
scanf("%c",&data);
if(data=='#')
{
return NULL;
}
else
{
Node* new_BT=malloc(sizeof(Node));
new_BT->data=data;
new_BT->lchild=insert(new_BT->lchild);
new_BT->rchild=insert(new_BT->rchild);
return new_BT;
}
}
void print(Node* BT){
Node* a[1000];
Node* p;
int head=0,tail=0;
a[tail]=BT;
tail++;
while(head!=tail)
{
p=a[head];
head++;
printf("%c",p->data);
if(p->lchild!=NULL)
{
a[tail]=p->lchild;
tail++;
}
if(p->rchild!=NULL)
{
a[tail]=p->rchild;
tail++;
}
}
}
void q_print(Node* BT){
if(BT==NULL)
{
return NULL;
}
printf(" %c",&BT->data);
q_print(BT->lchild);
q_print(BT->rchild);
}
根据前序和中序遍历还原二叉树:
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
char data;
struct Node *lchild;
struct Node *rchild;
}Node;
Node* insert(char a[],char b[],int n);
Node* insert(char a[],char b[],int n){
if(n==0)
{
return NULL;
}
int i,order;
for(i=0;i<n;i++)
{
if(a[0]==b[i])
{
order=i;
break;
}
}
Node* bt=malloc(sizeof(Node));
bt->data=a[0];
bt->lchild=insert(a+1,b,order);
bt->rchild=insert(a+1+order,b+1+order,n-1-order);
return bt;
}
int print(Node* bt);
int print(Node* bt){
if(bt==NULL)
{
return 0;
}
int n,m;
n=print(bt->lchild)+1;
m=print(bt->rchild)+1;
return n>m ? n:m;
}
int main()
{
int n;
char a[1000];
char b[1000];
scanf("%d",&n);
scanf("%s",a);
scanf("%s",b);
Node* bt=malloc(sizeof(Node));
bt=insert(a,b,n);
int m;
m=print(bt);
printf("%d",m);
return 0;
}