zoukankan      html  css  js  c++  java
  • 栈和队列的链式存储

    // main.c

    // Practice

    //

    // Created by all is well on 2018/10/14.

    // Copyright © 2018 all is well. All rights reserved.

    // 链式栈 和 链式队列

     #include <stdio.h>
     
     #include<stdlib.h>
     
      
    
     //头节点 ----- 数据域不赋值的节点
     
      
     
     
     //---------------Stack start---------------------------------------
     //带有头节点的栈比较方便
     //栈的节点
     typedef struct Node{
         int data;
         struct Node *next;
     }Node;
     
     //节点的大小
     int size = sizeof(Node);
    
     //初始化栈
     void StackInit(Node *head){
         head = (Node*)malloc(size);//栈的头节点
         head->next = NULL;
     }
     
     //判断栈是否为空栈
     int StackNotEmpty(Node *head){
         if(head->next == NULL)
             return 0;
         else
             return 1;
     }
     
     //进栈
     int StackPush(Node *head, int x){
         Node *p;
         if((p=(Node*)malloc(size)) == NULL){
             printf("申请内存失败!
    ");
             exit(0);
         }
         p->data = x;
         p->next = head->next;   //进栈
         head->next = p;
         return 1;
     }
     
     //出栈
     int StackPop(Node *head, int *x){
         Node *p = head->next;  //栈顶元素
         if(p == NULL){
             printf("堆栈已空出错!/n");
             return 0;
         }
         head->next = p->next;   //出栈
         *x = p->data;
         free(p);
         return 1;
     }
     
     //返回栈顶元素
     int StackTop(Node *head, int *x){
         Node *p = head->next;   //栈顶元素
         if(p == NULL){
             printf("栈顶已空出错!
    ");
             return 0;
         }
         *x = p->data;           //返回栈顶元素
         return 1;
     }
     
      
     //销毁栈。 释放栈的内存
     void destoryStack(Node *head){
         Node *p, *p1;
         p = head; //从头节点开始销毁
         while(p != NULL){
             p1 = p;
             p = p->next;
             free(p1);
         }
     }
     
     //-------------- Stack over----------------------------
     
      
    
     
     //---------------Queue start---------------------------
     //链式存储结构的队列
     //Queue的队头指针指向当前对头节点。 队尾指针指向当前的对尾节点
     //链式队列没有头节点更方便
     //节点的结构体类型
     typedef struct qnode{
         int data;
         struct qnode *next;
     }qnode;
     
     int qsize = sizeof(qnode);  //队列节点的大小
     
      
     //队列的结构体类型
     typedef struct Queue{
         qnode *front;//对头指针
         qnode *rear;//队尾指针
     }Queue;
     
      
     
     //初始化队列
     void QueueInit(Queue *Q){
         Q->front = NULL;    //队头指针置空
         Q->rear = NULL;     //队尾指针置空
     }
     
      
     
     //判断队列是否为空, 只需判断队头指针是否为空就好
     int QueueNotEmpty(Queue *Q){
         if(Q->front == NULL)
             return 0;
         else
             return 1;
     }
     
      
     //进队列。向对尾添加新节点
     int QueueAppend(Queue *Q, int x){
         qnode *p;
         if((p = (qnode*)malloc(qsize)) == NULL){
             printf("申请内存失败!/n");
             return 0;
         }
         p->data = x;
         p->next = NULL;
         if(Q->rear != NULL){
             Q->rear->next = p;//队列为非空时,队尾添加新的节点
             Q->rear = p;// 修改对尾的指针
         }
         if(Q->front == NULL)
             Q->front = p;//队列为空时, 新节点为队头指针
         return 1;
     }
     
     //出队列。 删除队头的节点,并返回队头节点的数据域
     int QueueDelete(Queue *Q, int *x){
         qnode *p =NULL;
         if(Q->front == NULL){
             printf("队列已空,当前队列无数据出列!/n");
             return 0;
         }
         *x = Q->front->data;            //队头节点的数据域
         p = Q->front;                   //队头节点
         Q->front = Q->front->next;      //出队列
         if(Q->front == NULL)            //当队头指针为空时, 对尾指针必为空
             Q->rear = NULL;
         free(p);
         return 1;
     }
     
     //取队头元素的数据域
     int QueueGet(Queue *Q, int *x){
         if(Q->front == NULL){
             printf("队列已空,无数据元素出列
    ");
             return 0;
         }
     
         *x = Q->front->data;    //队头的数据域
     
         return 1;
     
     }
     
     //--------------Queue over--------------------------
     
    
     int main(void){ 
         Node *head; 
         Queue *Q; 
         int s_x = 1, q_x = 2; 
         int x; 
         Q = (Queue *)malloc(sizeof(Queue)); 
         head = (Node *)malloc(size); 
         StackInit(head); 
         StackNotEmpty(head); 
         StackPush(head, s_x); 
         StackTop(head, &x); 
         printf("Top %d
    ", x); 
         StackPop(head, &x); 
         printf("Pop %d
    ", x);
           
         QueueInit(Q); 
         QueueNotEmpty(Q); 
         QueueAppend(Q, q_x); 
         QueueGet(Q, &x); 
         printf("QueueGet %d", x); 
         QueueDelete(Q, &x); 
         printf("QueueDelete %d", x); 
          
         printf("hello world!
    "); 
         return 0;
     
     }
    
  • 相关阅读:
    NetScaler ‘Counters’ Grab-Bag!
    NetScaler + Wireshark = A Perfect Combination!
    What’s That NetScaler Reset Packet?
    Give NetScaler a “Tune-Up”
    51Nod 1080 两个数的平方和(数论,经典题)
    51Nod 1289 大鱼吃小鱼(模拟,经典好题)
    1082 与7无关的数(思维题,巨坑)
    51Nod 1003 阶乘后面0的数量(数学,思维题)
    2017广东工业大学程序设计竞赛决赛 题解&源码(A,数学解方程,B,贪心博弈,C,递归,D,水,E,贪心,面试题,F,贪心,枚举,LCA,G,dp,记忆化搜索,H,思维题)
    后缀数组(一堆干货)
  • 原文地址:https://www.cnblogs.com/you-you/p/11950534.html
Copyright © 2011-2022 走看看