二级指针的用法

来源:互联网 发布:局域网 ip 端口号 编辑:程序博客网 时间:2024/04/29 05:26

1、二级指针(指向指针的指针)

那么二级指针有什么用呢?看一个用二级指针实现的链栈和链队列的例子

源代码中各个文件说明:
stack_queue.h文件中存放结点的定义以及函数的声明
stack.c文件中存放栈的实现
queue.c文件中中存放队列的实现

stack_queue.h文件:

 

#ifndef STACK_QUEUE_H#define STACK_QUEUE_H#include<stdlib.h>#define ERROR -10000typedef int NodeType;//令结点类型为int型//栈、队列中结点的定义typedef struct node{NodeType data;struct node * next;}Node,*PNode;//链栈用带头结点的单链表实现void init_stack(PNode* top);//初始化栈NodeType pop(PNode* top);//弹栈NodeType pep(PNode top);//取栈顶元素void push(PNode* top,NodeType data);//压栈int stack_empty(PNode top);//判断栈是否为空//链队列用带头结点的循环单链表实现void init_queue(PNode* rear);//初始化队列NodeType de_queue(PNode* rear);//出队NodeType get_queue(PNode rear);//取队头元素void en_queue(PNode* rear,NodeType data);//入队int queue_empty(PNode rear);//判断队列是否为空 #endif


stack.c文件:

 

#ifndef STACK_C#define STACK_C#include"stack_queue.h"//初始化栈,在建立一个头结点作为栈底元素,top是栈顶指针void init_stack(PNode* top){PNode p;p=((PNode)malloc(sizeof(Node)));*top=p;(*top)->next=NULL;}//出栈(要改变栈顶指针,栈顶指针下移,所以传入的是一个二级指针)NodeType pop(PNode* top){NodeType data;PNode q=*top;    if(!stack_empty(*top)){data=q->data;    *top=(*top)->next;//改变栈顶指针q->next=NULL;free(q);return data;}return ERROR;}//查看栈顶元素(不需要对栈进行修改,所以不需要传入二级指针)NodeType pep(PNode top){if(!stack_empty(top)){ return top->data;}return ERROR;}//入栈,栈顶指针上移void push(PNode* top,NodeType data){   PNode pnode=(PNode)malloc(sizeof(Node));   pnode->data=data;   pnode->next=*top;   (*top)=pnode;//改变栈顶指针}//判断栈是否为空(不需要对栈进行修改,所以不需要传入二级指针)int stack_empty(PNode top){if(top->next==NULL)return 1;elsereturn 0;}#endif


queue.c文件:

 

#ifndef QUEUE_C#define QUEUE_C#include"stack_queue.h"//初始化队列(建立一个带头结点的循环队列)void init_queue(PNode*  rear){PNode p;p=(PNode)malloc(sizeof(Node));p->next=p;    *rear=p;}//出队列(注意当只有一个元素时要改变队列的尾指针)NodeType de_queue(PNode * rear){    NodeType data;PNode pnode;if(!queue_empty(*rear)){    pnode=(*rear)->next->next;    data=pnode->data;    (*rear)->next->next=pnode->next;if(pnode==*rear)//除头结点外只有一个结点时*rear=(*rear)->next;pnode->next=NULL;    free(pnode);    return data;}else return ERROR;}//获取对头元素NodeType get_queue(PNode rear){if(!queue_empty(rear)){    return rear->next->next->data;}else return ERROR;}//入队列(每次入队列都要改变队尾指针)void en_queue(PNode * rear,NodeType data){PNode pnode;pnode=(PNode)malloc(sizeof(Node));pnode->next=(*rear)->next;pnode->data=data;(*rear)->next=pnode;*rear=pnode;//队尾指针后移一位}//判断是否为空int queue_empty(PNode rear){if(rear->next == rear)return 1;elsereturn 0;}#endif


 

test.c文件(测试):

#include"stack.c"#include"queue.c"#include<stdio.h>void main(){     PNode stack;    PNode queue;puts("测试链栈:");init_stack(&stack);push(&stack,1);push(&stack,2);printf("%d\n",pop(&stack));printf("%d\n",pop(&stack));puts("测试链队列:");init_queue(&queue);en_queue(&queue,1);en_queue(&queue,2);printf("%d\n",de_queue(&queue));printf("%d\n",de_queue(&queue));}



总结:指针也是传值传递,当我们要在被掉函数里面改变调用函数一级指针的值时,就需要以二级指针作为参数。这种情况是经常碰到的,比如在链表(无头结点)操作时是通过链表第一个元素来找到其他所有链表中的元素,如果删除操作时删除的正好是第一个元素,那么这时就要改变链表头指针的指向了。当然还有在二叉树操作时当删除的刚好是树根结点,此时也要改变一级指针的指向。

 

2、有关指针的数据类型小结(来着《c语言程序设计》谭浩强)