Skip to content

Commit 6e91775

Browse files
committedApr 28, 2018
第三章
1 parent 221d721 commit 6e91775

21 files changed

+717
-0
lines changed
 

‎3-1-2

12.9 KB
Binary file not shown.

‎3-1-3.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include<iostream>
2+
using namespace std;
3+
int main(){
4+
char x;
5+
cin>>x;
6+
int count=0;
7+
while(x!='q'){
8+
switch(x){
9+
case 'I':
10+
count++;
11+
break;
12+
case 'O':
13+
count--;
14+
break;
15+
default:
16+
cout<<"error";
17+
return 1;
18+
}
19+
if(count<0){
20+
cout<<"false"<<endl;
21+
return 1;
22+
}
23+
cin>>x;
24+
}
25+
if(count){
26+
cout<<"false"<<endl;
27+
return 1;
28+
}
29+
cout<<"true"<<endl;
30+
return 0;
31+
}

‎3-1-4

13.4 KB
Binary file not shown.

‎3-1-4.cpp

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include<iostream>
2+
#include"3-1-SqStack.cpp"
3+
using namespace std;
4+
typedef struct LNode{
5+
int data;
6+
struct LNode* next;
7+
}LNode, *LinkList;
8+
//bug 笔记:因为在SqStack31中没有用引用型参数
9+
//导致top没有值,出现莫名其妙的访问错误
10+
//所以,访问错误也可能是由于未赋值的变量引起的
11+
void fun(LinkList L,SqStack &s){
12+
LinkList p=L->next;
13+
int x=0;
14+
for(;p!=NULL;p=p->next){
15+
if(push(s,p->data)){
16+
//bug笔记:由于top无值,报栈满
17+
}else{
18+
cout<<"error:push"<<endl;
19+
return;
20+
}
21+
}
22+
for(p=L->next;p;p=p->next){
23+
if(pop(s,x)){
24+
if(x!=p->data){
25+
cout<<"false"<<endl;
26+
return;
27+
}
28+
}else{
29+
cout<<"error:pop"<<endl;
30+
}
31+
}
32+
cout<<"true"<<endl;
33+
return;
34+
35+
}
36+
void fun_start(LinkList L1,LinkList &rear,SqStack &s){
37+
fun(L1,s);
38+
}
39+
//带头结点
40+
void build_list(LinkList &L,LinkList &rear){
41+
LinkList s;
42+
int x=0;
43+
L=(LinkList)malloc(sizeof(LNode));
44+
L->next=NULL;
45+
rear=L;
46+
cin>>x;
47+
while(x!=9999){
48+
s=(LinkList)malloc(sizeof(LNode));
49+
s->data=x;
50+
rear->next=s;
51+
rear=rear->next;
52+
cin>>x;
53+
}
54+
rear->next=NULL;
55+
}
56+
57+
void print_list(LinkList &L){
58+
//打印链表
59+
//带头结点
60+
LinkList s=L->next;
61+
//while(s){
62+
//双链表
63+
while(s){
64+
cout<<s->data<<" ";
65+
s=s->next;
66+
}
67+
cout<<endl;
68+
LinkList p=L,tmp=L;
69+
while(L){
70+
p=L;
71+
L=L->next;
72+
free(p);
73+
}
74+
}
75+
////////////////////////////////////////////////////
76+
int main(){
77+
LinkList l1,l2,rear1,rear2;
78+
SqStack s;
79+
InitStack(s);
80+
build_list(l1,rear1);
81+
// build_list(l2);
82+
fun_start(l1,rear1,s);
83+
print_list(l1);
84+
// print_list(l2);
85+
return 0;
86+
}
87+
/////////////////////////////////////////////////

‎3-1-5

13.1 KB
Binary file not shown.

