1、题目:利用链表实现一个先入后出的栈结构,并提供栈操作的push和pop的接口
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct node Node;
typedef struct node * Link;
struct node
{
int num;
Link next;
};
void creat_node(Link *node)
{
*node = (Link)malloc(sizeof(Node));
if(*node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void creat_link(Link *head)
{
creat_node(head);
(*head)->next = NULL;
}
void incret_node(Link head,Link new_node)
{
Link p;
p = p->next;
if(head->next == NULL)
{
new_node->next = head->next;
head->next = new_node;
printf("push ok!\n");
}
else
{
new_node->next = head->next;
head->next = new_node;
printf("push ok!\n");
}
}
void display_node(Link *head)
{
if((*head)->next == NULL)
{
printf("zhan is empty!\n");
return;
}
while((*head)->next != NULL)
{
printf("pop = %d\n",(*head)->next->num);
(*head)->next = (*head)->next->next;
}
}
int main()
{
Link head;
Link new_node;
int i,n;
creat_link(&head);
srand(time(NULL));
printf("Please the number of number you want to input:");
scanf("%d",&n);
for(i = 0; i < n; i++)
{
creat_node(&new_node);
new_node->num = rand() % 100;
incret_node(head,new_node);
}
display_node(&head);
display_node(&head);
return 0;
}
结果:
2、题目:使用双向链表来实现双向队列
使用一个双向链表来实现一个双向的队列,并且让队列具有以下的操作:
(1)判断队列是否为空
(2)得到双向队列中元素的个数
(3)向左端添加一个新元素
(4)向右端添加一个新元素
(5)从左端删除一个元素
(6)从右端删除一个元素
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct node Node;
typedef struct node * Link;
struct node
{
int num;
Link next;
Link prior;
};
void creat_node(Link *node)
{
*node = (Link)malloc(sizeof(Node));
if(*node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void creat_link(Link *head)
{
creat_node(head);
(*head)->next = *head;
(*head)->prior = *head;
}
void incret_node(Link head,Link new_node)
{
if(head == NULL)
{
printf("que empty!\n");
}
new_node->prior = head->prior;
new_node->next = head;
head->prior = new_node;
new_node->prior->next = new_node;
}
void incret_left(Link head,Link new_node)
{
if(head == NULL)
{
printf("the que is empty!\n");
return;
}
new_node->next = head->next;
new_node->prior = head;
head->next = new_node;
new_node->next->prior = new_node;
}
void incret_right(Link head,Link new_node)
{
if(head == NULL)
{
printf("the que is empty!\n");
return;
}
new_node->next = head;
new_node->prior = head->prior;
head->prior->next = new_node;
head->prior = new_node;
}
void delet_left(Link head)
{
Link p;
p = head->next;
if(head == NULL)
{
printf("the que is empty!\n");
return;
}
p->prior->next = p->next;
p->next->prior = p->prior;
}
void delet_right(Link head)
{
Link p;
p = head->prior;
p->prior->next = p->next;
p->next->prior = p->prior;
}
void display_que(Link *head)
{
Link p,q;
int i = 0;
if(*head == NULL)
{
printf("que empty!\n");
return;
}
if((*head)->next == *head)
{
printf("que empty!\n");
return;
}
p = (*head)->next;
while((*head)->next != *head)
{
p = (*head)->next;
i++;
printf("%3d",p->num);
free(p);
(*head)->next = (*head)->next->next;
}
(*head)->prior = *head;
printf("\n");
printf("the number of words:%3d\n",i);
}
int main()
{
Link head;
Link new_node;
int i,n,m,t;
int g,k;
creat_link(&head);
srand(time(NULL));
printf("How many number you want to input:");
scanf("%d",&n);
for(i = 0; i < n; i++)
{
creat_node(&new_node);
new_node->num = rand() % 100;
printf("%3d",new_node->num);
incret_node(head,new_node);
}
printf("\n");
printf("the number of the words:%3d\n",n);
//display_que(&head);
printf("Please input the number you want to incret the left:");
scanf("%d",&m);
creat_node(&new_node);
new_node->num = m;
incret_left(head,new_node);
printf("Please input the number you want to incret the right:");
scanf("%d",&t);
creat_node(&new_node);
new_node->num = t;
incret_right(head,new_node);
display_que(&head);
printf("\n");
for(i = 0; i < n; i++)
{
creat_node(&new_node);
new_node->num = rand() % 100;
printf("%3d",new_node->num);
incret_node(head,new_node);
}
printf("\n");
printf("Delet left and ringht:\n");
delet_left(head);
delet_right(head);
display_que(&head);
return 0;
}
结果:
3、题目:通过链栈实现的简易计算器
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#define Max 100
struct node1
{
int data[Max];
int top;
};
struct node2
{
char data[Max];
int top;
};
typedef struct node1 Num;
typedef struct node2 Ch;
void init_stract1(Num *stract1)
{
stract1->top = -1;
}
void init_stract2(Ch *stract2)
{
stract2->top = -1;
}
int empty_stract1(Num *stract1)
{
if(stract1->top == -1)
{
return -1;
}
return 0;
}
int empty_stract2(Ch *stract2)
{
if(stract2->top == -1)
{
return -1;
}
return 0;
}
char get_stract2(Ch *stract2)
{
if(empty_stract2(stract2) == -1)
{
return -1;
}
return stract2->data[stract2->top];
}
void incret_stract1(Num *stract1,int num)
{
stract1->top++;
stract1->data[stract1->top] = num;
printf("push:%d\n",stract1->data[stract1->top]);
}
void incret_stract2(Ch *stract2,char str)
{
stract2->top++;
stract2->data[stract2->top] = str;
}
int get_priority(char ch)
{
if(ch == '+' || ch == '-')
{
return 1;
}
if(ch == '*' || ch == '/')
{
return 2;
}
}
int compare_priority(char ch,char str)
{
if(get_priority(ch) >= get_priority(str))
{
return 1;
}
return -1;
}
int push_stract1(Num *stract1)
{
if(empty_stract1(stract1) == -1)
{
return -1;
}
int num;
num = stract1->data[stract1->top];
stract1->top--;
return num;
}
char push_stract2(Ch *stract2)
{
if(empty_stract2(stract2) == -1)
{
return -1;
}
char ch;
ch = stract2->data[stract2->top];
stract2->top--;
return ch;
}
int count(int a,int b,char ch)
{
int rel;
switch(ch)
{
case '+':
{
rel = a + b;
break;
}
case '-':
{
rel = a - b;
break;
}
case '*':
{
rel = a * b;
break;
}
case '/':
{
rel = a / b;
break;
}
}
return rel;
}
int main()
{
Num *stract1;
Ch *stract2;
char str;
int i = 0;
int num;
char ch;
int a,b;
int rel;
stract1 = (Num *)malloc(sizeof(Num));
stract2 = (Ch *)malloc(sizeof(Ch));
init_stract1(stract1);
init_stract2(stract2);
while((str = getchar()) != '\n')
{
if(str >= '0' && str <= '9')
{
num = str - '0';
incret_stract1(stract1,num);
}
else
{
if(str == '+' || str == '-' || str == '*' || str == '/')
{
do{
ch = get_stract2(stract2);
if(ch == -1)
{
incret_stract2(stract2,str);
break;
}
else
{
if(compare_priority(ch,str) < 0)
{
incret_stract2(stract2,str);
break;
}
else
{
a = push_stract1(stract1);
b = push_stract1(stract1);
ch = push_stract2(stract2);
if(a == -1 || b == -1 || ch == -1)
{
printf("all is empty!\n");
}
rel = count(b,a,ch);
incret_stract1(stract1,rel);
}
}
}while(compare_priority(ch,str) > 0);
}
}
}
while(empty_stract1(stract1) != -1 && empty_stract2(stract2) != -1)
{
a = push_stract1(stract1);
b = push_stract1(stract1);
ch = push_stract2(stract2);
rel = count(b,a,ch);
incret_stract1(stract1,rel);
}
printf("reasult = %d\n",rel);
free(stract1);
free(stract2);
return 0;
}
结果:
4、题目:利用两个栈来模拟一个队列
利用两个栈S1,S2来模拟一个队列,已知栈的4个运算定义如下:
Push(S,x); //元素x入栈S
Pop(S,x); //S出栈并将出栈的值赋给x
StackEmpty(S); //判断栈是否为空
StackOverflow(S); //判断栈是否满
那么如何利用栈的运算来实现该队列的3个运算(形参由读者根据要求自己设计):
Enqueue; //将元素x入队
Dequeue; //出队,并将出队元素存储在x中
QueueEmpty; //判断队列是否为空
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define Max 100
struct stract
{
int stract[Max];
int top;
};
typedef struct stract Stract;
enum ptr {PUSH_OK,PUSH_NO,POP_OK,POP_NO,EMPTY_OK,EMPTY_NO};
void creat_stract(Stract *stract)
{
stract->top = -1;
}
void creat_(Stract **stract)
{
(*stract) = (Stract *)malloc(sizeof(Stract));
if((*stract) == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
int incret_stract(Stract *stract,int k)
{
if(stract->top != Max - 1)
{
stract->top++;
stract->stract[stract->top] = k;
return PUSH_OK;
}
else
{
return PUSH_NO;
}
}
int output(Stract *stract)
{
if(stract->top == -1)
{
return EMPTY_OK;
}
else
{
return stract->stract[stract->top--];
}
}
int main()
{
Stract *stract1;
Stract *stract2;
int i,n,ret,k;
creat_(&stract1);
creat_(&stract2);
creat_stract(stract1);
creat_stract(stract2);
srand(time(NULL));
printf("Please how many number you want to input:");
scanf("%d",&n);
for(i = 0; i < n; i++)
{
k = rand() % 100;
printf("%3d",k);
if(incret_stract(stract1,k) == PUSH_OK)
{
printf("push ok!");
}
else
{
printf("push no!");
break;
}
printf("\n");
}
for(i = 0; i < n; i++)
{
ret = output(stract1);
if(ret == EMPTY_OK)
{
printf("empty ok\n");
break;
}
else
{
incret_stract(stract2,ret);
}
}
for(i = 0; i < n; i++)
{
ret = output(stract2);
if(ret == EMPTY_OK)
{
printf("empty ok!\n");
break;
}
else
{
printf("%3d\n",ret);
}
}
return 0;
}
结果:
5、题目:假设称正读和反读都相同的字符序列为“回文”,例如,‘abba‘ 和 ‘abcba‘是回文,
‘abcde‘ 和 ‘ababab‘ 则不是回文。试写一个算法判别读入的一个以‘@‘为结束符的字符序列是否是“回文”。
(栈和队列)
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node Node;
typedef struct node * Link;
struct node
{
char op_ch;
Link next;
};
void creat_node(Link *node)
{
*node = (Link)malloc(sizeof(Node));
if(*node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void creat_link(Link *head)
{
creat_node(head);
(*head)->next = NULL;
}
void incret_zhan(Link head,Link new_node)
{
if(head == NULL)
{
printf("zhan is empty!\n");
return;
}
new_node->next = head->next;
head->next = new_node;
}
void incret_que(Link head,Link new_node)
{
if(head == NULL)
{
printf("que is empty!\n");
return;
}
Link p;
if(head->next == NULL)
{
new_node->next = head->next;
head->next = new_node;
}
else
{
p = head->next;
while(p->next != NULL)
{
p = p->next;
}
new_node->next = p->next;
p->next = new_node;
}
}
void display(Link *head1,Link *head2)
{
if(*head1 == NULL && *head2 == NULL)
{
printf("all is empty!\n");
return;
}
if((*head1)->next == NULL && (*head2)->next == NULL)
{
printf("all is empty!\n");
return;
}
Link p = NULL,q = NULL;
int i = 0;
char a[100],b[100];
while((*head1)->next != NULL)
{
p = (*head1)->next;
a[i++] = p->op_ch;
(*head1)->next = p->next;
free(p);
}
a[i] = '\0';
i = 0;
while((*head2)->next != NULL)
{
q = (*head2)->next;
b[i] = (*head2)->next->op_ch;
(*head2)->next = q->next;
free(q);
i++;
}
b[i] = '\0';
if(strcmp(a,b) == 0)
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
int main()
{
Link head1;
Link head2;
Link new_node1;
Link new_node2;
char str[100];
int i = 0;
gets(str);
creat_link(&head1);
creat_link(&head2);
while(str[i] != '\0')
{
if(str[i] == '@')
{
i++;
display(&head1,&head2);
}
creat_node(&new_node1);
creat_node(&new_node2);
new_node1->op_ch = str[i];
new_node2->op_ch = str[i];
incret_zhan(head1,new_node1);
incret_que(head2,new_node2);
i++;
}
display(&head1,&head2);
return 0;
}
结果: