栈
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node{
char value;
Node*bottom;
}Node;
typedef struct Stack{
int lenth;
Node* top;
}Stack;
Node* CreateStackNode(){
Node * p=(Node*)malloc(sizeof(Node));
p->value=0;
p->bottom=NULL;
return p;
}
void Push(Stack *stack,char ch){
Node *p=CreateStackNode();
p->value=ch;
p->bottom=(*stack).top;
(*stack).lenth++;
(*stack).top=p;
}
char Pop(Stack *stack){
Node* p=(*stack).top;
char number=(*stack).top->value;
(*stack).top=(*stack).top->bottom;
(*stack).lenth--;
return number;
}
int main(){
Stack stack;
stack.top=CreateStackNode();
stack.lenth=0;
char*str=(char*)malloc(sizeof(char)*100);
scanf("%s",str);
for(int i=0;i<strlen(str);i++){
if(str[i]>=48&&str[i]<=57){
Push(&stack,str[i]);
//TODO
}
else{
char ch1=Pop(&stack);
char ch2=Pop(&stack);
switch (str[i]) {
case '/':
case '÷':
Push(&stack,(ch2-48)/(ch1-48)+48);
//TODO
break;
case 'X':
case '×':
case '*':
Push(&stack,(ch2-48)*(ch1-48)+48);
//TODO
break;
case '+':
Push(&stack,(ch2-48)+(ch1-48)+48);
break;
case '-':
Push(&stack,(ch2-48)-(ch1-48)+48);
break;
default:
// exit(0);
break;
}
}
//TODO
}
printf("%c\n",stack.top->value);
free(str);
free(stack.top);
return 0;
}
队列
/*
顺式储存队列
*/
#include <stdio.h>
#define MAXSIZE 10
typedef struct Queue{
int front;//队列首
int rear;//队列末
int queue[MAXSIZE];
}Queue;
//求余实现循环队列的Add函数,简洁大方!
void AddQ(Queue* que,int number){
if(((que->rear+1)%MAXSIZE==que->front)||(que->rear==MAXSIZE-2&&que->front==-1)){
printf("The queue is full!\n");
return;
//TODO
}
que->rear=(que->rear+1)%MAXSIZE;
que->queue[que->rear]=number;
}
void Print(Queue *que){
if(que->front<que->rear){
for(int i=que->front+1;i<=que->rear;i++){
printf("%d ",que->queue[i]);
//TODO
}
//TODO
}
else if(que->front>=que->rear){
for(int i=que->front+1;i<MAXSIZE;i++){
printf("%d ",que->queue[i]);
//TODO
}
for(int i=0;i<=que->rear;i++){
printf("%d ",que->queue[i]);
//TODO
}
//TODO
}
}
int DeleteQ(Queue *que){
if(que->rear==que->front){
printf("The queue is empty!\n");
return EOF;
}
que->front=(que->front+1)%MAXSIZE;
return que->queue[que->front];
}
int main(){
Queue que;
que.front=-1;
que.rear=-1;
DeleteQ(&que);
for(int i=0;i<=MAXSIZE+1;i++){
AddQ(&que,i);
if(i==7){
DeleteQ(&que);
DeleteQ(&que);
//TODO
}
//TODO
}
Print(&que);
return 0;
}
/*
链式储存队列
*/
#include<stdlib.h>
#include <stdio.h>
typedef struct QNode{
int value;
QNode*next;
}QNode;
typedef struct Queue{
QNode*front;
QNode*rear;
}Queue;
QNode* CreateQNode(){
QNode* q=(QNode*)malloc(sizeof(QNode));
q->value=0;
q->next=NULL;
return q;
}
void AddQ(Queue*queue,int number){
QNode*q=CreateQNode();
q->value=number;
if(!(queue->front)&&!(queue->rear)){
queue->front=queue->rear=q;
}
else{
queue->rear->next=q;
queue->rear=q;
}
}
void PrintQ(Queue*queue){
if(queue->front==NULL){
printf("The queue is Null!\n");
return;
//TODO
}
QNode* q=queue->front;
while(q){
printf("%d ",q->value);
q=q->next;
//TODO
}
}
int DeleteQ(Queue*queue){
if(queue->front==NULL){
printf("The queue is Null!\n");
return EOF;
//TODO
}
if(queue->front==queue->rear){
queue->rear=NULL;
//TODO
}
QNode*q=queue->front;
int number=q->value;
queue->front=(queue->front)->next;
free(q);
return number;
}
int main(){
Queue queue;
queue.front=NULL;
queue.rear=NULL;
for(int i=0;i<10;i++){
AddQ(&queue,i);
//TODO
}
PrintQ(&queue);
printf("\n");
int c;
for(int i=0;i<11;i++){
if((c=DeleteQ(&queue))!=EOF){
printf("The out number is %d.\n",c);
//TODO
}
PrintQ(&queue);
printf("\n");
//TODO
}
free(queue.front);
free(queue.rear);
return 0;
}