数据结构实验二

#include <iostream>

using namespace std;

typedef struct sta {

char data[100];

int cnt;

}Sta,*Stalist;

Sta s;

void init()

{

s.cnt = 0;

return;

}

bool get()

{

char ch;

while (cin >> ch) {

if (ch == '#') break;

else {

if (ch == '(' || ch == '[' || ch == '{') {

s.cnt++;

s.data[s.cnt] = ch;

}

else if (ch == ')') {

if (s.cnt && s.data[s.cnt] == '(') {

s.cnt--;

}

else return 0;

}

else if (ch == ']') {

if (s.cnt && s.data[s.cnt] == '[') {

s.cnt--;

}

else return 0;

}

else if (ch == '}') {

if (s.cnt && s.data[s.cnt] == '{') {

s.cnt--;

}

else return 0;

}

}

}

if (s.cnt) return 0;

return 1;

}

int main()

{

init();

cout << get() << endl;

return 0;

}

#include <iostream>

using namespace std;

typedef struct sta {

int data[100];

int cnt;

}Sta,*Stalist;

Sta s;

void init()

{

s.cnt = 0;

return;

}

void push()

{

int x;

while (cin>>x) {

if (x == 0) return;

s.cnt++;

s.data[s.cnt] = x;

}

}

void pop()

{

if (s.cnt == 0) {

cout << "none" << endl;

return;

}

cout << s.data[s.cnt] << " ";

s.cnt--;

}

int main()

{

init();

push();

while (s.cnt != 0) {

pop();

}

cout << endl;

return 0;

}

#include <iostream>

using namespace std;

typedef struct node {

int data;

struct node* next;

}Qnode,*Linkq;

typedef struct {

Linkq f, r;

}Linkqueue;

int init(Linkqueue& Q)

{

Q.f = Q.r = new Qnode;

if (!Q.f) { 

cout << "error\n"; 

return 0; 

}

Q.f->next = NULL;

return 1;

}

bool Linkempty(Linkqueue Q)

{

if (Q.f == Q.r) return 1;

else return 0;

}

void Linkin(Linkqueue& Q, int x)

{

Linkq p;

p = new Qnode;

p->data = x;

p->next = Q.r->next;

Q.r->next = p;

Q.r = Q.r->next;

}

int Linkout(Linkqueue& Q)

{

if (Q.f != Q.r) {

int ans;

Linkq p;

p = Q.f->next;

ans = p->data;

Q.f->next = p->next;

if (Q.r == p) Q.f = Q.r;

delete p;

return ans;

}

}

void solve()

{

Linkqueue Q;

init(Q);

int x;

while (cin >> x) {

if (x == 0) break;

Linkin(Q, x);

}

while (!Linkempty(Q)) {

cout << Linkout(Q) << " ";

}

cout << endl;

}

int main()

{

solve();

return 0;

}

#include <iostream>

using namespace std;

const int N = 200;

typedef struct {

    int data[N];

    int f, r;

}Squeue;

void init(Squeue& Q)

{

    Q.f = Q.r = 0;

}

bool Seqempty(Squeue Q)

{

    if (Q.f == Q.r) return 1;

    return 0;

}

int Seqin(Squeue& Q, int x)

{

    if ((Q.r + 1) % N == Q.f) {

        cout << "队列满\n";

        return 0;

    }

    Q.data[Q.r] = x;

    Q.r = (Q.r + 1) % N;

    return 1;

}

int Seqpop(Squeue& Q)

{

    int ans;

    if (Q.f == Q.r) {

        cout << "Empty\n";

        return 0;

    }

    ans = Q.data[Q.f];

    Q.f = (Q.f + 1) % N;

    return ans;

}

void solve()

{

    Squeue Q;

    init(Q);

    int x;

    while (cin >> x) {

        if (x == 0) break;

        Seqin(Q, x);

    }

    while (!Seqempty(Q)) {

        cout << Seqpop(Q) << " ";

    }

    cout << endl;

}

int main()

{

    solve();

return 0;

}

#include <iostream>

using namespace std;

typedef struct sta {

int data[100];

int cnt;

}Sta, * Stalist;

Sta s;

void init()

{

s.cnt = 0;

return;

}

int d;

void push()

{

int x;

cin >> x >> d;

cout << x << "(10)=";

while (x) {

s.cnt++;

s.data[s.cnt] = x % d;

x /= d;

}

}

void pop()

{

if (s.cnt == 0) {

cout << "none" << endl;

return;

}

cout << s.data[s.cnt];

s.cnt--;

}

int main()

{

init();

push();

while (s.cnt != 0) {

pop();

}

printf("(%d)\n", d);

return 0;

}

#include <iostream>

using namespace std;

const int N = 200;

typedef struct {

    int data[N];

    int f, r;

}Squeue;

void init(Squeue& Q)  

{

    Q.f = Q.r = 0;

}

bool Seqempty(Squeue Q)

{

    if (Q.f == Q.r) return 1;

    return 0;

}

int Seqin(Squeue& Q, int x)