‎3-1-5.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include<iostream>
2+
using namespace std;
3+
#define MAXSIZE 10
4+
#define ELEMTYPE int
5+
typedef struct{
6+
ELEMTYPE data[MAXSIZE];
7+
int top1;
8+
int top2;
9+
}SharedStack;
10+
bool InitStack(SharedStack &s){
11+
s.top1=0;
12+
s.top2=MAXSIZE-1;
13+
}
14+
bool push1(SharedStack &s,ELEMTYPE x){
15+
if(s.top1<=s.top2){
16+
s.data[s.top1++]=x;
17+
return true;
18+
}
19+
cout<<"error:push1"<<endl;
20+
return false;
21+
}
22+
bool push2(SharedStack &s,ELEMTYPE x){
23+
if(s.top2>=s.top1){
24+
s.data[s.top2--]=x;
25+
return true;
26+
}
27+
cout<<"error:push2"<<endl;
28+
return false;
29+
}
30+
bool pop1(SharedStack &s,ELEMTYPE &x){
31+
if(s.top1){
32+
x=s.data[--s.top1];
33+
return true;
34+
}
35+
return false;
36+
37+
}
38+
bool pop2(SharedStack &s,ELEMTYPE &x){
39+
if(s.top2<MAXSIZE-1){
40+
x=s.data[++s.top2];
41+
return true;
42+
}
43+
return false;
44+
}
45+
int main(){
46+
SharedStack s;
47+
InitStack(s);
48+
int x;
49+
cin>>x;
50+
while(x!=9999){
51+
push1(s,x);
52+
cin>>x;
53+
}
54+
cin>>x;
55+
while(x!=9999){
56+
push2(s,x);
57+
cin>>x;
58+
}
59+
while(pop1(s,x))cout<<x<<" ";
60+
cout<<endl;
61+
while(pop2(s,x))cout<<x<<" ";
62+
cout<<endl;
63+
return 0;
64+
}

‎3-1-SqStack.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<iostream>
2+
using namespace std;
3+
#define MAXSIZE 10
4+
#define ELEMTYPE int
5+
typedef struct{
6+
ELEMTYPE data[MAXSIZE];
7+
int top; //top 指针
8+
}SqStack;
9+
void InitStack(SqStack &stk){
10+
stk.top=0;
11+
}
12+
bool StackEmpty(SqStack stk){
13+
if(!stk.top)return true;
14+
return false;
15+
}
16+
//注意:一定要加引用型!
17+
bool push(SqStack &stk,ELEMTYPE x){
18+
//MAXIZE
19+
if(stk.top<MAXSIZE){
20+
stk.data[stk.top++]=x;
21+
return true;
22+
}
23+
return false;
24+
}
25+
bool pop(SqStack &stk,ELEMTYPE &x){
26+
if(stk.top>0){
27+
x=stk.data[--stk.top];
28+
return true;
29+
}
30+
return false;
31+
}
32+
bool gettop(SqStack stk,ELEMTYPE &x){
33+
if(stk.top){
34+
x=stk.data[stk.top-1];
35+
return true;
36+
}
37+
return false;
38+
}

‎3-2-1

13 KB
Binary file not shown.

‎3-2-1.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include<iostream>
2+
#define MAXSIZE 10
3+
using namespace std;
4+
#define ELEMTYPE int
5+
typedef struct{
6+
ELEMTYPE data[MAXSIZE];
7+
int front;
8+
int rear;
9+
int tag;
10+
}Queue;
11+
void InitQue(Queue &q){
12+
q.front=0;
13+
q.rear=0;
14+
q.tag=0;
15+
}
16+
bool EnQueue(Queue &q,ELEMTYPE x){
17+
if(q.front==q.rear&&q.tag==1){
18+
cout<<"error:enq"<<endl;
19+
return false;}
20+
else{
21+
q.data[q.rear]=x;
22+
q.rear=(q.rear+1)%MAXSIZE;//rear为第一个可用结点
23+
if(q.rear==q.front){
24+
q.tag=1;
25+
}
26+
return true;
27+
}
28+
}
29+
bool DeQueue(Queue &q,ELEMTYPE &x){
30+
if(q.front==q.rear&&q.tag==0)return false;
31+
else{
32+
x=q.data[q.front];//q为队头有值结点
33+
q.front=(q.front+1)%MAXSIZE;
34+
if(q.front==q.rear){
35+
q.tag=0;
36+
}
37+
return true;
38+
}
39+
}
40+
int main(){
41+
Queue q;
42+
InitQue(q);
43+
int x;
44+
cin>>x;
45+
while(x!=9999){
46+
EnQueue(q,x);
47+
cin>>x;
48+
}
49+
while(DeQueue(q,x)){
50+
cout<<x<<" ";
51+
}
52+
cout<<endl;
53+
return 0;
54+
}

‎3-2-2

13.3 KB
Binary file not shown.

