Linux下的C语言学习笔记(3)
2016-08-06 11:36
393 查看
------------------------------day12---动态数据结构-静态链表-动态链表---------------------------------------
1,静态链表:
#include<stdio.h>
struct weapon{
int price;
int atk;
struct weapon * next;
};
int main(){
struct weapon a,b,c,*head;
a.price = 100;
a.atk = 100;
b.price = 200;
b.atk = 200;
c.price = 300;
c.atk = 300;
head = &a;
a.next = &b;
b.next = &c;
c.next = &NULL;
truct weapon * p;
p = head;
while(p!=NULL){
printf("%d,%d",p->atk,p->price);
p = p->next;
}
return 0;
}
2,动态链表:
#include<stdio.h>
#include<malloc.h>
struct weapon{
int price;
int atk;
struct weapon * next;
};
struct weapon * creat(){
struct weapon * head;
struct weapon * p1,*p2;
int n = 0;
//malloc分配内存块的函数sizeof判断数据类型长度符
p1=p2=(struct weapon *)malloc(sizeof(struct weapon));
scanf("%d,%d",&p1->price,&p1->atk);
head = NULL;
while(p1->price!=0){
n++;
if(n==1) head=p1;
else p2->next = p1;
p2=p1;
p1=(struct weapon *)malloc(sizeof(struct weapon));
scanf("d,%d",&p1->price,&p1->atk);
}
p2->next=NULL;
return (head);
}
int main(){
struct weapon *p ;
p=creat();
printf("%d,%d\n",p->price,p->atk);
return 0;
}
------------------------------day12---递归调用---------------------------------------
//函数自己调用自己
#include<stdio.h>
int func(int n){
int r;
if(n<0){
printf("data error\n");
}else if(n==0|| n==1){
r=1;
}else
r = n*func(n-1);
return r
}
int main(){
int n;
printf("please input n\n");
scanf("%d",&n);
int result = func(n);
printf("result is :\n",result);
return 0;
}
1,静态链表:
#include<stdio.h>
struct weapon{
int price;
int atk;
struct weapon * next;
};
int main(){
struct weapon a,b,c,*head;
a.price = 100;
a.atk = 100;
b.price = 200;
b.atk = 200;
c.price = 300;
c.atk = 300;
head = &a;
a.next = &b;
b.next = &c;
c.next = &NULL;
truct weapon * p;
p = head;
while(p!=NULL){
printf("%d,%d",p->atk,p->price);
p = p->next;
}
return 0;
}
2,动态链表:
#include<stdio.h>
#include<malloc.h>
struct weapon{
int price;
int atk;
struct weapon * next;
};
struct weapon * creat(){
struct weapon * head;
struct weapon * p1,*p2;
int n = 0;
//malloc分配内存块的函数sizeof判断数据类型长度符
p1=p2=(struct weapon *)malloc(sizeof(struct weapon));
scanf("%d,%d",&p1->price,&p1->atk);
head = NULL;
while(p1->price!=0){
n++;
if(n==1) head=p1;
else p2->next = p1;
p2=p1;
p1=(struct weapon *)malloc(sizeof(struct weapon));
scanf("d,%d",&p1->price,&p1->atk);
}
p2->next=NULL;
return (head);
}
int main(){
struct weapon *p ;
p=creat();
printf("%d,%d\n",p->price,p->atk);
return 0;
}
------------------------------day12---递归调用---------------------------------------
//函数自己调用自己
#include<stdio.h>
int func(int n){
int r;
if(n<0){
printf("data error\n");
}else if(n==0|| n==1){
r=1;
}else
r = n*func(n-1);
return r
}
int main(){
int n;
printf("please input n\n");
scanf("%d",&n);
int result = func(n);
printf("result is :\n",result);
return 0;
}
相关文章推荐
- Linux socket 初步
- Linux Kernel 4.0 RC5 发布!
- linux lsof详解
- linux 文件权限
- Linux 执行数学运算
- 10 篇对初学者和专家都有用的 Linux 命令教程
- 如何组织构建多文件 C 语言程序(二)
- Linux 与 Windows 对UNICODE 的处理方式
- Ubuntu12.04下QQ完美走起啊!走起啊!有木有啊!
- 解決Linux下Android开发真机调试设备不被识别问题
- 运维入门
- 运维提升
- Linux 自检和 SystemTap
- Ubuntu Linux使用体验
- [C/C++]反转链表
- c语言实现hashmap(转载)
- Linux 信号signal处理机制
- linux下mysql添加用户