第一篇 数据结构不难之线性结构:列表、链表、栈、队列
题目1 : 翻转链表
描述
翻转一个链表
特殊要求:请使用以下链表结构
class Node
{
int value;
Node next;
}
输入
输入包含多组数据。对于每组数据:
第一行是n,表示链表长度;当n=-1时,表示输入结束。(1 <= n <= 100)
第二行是n个整数,表示链表每一位所存储的内容。
输出
针对每组输入,输出翻转后的链表的内容。
样例输入
4
1 3 5 7
-1
样例输出
7 5 3 1
#include"stdio.h"
#include"stdlib.h"
struct Noode
{
int value;
struct Node *next;
};
typedef struct Noode Node;
Node *create(int n)
{
int i;
Node*head=NULL,*node=NULL,*end=NULL;
head=(Node*)malloc(sizeof(Node));
head->next=NULL;
for(i=0;i<n;i++)
{
node=(Node*)malloc(sizeof(Node));
scanf("%d",&node->value);
node->next=end;
end=node;
}
head->next=end;
return head;
}
void disp(Node*head)
{
Node*h=head;
h=h->next;
printf("%d",h->value);
while(h->next!=NULL)
{
h=h->next;
printf(" %d",h->value);
}
printf("\n");
}
main()
{
Node*head=NULL;
int n;
while(1)
{
scanf("%d",&n);
if(n==-1)
return 0;
head=create(n);
disp(head);
}
return 0;
}
题目2 :简单计算器
描述
编写一个程序可以完成基本的带括号的四则运算。其中除法(/)是整除,并且在负数除法时向0取整。(C/C++/Java默认的除法就是向0取整,python默认的是向负无穷取整。)
例如计算 100 * ( 2 + 12 ) - (20 / 3) * 2, 结果是1388。
输入
一个长度不超过100的字符串,代表要计算的算式。包含数字0-9以及±*/()。
输入保证计算过程不会超过32位有符号整数,并且其中的’-'都是减号没有负号。
输出
计算的结果。
样例输入
100*(2+12)-(20/3)*2
样例输出
1388
#include <stdio.h> /*包含头文件*/
#include <stdlib.h>
#define MAX_SIZE 1024 /*数组长度*/
int insert_operand(int *operand , int * top_num ,int num) /*数据压入数据栈*/
{
(*top_num) ++;
operand[*top_num] = num; /*保存数据*/
return 0; /*正常退出*/
}
int insert_oper (char * oper , int *top_oper , char ch) /*操作符压入符号栈*/
{
(*top_oper)++;
oper[*top_oper] = ch; /*保存操作符*/
return 0; /*正常退出*/
}
int compare(char *oper , int *top_oper , char ch) /*比较操作服优先级*/
{
if((oper[*top_oper] == '-' || oper[*top_oper] == '+') /*判断当前优先级是否比栈顶操作符优先级高*/
&& (ch == '*' || ch == '/'))
{
return 0; /*操作符压入栈*/
}
else if(*top_oper == -1 || ch == '('|| (oper[*top_oper] == '(' && ch != ')')) /*判断操作符栈是否为空;栈顶操作 符是否为'('*/
{
return 0; /*操作符压入栈*/
}
else if (oper[*top_oper] =='(' && ch == ')' ) /*判断括号内的表达式是否计算完毕*/
{
(*top_oper)--;
return 1; /*对()进行处理*/
}
else
{
return -1; /*进行操作符的运算*/
}
}
int deal_date(int *operand ,char *oper ,int *top_num, int *top_oper) /*进行数据运算*/
{
int num_1 = operand[*top_num]; /*取出数据栈中两个数据*/
int num_2 = operand[*top_num - 1];
int value = 0;
if(oper[*top_oper] == '+') /*加法操作*/
{
value = num_1 + num_2;
}
else if(oper[*top_oper] == '-') /*减法操作*/
{
value = num_2 - num_1;
}
else if(oper[*top_oper] == '*') /*乘法操作*/
{
value = num_2 * num_1;
}
else if(oper[*top_oper] == '/') /*除法操作*/
{
value = num_2 / num_1;
}
(*top_num) --; /*将数据栈顶下移一位*/
operand[*top_num] = value; /*将得到的值压入数据栈*/
(*top_oper) --; /*将操作符栈顶下移一位*/
}
int main()
{
int operand[MAX_SIZE] = {0}; /*数据栈,初始化*/
int top_num = -1;
char oper[MAX_SIZE] = {0}; /*操作符栈,初始化*/
int top_oper = -1;
char *str = (char *) malloc (sizeof(char) * 100); /*获取表达式(不带=)*/
scanf("%s",str);
char* temp;
char dest[MAX_SIZE];
int num = 0;
int i = 0;
while(*str != '\0')
{
temp = dest;
while(*str >= '0' && *str <= '9') /*判断是否是数据*/
{
*temp = *str;
str ++;
temp ++;
} /*遇到符号退出*/
if(*str != '(' && *(temp - 1) != '\0') /*判断符号是否为'('*/
{
*temp = '\0';
num = atoi(dest); /*将字符串转为数字*/
insert_operand(operand, &top_num,num); /*将数据压入数据栈*/
}
while(1)
{
i = compare(oper,&top_oper,*str); /*判断操作符优先级*/
if(i == 0)
{
insert_oper(oper,&top_oper,*str); /*压入操作符*/
break;
}
else if(i == 1) /*判断括号内的表达式是否结束*/
{
str++;
}
else if(i == -1) /*进行数据处理*/
{
deal_date(operand,oper,&top_num,&top_oper);
}
}
str ++; /*指向表达式下一个字符*/
}
printf("%d\n",operand[0]); /*输出结果*/
return 0; /*正常退出*/
}
题目3 :Exam44_MaxWindow
描述
有一个整型数组arr和一个大小为w的窗口从数组的最左边滑到最右边,窗口每次向右滑动一个位置。
例如,数组为[4,3,5,4,3,3,6,7],窗口大小为3时:依次出现的窗口为[4,3,5], [3,5,4], [5,4,3], [4,3,3], [3,3,6], [3,6,7]。
如果数组长度是n,窗口大小是w,则一共产生n-w+1个窗口。
输入
第一行 n 和 w,空格隔开,n为数组长度,w为窗口宽度
第二行为n个整数,作为数组元素,空格隔开
输出
n-w+1个数,每个数是对应窗口中的最大值。
例如上面的例子,应该返回[5,5,5,4,6,7]。
样例输入
8 3
4 3 5 4 3 3 6 7
样例输出
5 5 5 4 6 7
#include<stdio.h>
int max(int *b,int w)
{
int i,x;
x=b[0];
for(i=0;i<w;i++)
if(x<b[i])x=b[i];
return x;
}
int main()
{
int n,w,a[100],b[100],i,j;
scanf("%d%d",&n,&w);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-w+1;i++)
{
for(j=0;j<w;j++)
{
b[j]=a[i+j];
}
printf("%d ",max(b,w));
}
return 0;
}