Chinaunix首页 | 论坛 | 博客
  • 博客访问: 150938
  • 博文数量: 44
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 407
  • 用 户 组: 普通用户
  • 注册时间: 2015-11-10 13:28
个人简介

仰望星空

文章分类
文章存档

2016年(22)

2015年(22)

我的朋友

分类: C/C++

2015-12-15 14:06:01

一、栈
    栈的特性是先进后出,首先定义一个栈顶指针head或者Top都是可以的。入栈的时候总是在栈顶指针指向的位置插入元素,这样首先插入的元素就跑到后边去了。出栈的时候也是首先从栈顶指向的节点处先出栈,以此来实现先入后出。。
代码如下:其实还就是单链表,只不过单链表初始化的时候不是从后面,而是从前面插入元素。

点击(此处)折叠或打开

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

  3. #define TRUE 1
  4. #define FALSE 0

  5. typedef struct node
  6. {
  7.     int data;
  8.     struct node *next;
  9. }Stack;

  10. void StackInit(Stack *head)
  11. {    //栈的初始化
  12.     head->next = NULL;
  13. }

  14. int StackInput(Stack *head, int num)
  15. {    //入栈
  16.     Stack *p = (Stack *)malloc(sizeof(Stack));
  17.     p->data = num;

  18.     p->next = head->next;
  19.     head->next = p;

  20.     return 0;
  21. }

  22. int StackOutput(Stack *head)
  23. {    //出栈
  24.     int x;
  25.     Stack *p = (Stack *)malloc(sizeof(Stack));
  26.     p = head->next;
  27.     x = p->data;
  28.     head->next = p->next;
  29.     free(p);
  30.     return x;
  31. }

  32. int main()
  33. {
  34.     Stack *top;
  35.     int num[4], i;

  36.     top = (Stack *)malloc(sizeof(Stack));
  37.     StackInit(top);


  38.     for(i = 0; i < 4; i++)
  39.     {
  40.         printf("Please Input %d num:", i+1);
  41.         scanf("%d", &num[i]);
  42.         StackInput(top, num[i]);
  43.     }

  44.     printf("Output num is:\n");
  45.     for(i = 0; i < 4; i++) //出栈,顺序相反
  46.     {
  47.         printf("%d", StackOutput(top));
  48.     }
  49.     return 0;
  50. }
二、队列
    队列和栈正好是相反的,先入先出的结构,构造队列时必须有一个队首指着和一个队尾指针,当队首指针和队尾指针指向同一个地址时,代表这个队列是一个空队列。这两个指针同时也就是队列的精华所在,这两个指针也是有next的,队首指针的next指向的是队列的数据节点,而队尾指针的next指向的是NULL,这一点初始化的时候要注意一下。整个队列中也就只有这两个指针是符号结构体类型的变量,其他的还是和单链表一样的单一结构类型。
代码如下:

点击(此处)折叠或打开

  1. /* 队列 先入先出*/
  2. /*使用指针是可以的,但是一定要注意,指针是不是有空间的,也就是指针变量是否是赋值的
  3. 没有赋值的指针是不能够使用的,使用的话会出错的*/
  4. #include <stdlib.h>
  5. #include <stdio.h>

  6. #define TRUE 1
  7. #define FALSE 0

  8. typedef struct node
  9. { /* 节点类型 */
  10.   int data;
  11.   struct node *next;
  12. } qnode;


  13. typedef struct pointer
  14. {    //这仅仅是两个指针,指向队首和队尾,中间节点仍然是单链表的节点
  15.     qnode *front; /* 队首指针 */
  16.     qnode *rear; /* 队尾指针 */
  17. } qpointer;


  18. int QueneInit(qpointer *qp)
  19. {    //队列的初始化
  20.     qnode *q;
  21.     q = (qnode *)malloc(sizeof(qnode));

  22.     qp->front = q;        /*队首和队尾共同指向同一个节点*/
  23.     qp->front->next = NULL;
  24.     qp->rear = qp->front;
  25.     
  26. }


  27. int QuenePush(qpointer *qp, int x)
  28. {    //入队列
  29.     qnode *p;
  30.     p = (qnode *)malloc(sizeof(qnode));
  31.     p->data = x;
  32.     p->next = NULL;

  33.     qp->rear->next = p;
  34.     qp->rear = p;
  35. }

  36. int IsEmpty(qpointer *qp)
  37. {    //判断队列是否为空
  38.     if (qp->front == qp->rear)
  39.         return TRUE;
  40.     return FALSE; /*这句不能少,少了会出错的*/
  41. }


  42. int QuenePop(qpointer *qp)
  43. {    //出队列
  44.     qnode *q;
  45.     int x;
  46.     if(IsEmpty(qp)) //判断
  47.         return FALSE;

  48.     q = (qnode *)malloc(sizeof(qnode));
  49.     q = qp->front->next;
  50.     x = q->data;
  51.     qp->front->next = q->next;

  52.     if (qp->rear == q) //判断是不是已经到了队列的最后
  53.     {
  54.         qp->rear = qp->front;
  55.     }
  56.     free(q);
  57.     return x;
  58. }



  59. int main()
  60. {    // 为什么使用qpointer *qp 不行呢?
  61.     int num[4], i;
  62.     qpointer qp;
  63.     QueneInit(&qp);
  64.     
  65.     for(i = 0; i < 4; i++)
  66.     {
  67.         printf("Please Input Num:");
  68.         scanf("%d", &num[i]);
  69.         QuenePush(&qp, num[i]);
  70.     }

  71.     for(i = 0; i < 4; i++)
  72.     {
  73.         printf("%3d", QuenePop(&qp));
  74.     }
  75.     printf("\n");
  76.     return 0;
  77. }
/以上其实都是对链表的进一步运用而已/
阅读(2736) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~