{

    if ((Q.r + 1) % N == Q.f) {

        cout << "队列满\n";

        return 0;

    }

    Q.data[Q.r] = x;

    Q.r = (Q.r + 1) % N;

    return 1;

}

int Seqpop(Squeue& Q)

{

    int ans;

    if (Q.f == Q.r) {

        cout << "Empty\n";

        return 0;

    }

    ans = Q.data[Q.f];

    Q.f = (Q.f + 1) % N;

    return ans;

}

void solve()

{

    Squeue Q;

    init(Q);

    int n;

    cin >> n;

    for(int j=1;j<=n-1;j++)

        cout<<"  ";

    printf("%2d  \n",1);

    Seqin(Q, 1);

    Seqin(Q, 1);

    int s = 0;

    for (int i = 2; i <= n; i++) {

        Seqin(Q, 0);

        for(int j=1;j<=n-i;j++)

            cout<<"  ";

        for (int j = 1; j <= i + 1; j++) {

            int tmp;

            tmp = Seqpop(Q);

            Seqin(Q, s + tmp);

            s = tmp;

            if (s != 0) printf("%2d  ",s);

        }

      cout << endl;

    }

}

int main()

{

  solve();

return 0;

}

#include<stdio.h>

#include <bits/stdc++.h>

using namespace std;

typedef struct

{

    char data[1000];

    int top;             

}SqStack;



void initStack(SqStack &st)

{

    st.top = -1;

}

int isEmpty(SqStack st)

{

    if(st.top==-1)return 1;

    else

        return 0;

}

void Push(SqStack &S,char e)

{

    if(S.top==999)

    return;

    S.data[++S.top]=e;

}

void Pop(SqStack &S,char &e)

{

    if(isEmpty(S))

    {

        e='\0';

        return ;

    }

    e=S.data[S.top--];

}

char getPop(SqStack &S)

{

    if(isEmpty(S))return 0;

    return S.data[S.top];

}

int print_S(SqStack st)

{

    if( isEmpty(st) )

    {

        printf("Stack is Empty");

        return 0;

    }

    int iPointer = st.top;

    while(iPointer != -1)

    {

        printf("%d ",st.data[iPointer]);

        --iPointer;

    }

    printf("\n");

    return 1;

}

int precede(char ch1,char ch2)

{

    if(ch1=='+'||ch1=='-')

    {

        if(ch2=='+'||ch2=='#'||ch2=='-'||ch2==')')

          return 1;

        else

        return 3;

    }

    else if(ch1=='*'||ch1=='/')

    {

        if(ch2=='(')

           return 3;

        else

        return 1;

    }

    else if(ch1=='(')

    {

        if(ch2==')')return 2;

        else return 3;

    }

    else if(ch1=='#')

    {

        if(ch2=='#')return 2;

        else return 3;

    }

}

char hb(char b,char ch,char a)

{

   int a1,b1,c;

   char c1;

   a1=a-'0';

   b1=b-'0';

   if(ch=='+')

   c=a1+b1;

   else if(ch=='-')

   c=a1-b1;

   else if(ch=='*')

   c=a1*b1;

   else if(ch=='/')

   c=a1/b1;

   c1=c+'0';

   return c1;

}

int cal(SqStack &optr,SqStack &opnd)

{

    int x;

    char a,b,x1;

    char ch,ch1;

    Push(optr,'#');

    cin>>ch;

    while(ch!='#'||getPop(optr)!='#')

    {

        if(ch>='0'&&ch<='9')

        {

            Push(opnd,ch);

            cin>>ch;

        }

        else

        {

            switch(precede(getPop(optr),ch))

           {

            printf("%d",precede(getPop(optr),ch));

            case 1:Pop(optr,ch1);Pop(opnd,a);Pop(opnd,b);Push(opnd,hb(a,ch1,b));break;

            case 2:Pop(optr,ch);cin>>ch;break;

            case 3:Push(optr,ch);cin>>ch;break;

           }

        }

    }

    Pop(opnd,x1);

    x=x1-'0';

    return x;

}

int main()

{

    int s;

    char x;

    SqStack optr,opnd;

    initStack(optr);

    initStack(opnd);

    s=cal(optr,opnd);

    printf("%d\n",s);

    return 0;

}

#include <iostream>

using namespace std;

typedef struct node {

int data;

struct node* next;

}Snode,*Links;

void init(Links& top)

{

top = NULL;

}

bool Sempty(Links top)

{

if (top == NULL) return 1;

return 0;

}

void Sin(Links& top, int x)

{

Links p;

p = new Snode;

p->data = x;

p->next = top;

top = p;

}

int Sout(Links& top)

{

int ans;

Links p;

if (top != NULL) {

ans = top->data;

p = top;

top = top->next;

delete p;

return ans;

}

}

void solve()

{

Links top;

init(top);

int x;

while (cin >> x) {

if (x == 0) break;

Sin(top, x);

}

while (!Sempty(top)) {

cout << Sout(top) << " ";

}

cout << endl;

}

int main()

{

solve();

return 0;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值