宏定义,函数指针,链表的删除,链表函数指针栈综合应用

指针:
  1、指针与变量
  2、指针与数组(一维数组,二维数组)
  3、指针与结构体
  4、指针与函数的关系
  5、函数指针
    定义变量:类型 变量名;
    *程序的入口地址:也就是函数的内存中的地址
        返回值 (*pfun)(形参)
程序区:数据区(堆区,静态区,动态区)     代码区
    调用函数:
            函数名(实参)    ;  静态调用:在编译时就已经确定调用函数


宏:(一种标准)为程序提供方便

1、宏定义:简单的替换
  意义:(在代码中,大量存中同义字符串需要修改。)
  1.1无参数宏:
  定义:  #define 宏名   字符串
       将字符串以替宏名进行替代
注:为了与高级语言中的标识符区别,宏名都要大写。
  1.2有参数宏
    定义:  #define MAX(x)   x 
    
2、宏选择:在预编译时,来选择指定的语句。
   1、当宏名已经定义时,编译器将下列语句添加到代码中。否则不添加
    #ifdef  宏名    
    。。。
    #endif
   2、双分支:
    #ifdef  宏名
    ....
    #else
    ...
    #endif
  应用:防止头文件重复的包含
    1、当宏名已经定义时,编译器将下列语句添加到代码中。否则添加
    #ifndef  宏名    
    。。。
    #endif
    
3、链式线性表:1:1的联系
  1、增加:头插法
           尾插法
  2、链表栈:通过链表来实现栈的功能(FILO)    

   宏定义 : 

#include<stdio.h>
#define A 100
#define MAX(a) a
#define LOWORO(a) (short)(a&0xffff)
#define HIWORD(a) (short)((a>>16)&0xffff)
// 0000 0000 0000 0110 0000 0000 0010 1010
#define PI 3.1415926
#define AREA(r) PI*r*r

#define SUM(x) (x)*(x)
void main()
{
	printf("%d\n",A);
	printf("%d\n",MAX(100));//MAX(100)
//求int(4Byte)中低字节(----)
	int a=65537;
	printf("%d\n",(short)(a&0xffff));
	int b=65538;
	printf("%d\n",LOWORO(65538));//(short)(65538&0xffff)
//求高两字节
	int c=65537;
	printf("%d\n",HIWORD(c));
//求园的面积:
	printf("%f\n",AREA(2));//3.14*2*2
//乘积
	printf("%d\n",SUM(2+2));//#define SUM(x) x*x  时  2+2*2+2
}

  宏选择 : 

#include<stdio.h>

#define __ABC__
void main()
{
//宏选择:
#ifdef __ABC__
	printf("exit __ABC__\n");
#else
	printf("don`t exit __ABC__\n");	
#endif 	
}

  函数指针 : 

#include<stdio.h>
void fun(int a)
{
	printf("%d\n",a);
}

void main()
{
//定义变量:类型 变量名
	int a;	//存放整型值
//定义整型指针变量:存放整型变量的地址
	int* pa=&a;
//调用函数:
	fun(1);
	fun(2);

//定义函数指针变量:存放函数的入口地址
	 void (*pfun)(int);//pfun: void 函数名(int)

//调用函数:函数名(100)
	fun(100);	//fun就是程序的入口地址  静态调用:在编译时就已经确定调用 
	pfun(200);	//动态调用:在编译时无法确定那个函数,只能在运行时才能

}

  函数指针 :

#include<stdio.h>

//定义函数
//函数名就是一个函数的入口地址
void fun(int a)
{
	printf("%d\n",a);
}
//全局
int b=200;
void (*pfun2)(int)=fun;

struct s
{
	int a;
	int* pa;//指针变量
	void (*pfun3)(int);//定义函数指针变量
};

void main()
{
	void (*pfun)(int)=fun;
//调用函数:函数名(实参)
	fun(1);
	pfun(20);
	pfun2(300);
//定义变量:
	struct s abc={100,NULL,fun};
	printf("%d\n",abc.a);
	abc.a=100;
	abc.pfun3(300);
}
扫描二维码关注公众号,回复: 2677891 查看本文章

链表的删除 :

  首先需要写一个 link.h 的头文件 .

//前置声明
struct nd;
//重命名:
typedef int dtype;
//重命名:struct nd 变量类型
typedef struct nd  node;
typedef struct nd* pnode;//重命名:struct nd*  指针变量
struct nd
{
//数据域
	dtype data;
//指针域:
	pnode next;			
};

  然后就是主程序文件 .

#include<stdio.h>
#include "link.h"
#include<stdlib.h>
#include<stdbool.h>

bool insert(/*node..*/pnode* pph,dtype d)
{
//1分配空间
	pnode pnew=malloc(sizeof(node));	
	if(NULL==pnew)
		return false;	
	pnew->data=d;
//2修改指向域:
	pnew->next=*pph;
	*pph=pnew;

	return true;
	
}
void list(pnode ph)
{
	while(ph!=NULL)
	{
		printf("%d\n",ph->data);
		ph=ph->next;
	}
}
//删除节点:
bool delete(pnode* ph,dtype d)
{
//没有节点:
	pnode front=*ph,rear=*ph;
	while(rear!=NULL&&rear->data!=d)
	{
		front=rear;//在rear没指向下一个元素之前 (之前)
		rear=rear->next;//rear指向下一个元素    (之后)
	}
	if(NULL!=rear)//查找失败
	{
		if(rear==*ph)//头节点
			*ph=rear->next;
		else		//非头节点
			front->next=rear->next;
		free(rear);
		return true;
	}
	return false;
}

