初见顺序表、栈和队列

//顺序表
#define maxsize 100
typedef struct node
{
    int data[maxsize];
    int last;//每输入一个数就++;它记录了i可访问的最大序号
}list;
//建立空表
list* MakeEmpty()
{
    list *p;
    p=(list*)malloc(sizeof(list));
    p->last=-1;
    return p;
}
//查找
int find(et x,list *p)
{
	int i=0;
    while(i<=p->last&&p->data[i]!=x)//这里把找不到和刚好找到作为条件
    {
        i++;
        if(i>p->last) return -1;
        else return i;
    }
}
//插入(在第i位置插入)
void insert(int i,list*p)
{
    for(int j=p->last;j>=i-1;j--)
        p->data[j+1]=p->data[j];
    p->data[i-1]=x;
    p->last++;
    return;//指针修改的,可不返回
}
//删除(i位置删除)
void delete(int i,list*p)
{
for(int j=i;j<=p->last;j++)//(循环的是从0开始的标记)
p->data[j-1]=p->data[j];//操作的是实际的位置,i的实际位置是i-1
p->last--;
}
//栈stl形式,(出栈有点不同)
#include<stdio.h>
#define maxsize 100001
typedef struct node
{
    int data[maxsize];
    int pos;//指向顶部
    void news()//建立新栈
    {
        pos=-1;
    }
    void push(int x)//进栈
    {
        data[++pos]=x;
    }
    void pop()//弹出
    {
        pos--;
    }
    int top()//顶部元素查询
    {
        return data[pos];
    }
} Stack;
int main()
{
    Stack s;
    int c1,c2;
    s.news();
    char x;
    while(scanf("%c",&x))
    {
        if(x=='#')break;
        if(x>='0'&&x<='9')s.push(x-'0');
        else
        {
            c1 = s.top(); s.pop();
            c2 = s.top(); s.pop();
            switch (x)
            {
            case '+':
                s.push(c2+c1);
                break;
            case '-':
                s.push(c2-c1);
                break;
            case '*':
                s.push(c2*c1);
                break;
            case '/':
                s.push(c2/c1);
                break;
            }
        }
    }
    printf("%d\n",s.top());
}

发布了5 篇原创文章 · 获赞 6 · 访问量 858

猜你喜欢

转载自blog.csdn.net/weixin_44041700/article/details/101273184