题目1:(难度1)
输入一个字符串,判断它是不是回文字符串。
题目2:(难度2)
用两个栈实现队列
假设你拥有一种栈结构,使用两个栈实现下面的队列结构,要求支持 push 和 pop 操作:
typedef struct Queue
{
Stack s1, s2;
第二题
输入一个字符串,判断它是不是回文字符串。
题目2:(难度2)
用两个栈实现队列
假设你拥有一种栈结构,使用两个栈实现下面的队列结构,要求支持 push 和 pop 操作:
typedef struct Queue
{
Stack s1, s2;
} Queue;
第一题
#include <stdio.h>
#include <string.h>
#define LEN 100
void reverseString(char * input,char * output,int n);
int main(void)
{
int n;
char *input = (char*)malloc(sizeof(char)*LEN);
char *output = (char*)malloc(sizeof(char)*LEN);
memset(input,0,sizeof(char)*LEN);
memset(output,0,sizeof(char)*LEN);
printf("please input the string:\n");
scanf("%s",input);
n=strlen(input);
reverseString(input,output,n);
if(strcmp(input, output) == 0)
printf("\n是回文\n");
else
printf("\n不是回文\n");
return 0;
}
void reverseString(char * input,char * output,int n)
{
int i;
for(i=0;i<n;i++)
{
output[i]=input[n-i-1];
}
}
第二题
#include <stdio.h>
#include <stdlib.h>
typedef struct Stack
{
int data;
struct Stack *next;
}Stack;
typedef struct Queue
{
Stack * s1, * s2;
} Queue;
Queue createQueue(void);
void QueuePush(Queue *queue,int data);
int QueuePop(Queue *queue);
void StackPush(Stack **top_ptr,int data);//push会改变栈顶,应使用二级指针,即若栈顶为top,则参数为&top
int StackPop(Stack **top_ptr);
int StackIsEmpty(Stack *top);
int main()
{
Queue queue=createQueue();
int i;
for(i=0;i<10;i++)
{
QueuePush(&queue,i);
printf("Push number %d into the queue\n",i);
}
for(i=0;i<5;i++)
printf("Pop number %d from the queue\n",QueuePop(&queue));
for(i=10;i<15;i++)
{
QueuePush(&queue,i);
printf("Push number %d into the queue\n",i);
}
for(i=0;i<10;i++)
printf("Pop number %d from the queue\n",QueuePop(&queue));
return 0;
}
Queue createQueue(void)
{
Queue *new_queue=malloc(sizeof(Queue));
new_queue->s1=NULL;
new_queue->s2=NULL;
return *new_queue;
}
void QueuePush(Queue *queue,int data)
{
while(!StackIsEmpty(queue->s2))
StackPush(&(queue->s1),StackPop(&(queue->s2)));
StackPush(&(queue->s1),data);
}
int QueuePop(Queue *queue)
{
while(!StackIsEmpty(queue->s1))
StackPush(&(queue->s2),StackPop(&(queue->s1)));
StackPop(&(queue->s2));
}
void StackPush(Stack **top_ptr,int data)
{
Stack *new_ptr=malloc(sizeof(Stack));
new_ptr->data=data;
new_ptr->next=*top_ptr;
*top_ptr=new_ptr;
}
int StackPop(Stack **top_ptr)
{
char ret=(*top_ptr)->data;
Stack *new_top=(*top_ptr)->next;
free(*top_ptr);
*top_ptr=new_top;
return ret;
}
int StackIsEmpty(Stack *top)
{
return top==NULL;
}
同学用数组实现stack的第二题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#undef MPRINT
#define MDEBUG
#ifdef MDEBUG
# define MPRINT(fmt, args...) printf("<my debug>file: %s, line: %d. "fmt, __FILE__, __LINE__, ##args);
#else
# define MPRINT(fmt, args...)
#endif // MDEBUG
//stack
typedef int element_t;
typedef struct mstack {
int stack_size, stack_count;
element_t *st;
} stack_t;
stack_t *stack_init(int n);
int stack_empty(stack_t *s);
int stack_full(stack_t *s);
void push(stack_t *s, element_t x);
element_t pop(stack_t *s);
//queue
typedef struct mqueue {
int q_size, q_count;
stack_t *s1, *s2;
} queue_t;
queue_t *queue_init(int n);
int queue_empty(queue_t *q);
int queue_full(queue_t *q);
void enqueue(queue_t *q, element_t x);
element_t dequeue(queue_t *q);
#define Q_MAX 10
int main(int argc, char **argv)
{
queue_t *q = queue_init(Q_MAX);
srand((unsigned int)time(NULL));
int tmp;
printf("enqueue: ");
while (!queue_full(q)) {
tmp = rand()%100;
printf("%3d ", tmp);
enqueue(q, tmp);
}
printf("\ndequeue: ");
while (!queue_empty(q)) {
printf("%3d ", dequeue(q));
}
printf("\n");
return 0;
}
stack_t *stack_init(int n)
{
stack_t *s;
s = malloc(sizeof(*s));
if (NULL == s) {
MPRINT("malloc erro.\n");
return NULL;
}
s->stack_size = n;
s->stack_count = 0;
s->st = malloc(sizeof(*s->st)*s->stack_size);
if (NULL == s->st) {
MPRINT("malloc erro.\n");
return NULL;
}
memset(s->st, 0, sizeof(*s->st)*s->stack_size);
return s;
}
int stack_empty(stack_t *s)
{
return (s->stack_count <= 0);
}
int stack_full(stack_t *s)
{
return (s->stack_count >= s->stack_size);
}
void push(stack_t *s, element_t x)
{
//if (!stack_full(s)) {
s->st[s->stack_count++] = x;
//}
}
element_t pop(stack_t *s)
{
//if (stack_empty(s)) {
// MPRINT("stack is empty.\n");
//return 0;
//}
return s->st[--s->stack_count];
}
queue_t *queue_init(int n)
{
queue_t *q;
q = malloc(sizeof(*q));
if (NULL == q) {
MPRINT("malloc erro.\n");
return NULL;
}
q->q_size = n;
q->q_count = 0;
q->s1 = stack_init(q->q_size);
if (NULL == q->s1) {
MPRINT("queue s1 init erro.\n");
return NULL;
}
q->s2 = stack_init(q->q_size);
if (NULL == q->s2) {
MPRINT("queue s2 init erro.\n");
return NULL;
}
return q;
}
int queue_empty(queue_t *q)
{
return (q->q_count <= 0);
}
int queue_full(queue_t *q)
{
return (q->q_count >= q->q_size);
}
void enqueue(queue_t *q, element_t x)
{
if (queue_full(q)) {
MPRINT("queue is full.\n");
return;
}
while (!stack_empty(q->s2)) {
push(q->s1, pop(q->s2));
}
q->q_count ++;
push(q->s1, x);
}
element_t dequeue(queue_t *q)
{
if (queue_empty(q)) {
MPRINT("queue is empty.\n");
return 0;
}
while (!stack_empty(q->s1)) {
push(q->s2, pop(q->s1));
}
q->q_count --;
return pop(q->s2);
}