数组和链表实现简单的栈

//首先用数组实现,接着用c++类实现了链式栈
#include <stdio.h>
#include <stdlib.h>
//c语言数组实现简单的栈
#define MAXSIZE 100
typedef struct mystack{
    int data[MAXSIZE];
    int top;
} my_stack, *my_stack_type;
//创建一个栈,堆空间开辟内存
my_stack_type stac_creak()
{
    my_stack_type s = NULL;
    if(s = (my_stack_type)malloc(sizeof(my_stack)))
      
       { s ->top =0;
        printf("create done!\n");}
     return s;//返回指针可以,但不可以返回临时变量
}

bool is_full(my_stack_type s)

{
    if(s->top = MAXSIZE)
     return true;
    else
     return false;
}

bool is_empty(my_stack_type s)
{
    if(s ->top =0)
      return true;
    else
    return false;
}

void push_stack(my_stack_type s)
{
    if(!is_full(s))
    { printf("the stack is full\n");
     return;}
    else
    {
        printf("please input data\n");
        scanf("%d",&s->data[s->top]);
        s->top++;
    }
}
void pop_stack(my_stack_type s,int * temp)
{
    if(is_empty)
    {
        printf("the stack is empty\n");
        return;
    }
    else
    {
           s->top--;
        * temp = s->data[s->top];
        //s->top--;
    }
}

void clear_stack(my_stack_type s)
{
    free(s);
    printf("clear done!\n");
}

void print_stack(my_stack_type s)
{
    int i =s ->top-1;
    for(;i<=0;i-- )
    {
       printf("%d\t",s->data[s->top]) ;
        
    }
}

int main()
{
    int a;
    my_stack_type s =stac_creak();
   // is_full(s);
    push_stack(s);
    push_stack(s);
    push_stack(s);
    print_stack(s);
    pop_stack(s,&a);
    print_stack(s);
    
    //printf("%d\n%d\n" ,5 ,6);
    return 0;
}
#include <iostream>
#include <cstdlib>
using namespace std;
 
 struct Node
    {
        int data;
        Node *next;
    };


class Stack
{
private:
   
    Node *head;//链表的头结点
    Node *p; //链表的当前结点
    int length;//结点的长度
public:
Stack()//初始化一个空栈
    {
        head = NULL;
        length = 0;
    }
void push(int n)//入栈
    {   //创建结点
        Node *q = new Node;
        q->data = n;
if (head == NULL)//创建第一个结点
        {
            q->next = head;
            head = q;
            p = q;
        }
else
        {
            q->next = p;
            p = q;
        }
        length ++;
    }
    int pop()//出栈并且将出栈的元素返回
    {
        if (length <= 0)
        {
         abort();//跳出一个异常
        }
        Node *q;//创建一个临时结点
        int data;
        q = p;
        data = p->data;
        p = p->next;//结点下移
        delete(q);
        length --;
        return data;
    }
    int size()//返回元素个数
    {
        return length;
    }
    int top()//返回栈顶元素
    {
        return p->data;
    }
    bool isEmpty()//判断栈是不是空的
    {
        if (length == 0)
        {
       return true;
        }
       else
        {
            return false;
        }
    }
void clear()//清空栈中的所有元素
    {
        if (length > 0)
        {
            pop();
        }
    }
};
int main()
{
//以下为测试代码
    Stack s;
    s.push(1);
    s.push(2);
    s.push(3);
while(!s.isEmpty())
    {
        cout<<s.pop()<<' ';
    }
    cout << endl;
    s.push(1);
    s.push(2);
    s.push(3);
    int a =s.pop();
    
while(!s.isEmpty())
    {
        cout<<s.pop()<<' ';
    }
    
    
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值