实习四个月—周总结

条件编译

#include <stdio.h>
#if   0
条件编译有3中形式

1、第一种形式:
   (1)第1种结构
     #if    常量表达式
        语句块
     #endif

   (2)第2中结构
     #if    常量表达式
        语句块1
     #else
        语句块2
     #endif
	
	(3) 第3中结构
     #if    常量表达式1
        语句块1
     #elif   常量表达式2
        语句块2
     #elif   常量表达式3
        语句块3
     ...
     #else
	    语句块n
     #endif

2、第二种形式

   (1)第1种结构
    #ifdef   标识符
       语句块
    #endif

   (2)第2种结构
    #ifdef   标识符
       语句块1
    #else
	   语句块2
    #endif


3、第三种形式
   (1)第1种结构
    #ifndef  标识符
       语句块
    #endif

   (2)第2种结构
    #ifndef   标识符
       语句块1
    #else
	   语句块2
    #endif



#endif
#define  WIN     0
#define  LINUX   0
#define  MAC     1
int main01()
{
#if   WIN
	printf("欢迎进入window系统!\n");
#elif   LINUE
	printf("欢迎进入LINUX系统\n");
#elif   MAC
	printf("欢迎进入 MAC系统!\n");
#else
	printf("系统不明确!\n");
#endif
	return 0;
}


int main02()
{

#ifdef   WIN
	printf("欢迎进入windows系统!\n");
#else
	printf("其他系统!\n");
#endif
	return 0;
}




int main()
{
#ifndef WIN
	printf("其他系统!\n");
#else
	printf("欢迎进入window系统!\n");
#endif
	return 0;
}

malloc—free

#include <stdio.h>
#include <stdlib.h>



#if  0
申请内存:  malloc


释放内存:  free


注意事项:
1、内存越界

2、内存泄露:  不用的内存空间,要及时释放

3、二次释放:  不要二次释放

4、释放完内存之后,要记得将指针指向NULL

#endif

int* get_mem(int n)
{
	int *p = NULL;
	p = (int*)malloc(sizeof(int)*n);
	if(NULL == p)
	{
		printf("malloc error! %s, %d!", __FILE__, __LINE__);
		return NULL ;
	}

	return p;
}

void set_data(int *p, int n)
{
	for(int i=0; i<n; i++)
	{
		p[i] = i+1;
	}
	return;
}


void  show_data(int *p, int n)
{
	for(int i=0; i<n; i++)
	{
		printf("%d ", *(p+i));
	}
	printf("\n");
}



void  free_mem(int *p)
{
	free(p);
}


int main()
{
	int *p_mem = NULL;

	p_mem = get_mem(5);

	set_data(p_mem, 5);

	show_data(p_mem, 5);

	free_mem(p_mem);
	p_mem = NULL;

//	free(p_mem);  //二次free, 有问题!!!
	return 0;
}

链表

#include <stdio.h>
#include <stdlib.h>

struct node
{
	int data;      //数据域
	struct node *next;  //指针域
};



struct node *create_list_by_tail()
{
	struct node *head = NULL; //头指针
	struct node *pnew = NULL; //申请新的节点
	struct node *tail = NULL; //保存最后一个节点的地址

	int x;
	scanf("%d", &x);
    while(getchar()!='\n');

	while(x)
	{
		//1、创建新的节点
		pnew = (struct node*)malloc(sizeof(struct node));
		if(NULL == pnew)
		{
			printf("malloc error, %s,%d\n", __FILE__, __LINE__);
			exit(-1);
		}
		pnew->data = x;
		pnew->next = NULL;

		//2、加入链表
		if(head == NULL)
		{
			head = pnew;
			tail = pnew;
		}
		else
		{
			tail->next = pnew;
			tail = pnew;
		}
	
		scanf("%d", &x);
        while(getchar()!='\n');
	}
	return head;
}



struct node *create_list_by_head()
{
	struct node *head = NULL, *pnew = NULL;

	int x;
	scanf("%d", &x);
	while(getchar()!='\n');

	while(x)
	{
		//1、创建新的节点,并赋值
		pnew = (struct node*)malloc(sizeof(struct node));
		if(NULL == pnew)
		{
			printf("malloc error, %s, %d\n", __FILE__,__LINE__);
			exit(-1);
		}
		pnew->data = x;
		pnew->next = NULL;


		//2、加入链表
#if   0
		if(NULL == head)
		{
			head = pnew;
		}
		else
		{
			pnew->next = head;
			head = pnew;
		}
#endif
		
		pnew->next = head;
		head = pnew;
		
		
		scanf("%d", &x);
	    while(getchar()!='\n');
	}
	return head;
}


void  show_list(struct node *head)
{
	if(NULL == head)
	{
		printf("list empty!\n");
		return ;
	}


	struct node *p = head;
	while(p!=NULL)
	{
		printf("[%d|%p]-->", p->data, p->next);
		p = p->next;
	}
	printf("\n");
}





int main()
{
	struct node * head = NULL;

	//head = create_list_by_tail();
	
	head = create_list_by_head();

	show_list(head);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值