zoukankan      html  css  js  c++  java
  • 栈和队列

      1 /*
      2 头指针 ---->不存数据--->A->B->C->NULL
      3 
      4     增  头插      第一个结点前面插入    修改head指针的方向
      5     删除      头删  删除第一个结点   需要修改head指针   分情况讨论
      6     第一个结点不存数据    为了操作方便
      7 
      8     结构体  结构体指针...
      9 
     10 -----------------------------------------------------------
     11 数据+对应增删改查
     12 
     13     栈    先进后出的数据结构
     14 
     15         ctrl+z 撤回  能够撤回多次
     16         必须记录下最近的每一次操作  每次撤回最近的一次操作
     17             最后做的操作  最先被撤回----->先进后出的数据  后进后出的数据结构
     18             
     19         栈区   内存中的一个区域
     20         下棋  悔棋  每一步都记录下来  每次悔棋  退回上一次的操作
     21 
     22         记录-->增加一个数据  撤销  --->删除一个数据
     23 
     24         栈的实现  顺序表  选择尾插和尾删(如果选择数组开头作为插入和删除的位置)
     25                  链表的方式  选择一端进行插入和删除
     26                     头插+头删  (尾插+尾删)
     27         
     28     队列  先进后出的数据结构 排队  先到先得
     29           先排队  那么先买到(先出队)
     30 
     31 
     32 */
     33 #include<stdio.h>
     34 #include<stdlib.h>
     35 typedef struct node
     36 {
     37     int data;
     38     struct node*next;
     39 }NODE;
     40 void push(NODE*head, int data);//压栈  入栈  插入一个数据 (必须插入到栈顶)
     41 int isEmpty(NODE*head);
     42 int pop(NODE*head);
     43 int main()
     44 {
     45     NODE*head = (NODE*)malloc(sizeof(NODE));
     46     head->next = NULL;
     47     //栈和队列主要操作就是插入和删除
     48 
     49 #if 0//示列  进制转换
     50     int x = 233;
     51     while (x != 0)
     52     {
     53         push(head, x % 2);
     54         x /= 2;        //x>>=1;
     55     }
     56     while (isEmpty(head) == 0)
     57     {
     58         printf("%d", pop(head));
     59     }
     60 #endif
     61 #if 0//
     62     char name[] = "lost rivers";
     63     int i = 0;
     64     while (name[i] != '')
     65     {
     66         push(head, name[i]);
     67         ++i;
     68     }
     69     while (isEmpty(head) == 0)
     70     {
     71         printf("%c", pop(head));
     72     }
     73 #endif
     74 #if 1//
     75     int x = 123456;
     76     while (x!=0)
     77     {
     78         push(head, x%10);
     79         x /= 10;
     80     }
     81     while (isEmpty(head) == 0)
     82     {
     83         x = x * 10 + pop(head);
     84     }
     85     printf("%d", x);
     86 #endif
     87     free(head);
     88     getchar();
     89     return 0;
     90 }
     91 void push(NODE*head, int data)
     92 {
     93     //头插
     94     NODE*p = (NODE*)malloc(sizeof(NODE));
     95     p->data = data;//保存数据
     96 
     97     p->next = head->next;
     98     head->next = p;
     99 }
    100 int pop(NODE*head)
    101 {
    102     //头删
    103     if (head->next == NULL)        return -1;
    104     else
    105     {
    106         int data = head->next->data;//后续要用
    107         //头删方式进行删除
    108         NODE*p = head->next;
    109         //head->next = head->next->next;
    110         head->next = p->next;
    111         free(p);
    112         return data;//返回已经删除的值
    113     }
    114 }
    115 int isEmpty(NODE*head)
    116 {
    117     return head->next == NULL;//返回1表示栈为空 返回0表示栈为空
    118 
    119 }

    队列

      1 #include<stdio.h>
      2 
      3 typedef struct list
      4 {
      5     int arr[20];//存放数据
      6     int top;//队头
      7     int rear;//队尾
      8     int size;//数组大小
      9 }LIST;
     10 
     11 void init(LIST*p);//初始化
     12 void push(LIST*p, int data);//入队
     13 int pop(LIST*p);//出队
     14 int isEmpty(LIST*p);//判断队空
     15 int main()
     16 {
     17     LIST myList;
     18     init(&myList);
     19 
     20     int x = 123456;
     21     while (x != 0)
     22     {
     23         push(&myList, x % 10);
     24         x /= 10;
     25     }
     26 
     27     while (isEmpty(&myList) == 0)
     28     {
     29         x = x * 10 + pop(&myList);
     30     }
     31     printf("%d", x);
     32 
     33     getchar();
     34     return 0;
     35 
     36 }
     37 void init(LIST*p)
     38 {
     39     p->size = 20;
     40     p->top = p->rear = 0;
     41 }
     42 
     43 void push(LIST*p, int data)
     44 {
     45     //先判断队列有没有满  队尾+1 是不是队头
     46     if ((p->rear + 1)%p->size!= p->top)//没有满
     47     {
     48         p->arr[p->rear] = data;//插入到队尾
     49         p->rear = (p->rear + 1) % p->size;
     50     }
     51 }
     52 
     53 int pop(LIST*p)
     54 {
     55     int data = -1;//先准备一个变量
     56     if (p->rear != p->top)//判断是不是队空
     57     {
     58         data = p->arr[p->top];//保留数据
     59         p->top = (p->top + 1)%p->size;
     60     }
     61     return data;
     62 }
     63 
     64 int isEmpty(LIST*p)
     65 {
     66     return p->rear == p->top;
     67 }
     68 
     69 
     70 
     71 
     72 /*
     73     栈和队列  栈  先进后出的数据结构
     74              队列  先进先出的数据结构
     75 
     76              吃了吐  吃了拉
     77 
     78 
     79         线性表  ---> 顺序表/链表
     80             
     81             栈和队列  功能受限的线性表   插入删除
     82 
     83                 栈在一端进行插入和删除
     84                 队列一端插入 另外一端删除
     85                 
     86 
     87     代码围绕顺序表或者链表进行
     88           尾插+ 头删
     89 
     90 
     91 
     92 
     93 
     94 
     95         C语言  语法部分  
     96 
     97         数据结构和算法部分    算法 排序  对数列从无序变成有序  本质上操作数据
     98                     
     99                     算法 数组(顺序表)来实现的
    100 
    101 
    102                 数据结构 组织和管理数据   影响数据的使用方式和效率
    103 
    104 */
  • 相关阅读:
    thread同步测试
    thread互斥测试
    实验二测试
    第六章学习笔记
    opensslAPI
    第五章学习笔记
    stat命令的实现-mysate(必做)
    团队作业(三)
    第四章学习笔记
    学习笔记9
  • 原文地址:https://www.cnblogs.com/liugangjiayou/p/10652662.html
Copyright © 2011-2022 走看看