二级指针的用法

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

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

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

stack_queue.h文件:

 

[cpp]  view plain copy
  1. <span style="font-size:16px;">#ifndef STACK_QUEUE_H  
  2. #define STACK_QUEUE_H  
  3.   
  4. #include<stdlib.h>  
  5.   
  6. #define ERROR -10000  
  7.   
  8. typedef int NodeType;//令结点类型为int型  
  9.   
  10. //栈、队列中结点的定义  
  11. typedef struct node  
  12. {  
  13.     NodeType data;  
  14.     struct node * next;  
  15. }Node,*PNode;  
  16.   
  17.   
  18. //链栈用带头结点的单链表实现  
  19.   
  20. void init_stack(PNode* top);//初始化栈  
  21. NodeType pop(PNode* top);//弹栈  
  22. NodeType pep(PNode top);//取栈顶元素  
  23. void push(PNode* top,NodeType data);//压栈  
  24. int stack_empty(PNode top);//判断栈是否为空  
  25.   
  26.   
  27. //链队列用带头结点的循环单链表实现  
  28.   
  29. void init_queue(PNode* rear);//初始化队列  
  30. NodeType de_queue(PNode* rear);//出队  
  31. NodeType get_queue(PNode rear);//取队头元素  
  32. void en_queue(PNode* rear,NodeType data);//入队  
  33. int queue_empty(PNode rear);//判断队列是否为空   
  34.   
  35. #endif</span>  


stack.c文件:

 

[cpp]  view plain copy
  1. <span style="font-size:16px;">#ifndef STACK_C  
  2. #define STACK_C  
  3.   
  4. #include"stack_queue.h"  
  5.   
  6.   
  7. //初始化栈,在建立一个头结点作为栈底元素,top是栈顶指针  
  8. void init_stack(PNode* top)  
  9. {  
  10.     PNode p;  
  11.     p=((PNode)malloc(sizeof(Node)));  
  12.     *top=p;  
  13.     (*top)->next=NULL;  
  14. }  
  15.   
  16. //出栈(要改变栈顶指针,栈顶指针下移,所以传入的是一个二级指针)  
  17. NodeType pop(PNode* top)  
  18. {  
  19.     NodeType data;  
  20.     PNode q=*top;  
  21.     if(!stack_empty(*top))  
  22.     {  
  23.         data=q->data;  
  24.         *top=(*top)->next;//改变栈顶指针  
  25.         q->next=NULL;  
  26.         free(q);  
  27.         return data;  
  28.     }  
  29.     return ERROR;  
  30. }  
  31.   
  32. //查看栈顶元素(不需要对栈进行修改,所以不需要传入二级指针)  
  33. NodeType pep(PNode top)  
  34. {  
  35.     if(!stack_empty(top))  
  36.     {  
  37.          return top->data;  
  38.     }  
  39.     return ERROR;  
  40. }  
  41.   
  42. //入栈,栈顶指针上移  
  43. void push(PNode* top,NodeType data)  
  44. {  
  45.    PNode pnode=(PNode)malloc(sizeof(Node));  
  46.    pnode->data=data;  
  47.    pnode->next=*top;  
  48.    (*top)=pnode;//改变栈顶指针  
  49. }  
  50.   
  51. //判断栈是否为空(不需要对栈进行修改,所以不需要传入二级指针)  
  52. int stack_empty(PNode top)  
  53. {  
  54.     if(top->next==NULL)  
  55.         return 1;  
  56.     else  
  57.         return 0;  
  58. }  
  59.   
  60. #endif</span>  


queue.c文件:

 

[cpp]  view plain copy
  1. #ifndef QUEUE_C  
  2. #define QUEUE_C  
  3. #include"stack_queue.h"  
  4.   
  5. //初始化队列(建立一个带头结点的循环队列)  
  6. void init_queue(PNode*  rear)  
  7. {  
  8.     PNode p;  
  9.     p=(PNode)malloc(sizeof(Node));  
  10.     p->next=p;  
  11.     *rear=p;  
  12. }  
  13.   
  14. //出队列(注意当只有一个元素时要改变队列的尾指针)  
  15. NodeType de_queue(PNode * rear)  
  16. {  
  17.     NodeType data;  
  18.     PNode pnode;  
  19.     if(!queue_empty(*rear)){  
  20.         pnode=(*rear)->next->next;  
  21.         data=pnode->data;  
  22.         (*rear)->next->next=pnode->next;  
  23.         if(pnode==*rear)//除头结点外只有一个结点时  
  24.             *rear=(*rear)->next;  
  25.         pnode->next=NULL;  
  26.         free(pnode);  
  27.         return data;  
  28.     }  
  29.     else   
  30.         return ERROR;  
  31. }  
  32.   
  33. //获取对头元素  
  34. NodeType get_queue(PNode rear)  
  35. {  
  36.     if(!queue_empty(rear)){  
  37.         return rear->next->next->data;  
  38.     }  
  39.     else   
  40.         return ERROR;  
  41. }  
  42.   
  43. //入队列(每次入队列都要改变队尾指针)  
  44. void en_queue(PNode * rear,NodeType data)  
  45. {  
  46.     PNode pnode;  
  47.     pnode=(PNode)malloc(sizeof(Node));  
  48.     pnode->next=(*rear)->next;  
  49.     pnode->data=data;  
  50.     (*rear)->next=pnode;  
  51.     *rear=pnode;//队尾指针后移一位  
  52. }  
  53.   
  54. //判断是否为空  
  55. int queue_empty(PNode rear)  
  56. {  
  57.     if(rear->next == rear)  
  58.         return 1;  
  59.     else  
  60.         return 0;  
  61. }  
  62. #endif  


 

test.c文件(测试):

[cpp]  view plain copy
  1. <span style="font-size:16px;">#include"stack.c"  
  2. #include"queue.c"  
  3. #include<stdio.h>  
  4.   
  5. void main()  
  6. {   
  7.     PNode stack;  
  8.     PNode queue;  
  9.   
  10.     puts("测试链栈:");  
  11.     init_stack(&stack);  
  12.     push(&stack,1);  
  13.     push(&stack,2);  
  14.     printf("%d\n",pop(&stack));  
  15.     printf("%d\n",pop(&stack));  
  16.   
  17.     puts("测试链队列:");  
  18.     init_queue(&queue);  
  19.     en_queue(&queue,1);  
  20.     en_queue(&queue,2);  
  21.     printf("%d\n",de_queue(&queue));  
  22.     printf("%d\n",de_queue(&queue));  
  23. }</span>  



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

 

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值