‎3-2-2.cpp

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include<iostream>
2+
#include"3-1-SqStack.cpp"
3+
#define MAXSIZE 10
4+
//注意MAXSIZE与3-1冲突
5+
using namespace std;
6+
#define ELEMTYPE int
7+
typedef struct{
8+
ELEMTYPE data[MAXSIZE];
9+
int front;
10+
int rear;
11+
int tag;
12+
}Queue;
13+
void InitQue(Queue &q){
14+
q.front=0;
15+
q.rear=0;
16+
q.tag=0;
17+
}
18+
bool EnQueue(Queue &q,ELEMTYPE x){
19+
if(q.front==q.rear&&q.tag==1){
20+
cout<<"error:enq"<<endl;
21+
return false;}
22+
else{
23+
q.data[q.rear]=x;
24+
q.rear=(q.rear+1)%MAXSIZE;//rear为第一个可用结点
25+
if(q.rear==q.front){
26+
q.tag=1;
27+
}
28+
return true;
29+
}
30+
}
31+
bool DeQueue(Queue &q,ELEMTYPE &x){
32+
if(q.front==q.rear&&q.tag==0)return false;
33+
else{
34+
x=q.data[q.front];//q为队头有值结点
35+
q.front=(q.front+1)%MAXSIZE;
36+
if(q.front==q.rear){
37+
q.tag=0;
38+
}
39+
return true;
40+
}
41+
}
42+
void fun(Queue &q,SqStack &s){
43+
int x;
44+
while(DeQueue(q,x)){
45+
push(s,x);
46+
}
47+
while(pop(s,x)){
48+
EnQueue(q,x);
49+
}
50+
}
51+
int main(){
52+
Queue q;
53+
SqStack s;
54+
InitStack(s);
55+
InitQue(q);
56+
int x;
57+
cin>>x;
58+
while(x!=9999){
59+
EnQueue(q,x);
60+
cin>>x;
61+
}
62+
fun(q,s);
63+
while(DeQueue(q,x)){
64+
cout<<x<<" ";
65+
}
66+
cout<<endl;
67+
return 0;
68+
}

‎3-2-3

13.3 KB
Binary file not shown.

‎3-2-3.cpp

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include<iostream>
2+
using namespace std;
3+
#define MAXSIZE 10
4+
#define ELEMTYPE int
5+
typedef struct{
6+
ELEMTYPE data[MAXSIZE];
7+
int top1;
8+
int top2;
9+
}SharedStack;
10+
bool InitStack(SharedStack &s){
11+
s.top1=0;
12+
s.top2=MAXSIZE-1;
13+
}
14+
bool push1(SharedStack &s,ELEMTYPE x){
15+
if(s.top1<=s.top2){
16+
s.data[s.top1++]=x;
17+
return true;
18+
}
19+
cout<<"error:push1"<<endl;
20+
return false;
21+
}
22+
bool push2(SharedStack &s,ELEMTYPE x){
23+
if(s.top2>=s.top1){
24+
s.data[s.top2--]=x;
25+
return true;
26+
}
27+
cout<<"error:push2"<<endl;
28+
return false;
29+
}
30+
bool pop1(SharedStack &s,ELEMTYPE &x){
31+
if(s.top1){
32+
x=s.data[--s.top1];
33+
return true;
34+
}
35+
return false;
36+
}
37+
bool pop2(SharedStack &s,ELEMTYPE &x){
38+
if(s.top2<MAXSIZE-1){
39+
x=s.data[++s.top2];
40+
return true;
41+
}
42+
return false;
43+
}
44+
bool EnQueue(SharedStack &s,ELEMTYPE &x){
45+
if(push1(s,x))return true;
46+
return false;
47+
}
48+
bool DeQueue(SharedStack &s,ELEMTYPE &x){
49+
while(pop1(s,x))push2(s,x);
50+
int tmp=0;
51+
bool tag=false;
52+
if(pop2(s,x)){
53+
tmp=x;
54+
tag=true;
55+
}
56+
while(pop2(s,x))push1(s,x);
57+
if(tag){
58+
x=tmp;
59+
return true;
60+
}
61+
return false;
62+
}
63+
bool QueueEmpty(SharedStack &s){
64+
if(s.top1==0&&s.top2==MAXSIZE-1)return true;
65+
return false;
66+
}
67+
int main(){
68+
SharedStack s;
69+
InitStack(s);
70+
int x;
71+
cin>>x;
72+
while(x!=9999){
73+
EnQueue(s,x);
74+
cin>>x;
75+
}
76+
while(DeQueue(s,x))cout<<x<<" ";
77+
cout<<endl;
78+
return 0;
79+
}

‎3-3-1

13.1 KB
Binary file not shown.