void main()
{
//头节点:
	pnode phead=NULL;
	dtype d=100;
//插入元素
	insert(&phead,d);
	insert(&phead,200);
	insert(&phead,300);
//遍历:
	list(phead);	
	printf("*******************\n");
//删除
	delete(&phead,200);
	list(phead);
}

  链表的删除 (和上面的一样的就是写在了一个文件里面而已) 

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

struct nd;

typedef int dtype;
typedef struct nd node;
typedef struct nd* pnode;

struct nd
{
	dtype data;
	pnode next;
};

bool insert(node** pph,dtype d)
{
	pnode pnew=malloc(sizeof(node));
	if(NULL==pnew)
		return false;
	pnew->data=d;
	pnew->next=*pph;
	*pph=pnew;
	return true;
}

void list(pnode loc)
{
	while(loc!=NULL)
	{
		printf("%d\n",loc->data);
		loc=loc->next;
	}
}

bool delete(pnode* ph,dtype d)
{
	pnode front=*ph,rear=*ph;
	while(rear!=NULL && rear->data!=d)
	{
		front=rear;
		rear=rear->next;
	}
	if(rear!=NULL)
	{
		if(rear==*ph)
			*ph=rear->next;
		else
			front->next=rear->next;
		free(rear);
		return true;
	}
		return false;
}

void main()
{
	pnode head = NULL;
	insert(&head,100);
	insert(&head,200);
	list(head);
	printf("******删除******\n");
	delete(&head,100);
	list(head);
}

  函数指针与栈与链表的综合应用 : 

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

struct stack;
bool insert(struct stack* ps,int d);
bool isempty(struct stack* ps)
bool isfull(struct stack* ps)
void init(struct stack* ps)
void list(struct stack* ps)


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

//栈
struct stack
{
//容器:顺序表  链表
	struct node* head;//指向栈顶元素,head存储链表的第一个元素
//方法:定义函数指针变量
	bool (*pinsert)(struct stack*,int );
	void (*pinit)(struct stack*);
	bool (*pisempty)(struct stack*);
	bool (*pisfull)(struct stack*);
	void (*plist)(struct stack*);
};

//判断栈为空
bool isempty(struct stack* ps)
{
	if(ps->head==NULL)
		return true;
	else
		return false;
}

//判断栈为满
bool isfull(struct stack* ps)
{
	return true;
}

//初始化
void init(struct stack* ps)
{
	//指向为空
	ps->head=NULL;
	//存放函数指针变量
	ps->pinit=init;
	ps->pinsert=insert;
	ps->plist=list;
	ps->pisfull=isfull;
	ps->pismepty=isempty;
}

//方法:对栈进行操作push
bool insert(struct stack* ps,int d)
{
//1.分配空间:头插法
	struct node* pnew=malloc(sizeof(struct node));
	if(NULL==pnew)
		return false;
	pnew->data=d;
//2.修改指向域
	pnew->next=ps->head;
	ps->data=pnew;
	return true;
}

//遍历
void list(struct stack* ps)
{
	struct node* loc=ps->head;
	while(loc!=NULL)
	{
		printf("%s %s\n",loc->data);
		loc=loc->next;
	}
}

void main()
{
//定义变量:
	struct stack s;
	s.init(&s);
	s.insert(&s,1);
}

  函数指针与栈与链表的综合应用 , 实现使用递归来逆序打印 , 还有倒置打印 .

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

//节点:数组中的一个元素
struct node
{
	int data;
	struct node* next;
};

//定义头结点
struct stack
{
//头结点指针变量:
	struct node* top;
//定义方法指针变量
	bool (*push)(struct stack*,int);//返回值bool类型(void)
	void (*initstack)(struct stack*);
	bool (*pop)(struct stack*,int*);
};

bool push(struct stack* ps,int d)
{
//分配空间
	struct node* pnew = malloc(sizeof(struct node));
	if(NULL==pnew)
		return false;
	pnew->data=d;
//修改指向域
	pnew->next=ps->top;
	ps->top=pnew;
	return true;
}

//出栈
bool pop(struct stack* ps,int* value)
{
	//判断栈为空
	if(NULL==ps->top)
		return false;
	*value=ps->top->data;
	//下一个元素成为栈顶元素
	struct node* ptemp=ps->top;
	ps->top=ps->top->next;
	//释放空间
	free(ptemp);
	return true;
}

//初始化结构体
void initstack(struct stack* ps)
{
	ps->top=NULL;
	ps->push=push;	//初始化
	ps->pop=pop;
}

//逆序打印
void reverser(struct stack* s)
{
	int value=0;
	if(pop(s,&value)==true)
	{
		reverser(s);
		printf("%d\n",value);
	}
	else
		return;
}

//倒置
void reverser2(struct stack* s,struct node* pfront,struct node* prear)
{
	if(prear==NULL)//最后递归
	{
		s->top=pfront;
		return ;
	}
	reverser2(s,prear,prear->next);
	prear->next=pfront;
}

void main()
{
//需要头结点的指针变量
	struct stack s;
	s.initstack=initstack;//初始化
	s.initstack(&s);

//压入
	s.push(&s,100);
	s.push(&s,200);
	s.push(&s,300);
	s.push(&s,400);
	s.push(&s,500);

	reverser2(&s,NULL,s.top);
	struct node* loc=s.top;
	while(loc!=NULL)
	{
		printf("%d\n",loc->data);
		loc=loc->next;
	}
	
/*
	int value;
	while(s.pop(&s,&value)!=false)
	{
		printf("%d\n",value);
	}
*/
}

猜你喜欢

转载自blog.csdn.net/Superman___007/article/details/81322599