栈和队列练习题

11111111111111111111111111
#include<stdio.h>
#include<stdlib.h>
typedef int datatype;
typedef struct node
{
datatype data;
struct node*next;
}node;
typedef struct stack
{
node*top;
}stack;
void initstack(stack*&s)
{
s = (stack*)malloc(sizeof(stack));
s->top = NULL;
};
void push(stack*&s, datatype data)
{
node*p = (node*)malloc(sizeof(node));
p->data = data;
p->next = s->top;
s->top = p;
} i
nt pop(stack*&s, datatype &data)
{
if (s->top == NULL)
return 0;
else
{
data = s->top->data;
s->top = s->top->next;
return 1;
}
} v
oid readchar(char*string, int &cnt)
{
int i = 0;
while ((string[i++] = getchar())!='\n');
cnt = i - 1;
} i
nt main()
{
char string[100];
int cnt, i;
stack*s;
datatype temp;
initstack(s);
readchar(string, cnt);
for ( i = 0; i < cnt / 2; i++)
{
push(s, string[i]);
} w
hile (pop(s, temp) &&i>0&& temp == string[cnt-i])i--;
if (i == 0)
printf("回文");
else
printf("非回文");
getchar();
} 4
44444444444444444444
#include<stdio.h>
#include<stdlib.h>
int f(int m, int n)
{
if (m*n == 0)
return m + n + 1;
else return f(m - 1, f(m, n - 1));
} i
nt main()
{
printf("%d", f(2, 1));
getchar();
} 5
55555555555555555555555
#include<stdio.h>
#include<stdlib.h>
typedef char datatype;
typedef struct node
{
datatype data;
struct node*next;
}node;
typedef struct stack
{
node*top;
}stack;
void initstack(stack*&s)
{
s = (stack*)malloc(sizeof(stack));
s->top = NULL;
};
void push(stack*&s, datatype data)
{
node*p = (node*)malloc(sizeof(node));
p->data = data;
p->next = s->top;
s->top = p;
} i
nt pop(stack*&s, datatype &data)
{
if (s->top == NULL)
return 0;
else
{
data = s->top->data;
s->top = s->top->next;
return 1;
}
} i
nt empty(stack*&s)
{
if (s->top == NULL)
return 1;
else
return 0;
} d
atatype gettop(stack*&s)
{
if (!empty(s))
return s->top->data;
else
return -1;
} i
nt correct(datatype data[], int cnt)
{
stack*s;
initstack(s);
datatype temp;
int flag = 1;
for (int i = 0; i < cnt&&flag; i++)
{
if (empty(s))
push(s, data[i]);
else
{
switch (data[i])
{ c
ase '[':push(s, data[i]); break;
case '(':push(s, data[i]); break;
case'{':push(s, data[i]); break;
case']':gettop(s) == '[' ? pop(s, temp) : flag = 0; break;
case')':gettop(s) == '(' ? pop(s, temp) : flag = 0; break;
case'}':gettop(s) == '{' ? pop(s, temp) : flag = 0; break;
}
}
} if
(!flag||!empty(s))
return 0;
else return 1;
} i
nt main()
{
datatype data[] = { '{','[','(',')','[',']',']','}' ,'{' ,'}'};
printf("%d", correct(data, 10));
getchar();
} 2
2222222222222222222222
#include<stdio.h>
#include<stdlib.h>
typedef int datatype;
typedef struct node
{
datatype data;
struct node*next;
}node;
typedef struct stack
{
node*top;
}stack;
typedef struct
{
node*head;
}linklist;
void initstack(stack*&s)
{
s = (stack*)malloc(sizeof(stack));
s->top = NULL;
};
void push(stack*&s, datatype data)
{
node*p = (node*)malloc(sizeof(node));
p->data = data;
p->next = s->top;
s->top = p;
}i
nt pop(stack*&s, datatype &data)
{
if (s->top == NULL)
return 0;
else
{
data = s->top->data;
s->top = s->top->next;
return 1;
}
}v
oid initlinklist(linklist*&L,datatype data[],int cnt)
{
L = (linklist*)malloc(sizeof(linklist));
L->head = (node*)malloc(sizeof(node));
node*p, *q;
p = q = L->head;
for (int i = 0; i < cnt; i++)
{
p = (node*)malloc(sizeof(node));
p->data = data[i];
q->next = p;
q = p;
}p
->next = NULL;
}v
oid reverse(linklist*L)
{
node*p = L->head->next, *q=p;
stack*s;
initstack(s);
while (p != NULL)
{
push(s, p->data);
p = p->next;
free(q);
q = p;
} p
= q = L->head;
datatype temp;
while (pop(s, temp))
{
p = (node*)malloc(sizeof(node));
p->data = temp;
q->next = p;
q = p;
} p
->next = NULL;
} v
oid printlist(linklist*L)
{
node*p = L->head->next;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
} i
nt main()
{
datatype data[] = { 0,1,2,3,4,5,6,7,8,9 };
linklist*L;
initlinklist(L, data, 10);
reverse(L);
printlist(L);
getchar();
} 3
3333333333333333333333
#include<stdio.h>
#include<stdlib.h>
typedef int datatype;
typedef struct node
{
datatype data;
struct node*next;
}node;
typedef struct stack
{
node*top;
}stack;
void initstack(stack*&s)
{
s = (stack*)malloc(sizeof(stack));
s->top = NULL;
};
void push(stack*&s, datatype data)
{
node*p = (node*)malloc(sizeof(node));
p->data = data;
p->next = s->top;
s->top = p;
}i
nt pop(stack*&s, datatype &data)
{
if (s->top == NULL)
return 0;
else
{
data = s->top->data;
s->top = s->top->next;
return 1;
}
}i
nt main()
{
datatype data[] = { 0,1,2,5,4,4,4,5,8,9,5,9,6,1 };
stack*S, *ST;
datatype temp;
initstack(S);
initstack(ST);
for (int i = 0; i < 14; i++)
push(S, data[i]);
for (int i = 0; i < 14; i++)
{
pop(S, temp);
if (temp != 4)
push(ST, temp);
} w
hile (pop(ST, temp))
{
push(S, temp);
} g
etchar();
} 6
66666666666666666666
#include<stdio.h>
#include<stdlib.h>
#define M 5
typedef int datatype;
typedef struct
{
datatype data[M];
int len;
int rear;
}queue;
void initqueue(queue*&q)
{
q = (queue*)malloc(sizeof(queue));
q->rear = 0;
q->len = 0;
} i
nt isfull(queue*q)
{
if (q->len == M)
return 1;
else return 0;
} i
nt isempty(queue*q)
{
if (q->len == 0)
return 1;
else
return 0;
} i
nt enqueue(queue*q, datatype data)
{
if (isfull(q))
return 0;
else
{
q->data[q->rear] = data;
q->len++;
q->rear = (q->rear+1) % M;
return 1;
}
} i
nt dequeue(queue*q, datatype &data)
{
if (isempty(q))
return 0;
else
{
data = q->data[(q->rear + M - q->len) % M];
q->len--;
return 1;
}
} i
nt main()
{
queue*q;
datatype temp;
initqueue(q);
enqueue(q, 5);
enqueue(q, 4);
enqueue(q, 3);
enqueue(q, 2);
enqueue(q, 1);
dequeue(q, temp);
printf("%d", temp);
getchar();
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值