‎3-3-1.cpp

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include<iostream>
2+
using namespace std;
3+
#define MAXSIZE 10
4+
#define ELEMTYPE char
5+
typedef struct{
6+
ELEMTYPE data[MAXSIZE];
7+
int top; //top 指针
8+
}SqStack;
9+
void InitStack(SqStack &stk){
10+
stk.top=0;
11+
}
12+
bool StackEmpty(SqStack stk){
13+
if(!stk.top)return true;
14+
return false;
15+
}
16+
//注意:一定要加引用型!
17+
bool push(SqStack &stk,ELEMTYPE x){
18+
//MAXIZE
19+
if(stk.top<MAXSIZE){
20+
stk.data[stk.top++]=x;
21+
return true;
22+
}
23+
return false;
24+
}
25+
bool pop(SqStack &stk,ELEMTYPE &x){
26+
if(stk.top>0){
27+
x=stk.data[--stk.top];
28+
return true;
29+
}
30+
return false;
31+
}
32+
bool gettop(SqStack stk,ELEMTYPE &x){
33+
if(stk.top){
34+
x=stk.data[stk.top-1];
35+
return true;
36+
}
37+
return false;
38+
}
39+
int main(){
40+
SqStack s;
41+
InitStack(s);
42+
char x,tmp;
43+
cin>>x;
44+
while(x!='q'){
45+
switch(x){
46+
case '{':
47+
push(s,x);
48+
break;
49+
case '}':
50+
if(!pop(s,tmp))return 1;
51+
if(tmp!='{'){
52+
cout<<"error:}"<<endl;
53+
return 1;
54+
}
55+
break;
56+
case '(':
57+
push(s,x);
58+
break;
59+
case ')':
60+
if(!pop(s,tmp))return 1;
61+
if(tmp!='('){
62+
cout<<"error:)"<<endl;
63+
return 1;
64+
}
65+
break;
66+
case '[':
67+
push(s,x);
68+
break;
69+
case ']':
70+
if(!pop(s,tmp))return 1;
71+
if(tmp!='['){
72+
cout<<"error:]"<<endl;
73+
return 1;
74+
}
75+
break;
76+
default:
77+
cout<<"error:char"<<endl;
78+
return 1;
79+
}
80+
cin>>x;
81+
}
82+
if(StackEmpty){
83+
cout<<"error:last"<<endl;
84+
return 1;
85+
}
86+
cout<<"true"<<endl;
87+
return 0;
88+
}

‎3-3-2

13.2 KB
Binary file not shown.

‎3-3-2.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include<iostream>
2+
using namespace std;
3+
#define MAXSIZE 10
4+
#define ELEMTYPE char
5+
typedef struct{
6+
ELEMTYPE data[MAXSIZE];
7+
int top; //top 指针
8+
}SqStack;
9+
void InitStack(SqStack &stk){
10+
stk.top=0;
11+
}
12+
bool StackEmpty(SqStack stk){
13+
if(!stk.top)return true;
14+
return false;
15+
}
16+
//注意:一定要加引用型!
17+
bool push(SqStack &stk,ELEMTYPE x){
18+
//MAXIZE
19+
if(stk.top<MAXSIZE){
20+
stk.data[stk.top++]=x;
21+
return true;
22+
}
23+
return false;
24+
}
25+
bool pop(SqStack &stk,ELEMTYPE &x){
26+
if(stk.top>0){
27+
x=stk.data[--stk.top];
28+
return true;
29+
}
30+
return false;
31+
}
32+
bool gettop(SqStack stk,ELEMTYPE &x){
33+
if(stk.top){
34+
x=stk.data[stk.top-1];
35+
return true;
36+
}
37+
return false;
38+
}
39+
40+
int main(){
41+
SqStack s;
42+
InitStack(s);
43+
char x;
44+
cin>>x;
45+
while(x!='q'){
46+
switch(x){
47+
case 'H':
48+
push(s,x);
49+
break;
50+
case 'S':
51+
cout<<x<<" ";
52+
}
53+
cin>>x;
54+
}
55+
while(pop(s,x))cout<<x<<" ";
56+
cout<<endl;
57+
return 0;
58+
}

‎3-3-3

13.1 KB
Binary file not shown.

