zoukankan      html  css  js  c++  java
  • C语言实现常用数据结构——栈

    #include<stdio.h>
    #include<stdlib.h>
    //用链表实现栈
    typedef struct Node {
        int data;
        struct Node *next;
    } node;
    
    int IsEmpty(node *p) {
        return p->next==NULL;
    }
    
    node *CreateStack() {
        node *p=(node*)malloc(sizeof(node));
        p->next=NULL;
        return p;
    }
    
    
    node *Push(node *p,int x) {
        node *TmpCell=(node*)malloc(sizeof(node));
        TmpCell->data=x;
        TmpCell->next=p->next;
        p->next=TmpCell;
        return p;
    }
    
    void Pop(node *p) {
        node *cell;
        if(p->next==NULL) {
            printf("error,-1");
        } else {
            cell=p->next;
            p->next=cell->next;
            free(cell);
        }
    }
    
    void display(node *p) {
        node *temp=p;
        while(temp->next) {
            printf("%d",temp->next->data);
            temp=temp->next;
        }
        printf("%c",'
    ');
    }
    
    main() {
        node *p=CreateStack();
        Push(p,1);
        Push(p,2);
        Push(p,3);
        Push(p,4);
        display(p);
        Pop(p);
        display(p);
        Pop(p);
        display(p);
    }
  • 相关阅读:
    简单批处理语法结构
    简单批处理常用命令
    简单批处理符号简介
    简单批处理内部命令
    jQuery操作DOM
    jQuery中的事件与动画
    jQuery选择器
    初始面向对象
    初识jQuery
    操作DOM
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/9593673.html
Copyright © 2011-2022 走看看