‎3-3-3.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include<iostream>
2+
using namespace std;
3+
#define MAXSIZE 10
4+
#define ELEMTYPE node
5+
typedef struct{
6+
int p;
7+
int no;
8+
}node;
9+
typedef struct{
10+
ELEMTYPE data[MAXSIZE];
11+
int top; //top 指针
12+
}SqStack;
13+
void InitStack(SqStack &stk){
14+
stk.top=0;
15+
}
16+
bool StackEmpty(SqStack stk){
17+
if(!stk.top)return true;
18+
return false;
19+
}
20+
//注意:一定要加引用型!
21+
bool push(SqStack &stk,ELEMTYPE x){
22+
//MAXIZE
23+
if(stk.top<MAXSIZE){
24+
stk.data[stk.top++]=x;
25+
return true;
26+
}
27+
return false;
28+
}
29+
bool pop(SqStack &stk,ELEMTYPE &x){
30+
if(stk.top>0){
31+
x=stk.data[--stk.top];
32+
return true;
33+
}
34+
return false;
35+
}
36+
bool gettop(SqStack stk,ELEMTYPE &x){
37+
if(stk.top){
38+
x=stk.data[stk.top-1];
39+
return true;
40+
}
41+
return false;
42+
}
43+
44+
int main(){
45+
SqStack s;
46+
InitStack(s);
47+
int x,n,p,t;
48+
cout<<"x: ";
49+
cin>>x;
50+
cout<<"n: ";
51+
cin>>n;
52+
t=n;
53+
ELEMTYPE nd,tmp;
54+
for(;n>=2;n--){
55+
nd.no=n;
56+
push(s,nd);
57+
}
58+
int p0=1,p1=2*x;
59+
while(pop(s,tmp)){
60+
p=2*x*p1-2*(tmp.no-1)*p0;
61+
// cout<<"p"<<tmp.no<<"="<<p<<"="<<"2x*"<<p1<<"-2*("<<tmp.no-1<<")*"<<p0<<" ";
62+
p0=p1;
63+
p1=p;
64+
}
65+
if(t>1)cout<<p<<endl;
66+
else if(t==0)cout<<1<<endl;
67+
else if(t==1)cout<<2*x<<endl;
68+
return 0;
69+
}

‎3-3-4

13.1 KB
Binary file not shown.

‎3-3-4.cpp

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include<iostream>
2+
#define MAXSIZE 50
3+
using namespace std;
4+
#define ELEMTYPE int
5+
typedef struct{
6+
ELEMTYPE data[MAXSIZE];
7+
int front;
8+
int rear;
9+
int tag;
10+
}Queue;
11+
void InitQue(Queue &q){
12+
q.front=0;
13+
q.rear=0;
14+
q.tag=0;
15+
}
16+
bool EnQueue(Queue &q,ELEMTYPE x){
17+
if(q.front==q.rear&&q.tag==1){
18+
cout<<"error:enq"<<endl;
19+
return false;}
20+
else{
21+
q.data[q.rear]=x;
22+
q.rear=(q.rear+1)%MAXSIZE;//rear为第一个可用结点
23+
if(q.rear==q.front){
24+
q.tag=1;
25+
}
26+
return true;
27+
}
28+
}
29+
bool DeQueue(Queue &q,ELEMTYPE &x){
30+
if(q.front==q.rear&&q.tag==0)return false;
31+
else{
32+
x=q.data[q.front];//q为队头有值结点
33+
q.front=(q.front+1)%MAXSIZE;
34+
if(q.front==q.rear){
35+
q.tag=0;
36+
}
37+
return true;
38+
}
39+
}
40+
bool QueueEmpty(Queue q){
41+
if(q.front==q.rear&&q.tag==0)return true;
42+
return false;
43+
}
44+
int main(){
45+
Queue q1;//客车
46+
InitQue(q1);
47+
Queue q2;//货车
48+
InitQue(q2);
49+
int x;
50+
cout<<"客车:";
51+
cin>>x;
52+
while(x!=9999){
53+
EnQueue(q1,x);
54+
cin>>x;
55+
}
56+
cout<<"货车:";
57+
cin>>x;
58+
while(x!=9999){
59+
EnQueue(q2,x);
60+
cin>>x;
61+
}
62+
int n=0,count=0;
63+
while(!QueueEmpty(q1)||!QueueEmpty(q2)){
64+
n=0;
65+
while(n<10&&(!QueueEmpty(q1)||!QueueEmpty(q2))){
66+
if(count<4&&!QueueEmpty(q1)){
67+
DeQueue(q1,x);
68+
cout<<x<<" ";
69+
count++;
70+
n++;
71+
}else if(!QueueEmpty(q2)){
72+
count=0;
73+
DeQueue(q2,x);
74+
cout<<x<<" ";
75+
n++;
76+
}
77+
}
78+
cout<<endl;
79+
}
80+
return 0;
81+
}

0 commit comments

Comments
 (0)
Please sign in to comment.